1 | \input texinfo @c -*- Texinfo -*-
|
---|
2 | @c %**start of header
|
---|
3 | @setfilename make.info
|
---|
4 |
|
---|
5 | @include version.texi
|
---|
6 | @set EDITION 0.70
|
---|
7 | @set RCSID $Id: make.texi,v 1.45 2006/04/01 06:36:40 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 provided by Lisa M. Opus Goldstein <[email protected]>, 5 May 2004
|
---|
17 | @set ISBN 1-882114-83-5
|
---|
18 | @c %**end of header
|
---|
19 |
|
---|
20 | @copying
|
---|
21 | This file documents the GNU @code{make} utility, which determines
|
---|
22 | automatically which pieces of a large program need to be recompiled,
|
---|
23 | and issues the commands to recompile them.
|
---|
24 |
|
---|
25 | This is Edition @value{EDITION}, last updated @value{UPDATED},
|
---|
26 | of @cite{The GNU Make Manual}, for GNU @code{make} version @value{VERSION}.
|
---|
27 |
|
---|
28 | Copyright @copyright{} 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
|
---|
29 | 1996, 1997, 1998, 1999, 2000, 2002, 2003, 2004, 2005, 2006
|
---|
30 | Free Software Foundation, Inc.
|
---|
31 |
|
---|
32 | @quotation
|
---|
33 | Permission is granted to copy, distribute and/or modify this document
|
---|
34 | under the terms of the GNU Free Documentation License, Version 1.2 or
|
---|
35 | any later version published by the Free Software Foundation; with no
|
---|
36 | Invariant Sections, with the Front-Cover Texts being ``A GNU Manual,''
|
---|
37 | and with the Back-Cover Texts as in (a) below. A copy of the
|
---|
38 | license is included in the section entitled ``GNU Free Documentation
|
---|
39 | License.''
|
---|
40 |
|
---|
41 | (a) The FSF's Back-Cover Text is: ``You have freedom to copy and modify
|
---|
42 | this GNU Manual, like GNU software. Copies published by the Free
|
---|
43 | Software Foundation raise funds for GNU development.''
|
---|
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 GNU Packages
|
---|
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
|
---|
71 | Published by the Free Software Foundation @*
|
---|
72 | 51 Franklin St. -- Fifth Floor @*
|
---|
73 | Boston, MA 02110-1301 USA @*
|
---|
74 | ISBN @value{ISBN} @*
|
---|
75 | @sp 2
|
---|
76 | Cover art by Etienne Suvasa.
|
---|
77 | @end titlepage
|
---|
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 | * Commands:: Commands 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 |
|
---|
119 | Overview of @code{make}
|
---|
120 |
|
---|
121 | * Preparing:: Preparing and Running Make
|
---|
122 | * Reading:: On Reading this Text
|
---|
123 | * Bugs:: Problems and Bugs
|
---|
124 |
|
---|
125 | An 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 Commands
|
---|
132 | * Combine By Prerequisite:: Another Style of Makefile
|
---|
133 | * Cleanup:: Rules for Cleaning the Directory
|
---|
134 |
|
---|
135 | Writing 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 | * MAKEFILE_LIST Variable:: Discover which makefiles have been read.
|
---|
142 | * Special Variables:: Other special variables.
|
---|
143 | * Remaking Makefiles:: How makefiles get remade.
|
---|
144 | * Overriding Makefiles:: How to override part of one makefile
|
---|
145 | with another makefile.
|
---|
146 | * Reading Makefiles:: How makefiles are parsed.
|
---|
147 | * Secondary Expansion:: How and when secondary expansion is performed.
|
---|
148 |
|
---|
149 | Writing Rules
|
---|
150 |
|
---|
151 | * Rule Example:: An example explained.
|
---|
152 | * Rule Syntax:: General syntax explained.
|
---|
153 | * Prerequisite Types:: There are two types of prerequisites.
|
---|
154 | * Wildcards:: Using wildcard characters such as `*'.
|
---|
155 | * Directory Search:: Searching other directories for source files.
|
---|
156 | * Phony Targets:: Using a target that is not a real file's name.
|
---|
157 | * Force Targets:: You can use a target without commands
|
---|
158 | or prerequisites to mark other targets
|
---|
159 | as phony.
|
---|
160 | * Empty Targets:: When only the date matters and the
|
---|
161 | files are empty.
|
---|
162 | * Special Targets:: Targets with special built-in meanings.
|
---|
163 | * Multiple Targets:: When to make use of several targets in a rule.
|
---|
164 | * Multiple Rules:: How to use several rules with the same target.
|
---|
165 | * Static Pattern:: Static pattern rules apply to multiple targets
|
---|
166 | and can vary the prerequisites according to
|
---|
167 | the target name.
|
---|
168 | * Double-Colon:: How to use a special kind of rule to allow
|
---|
169 | several independent rules for one target.
|
---|
170 | * Automatic Prerequisites:: How to automatically generate rules giving
|
---|
171 | prerequisites from source files themselves.
|
---|
172 |
|
---|
173 | Using Wildcard Characters in File Names
|
---|
174 |
|
---|
175 | * Wildcard Examples:: Several examples
|
---|
176 | * Wildcard Pitfall:: Problems to avoid.
|
---|
177 | * Wildcard Function:: How to cause wildcard expansion where
|
---|
178 | it does not normally take place.
|
---|
179 |
|
---|
180 | Searching Directories for Prerequisites
|
---|
181 |
|
---|
182 | * General Search:: Specifying a search path that applies
|
---|
183 | to every prerequisite.
|
---|
184 | * Selective Search:: Specifying a search path
|
---|
185 | for a specified class of names.
|
---|
186 | * Search Algorithm:: When and how search paths are applied.
|
---|
187 | * Commands/Search:: How to write shell commands that work together
|
---|
188 | with search paths.
|
---|
189 | * Implicit/Search:: How search paths affect implicit rules.
|
---|
190 | * Libraries/Search:: Directory search for link libraries.
|
---|
191 |
|
---|
192 | Static Pattern Rules
|
---|
193 |
|
---|
194 | * Static Usage:: The syntax of static pattern rules.
|
---|
195 | * Static versus Implicit:: When are they better than implicit rules?
|
---|
196 |
|
---|
197 | Writing the Commands in Rules
|
---|
198 |
|
---|
199 | * Command Syntax:: Command syntax features and pitfalls.
|
---|
200 | * Echoing:: How to control when commands are echoed.
|
---|
201 | * Execution:: How commands are executed.
|
---|
202 | * Parallel:: How commands can be executed in parallel.
|
---|
203 | * Errors:: What happens after a command execution error.
|
---|
204 | * Interrupts:: What happens when a command is interrupted.
|
---|
205 | * Recursion:: Invoking @code{make} from makefiles.
|
---|
206 | * Sequences:: Defining canned sequences of commands.
|
---|
207 | * Empty Commands:: Defining useful, do-nothing commands.
|
---|
208 |
|
---|
209 | Command Syntax
|
---|
210 |
|
---|
211 | * Splitting Lines:: Breaking long command lines for readability.
|
---|
212 | * Variables in Commands:: Using @code{make} variables in commands.
|
---|
213 |
|
---|
214 | Command Execution
|
---|
215 |
|
---|
216 | * Choosing the Shell:: How @code{make} chooses the shell used
|
---|
217 | to run commands.
|
---|
218 |
|
---|
219 | Recursive Use of @code{make}
|
---|
220 |
|
---|
221 | * MAKE Variable:: The special effects of using @samp{$(MAKE)}.
|
---|
222 | * Variables/Recursion:: How to communicate variables to a sub-@code{make}.
|
---|
223 | * Options/Recursion:: How to communicate options to a sub-@code{make}.
|
---|
224 | * -w Option:: How the @samp{-w} or @samp{--print-directory} option
|
---|
225 | helps debug use of recursive @code{make} commands.
|
---|
226 |
|
---|
227 | How to Use Variables
|
---|
228 |
|
---|
229 | * Reference:: How to use the value of a variable.
|
---|
230 | * Flavors:: Variables come in two flavors.
|
---|
231 | * Advanced:: Advanced features for referencing a variable.
|
---|
232 | * Values:: All the ways variables get their values.
|
---|
233 | * Setting:: How to set a variable in the makefile.
|
---|
234 | * Appending:: How to append more text to the old value
|
---|
235 | of a variable.
|
---|
236 | * Override Directive:: How to set a variable in the makefile even if
|
---|
237 | the user has set it with a command argument.
|
---|
238 | * Defining:: An alternate way to set a variable
|
---|
239 | to a verbatim string.
|
---|
240 | * Environment:: Variable values can come from the environment.
|
---|
241 | * Target-specific:: Variable values can be defined on a per-target
|
---|
242 | basis.
|
---|
243 | * Pattern-specific:: Target-specific variable values can be applied
|
---|
244 | to a group of targets that match a pattern.
|
---|
245 |
|
---|
246 | Advanced 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 |
|
---|
252 | Conditional 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 |
|
---|
258 | Functions 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 |
|
---|
273 | How 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 commands
|
---|
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 |
|
---|
289 | Using Implicit Rules
|
---|
290 |
|
---|
291 | * Using Implicit:: How to use an existing implicit rule
|
---|
292 | to get the commands 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 commands 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 |
|
---|
303 | Defining 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 | commands 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 |
|
---|
315 | Using @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 |
|
---|
323 | Implicit 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 |
|
---|
334 | The @code{make} utility automatically determines which pieces of a large
|
---|
335 | program need to be recompiled, and issues commands to recompile them.
|
---|
336 | This manual describes GNU @code{make}, which was implemented by Richard
|
---|
337 | Stallman and Roland McGrath. Development since Version 3.76 has been
|
---|
338 | handled by Paul D. Smith.
|
---|
339 |
|
---|
340 | GNU @code{make} conforms to section 6.2 of @cite{IEEE Standard
|
---|
341 | 1003.2-1992} (POSIX.2).
|
---|
342 | @cindex POSIX
|
---|
343 | @cindex IEEE Standard 1003.2
|
---|
344 | @cindex standards conformance
|
---|
345 |
|
---|
346 | Our 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
|
---|
348 | shell command. Indeed, @code{make} is not limited to programs. You can
|
---|
349 | use it to describe any task where some files must be updated automatically
|
---|
350 | from 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 |
|
---|
363 | To prepare to use @code{make}, you must write a file called
|
---|
364 | the @dfn{makefile} that describes the relationships among files
|
---|
365 | in your program and provides commands for updating each file.
|
---|
366 | In a program, typically, the executable file is updated from object
|
---|
367 | files, which are in turn made by compiling source files.@refill
|
---|
368 |
|
---|
369 | Once a suitable makefile exists, each time you change some source files,
|
---|
370 | this simple shell command:
|
---|
371 |
|
---|
372 | @example
|
---|
373 | make
|
---|
374 | @end example
|
---|
375 |
|
---|
376 | @noindent
|
---|
377 | suffices to perform all necessary recompilations. The @code{make} program
|
---|
378 | uses the makefile data base and the last-modification times of the files to
|
---|
379 | decide which of the files need to be updated. For each of those files, it
|
---|
380 | issues the commands recorded in the data base.
|
---|
381 |
|
---|
382 | You can provide command line arguments to @code{make} to control which
|
---|
383 | files 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 |
|
---|
389 | If you are new to @code{make}, or are looking for a general
|
---|
390 | introduction, read the first few sections of each chapter, skipping the
|
---|
391 | later sections. In each chapter, the first few sections contain
|
---|
392 | introductory or general information and the later sections contain
|
---|
393 | specialized or technical information.
|
---|
394 | @ifnottex
|
---|
395 | The exception is the second chapter, @ref{Introduction, ,An
|
---|
396 | Introduction to Makefiles}, all of which is introductory.
|
---|
397 | @end ifnottex
|
---|
398 | @iftex
|
---|
399 | The exception is @ref{Introduction, ,An Introduction to Makefiles},
|
---|
400 | all of which is introductory.
|
---|
401 | @end iftex
|
---|
402 |
|
---|
403 | If 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
|
---|
406 | Features}, which explains the few things GNU @code{make} lacks that
|
---|
407 | others have.
|
---|
408 |
|
---|
409 | For a quick summary, see @ref{Options Summary}, @ref{Quick Reference},
|
---|
410 | and @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 |
|
---|
418 | If you have problems with GNU @code{make} or think you've found a bug,
|
---|
419 | please report it to the developers; we cannot promise to do anything but
|
---|
420 | we might well want to fix it.
|
---|
421 |
|
---|
422 | Before reporting a bug, make sure you've actually found a real bug.
|
---|
423 | Carefully reread the documentation and see if it really says you can do
|
---|
424 | what you're trying to do. If it's not clear whether you should be able
|
---|
425 | to do something or not, report that too; it's a bug in the
|
---|
426 | documentation!
|
---|
427 |
|
---|
428 | Before reporting a bug or trying to fix it yourself, try to isolate it
|
---|
429 | to the smallest possible makefile that reproduces the problem. Then
|
---|
430 | send us the makefile and the exact results @code{make} gave you,
|
---|
431 | including any error or warning messages. Please don't paraphrase
|
---|
432 | these messages: it's best to cut and paste them into your report.
|
---|
433 | When generating this small makefile, be sure to not use any non-free
|
---|
434 | or unusual tools in your commands: you can almost always emulate what
|
---|
435 | such a tool would do with simple shell commands. Finally, be sure to
|
---|
436 | explain what you expected to occur; this will help us decide whether
|
---|
437 | the problem was really in the documentation.
|
---|
438 |
|
---|
439 | Once you have a precise problem you can report it in one of two ways.
|
---|
440 | Either send electronic mail to:
|
---|
441 |
|
---|
442 | @example
|
---|
443 | bug-make@@gnu.org
|
---|
444 | @end example
|
---|
445 |
|
---|
446 | @noindent
|
---|
447 | or use our Web-based project management tool, at:
|
---|
448 |
|
---|
449 | @example
|
---|
450 | http://savannah.gnu.org/projects/make/
|
---|
451 | @end example
|
---|
452 |
|
---|
453 | @noindent
|
---|
454 | In addition to the information above, please be careful to include the
|
---|
455 | version number of @code{make} you are using. You can get this
|
---|
456 | information with the command @samp{make --version}. Be sure also to
|
---|
457 | include the type of machine and operating system you are using. One
|
---|
458 | way to obtain this information is by looking at the final lines of
|
---|
459 | output 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 |
|
---|
465 | You need a file called a @dfn{makefile} to tell @code{make} what to do.
|
---|
466 | Most often, the makefile tells @code{make} how to compile and link a
|
---|
467 | program.
|
---|
468 | @cindex makefile
|
---|
469 |
|
---|
470 | In this chapter, we will discuss a simple makefile that describes how to
|
---|
471 | compile and link a text editor which consists of eight C source files
|
---|
472 | and three header files. The makefile can also tell @code{make} how to
|
---|
473 | run miscellaneous commands when explicitly asked (for example, to remove
|
---|
474 | certain files as a clean-up operation). To see a more complex example
|
---|
475 | of a makefile, see @ref{Complex Makefile}.
|
---|
476 |
|
---|
477 | When @code{make} recompiles the editor, each changed C source file
|
---|
478 | must be recompiled. If a header file has changed, each C source file
|
---|
479 | that includes the header file must be recompiled to be safe. Each
|
---|
480 | compilation produces an object file corresponding to the source file.
|
---|
481 | Finally, if any source file has been recompiled, all the object files,
|
---|
482 | whether newly made or saved from previous compilations, must be linked
|
---|
483 | together 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 Commands
|
---|
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 |
|
---|
504 | A simple makefile consists of ``rules'' with the following shape:
|
---|
505 |
|
---|
506 | @cindex targets, introduction to
|
---|
507 | @cindex prerequisites, introduction to
|
---|
508 | @cindex commands, introduction to
|
---|
509 | @example
|
---|
510 | @group
|
---|
511 | @var{target} @dots{} : @var{prerequisites} @dots{}
|
---|
512 | @var{command}
|
---|
513 | @dots{}
|
---|
514 | @dots{}
|
---|
515 | @end group
|
---|
516 | @end example
|
---|
517 |
|
---|
518 | A @dfn{target} is usually the name of a file that is generated by a
|
---|
519 | program; examples of targets are executable or object files. A target
|
---|
520 | can also be the name of an action to carry out, such as @samp{clean}
|
---|
521 | (@pxref{Phony Targets}).
|
---|
522 |
|
---|
523 | A @dfn{prerequisite} is a file that is used as input to create the
|
---|
524 | target. A target often depends on several files.
|
---|
525 |
|
---|
526 | @cindex tabs in rules
|
---|
527 | A @dfn{command} is an action that @code{make} carries out.
|
---|
528 | A rule may have more than one command, each on its own line.
|
---|
529 | @strong{Please note:} you need to put a tab character at the beginning of
|
---|
530 | every command line! This is an obscurity that catches the unwary.
|
---|
531 |
|
---|
532 | Usually a command is in a rule with prerequisites and serves to create a
|
---|
533 | target file if any of the prerequisites change. However, the rule that
|
---|
534 | specifies commands for the target need not have prerequisites. For
|
---|
535 | example, the rule containing the delete command associated with the
|
---|
536 | target @samp{clean} does not have prerequisites.
|
---|
537 |
|
---|
538 | A @dfn{rule}, then, explains how and when to remake certain files
|
---|
539 | which are the targets of the particular rule. @code{make} carries out
|
---|
540 | the commands on the prerequisites to create or update the target. A
|
---|
541 | rule can also explain how and when to carry out an action.
|
---|
542 | @xref{Rules, , Writing Rules}.
|
---|
543 |
|
---|
544 | A makefile may contain other text besides rules, but a simple makefile
|
---|
545 | need only contain rules. Rules may look somewhat more complicated
|
---|
546 | than shown in this template, but all fit the pattern more or less.
|
---|
547 |
|
---|
548 | @node Simple Makefile, How Make Works, Rule Introduction, Introduction
|
---|
549 | @section A Simple Makefile
|
---|
550 | @cindex simple makefile
|
---|
551 | @cindex makefile, simple
|
---|
552 |
|
---|
553 | Here is a straightforward makefile that describes the way an
|
---|
554 | executable file called @code{edit} depends on eight object files
|
---|
555 | which, in turn, depend on eight C source and three header files.
|
---|
556 |
|
---|
557 | In this example, all the C files include @file{defs.h}, but only those
|
---|
558 | defining editing commands include @file{command.h}, and only low
|
---|
559 | level files that change the editor buffer include @file{buffer.h}.
|
---|
560 |
|
---|
561 | @example
|
---|
562 | @group
|
---|
563 | edit : main.o kbd.o command.o display.o \
|
---|
564 | insert.o search.o files.o utils.o
|
---|
565 | cc -o edit main.o kbd.o command.o display.o \
|
---|
566 | insert.o search.o files.o utils.o
|
---|
567 |
|
---|
568 | main.o : main.c defs.h
|
---|
569 | cc -c main.c
|
---|
570 | kbd.o : kbd.c defs.h command.h
|
---|
571 | cc -c kbd.c
|
---|
572 | command.o : command.c defs.h command.h
|
---|
573 | cc -c command.c
|
---|
574 | display.o : display.c defs.h buffer.h
|
---|
575 | cc -c display.c
|
---|
576 | insert.o : insert.c defs.h buffer.h
|
---|
577 | cc -c insert.c
|
---|
578 | search.o : search.c defs.h buffer.h
|
---|
579 | cc -c search.c
|
---|
580 | files.o : files.c defs.h buffer.h command.h
|
---|
581 | cc -c files.c
|
---|
582 | utils.o : utils.c defs.h
|
---|
583 | cc -c utils.c
|
---|
584 | clean :
|
---|
585 | rm edit main.o kbd.o command.o display.o \
|
---|
586 | insert.o search.o files.o utils.o
|
---|
587 | @end group
|
---|
588 | @end example
|
---|
589 |
|
---|
590 | @noindent
|
---|
591 | We split each long line into two lines using backslash-newline; this is
|
---|
592 | like using one long line, but is easier to read.
|
---|
593 | @cindex continuation lines
|
---|
594 | @cindex @code{\} (backslash), for continuation lines
|
---|
595 | @cindex backslash (@code{\}), for continuation lines
|
---|
596 | @cindex quoting newline, in makefile
|
---|
597 | @cindex newline, quoting, in makefile
|
---|
598 |
|
---|
599 | To use this makefile to create the executable file called @file{edit},
|
---|
600 | type:
|
---|
601 |
|
---|
602 | @example
|
---|
603 | make
|
---|
604 | @end example
|
---|
605 |
|
---|
606 | To use this makefile to delete the executable file and all the object
|
---|
607 | files from the directory, type:
|
---|
608 |
|
---|
609 | @example
|
---|
610 | make clean
|
---|
611 | @end example
|
---|
612 |
|
---|
613 | In the example makefile, the targets include the executable file
|
---|
614 | @samp{edit}, and the object files @samp{main.o} and @samp{kbd.o}. The
|
---|
615 | prerequisites are files such as @samp{main.c} and @samp{defs.h}.
|
---|
616 | In fact, each @samp{.o} file is both a target and a prerequisite.
|
---|
617 | Commands include @w{@samp{cc -c main.c}} and @w{@samp{cc -c kbd.c}}.
|
---|
618 |
|
---|
619 | When a target is a file, it needs to be recompiled or relinked if any
|
---|
620 | of its prerequisites change. In addition, any prerequisites that are
|
---|
621 | themselves automatically generated should be updated first. In this
|
---|
622 | example, @file{edit} depends on each of the eight object files; the
|
---|
623 | object file @file{main.o} depends on the source file @file{main.c} and
|
---|
624 | on the header file @file{defs.h}.
|
---|
625 |
|
---|
626 | A shell command follows each line that contains a target and
|
---|
627 | prerequisites. These shell commands say how to update the target file.
|
---|
628 | A tab character must come at the beginning of every command line to
|
---|
629 | distinguish command lines from other lines in the makefile. (Bear in
|
---|
630 | mind that @code{make} does not know anything about how the commands
|
---|
631 | work. It is up to you to supply commands that will update the target
|
---|
632 | file properly. All @code{make} does is execute the commands in the rule
|
---|
633 | you have specified when the target file needs to be updated.)
|
---|
634 | @cindex shell command
|
---|
635 |
|
---|
636 | The target @samp{clean} is not a file, but merely the name of an
|
---|
637 | action. Since you
|
---|
638 | normally
|
---|
639 | do not want to carry out the actions in this rule, @samp{clean} is not a prerequisite of any other rule.
|
---|
640 | Consequently, @code{make} never does anything with it unless you tell
|
---|
641 | it specifically. Note that this rule not only is not a prerequisite, it
|
---|
642 | also does not have any prerequisites, so the only purpose of the rule
|
---|
643 | is to run the specified commands. Targets that do not refer to files
|
---|
644 | but are just actions are called @dfn{phony targets}. @xref{Phony
|
---|
645 | Targets}, for information about this kind of target. @xref{Errors, ,
|
---|
646 | Errors in Commands}, to see how to cause @code{make} to ignore errors
|
---|
647 | from @code{rm} or any other command.
|
---|
648 | @cindex @code{clean} target
|
---|
649 | @cindex @code{rm} (shell command)
|
---|
650 |
|
---|
651 | @node How Make Works, Variables Simplify, Simple Makefile, Introduction
|
---|
652 | @comment node-name, next, previous, up
|
---|
653 | @section How @code{make} Processes a Makefile
|
---|
654 | @cindex processing a makefile
|
---|
655 | @cindex makefile, how @code{make} processes
|
---|
656 |
|
---|
657 | By default, @code{make} starts with the first target (not targets whose
|
---|
658 | names start with @samp{.}). This is called the @dfn{default goal}.
|
---|
659 | (@dfn{Goals} are the targets that @code{make} strives ultimately to
|
---|
660 | update. You can override this behavior using the command line
|
---|
661 | (@pxref{Goals, , Arguments to Specify the Goals}) or with the
|
---|
662 | @code{.DEFAULT_GOAL} special variable (@pxref{Special Variables, ,
|
---|
663 | Other Special Variables}).
|
---|
664 | @cindex default goal
|
---|
665 | @cindex goal, default
|
---|
666 | @cindex goal
|
---|
667 |
|
---|
668 | In the simple example of the previous section, the default goal is to
|
---|
669 | update the executable program @file{edit}; therefore, we put that rule
|
---|
670 | first.
|
---|
671 |
|
---|
672 | Thus, when you give the command:
|
---|
673 |
|
---|
674 | @example
|
---|
675 | make
|
---|
676 | @end example
|
---|
677 |
|
---|
678 | @noindent
|
---|
679 | @code{make} reads the makefile in the current directory and begins by
|
---|
680 | processing the first rule. In the example, this rule is for relinking
|
---|
681 | @file{edit}; but before @code{make} can fully process this rule, it
|
---|
682 | must process the rules for the files that @file{edit} depends on,
|
---|
683 | which in this case are the object files. Each of these files is
|
---|
684 | processed according to its own rule. These rules say to update each
|
---|
685 | @samp{.o} file by compiling its source file. The recompilation must
|
---|
686 | be done if the source file, or any of the header files named as
|
---|
687 | prerequisites, is more recent than the object file, or if the object
|
---|
688 | file does not exist.
|
---|
689 |
|
---|
690 | The other rules are processed because their targets appear as
|
---|
691 | prerequisites of the goal. If some other rule is not depended on by the
|
---|
692 | goal (or anything it depends on, etc.), that rule is not processed,
|
---|
693 | unless you tell @code{make} to do so (with a command such as
|
---|
694 | @w{@code{make clean}}).
|
---|
695 |
|
---|
696 | Before recompiling an object file, @code{make} considers updating its
|
---|
697 | prerequisites, the source file and header files. This makefile does not
|
---|
698 | specify anything to be done for them---the @samp{.c} and @samp{.h} files
|
---|
699 | are not the targets of any rules---so @code{make} does nothing for these
|
---|
700 | files. But @code{make} would update automatically generated C programs,
|
---|
701 | such as those made by Bison or Yacc, by their own rules at this time.
|
---|
702 |
|
---|
703 | After recompiling whichever object files need it, @code{make} decides
|
---|
704 | whether to relink @file{edit}. This must be done if the file
|
---|
705 | @file{edit} does not exist, or if any of the object files are newer than
|
---|
706 | it. If an object file was just recompiled, it is now newer than
|
---|
707 | @file{edit}, so @file{edit} is relinked.
|
---|
708 | @cindex relinking
|
---|
709 |
|
---|
710 | Thus, if we change the file @file{insert.c} and run @code{make},
|
---|
711 | @code{make} will compile that file to update @file{insert.o}, and then
|
---|
712 | link @file{edit}. If we change the file @file{command.h} and run
|
---|
713 | @code{make}, @code{make} will recompile the object files @file{kbd.o},
|
---|
714 | @file{command.o} and @file{files.o} and then link the file @file{edit}.
|
---|
715 |
|
---|
716 | @node Variables Simplify, make Deduces, How Make Works, Introduction
|
---|
717 | @section Variables Make Makefiles Simpler
|
---|
718 | @cindex variables
|
---|
719 | @cindex simplifying with variables
|
---|
720 |
|
---|
721 | In our example, we had to list all the object files twice in the rule for
|
---|
722 | @file{edit} (repeated here):
|
---|
723 |
|
---|
724 | @example
|
---|
725 | @group
|
---|
726 | edit : main.o kbd.o command.o display.o \
|
---|
727 | insert.o search.o files.o utils.o
|
---|
728 | cc -o edit main.o kbd.o command.o display.o \
|
---|
729 | insert.o search.o files.o utils.o
|
---|
730 | @end group
|
---|
731 | @end example
|
---|
732 |
|
---|
733 | @cindex @code{objects}
|
---|
734 | Such duplication is error-prone; if a new object file is added to the
|
---|
735 | system, we might add it to one list and forget the other. We can eliminate
|
---|
736 | the risk and simplify the makefile by using a variable. @dfn{Variables}
|
---|
737 | allow a text string to be defined once and substituted in multiple places
|
---|
738 | later (@pxref{Using Variables, ,How to Use Variables}).
|
---|
739 |
|
---|
740 | @cindex @code{OBJECTS}
|
---|
741 | @cindex @code{objs}
|
---|
742 | @cindex @code{OBJS}
|
---|
743 | @cindex @code{obj}
|
---|
744 | @cindex @code{OBJ}
|
---|
745 | It is standard practice for every makefile to have a variable named
|
---|
746 | @code{objects}, @code{OBJECTS}, @code{objs}, @code{OBJS}, @code{obj},
|
---|
747 | or @code{OBJ} which is a list of all object file names. We would
|
---|
748 | define such a variable @code{objects} with a line like this in the
|
---|
749 | makefile:@refill
|
---|
750 |
|
---|
751 | @example
|
---|
752 | @group
|
---|
753 | objects = main.o kbd.o command.o display.o \
|
---|
754 | insert.o search.o files.o utils.o
|
---|
755 | @end group
|
---|
756 | @end example
|
---|
757 |
|
---|
758 | @noindent
|
---|
759 | Then, each place we want to put a list of the object file names, we can
|
---|
760 | substitute the variable's value by writing @samp{$(objects)}
|
---|
761 | (@pxref{Using Variables, ,How to Use Variables}).
|
---|
762 |
|
---|
763 | Here is how the complete simple makefile looks when you use a variable
|
---|
764 | for the object files:
|
---|
765 |
|
---|
766 | @example
|
---|
767 | @group
|
---|
768 | objects = main.o kbd.o command.o display.o \
|
---|
769 | insert.o search.o files.o utils.o
|
---|
770 |
|
---|
771 | edit : $(objects)
|
---|
772 | cc -o edit $(objects)
|
---|
773 | main.o : main.c defs.h
|
---|
774 | cc -c main.c
|
---|
775 | kbd.o : kbd.c defs.h command.h
|
---|
776 | cc -c kbd.c
|
---|
777 | command.o : command.c defs.h command.h
|
---|
778 | cc -c command.c
|
---|
779 | display.o : display.c defs.h buffer.h
|
---|
780 | cc -c display.c
|
---|
781 | insert.o : insert.c defs.h buffer.h
|
---|
782 | cc -c insert.c
|
---|
783 | search.o : search.c defs.h buffer.h
|
---|
784 | cc -c search.c
|
---|
785 | files.o : files.c defs.h buffer.h command.h
|
---|
786 | cc -c files.c
|
---|
787 | utils.o : utils.c defs.h
|
---|
788 | cc -c utils.c
|
---|
789 | clean :
|
---|
790 | rm edit $(objects)
|
---|
791 | @end group
|
---|
792 | @end example
|
---|
793 |
|
---|
794 | @node make Deduces, Combine By Prerequisite, Variables Simplify, Introduction
|
---|
795 | @section Letting @code{make} Deduce the Commands
|
---|
796 | @cindex deducing commands (implicit rules)
|
---|
797 | @cindex implicit rule, introduction to
|
---|
798 | @cindex rule, implicit, introduction to
|
---|
799 |
|
---|
800 | It is not necessary to spell out the commands for compiling the individual
|
---|
801 | C source files, because @code{make} can figure them out: it has an
|
---|
802 | @dfn{implicit rule} for updating a @samp{.o} file from a correspondingly
|
---|
803 | named @samp{.c} file using a @samp{cc -c} command. For example, it will
|
---|
804 | use the command @samp{cc -c main.c -o main.o} to compile @file{main.c} into
|
---|
805 | @file{main.o}. We can therefore omit the commands from the rules for the
|
---|
806 | object files. @xref{Implicit Rules, ,Using Implicit Rules}.@refill
|
---|
807 |
|
---|
808 | When a @samp{.c} file is used automatically in this way, it is also
|
---|
809 | automatically added to the list of prerequisites. We can therefore omit
|
---|
810 | the @samp{.c} files from the prerequisites, provided we omit the commands.
|
---|
811 |
|
---|
812 | Here is the entire example, with both of these changes, and a variable
|
---|
813 | @code{objects} as suggested above:
|
---|
814 |
|
---|
815 | @example
|
---|
816 | @group
|
---|
817 | objects = main.o kbd.o command.o display.o \
|
---|
818 | insert.o search.o files.o utils.o
|
---|
819 |
|
---|
820 | edit : $(objects)
|
---|
821 | cc -o edit $(objects)
|
---|
822 |
|
---|
823 | main.o : defs.h
|
---|
824 | kbd.o : defs.h command.h
|
---|
825 | command.o : defs.h command.h
|
---|
826 | display.o : defs.h buffer.h
|
---|
827 | insert.o : defs.h buffer.h
|
---|
828 | search.o : defs.h buffer.h
|
---|
829 | files.o : defs.h buffer.h command.h
|
---|
830 | utils.o : defs.h
|
---|
831 |
|
---|
832 | .PHONY : clean
|
---|
833 | clean :
|
---|
834 | rm edit $(objects)
|
---|
835 | @end group
|
---|
836 | @end example
|
---|
837 |
|
---|
838 | @noindent
|
---|
839 | This is how we would write the makefile in actual practice. (The
|
---|
840 | complications associated with @samp{clean} are described elsewhere.
|
---|
841 | See @ref{Phony Targets}, and @ref{Errors, ,Errors in Commands}.)
|
---|
842 |
|
---|
843 | Because implicit rules are so convenient, they are important. You
|
---|
844 | will see them used frequently.@refill
|
---|
845 |
|
---|
846 | @node Combine By Prerequisite, Cleanup, make Deduces, Introduction
|
---|
847 | @section Another Style of Makefile
|
---|
848 | @cindex combining rules by prerequisite
|
---|
849 |
|
---|
850 | When the objects of a makefile are created only by implicit rules, an
|
---|
851 | alternative style of makefile is possible. In this style of makefile,
|
---|
852 | you group entries by their prerequisites instead of by their targets.
|
---|
853 | Here is what one looks like:
|
---|
854 |
|
---|
855 | @example
|
---|
856 | @group
|
---|
857 | objects = main.o kbd.o command.o display.o \
|
---|
858 | insert.o search.o files.o utils.o
|
---|
859 |
|
---|
860 | edit : $(objects)
|
---|
861 | cc -o edit $(objects)
|
---|
862 |
|
---|
863 | $(objects) : defs.h
|
---|
864 | kbd.o command.o files.o : command.h
|
---|
865 | display.o insert.o search.o files.o : buffer.h
|
---|
866 | @end group
|
---|
867 | @end example
|
---|
868 |
|
---|
869 | @noindent
|
---|
870 | Here @file{defs.h} is given as a prerequisite of all the object files;
|
---|
871 | @file{command.h} and @file{buffer.h} are prerequisites of the specific
|
---|
872 | object files listed for them.
|
---|
873 |
|
---|
874 | Whether this is better is a matter of taste: it is more compact, but some
|
---|
875 | people dislike it because they find it clearer to put all the information
|
---|
876 | about each target in one place.
|
---|
877 |
|
---|
878 | @node Cleanup, , Combine By Prerequisite, Introduction
|
---|
879 | @section Rules for Cleaning the Directory
|
---|
880 | @cindex cleaning up
|
---|
881 | @cindex removing, to clean up
|
---|
882 |
|
---|
883 | Compiling a program is not the only thing you might want to write rules
|
---|
884 | for. Makefiles commonly tell how to do a few other things besides
|
---|
885 | compiling a program: for example, how to delete all the object files
|
---|
886 | and executables so that the directory is @samp{clean}.
|
---|
887 |
|
---|
888 | @cindex @code{clean} target
|
---|
889 | Here is how we
|
---|
890 | could write a @code{make} rule for cleaning our example editor:
|
---|
891 |
|
---|
892 | @example
|
---|
893 | @group
|
---|
894 | clean:
|
---|
895 | rm edit $(objects)
|
---|
896 | @end group
|
---|
897 | @end example
|
---|
898 |
|
---|
899 | In practice, we might want to write the rule in a somewhat more
|
---|
900 | complicated manner to handle unanticipated situations. We would do this:
|
---|
901 |
|
---|
902 | @example
|
---|
903 | @group
|
---|
904 | .PHONY : clean
|
---|
905 | clean :
|
---|
906 | -rm edit $(objects)
|
---|
907 | @end group
|
---|
908 | @end example
|
---|
909 |
|
---|
910 | @noindent
|
---|
911 | This prevents @code{make} from getting confused by an actual file
|
---|
912 | called @file{clean} and causes it to continue in spite of errors from
|
---|
913 | @code{rm}. (See @ref{Phony Targets}, and @ref{Errors, ,Errors in
|
---|
914 | Commands}.)
|
---|
915 |
|
---|
916 | @noindent
|
---|
917 | A rule such as this should not be placed at the beginning of the
|
---|
918 | makefile, because we do not want it to run by default! Thus, in the
|
---|
919 | example makefile, we want the rule for @code{edit}, which recompiles
|
---|
920 | the editor, to remain the default goal.
|
---|
921 |
|
---|
922 | Since @code{clean} is not a prerequisite of @code{edit}, this rule will not
|
---|
923 | run at all if we give the command @samp{make} with no arguments. In
|
---|
924 | order to make the rule run, we have to type @samp{make clean}.
|
---|
925 | @xref{Running, ,How to Run @code{make}}.
|
---|
926 |
|
---|
927 | @node Makefiles, Rules, Introduction, Top
|
---|
928 | @chapter Writing Makefiles
|
---|
929 |
|
---|
930 | @cindex makefile, how to write
|
---|
931 | The information that tells @code{make} how to recompile a system comes from
|
---|
932 | reading a data base called the @dfn{makefile}.
|
---|
933 |
|
---|
934 | @menu
|
---|
935 | * Makefile Contents:: What makefiles contain.
|
---|
936 | * Makefile Names:: How to name your makefile.
|
---|
937 | * Include:: How one makefile can use another makefile.
|
---|
938 | * MAKEFILES Variable:: The environment can specify extra makefiles.
|
---|
939 | * MAKEFILE_LIST Variable:: Discover which makefiles have been read.
|
---|
940 | * Special Variables:: Other special variables.
|
---|
941 | * Remaking Makefiles:: How makefiles get remade.
|
---|
942 | * Overriding Makefiles:: How to override part of one makefile
|
---|
943 | with another makefile.
|
---|
944 | * Reading Makefiles:: How makefiles are parsed.
|
---|
945 | * Secondary Expansion:: How and when secondary expansion is performed.
|
---|
946 | @end menu
|
---|
947 |
|
---|
948 | @node Makefile Contents, Makefile Names, Makefiles, Makefiles
|
---|
949 | @section What Makefiles Contain
|
---|
950 |
|
---|
951 | Makefiles contain five kinds of things: @dfn{explicit rules},
|
---|
952 | @dfn{implicit rules}, @dfn{variable definitions}, @dfn{directives},
|
---|
953 | and @dfn{comments}. Rules, variables, and directives are described at
|
---|
954 | length in later chapters.@refill
|
---|
955 |
|
---|
956 | @itemize @bullet
|
---|
957 | @cindex rule, explicit, definition of
|
---|
958 | @cindex explicit rule, definition of
|
---|
959 | @item
|
---|
960 | An @dfn{explicit rule} says when and how to remake one or more files,
|
---|
961 | called the rule's @dfn{targets}. It lists the other files that the
|
---|
962 | targets depend on, called the @dfn{prerequisites} of the target, and
|
---|
963 | may also give commands to use to create or update the targets.
|
---|
964 | @xref{Rules, ,Writing Rules}.
|
---|
965 |
|
---|
966 | @cindex rule, implicit, definition of
|
---|
967 | @cindex implicit rule, definition of
|
---|
968 | @item
|
---|
969 | An @dfn{implicit rule} says when and how to remake a class of files
|
---|
970 | based on their names. It describes how a target may depend on a file
|
---|
971 | with a name similar to the target and gives commands to create or
|
---|
972 | update such a target. @xref{Implicit Rules, ,Using Implicit Rules}.
|
---|
973 |
|
---|
974 | @cindex variable definition
|
---|
975 | @item
|
---|
976 | A @dfn{variable definition} is a line that specifies a text string
|
---|
977 | value for a variable that can be substituted into the text later. The
|
---|
978 | simple makefile example shows a variable definition for @code{objects}
|
---|
979 | as a list of all object files (@pxref{Variables Simplify, , Variables
|
---|
980 | Make Makefiles Simpler}).
|
---|
981 |
|
---|
982 | @cindex directive
|
---|
983 | @item
|
---|
984 | A @dfn{directive} is a command for @code{make} to do something special while
|
---|
985 | reading the makefile. These include:
|
---|
986 |
|
---|
987 | @itemize @bullet
|
---|
988 | @item
|
---|
989 | Reading another makefile (@pxref{Include, ,Including Other Makefiles}).
|
---|
990 |
|
---|
991 | @item
|
---|
992 | Deciding (based on the values of variables) whether to use or
|
---|
993 | ignore a part of the makefile (@pxref{Conditionals, ,Conditional Parts of Makefiles}).
|
---|
994 |
|
---|
995 | @item
|
---|
996 | Defining a variable from a verbatim string containing multiple lines
|
---|
997 | (@pxref{Defining, ,Defining Variables Verbatim}).
|
---|
998 | @end itemize
|
---|
999 |
|
---|
1000 | @cindex comments, in makefile
|
---|
1001 | @cindex @code{#} (comments), in makefile
|
---|
1002 | @item
|
---|
1003 | @samp{#} in a line of a makefile starts a @dfn{comment}. It and the
|
---|
1004 | rest of the line are ignored, except that a trailing backslash not
|
---|
1005 | escaped by another backslash will continue the comment across multiple
|
---|
1006 | lines. A line containing just a comment (with perhaps spaces before
|
---|
1007 | it) is effectively blank, and is ignored. If you want a literal
|
---|
1008 | @code{#}, escape it with a backslash (e.g., @code{\#}). Comments may
|
---|
1009 | appear on any line in the makefile, although they are treated
|
---|
1010 | specially in certain situations.
|
---|
1011 |
|
---|
1012 | Within a command script (if the line begins with a TAB character) the
|
---|
1013 | entire line is passed to the shell, just as with any other line that
|
---|
1014 | begins with a TAB. The shell decides how to interpret the text:
|
---|
1015 | whether or not this is a comment is up to the shell.
|
---|
1016 |
|
---|
1017 | Within a @code{define} directive, comments are not ignored during the
|
---|
1018 | definition of the variable, but rather kept intact in the value of the
|
---|
1019 | variable. When the variable is expanded they will either be treated
|
---|
1020 | as @code{make} comments or as command script text, depending on the
|
---|
1021 | context in which the variable is evaluated.
|
---|
1022 | @end itemize
|
---|
1023 |
|
---|
1024 | @node Makefile Names, Include, Makefile Contents, Makefiles
|
---|
1025 | @section What Name to Give Your Makefile
|
---|
1026 | @cindex makefile name
|
---|
1027 | @cindex name of makefile
|
---|
1028 | @cindex default makefile name
|
---|
1029 | @cindex file name of makefile
|
---|
1030 |
|
---|
1031 | @c following paragraph rewritten to avoid overfull hbox
|
---|
1032 | By default, when @code{make} looks for the makefile, it tries the
|
---|
1033 | following names, in order: @file{GNUmakefile}, @file{makefile}
|
---|
1034 | and @file{Makefile}.@refill
|
---|
1035 | @findex Makefile
|
---|
1036 | @findex GNUmakefile
|
---|
1037 | @findex makefile
|
---|
1038 |
|
---|
1039 | @cindex @code{README}
|
---|
1040 | Normally you should call your makefile either @file{makefile} or
|
---|
1041 | @file{Makefile}. (We recommend @file{Makefile} because it appears
|
---|
1042 | prominently near the beginning of a directory listing, right near other
|
---|
1043 | important files such as @file{README}.) The first name checked,
|
---|
1044 | @file{GNUmakefile}, is not recommended for most makefiles. You should
|
---|
1045 | use this name if you have a makefile that is specific to GNU
|
---|
1046 | @code{make}, and will not be understood by other versions of
|
---|
1047 | @code{make}. Other @code{make} programs look for @file{makefile} and
|
---|
1048 | @file{Makefile}, but not @file{GNUmakefile}.
|
---|
1049 |
|
---|
1050 | If @code{make} finds none of these names, it does not use any makefile.
|
---|
1051 | Then you must specify a goal with a command argument, and @code{make}
|
---|
1052 | will attempt to figure out how to remake it using only its built-in
|
---|
1053 | implicit rules. @xref{Implicit Rules, ,Using Implicit Rules}.
|
---|
1054 |
|
---|
1055 | @cindex @code{-f}
|
---|
1056 | @cindex @code{--file}
|
---|
1057 | @cindex @code{--makefile}
|
---|
1058 | If you want to use a nonstandard name for your makefile, you can specify
|
---|
1059 | the makefile name with the @samp{-f} or @samp{--file} option. The
|
---|
1060 | arguments @w{@samp{-f @var{name}}} or @w{@samp{--file=@var{name}}} tell
|
---|
1061 | @code{make} to read the file @var{name} as the makefile. If you use
|
---|
1062 | more than one @samp{-f} or @samp{--file} option, you can specify several
|
---|
1063 | makefiles. All the makefiles are effectively concatenated in the order
|
---|
1064 | specified. The default makefile names @file{GNUmakefile},
|
---|
1065 | @file{makefile} and @file{Makefile} are not checked automatically if you
|
---|
1066 | specify @samp{-f} or @samp{--file}.@refill
|
---|
1067 | @cindex specifying makefile name
|
---|
1068 | @cindex makefile name, how to specify
|
---|
1069 | @cindex name of makefile, how to specify
|
---|
1070 | @cindex file name of makefile, how to specify
|
---|
1071 |
|
---|
1072 | @node Include, MAKEFILES Variable, Makefile Names, Makefiles
|
---|
1073 | @section Including Other Makefiles
|
---|
1074 | @cindex including other makefiles
|
---|
1075 | @cindex makefile, including
|
---|
1076 |
|
---|
1077 | @findex include
|
---|
1078 | The @code{include} directive tells @code{make} to suspend reading the
|
---|
1079 | current makefile and read one or more other makefiles before continuing.
|
---|
1080 | The directive is a line in the makefile that looks like this:
|
---|
1081 |
|
---|
1082 | @example
|
---|
1083 | include @var{filenames}@dots{}
|
---|
1084 | @end example
|
---|
1085 |
|
---|
1086 | @noindent
|
---|
1087 | @var{filenames} can contain shell file name patterns. If
|
---|
1088 | @var{filenames} is empty, nothing is included and no error is printed.
|
---|
1089 | @cindex shell file name pattern (in @code{include})
|
---|
1090 | @cindex shell wildcards (in @code{include})
|
---|
1091 | @cindex wildcard, in @code{include}
|
---|
1092 |
|
---|
1093 | Extra spaces are allowed and ignored at the beginning of the line, but
|
---|
1094 | a tab is not allowed. (If the line begins with a tab, it will be
|
---|
1095 | considered a command line.) Whitespace is required between
|
---|
1096 | @code{include} and the file names, and between file names; extra
|
---|
1097 | whitespace is ignored there and at the end of the directive. A
|
---|
1098 | comment starting with @samp{#} is allowed at the end of the line. If
|
---|
1099 | the file names contain any variable or function references, they are
|
---|
1100 | expanded. @xref{Using Variables, ,How to Use Variables}.
|
---|
1101 |
|
---|
1102 | For example, if you have three @file{.mk} files, @file{a.mk},
|
---|
1103 | @file{b.mk}, and @file{c.mk}, and @code{$(bar)} expands to
|
---|
1104 | @code{bish bash}, then the following expression
|
---|
1105 |
|
---|
1106 | @example
|
---|
1107 | include foo *.mk $(bar)
|
---|
1108 | @end example
|
---|
1109 |
|
---|
1110 | is equivalent to
|
---|
1111 |
|
---|
1112 | @example
|
---|
1113 | include foo a.mk b.mk c.mk bish bash
|
---|
1114 | @end example
|
---|
1115 |
|
---|
1116 | When @code{make} processes an @code{include} directive, it suspends
|
---|
1117 | reading of the containing makefile and reads from each listed file in
|
---|
1118 | turn. When that is finished, @code{make} resumes reading the
|
---|
1119 | makefile in which the directive appears.
|
---|
1120 |
|
---|
1121 | One occasion for using @code{include} directives is when several programs,
|
---|
1122 | handled by individual makefiles in various directories, need to use a
|
---|
1123 | common set of variable definitions
|
---|
1124 | (@pxref{Setting, ,Setting Variables}) or pattern rules
|
---|
1125 | (@pxref{Pattern Rules, ,Defining and Redefining Pattern Rules}).
|
---|
1126 |
|
---|
1127 | Another such occasion is when you want to generate prerequisites from
|
---|
1128 | source files automatically; the prerequisites can be put in a file that
|
---|
1129 | is included by the main makefile. This practice is generally cleaner
|
---|
1130 | than that of somehow appending the prerequisites to the end of the main
|
---|
1131 | makefile as has been traditionally done with other versions of
|
---|
1132 | @code{make}. @xref{Automatic Prerequisites}.
|
---|
1133 | @cindex prerequisites, automatic generation
|
---|
1134 | @cindex automatic generation of prerequisites
|
---|
1135 | @cindex generating prerequisites automatically
|
---|
1136 |
|
---|
1137 | @cindex @code{-I}
|
---|
1138 | @cindex @code{--include-dir}
|
---|
1139 | @cindex included makefiles, default directories
|
---|
1140 | @cindex default directories for included makefiles
|
---|
1141 | @findex /usr/gnu/include
|
---|
1142 | @findex /usr/local/include
|
---|
1143 | @findex /usr/include
|
---|
1144 | If the specified name does not start with a slash, and the file is not
|
---|
1145 | found in the current directory, several other directories are searched.
|
---|
1146 | First, any directories you have specified with the @samp{-I} or
|
---|
1147 | @samp{--include-dir} option are searched
|
---|
1148 | (@pxref{Options Summary, ,Summary of Options}).
|
---|
1149 | Then the following directories (if they exist)
|
---|
1150 | are searched, in this order:
|
---|
1151 | @file{@var{prefix}/include} (normally @file{/usr/local/include}
|
---|
1152 | @footnote{GNU Make compiled for MS-DOS and MS-Windows behaves as if
|
---|
1153 | @var{prefix} has been defined to be the root of the DJGPP tree
|
---|
1154 | hierarchy.})
|
---|
1155 | @file{/usr/gnu/include},
|
---|
1156 | @file{/usr/local/include}, @file{/usr/include}.
|
---|
1157 |
|
---|
1158 | If an included makefile cannot be found in any of these directories, a
|
---|
1159 | warning message is generated, but it is not an immediately fatal error;
|
---|
1160 | processing of the makefile containing the @code{include} continues.
|
---|
1161 | Once it has finished reading makefiles, @code{make} will try to remake
|
---|
1162 | any that are out of date or don't exist.
|
---|
1163 | @xref{Remaking Makefiles, ,How Makefiles Are Remade}.
|
---|
1164 | Only after it has tried to find a way to remake a makefile and failed,
|
---|
1165 | will @code{make} diagnose the missing makefile as a fatal error.
|
---|
1166 |
|
---|
1167 | If you want @code{make} to simply ignore a makefile which does not exist
|
---|
1168 | and cannot be remade, with no error message, use the @w{@code{-include}}
|
---|
1169 | directive instead of @code{include}, like this:
|
---|
1170 |
|
---|
1171 | @example
|
---|
1172 | -include @var{filenames}@dots{}
|
---|
1173 | @end example
|
---|
1174 |
|
---|
1175 | This acts like @code{include} in every way except that there is no
|
---|
1176 | error (not even a warning) if any of the @var{filenames} do not exist.
|
---|
1177 | For compatibility with some other @code{make} implementations,
|
---|
1178 | @code{sinclude} is another name for @w{@code{-include}}.
|
---|
1179 |
|
---|
1180 | @node MAKEFILES Variable, MAKEFILE_LIST Variable, Include, Makefiles
|
---|
1181 | @section The Variable @code{MAKEFILES}
|
---|
1182 | @cindex makefile, and @code{MAKEFILES} variable
|
---|
1183 | @cindex including (@code{MAKEFILES} variable)
|
---|
1184 |
|
---|
1185 | @vindex MAKEFILES
|
---|
1186 | If the environment variable @code{MAKEFILES} is defined, @code{make}
|
---|
1187 | considers its value as a list of names (separated by whitespace) of
|
---|
1188 | additional makefiles to be read before the others. This works much like
|
---|
1189 | the @code{include} directive: various directories are searched for those
|
---|
1190 | files (@pxref{Include, ,Including Other Makefiles}). In addition, the
|
---|
1191 | default goal is never taken from one of these makefiles and it is not an
|
---|
1192 | error if the files listed in @code{MAKEFILES} are not found.@refill
|
---|
1193 |
|
---|
1194 | @cindex recursion, and @code{MAKEFILES} variable
|
---|
1195 | The main use of @code{MAKEFILES} is in communication between recursive
|
---|
1196 | invocations of @code{make} (@pxref{Recursion, ,Recursive Use of
|
---|
1197 | @code{make}}). It usually is not desirable to set the environment
|
---|
1198 | variable before a top-level invocation of @code{make}, because it is
|
---|
1199 | usually better not to mess with a makefile from outside. However, if
|
---|
1200 | you are running @code{make} without a specific makefile, a makefile in
|
---|
1201 | @code{MAKEFILES} can do useful things to help the built-in implicit
|
---|
1202 | rules work better, such as defining search paths (@pxref{Directory Search}).
|
---|
1203 |
|
---|
1204 | Some users are tempted to set @code{MAKEFILES} in the environment
|
---|
1205 | automatically on login, and program makefiles to expect this to be done.
|
---|
1206 | This is a very bad idea, because such makefiles will fail to work if run by
|
---|
1207 | anyone else. It is much better to write explicit @code{include} directives
|
---|
1208 | in the makefiles. @xref{Include, , Including Other Makefiles}.
|
---|
1209 |
|
---|
1210 | @node MAKEFILE_LIST Variable, Special Variables, MAKEFILES Variable, Makefiles
|
---|
1211 | @comment node-name, next, previous, up
|
---|
1212 | @section The Variable @code{MAKEFILE_LIST}
|
---|
1213 | @cindex makefiles, and @code{MAKEFILE_LIST} variable
|
---|
1214 | @cindex including (@code{MAKEFILE_LIST} variable)
|
---|
1215 | @vindex MAKEFILE_LIST
|
---|
1216 |
|
---|
1217 | As @code{make} reads various makefiles, including any obtained from the
|
---|
1218 | @code{MAKEFILES} variable, the command line, the default files, or
|
---|
1219 | from @code{include} directives, their names will be automatically
|
---|
1220 | appended to the @code{MAKEFILE_LIST} variable. They are added right
|
---|
1221 | before @code{make} begins to parse them.
|
---|
1222 |
|
---|
1223 | This means that if the first thing a makefile does is examine the last
|
---|
1224 | word in this variable, it will be the name of the current makefile.
|
---|
1225 | Once the current makefile has used @code{include}, however, the last
|
---|
1226 | word will be the just-included makefile.
|
---|
1227 |
|
---|
1228 | If a makefile named @code{Makefile} has this content:
|
---|
1229 |
|
---|
1230 | @example
|
---|
1231 | @group
|
---|
1232 | name1 := $(lastword $(MAKEFILE_LIST))
|
---|
1233 |
|
---|
1234 | include inc.mk
|
---|
1235 |
|
---|
1236 | name2 := $(lastword $(MAKEFILE_LIST))
|
---|
1237 |
|
---|
1238 | all:
|
---|
1239 | @@echo name1 = $(name1)
|
---|
1240 | @@echo name2 = $(name2)
|
---|
1241 | @end group
|
---|
1242 | @end example
|
---|
1243 |
|
---|
1244 | @noindent
|
---|
1245 | then you would expect to see this output:
|
---|
1246 |
|
---|
1247 | @example
|
---|
1248 | @group
|
---|
1249 | name1 = Makefile
|
---|
1250 | name2 = inc.mk
|
---|
1251 | @end group
|
---|
1252 | @end example
|
---|
1253 |
|
---|
1254 | @xref{Text Functions}, for more information on the @code{word} and
|
---|
1255 | @code{words} functions used above. @xref{Flavors, The Two Flavors of
|
---|
1256 | Variables}, for more information on simply-expanded (@code{:=})
|
---|
1257 | variable definitions.
|
---|
1258 |
|
---|
1259 | @node Special Variables, Remaking Makefiles, MAKEFILE_LIST Variable, Makefiles
|
---|
1260 | @comment node-name, next, previous, up
|
---|
1261 | @section Other Special Variables
|
---|
1262 | @cindex makefiles, and special variables
|
---|
1263 | @cindex special variables
|
---|
1264 |
|
---|
1265 | GNU @code{make} also supports other special variables. Unless
|
---|
1266 | otherwise documented here, these values lose their special properties
|
---|
1267 | if they are set by a makefile or on the command line.
|
---|
1268 |
|
---|
1269 | @table @code
|
---|
1270 |
|
---|
1271 | @vindex .DEFAULT_GOAL @r{(define default goal)}
|
---|
1272 | @item .DEFAULT_GOAL
|
---|
1273 | Sets the default goal to be used if no targets were specified on the
|
---|
1274 | command line (@pxref{Goals, , Arguments to Specify the Goals}). The
|
---|
1275 | @code{.DEFAULT_GOAL} variable allows you to discover the current
|
---|
1276 | default goal, restart the default goal selection algorithm by clearing
|
---|
1277 | its value, or to explicitly set the default goal. The following
|
---|
1278 | example illustrates these cases:
|
---|
1279 |
|
---|
1280 | @example
|
---|
1281 | @group
|
---|
1282 | # Query the default goal.
|
---|
1283 | ifeq ($(.DEFAULT_GOAL),)
|
---|
1284 | $(warning no default goal is set)
|
---|
1285 | endif
|
---|
1286 |
|
---|
1287 | .PHONY: foo
|
---|
1288 | foo: ; @@echo $@@
|
---|
1289 |
|
---|
1290 | $(warning default goal is $(.DEFAULT_GOAL))
|
---|
1291 |
|
---|
1292 | # Reset the default goal.
|
---|
1293 | .DEFAULT_GOAL :=
|
---|
1294 |
|
---|
1295 | .PHONY: bar
|
---|
1296 | bar: ; @@echo $@@
|
---|
1297 |
|
---|
1298 | $(warning default goal is $(.DEFAULT_GOAL))
|
---|
1299 |
|
---|
1300 | # Set our own.
|
---|
1301 | .DEFAULT_GOAL := foo
|
---|
1302 | @end group
|
---|
1303 | @end example
|
---|
1304 |
|
---|
1305 | This makefile prints:
|
---|
1306 |
|
---|
1307 | @example
|
---|
1308 | @group
|
---|
1309 | no default goal is set
|
---|
1310 | default goal is foo
|
---|
1311 | default goal is bar
|
---|
1312 | foo
|
---|
1313 | @end group
|
---|
1314 | @end example
|
---|
1315 |
|
---|
1316 | Note that assigning more than one target name to @code{.DEFAULT_GOAL} is
|
---|
1317 | illegal and will result in an error.
|
---|
1318 |
|
---|
1319 | @vindex MAKE_RESTARTS @r{(number of times @code{make} has restarted)}
|
---|
1320 | @item MAKE_RESTARTS
|
---|
1321 | This variable is set only if this instance of @code{make} has
|
---|
1322 | restarted (@pxref{Remaking Makefiles, , How Makefiles Are Remade}): it
|
---|
1323 | will contain the number of times this instance has restarted. Note
|
---|
1324 | this is not the same as recursion (counted by the @code{MAKELEVEL}
|
---|
1325 | variable). You should not set, modify, or export this variable.
|
---|
1326 |
|
---|
1327 | @vindex .VARIABLES @r{(list of variables)}
|
---|
1328 | @item .VARIABLES
|
---|
1329 | Expands to a list of the @emph{names} of all global variables defined
|
---|
1330 | so far. This includes variables which have empty values, as well as
|
---|
1331 | built-in variables (@pxref{Implicit Variables, , Variables Used by
|
---|
1332 | Implicit Rules}), but does not include any variables which are only
|
---|
1333 | defined in a target-specific context. Note that any value you assign
|
---|
1334 | to this variable will be ignored; it will always return its special
|
---|
1335 | value.
|
---|
1336 |
|
---|
1337 | @c @vindex .TARGETS @r{(list of targets)}
|
---|
1338 | @c @item .TARGETS
|
---|
1339 | @c The second special variable is @code{.TARGETS}. When expanded, the
|
---|
1340 | @c value consists of a list of all targets defined in all makefiles read
|
---|
1341 | @c up until that point. Note it's not enough for a file to be simply
|
---|
1342 | @c mentioned in the makefile to be listed in this variable, even if it
|
---|
1343 | @c would match an implicit rule and become an ``implicit target''. The
|
---|
1344 | @c file must appear as a target, on the left-hand side of a ``:'', to be
|
---|
1345 | @c considered a target for the purposes of this variable.
|
---|
1346 |
|
---|
1347 | @vindex .FEATURES @r{(list of supported features)}
|
---|
1348 | @item .FEATURES
|
---|
1349 | Expands to a list of special features supported by this version of
|
---|
1350 | @code{make}. Possible values include:
|
---|
1351 |
|
---|
1352 | @table @samp
|
---|
1353 |
|
---|
1354 | @item archives
|
---|
1355 | Supports @code{ar} (archive) files using special filename syntax.
|
---|
1356 | @xref{Archives, ,Using @code{make} to Update Archive Files}.
|
---|
1357 |
|
---|
1358 | @item check-symlink
|
---|
1359 | Supports the @code{-L} (@code{--check-symlink-times}) flag.
|
---|
1360 | @xref{Options Summary, ,Summary of Options}.
|
---|
1361 |
|
---|
1362 | @item else-if
|
---|
1363 | Supports ``else if'' non-nested conditionals. @xref{Conditional
|
---|
1364 | Syntax, ,Syntax of Conditionals}.
|
---|
1365 |
|
---|
1366 | @item jobserver
|
---|
1367 | Supports ``job server'' enhanced parallel builds. @xref{Parallel,
|
---|
1368 | ,Parallel Execution}.
|
---|
1369 |
|
---|
1370 | @item second-expansion
|
---|
1371 | Supports secondary expansion of prerequisite lists.
|
---|
1372 |
|
---|
1373 | @item order-only
|
---|
1374 | Supports order-only prerequisites. @xref{Prerequisite Types, ,Types
|
---|
1375 | of Prerequisites}.
|
---|
1376 |
|
---|
1377 | @item target-specific
|
---|
1378 | Supports target-specific and pattern-specific variable assignments.
|
---|
1379 | @xref{Target-specific, ,Target-specific Variable Values}.
|
---|
1380 |
|
---|
1381 | @end table
|
---|
1382 |
|
---|
1383 | @vindex .INCLUDE_DIRS @r{(list of include directories)}
|
---|
1384 | @item .INCLUDE_DIRS
|
---|
1385 | Expands to a list of directories that @code{make} searches for
|
---|
1386 | included makefiles (@pxref{Include, , Including Other Makefiles}).
|
---|
1387 |
|
---|
1388 | @end table
|
---|
1389 |
|
---|
1390 | @node Remaking Makefiles, Overriding Makefiles, Special Variables, Makefiles
|
---|
1391 | @section How Makefiles Are Remade
|
---|
1392 |
|
---|
1393 | @cindex updating makefiles
|
---|
1394 | @cindex remaking makefiles
|
---|
1395 | @cindex makefile, remaking of
|
---|
1396 | Sometimes makefiles can be remade from other files, such as RCS or SCCS
|
---|
1397 | files. If a makefile can be remade from other files, you probably want
|
---|
1398 | @code{make} to get an up-to-date version of the makefile to read in.
|
---|
1399 |
|
---|
1400 | To this end, after reading in all makefiles, @code{make} will consider
|
---|
1401 | each as a goal target and attempt to update it. If a makefile has a
|
---|
1402 | rule which says how to update it (found either in that very makefile or
|
---|
1403 | in another one) or if an implicit rule applies to it (@pxref{Implicit
|
---|
1404 | Rules, ,Using Implicit Rules}), it will be updated if necessary. After
|
---|
1405 | all makefiles have been checked, if any have actually been changed,
|
---|
1406 | @code{make} starts with a clean slate and reads all the makefiles over
|
---|
1407 | again. (It will also attempt to update each of them over again, but
|
---|
1408 | normally this will not change them again, since they are already up to
|
---|
1409 | date.)@refill
|
---|
1410 |
|
---|
1411 | If you know that one or more of your makefiles cannot be remade and you
|
---|
1412 | want to keep @code{make} from performing an implicit rule search on
|
---|
1413 | them, perhaps for efficiency reasons, you can use any normal method of
|
---|
1414 | preventing implicit rule lookup to do so. For example, you can write an
|
---|
1415 | explicit rule with the makefile as the target, and an empty command
|
---|
1416 | string (@pxref{Empty Commands, ,Using Empty Commands}).
|
---|
1417 |
|
---|
1418 | If the makefiles specify a double-colon rule to remake a file with
|
---|
1419 | commands but no prerequisites, that file will always be remade
|
---|
1420 | (@pxref{Double-Colon}). In the case of makefiles, a makefile that has a
|
---|
1421 | double-colon rule with commands but no prerequisites will be remade every
|
---|
1422 | time @code{make} is run, and then again after @code{make} starts over
|
---|
1423 | and reads the makefiles in again. This would cause an infinite loop:
|
---|
1424 | @code{make} would constantly remake the makefile, and never do anything
|
---|
1425 | else. So, to avoid this, @code{make} will @strong{not} attempt to
|
---|
1426 | remake makefiles which are specified as targets of a double-colon rule
|
---|
1427 | with commands but no prerequisites.@refill
|
---|
1428 |
|
---|
1429 | If you do not specify any makefiles to be read with @samp{-f} or
|
---|
1430 | @samp{--file} options, @code{make} will try the default makefile names;
|
---|
1431 | @pxref{Makefile Names, ,What Name to Give Your Makefile}. Unlike
|
---|
1432 | makefiles explicitly requested with @samp{-f} or @samp{--file} options,
|
---|
1433 | @code{make} is not certain that these makefiles should exist. However,
|
---|
1434 | if a default makefile does not exist but can be created by running
|
---|
1435 | @code{make} rules, you probably want the rules to be run so that the
|
---|
1436 | makefile can be used.
|
---|
1437 |
|
---|
1438 | Therefore, if none of the default makefiles exists, @code{make} will try
|
---|
1439 | to make each of them in the same order in which they are searched for
|
---|
1440 | (@pxref{Makefile Names, ,What Name to Give Your Makefile})
|
---|
1441 | until it succeeds in making one, or it runs out of names to try. Note
|
---|
1442 | that it is not an error if @code{make} cannot find or make any makefile;
|
---|
1443 | a makefile is not always necessary.@refill
|
---|
1444 |
|
---|
1445 | When you use the @samp{-t} or @samp{--touch} option
|
---|
1446 | (@pxref{Instead of Execution, ,Instead of Executing the Commands}),
|
---|
1447 | you would not want to use an out-of-date makefile to decide which
|
---|
1448 | targets to touch. So the @samp{-t} option has no effect on updating
|
---|
1449 | makefiles; they are really updated even if @samp{-t} is specified.
|
---|
1450 | Likewise, @samp{-q} (or @samp{--question}) and @samp{-n} (or
|
---|
1451 | @samp{--just-print}) do not prevent updating of makefiles, because an
|
---|
1452 | out-of-date makefile would result in the wrong output for other targets.
|
---|
1453 | Thus, @samp{make -f mfile -n foo} will update @file{mfile}, read it in,
|
---|
1454 | and then print the commands to update @file{foo} and its prerequisites
|
---|
1455 | without running them. The commands printed for @file{foo} will be those
|
---|
1456 | specified in the updated contents of @file{mfile}.
|
---|
1457 |
|
---|
1458 | However, on occasion you might actually wish to prevent updating of even
|
---|
1459 | the makefiles. You can do this by specifying the makefiles as goals in
|
---|
1460 | the command line as well as specifying them as makefiles. When the
|
---|
1461 | makefile name is specified explicitly as a goal, the options @samp{-t}
|
---|
1462 | and so on do apply to them.
|
---|
1463 |
|
---|
1464 | Thus, @samp{make -f mfile -n mfile foo} would read the makefile
|
---|
1465 | @file{mfile}, print the commands needed to update it without actually
|
---|
1466 | running them, and then print the commands needed to update @file{foo}
|
---|
1467 | without running them. The commands for @file{foo} will be those
|
---|
1468 | specified by the existing contents of @file{mfile}.
|
---|
1469 |
|
---|
1470 | @node Overriding Makefiles, Reading Makefiles, Remaking Makefiles, Makefiles
|
---|
1471 | @section Overriding Part of Another Makefile
|
---|
1472 |
|
---|
1473 | @cindex overriding makefiles
|
---|
1474 | @cindex makefile, overriding
|
---|
1475 | Sometimes it is useful to have a makefile that is mostly just like
|
---|
1476 | another makefile. You can often use the @samp{include} directive to
|
---|
1477 | include one in the other, and add more targets or variable definitions.
|
---|
1478 | However, if the two makefiles give different commands for the same
|
---|
1479 | target, @code{make} will not let you just do this. But there is another way.
|
---|
1480 |
|
---|
1481 | @cindex match-anything rule, used to override
|
---|
1482 | In the containing makefile (the one that wants to include the other),
|
---|
1483 | you can use a match-anything pattern rule to say that to remake any
|
---|
1484 | target that cannot be made from the information in the containing
|
---|
1485 | makefile, @code{make} should look in another makefile.
|
---|
1486 | @xref{Pattern Rules}, for more information on pattern rules.
|
---|
1487 |
|
---|
1488 | For example, if you have a makefile called @file{Makefile} that says how
|
---|
1489 | to make the target @samp{foo} (and other targets), you can write a
|
---|
1490 | makefile called @file{GNUmakefile} that contains:
|
---|
1491 |
|
---|
1492 | @example
|
---|
1493 | foo:
|
---|
1494 | frobnicate > foo
|
---|
1495 |
|
---|
1496 | %: force
|
---|
1497 | @@$(MAKE) -f Makefile $@@
|
---|
1498 | force: ;
|
---|
1499 | @end example
|
---|
1500 |
|
---|
1501 | If you say @samp{make foo}, @code{make} will find @file{GNUmakefile},
|
---|
1502 | read it, and see that to make @file{foo}, it needs to run the command
|
---|
1503 | @samp{frobnicate > foo}. If you say @samp{make bar}, @code{make} will
|
---|
1504 | find no way to make @file{bar} in @file{GNUmakefile}, so it will use the
|
---|
1505 | commands from the pattern rule: @samp{make -f Makefile bar}. If
|
---|
1506 | @file{Makefile} provides a rule for updating @file{bar}, @code{make}
|
---|
1507 | will apply the rule. And likewise for any other target that
|
---|
1508 | @file{GNUmakefile} does not say how to make.
|
---|
1509 |
|
---|
1510 | The way this works is that the pattern rule has a pattern of just
|
---|
1511 | @samp{%}, so it matches any target whatever. The rule specifies a
|
---|
1512 | prerequisite @file{force}, to guarantee that the commands will be run even
|
---|
1513 | if the target file already exists. We give @file{force} target empty
|
---|
1514 | commands to prevent @code{make} from searching for an implicit rule to
|
---|
1515 | build it---otherwise it would apply the same match-anything rule to
|
---|
1516 | @file{force} itself and create a prerequisite loop!
|
---|
1517 |
|
---|
1518 | @node Reading Makefiles, Secondary Expansion, Overriding Makefiles, Makefiles
|
---|
1519 | @section How @code{make} Reads a Makefile
|
---|
1520 | @cindex reading makefiles
|
---|
1521 | @cindex makefile, parsing
|
---|
1522 |
|
---|
1523 | GNU @code{make} does its work in two distinct phases. During the first
|
---|
1524 | phase it reads all the makefiles, included makefiles, etc. and
|
---|
1525 | internalizes all the variables and their values, implicit and explicit
|
---|
1526 | rules, and constructs a dependency graph of all the targets and their
|
---|
1527 | prerequisites. During the second phase, @code{make} uses these internal
|
---|
1528 | structures to determine what targets will need to be rebuilt and to
|
---|
1529 | invoke the rules necessary to do so.
|
---|
1530 |
|
---|
1531 | It's important to understand this two-phase approach because it has a
|
---|
1532 | direct impact on how variable and function expansion happens; this is
|
---|
1533 | often a source of some confusion when writing makefiles. Here we will
|
---|
1534 | present a summary of the phases in which expansion happens for different
|
---|
1535 | constructs within the makefile. We say that expansion is
|
---|
1536 | @dfn{immediate} if it happens during the first phase: in this case
|
---|
1537 | @code{make} will expand any variables or functions in that section of a
|
---|
1538 | construct as the makefile is parsed. We say that expansion is
|
---|
1539 | @dfn{deferred} if expansion is not performed immediately. Expansion of
|
---|
1540 | deferred construct is not performed until either the construct appears
|
---|
1541 | later in an immediate context, or until the second phase.
|
---|
1542 |
|
---|
1543 | You may not be familiar with some of these constructs yet. You can
|
---|
1544 | reference this section as you become familiar with them, in later
|
---|
1545 | chapters.
|
---|
1546 |
|
---|
1547 | @subheading Variable Assignment
|
---|
1548 | @cindex +=, expansion
|
---|
1549 | @cindex =, expansion
|
---|
1550 | @cindex ?=, expansion
|
---|
1551 | @cindex +=, expansion
|
---|
1552 | @cindex define, expansion
|
---|
1553 |
|
---|
1554 | Variable definitions are parsed as follows:
|
---|
1555 |
|
---|
1556 | @example
|
---|
1557 | @var{immediate} = @var{deferred}
|
---|
1558 | @var{immediate} ?= @var{deferred}
|
---|
1559 | @var{immediate} := @var{immediate}
|
---|
1560 | @var{immediate} += @var{deferred} or @var{immediate}
|
---|
1561 |
|
---|
1562 | define @var{immediate}
|
---|
1563 | @var{deferred}
|
---|
1564 | endef
|
---|
1565 | @end example
|
---|
1566 |
|
---|
1567 | For the append operator, @samp{+=}, the right-hand side is considered
|
---|
1568 | immediate if the variable was previously set as a simple variable
|
---|
1569 | (@samp{:=}), and deferred otherwise.
|
---|
1570 |
|
---|
1571 | @subheading Conditional Statements
|
---|
1572 | @cindex ifdef, expansion
|
---|
1573 | @cindex ifeq, expansion
|
---|
1574 | @cindex ifndef, expansion
|
---|
1575 | @cindex ifneq, expansion
|
---|
1576 |
|
---|
1577 | All instances of conditional syntax are parsed immediately, in their
|
---|
1578 | entirety; this includes the @code{ifdef}, @code{ifeq}, @code{ifndef},
|
---|
1579 | and @code{ifneq} forms. Of course this means that automatic variables
|
---|
1580 | cannot be used in conditional statements, as automatic variables are
|
---|
1581 | not set until the command script for that rule is invoked. If you
|
---|
1582 | need to use automatic variables in a conditional you @emph{must} use
|
---|
1583 | shell conditional syntax, in your command script proper, for these
|
---|
1584 | tests, not @code{make} conditionals.
|
---|
1585 |
|
---|
1586 | @subheading Rule Definition
|
---|
1587 | @cindex target, expansion
|
---|
1588 | @cindex prerequisite, expansion
|
---|
1589 | @cindex implicit rule, expansion
|
---|
1590 | @cindex pattern rule, expansion
|
---|
1591 | @cindex explicit rule, expansion
|
---|
1592 |
|
---|
1593 | A rule is always expanded the same way, regardless of the form:
|
---|
1594 |
|
---|
1595 | @example
|
---|
1596 | @var{immediate} : @var{immediate} ; @var{deferred}
|
---|
1597 | @var{deferred}
|
---|
1598 | @end example
|
---|
1599 |
|
---|
1600 | That is, the target and prerequisite sections are expanded immediately,
|
---|
1601 | and the commands used to construct the target are always deferred. This
|
---|
1602 | general rule is true for explicit rules, pattern rules, suffix rules,
|
---|
1603 | static pattern rules, and simple prerequisite definitions.
|
---|
1604 |
|
---|
1605 | @node Secondary Expansion, , Reading Makefiles, Makefiles
|
---|
1606 | @section Secondary Expansion
|
---|
1607 | @cindex secondary expansion
|
---|
1608 | @cindex expansion, secondary
|
---|
1609 |
|
---|
1610 | @findex .SECONDEXPANSION
|
---|
1611 | In the previous section we learned that GNU @code{make} works in two
|
---|
1612 | distinct phases: a read-in phase and a target-update phase
|
---|
1613 | (@pxref{Reading Makefiles, , How @code{make} Reads a Makefile}). GNU
|
---|
1614 | make also has the ability to enable a @emph{second expansion} of the
|
---|
1615 | prerequisites (only) for some or all targets defined in the makefile.
|
---|
1616 | In order for this second expansion to occur, the special target
|
---|
1617 | @code{.SECONDEXPANSION} must be defined before the first prerequisite
|
---|
1618 | list that makes use of this feature.
|
---|
1619 |
|
---|
1620 | If that special target is defined then in between the two phases
|
---|
1621 | mentioned above, right at the end of the read-in phase, all the
|
---|
1622 | prerequisites of the targets defined after the special target are
|
---|
1623 | expanded a @emph{second time}. In most circumstances this secondary
|
---|
1624 | expansion will have no effect, since all variable and function
|
---|
1625 | references will have been expanded during the initial parsing of the
|
---|
1626 | makefiles. In order to take advantage of the secondary expansion
|
---|
1627 | phase of the parser, then, it's necessary to @emph{escape} the
|
---|
1628 | variable or function reference in the makefile. In this case the
|
---|
1629 | first expansion merely un-escapes the reference but doesn't expand it,
|
---|
1630 | and expansion is left to the secondary expansion phase. For example,
|
---|
1631 | consider this makefile:
|
---|
1632 |
|
---|
1633 | @example
|
---|
1634 | .SECONDEXPANSION:
|
---|
1635 | ONEVAR = onefile
|
---|
1636 | TWOVAR = twofile
|
---|
1637 | myfile: $(ONEVAR) $$(TWOVAR)
|
---|
1638 | @end example
|
---|
1639 |
|
---|
1640 | After the first expansion phase the prerequisites list of the
|
---|
1641 | @file{myfile} target will be @code{onefile} and @code{$(TWOVAR)}; the
|
---|
1642 | first (unescaped) variable reference to @var{ONEVAR} is expanded,
|
---|
1643 | while the second (escaped) variable reference is simply unescaped,
|
---|
1644 | without being recognized as a variable reference. Now during the
|
---|
1645 | secondary expansion the first word is expanded again but since it
|
---|
1646 | contains no variable or function references it remains the static
|
---|
1647 | value @file{onefile}, while the second word is now a normal reference
|
---|
1648 | to the variable @var{TWOVAR}, which is expanded to the value
|
---|
1649 | @file{twofile}. The final result is that there are two prerequisites,
|
---|
1650 | @file{onefile} and @file{twofile}.
|
---|
1651 |
|
---|
1652 | Obviously, this is not a very interesting case since the same result
|
---|
1653 | could more easily have been achieved simply by having both variables
|
---|
1654 | appear, unescaped, in the prerequisites list. One difference becomes
|
---|
1655 | apparent if the variables are reset; consider this example:
|
---|
1656 |
|
---|
1657 | @example
|
---|
1658 | .SECONDEXPANSION:
|
---|
1659 | AVAR = top
|
---|
1660 | onefile: $(AVAR)
|
---|
1661 | twofile: $$(AVAR)
|
---|
1662 | AVAR = bottom
|
---|
1663 | @end example
|
---|
1664 |
|
---|
1665 | Here the prerequisite of @file{onefile} will be expanded immediately,
|
---|
1666 | and resolve to the value @file{top}, while the prerequisite of
|
---|
1667 | @file{twofile} will not be full expanded until the secondary expansion
|
---|
1668 | and yield a value of @file{bottom}.
|
---|
1669 |
|
---|
1670 | This is marginally more exciting, but the true power of this feature
|
---|
1671 | only becomes apparent when you discover that secondary expansions
|
---|
1672 | always take place within the scope of the automatic variables for that
|
---|
1673 | target. This means that you can use variables such as @code{$@@},
|
---|
1674 | @code{$*}, etc. during the second expansion and they will have their
|
---|
1675 | expected values, just as in the command script. All you have to do is
|
---|
1676 | defer the expansion by escaping the @code{$}. Also, secondary
|
---|
1677 | expansion occurs for both explicit and implicit (pattern) rules.
|
---|
1678 | Knowing this, the possible uses for this feature increase
|
---|
1679 | dramatically. For example:
|
---|
1680 |
|
---|
1681 | @example
|
---|
1682 | .SECONDEXPANSION:
|
---|
1683 | main_OBJS := main.o try.o test.o
|
---|
1684 | lib_OBJS := lib.o api.o
|
---|
1685 |
|
---|
1686 | main lib: $$($$@@_OBJS)
|
---|
1687 | @end example
|
---|
1688 |
|
---|
1689 | Here, after the initial expansion the prerequisites of both the
|
---|
1690 | @file{main} and @file{lib} targets will be @code{$($@@_OBJS)}. During
|
---|
1691 | the secondary expansion, the @code{$@@} variable is set to the name of
|
---|
1692 | the target and so the expansion for the @file{main} target will yield
|
---|
1693 | @code{$(main_OBJS)}, or @code{main.o try.o test.o}, while the
|
---|
1694 | secondary expansion for the @file{lib} target will yield
|
---|
1695 | @code{$(lib_OBJS)}, or @code{lib.o api.o}.
|
---|
1696 |
|
---|
1697 | You can also mix functions here, as long as they are properly escaped:
|
---|
1698 |
|
---|
1699 | @example
|
---|
1700 | main_SRCS := main.c try.c test.c
|
---|
1701 | lib_SRCS := lib.c api.c
|
---|
1702 |
|
---|
1703 | .SECONDEXPANSION:
|
---|
1704 | main lib: $$(patsubst %.c,%.o,$$($$@@_SRCS))
|
---|
1705 | @end example
|
---|
1706 |
|
---|
1707 | This version allows users to specify source files rather than object
|
---|
1708 | files, but gives the same resulting prerequisites list as the previous
|
---|
1709 | example.
|
---|
1710 |
|
---|
1711 | Evaluation of automatic variables during the secondary expansion
|
---|
1712 | phase, especially of the target name variable @code{$$@@}, behaves
|
---|
1713 | similarly to evaluation within command scripts. However, there are
|
---|
1714 | some subtle differences and ``corner cases'' which come into play for
|
---|
1715 | the different types of rule definitions that @code{make} understands.
|
---|
1716 | The subtleties of using the different automatic variables are
|
---|
1717 | described below.
|
---|
1718 |
|
---|
1719 | @subheading Secondary Expansion of Explicit Rules
|
---|
1720 | @cindex secondary expansion and explicit rules
|
---|
1721 | @cindex explicit rules, secondary expansion of
|
---|
1722 |
|
---|
1723 | During the secondary expansion of explicit rules, @code{$$@@} and
|
---|
1724 | @code{$$%} evaluate, respectively, to the file name of the target and,
|
---|
1725 | when the target is an archive member, the target member name. The
|
---|
1726 | @code{$$<} variable evaluates to the first prerequisite in the first
|
---|
1727 | rule for this target. @code{$$^} and @code{$$+} evaluate to the list
|
---|
1728 | of all prerequisites of rules @emph{that have already appeared} for
|
---|
1729 | the same target (@code{$$+} with repetitions and @code{$$^}
|
---|
1730 | without). The following example will help illustrate these behaviors:
|
---|
1731 |
|
---|
1732 | @example
|
---|
1733 | .SECONDEXPANSION:
|
---|
1734 |
|
---|
1735 | foo: foo.1 bar.1 $$< $$^ $$+ # line #1
|
---|
1736 |
|
---|
1737 | foo: foo.2 bar.2 $$< $$^ $$+ # line #2
|
---|
1738 |
|
---|
1739 | foo: foo.3 bar.3 $$< $$^ $$+ # line #3
|
---|
1740 | @end example
|
---|
1741 |
|
---|
1742 | In the first prerequisite list, all three variables (@code{$$<},
|
---|
1743 | @code{$$^}, and @code{$$+}) expand to the empty string. In the
|
---|
1744 | second, they will have values @code{foo.1}, @code{foo.1 bar.1}, and
|
---|
1745 | @code{foo.1 bar.1} respectively. In the third they will have values
|
---|
1746 | @code{foo.1}, @code{foo.1 bar.1 foo.2 bar.2}, and @code{foo.1 bar.1
|
---|
1747 | foo.2 bar.2} respectively.
|
---|
1748 |
|
---|
1749 | Rules undergo secondary expansion in makefile order, except that
|
---|
1750 | the rule with the command script is always evaluated last.
|
---|
1751 |
|
---|
1752 | The variables @code{$$?} and @code{$$*} are not available and expand
|
---|
1753 | to the empty string.
|
---|
1754 |
|
---|
1755 | @subheading Secondary Expansion of Static Pattern Rules
|
---|
1756 | @cindex secondary expansion and static pattern rules
|
---|
1757 | @cindex static pattern rules, secondary expansion of
|
---|
1758 |
|
---|
1759 | Rules for secondary expansion of static pattern rules are identical to
|
---|
1760 | those for explicit rules, above, with one exception: for static
|
---|
1761 | pattern rules the @code{$$*} variable is set to the pattern stem. As
|
---|
1762 | with explicit rules, @code{$$?} is not available and expands to the
|
---|
1763 | empty string.
|
---|
1764 |
|
---|
1765 | @subheading Secondary Expansion of Implicit Rules
|
---|
1766 | @cindex secondary expansion and implicit rules
|
---|
1767 | @cindex implicit rules, secondary expansion of
|
---|
1768 |
|
---|
1769 | As @code{make} searches for an implicit rule, it substitutes the stem
|
---|
1770 | and then performs secondary expansion for every rule with a matching
|
---|
1771 | target pattern. The value of the automatic variables is derived in
|
---|
1772 | the same fashion as for static pattern rules. As an example:
|
---|
1773 |
|
---|
1774 | @example
|
---|
1775 | .SECONDEXPANSION:
|
---|
1776 |
|
---|
1777 | foo: bar
|
---|
1778 |
|
---|
1779 | foo foz: fo%: bo%
|
---|
1780 |
|
---|
1781 | %oo: $$< $$^ $$+ $$*
|
---|
1782 | @end example
|
---|
1783 |
|
---|
1784 | When the implicit rule is tried for target @file{foo}, @code{$$<}
|
---|
1785 | expands to @file{bar}, @code{$$^} expands to @file{bar boo},
|
---|
1786 | @code{$$+} also expands to @file{bar boo}, and @code{$$*} expands to
|
---|
1787 | @file{f}.
|
---|
1788 |
|
---|
1789 | Note that the directory prefix (D), as described in @ref{Implicit Rule
|
---|
1790 | Search, ,Implicit Rule Search Algorithm}, is appended (after
|
---|
1791 | expansion) to all the patterns in the prerequisites list. As an
|
---|
1792 | example:
|
---|
1793 |
|
---|
1794 | @example
|
---|
1795 | .SECONDEXPANSION:
|
---|
1796 |
|
---|
1797 | /tmp/foo.o:
|
---|
1798 |
|
---|
1799 | %.o: $$(addsuffix /%.c,foo bar) foo.h
|
---|
1800 | @end example
|
---|
1801 |
|
---|
1802 | The prerequisite list after the secondary expansion and directory
|
---|
1803 | prefix reconstruction will be @file{/tmp/foo/foo.c /tmp/var/bar/foo.c
|
---|
1804 | foo.h}. If you are not interested in this reconstruction, you can use
|
---|
1805 | @code{$$*} instead of @code{%} in the prerequisites list.
|
---|
1806 |
|
---|
1807 | @node Rules, Commands, Makefiles, Top
|
---|
1808 | @chapter Writing Rules
|
---|
1809 | @cindex writing rules
|
---|
1810 | @cindex rule, how to write
|
---|
1811 | @cindex target
|
---|
1812 | @cindex prerequisite
|
---|
1813 |
|
---|
1814 | A @dfn{rule} appears in the makefile and says when and how to remake
|
---|
1815 | certain files, called the rule's @dfn{targets} (most often only one per rule).
|
---|
1816 | It lists the other files that are the @dfn{prerequisites} of the target, and
|
---|
1817 | @dfn{commands} to use to create or update the target.
|
---|
1818 |
|
---|
1819 | @cindex default goal
|
---|
1820 | @cindex goal, default
|
---|
1821 | The order of rules is not significant, except for determining the
|
---|
1822 | @dfn{default goal}: the target for @code{make} to consider, if you do
|
---|
1823 | not otherwise specify one. The default goal is the target of the first
|
---|
1824 | rule in the first makefile. If the first rule has multiple targets,
|
---|
1825 | only the first target is taken as the default. There are two
|
---|
1826 | exceptions: a target starting with a period is not a default unless it
|
---|
1827 | contains one or more slashes, @samp{/}, as well; and, a target that
|
---|
1828 | defines a pattern rule has no effect on the default goal.
|
---|
1829 | (@xref{Pattern Rules, ,Defining and Redefining Pattern Rules}.)
|
---|
1830 |
|
---|
1831 | Therefore, we usually write the makefile so that the first rule is the
|
---|
1832 | one for compiling the entire program or all the programs described by
|
---|
1833 | the makefile (often with a target called @samp{all}).
|
---|
1834 | @xref{Goals, ,Arguments to Specify the Goals}.
|
---|
1835 |
|
---|
1836 | @menu
|
---|
1837 | * Rule Example:: An example explained.
|
---|
1838 | * Rule Syntax:: General syntax explained.
|
---|
1839 | * Prerequisite Types:: There are two types of prerequisites.
|
---|
1840 | * Wildcards:: Using wildcard characters such as `*'.
|
---|
1841 | * Directory Search:: Searching other directories for source files.
|
---|
1842 | * Phony Targets:: Using a target that is not a real file's name.
|
---|
1843 | * Force Targets:: You can use a target without commands
|
---|
1844 | or prerequisites to mark other targets
|
---|
1845 | as phony.
|
---|
1846 | * Empty Targets:: When only the date matters and the
|
---|
1847 | files are empty.
|
---|
1848 | * Special Targets:: Targets with special built-in meanings.
|
---|
1849 | * Multiple Targets:: When to make use of several targets in a rule.
|
---|
1850 | * Multiple Rules:: How to use several rules with the same target.
|
---|
1851 | * Static Pattern:: Static pattern rules apply to multiple targets
|
---|
1852 | and can vary the prerequisites according to
|
---|
1853 | the target name.
|
---|
1854 | * Double-Colon:: How to use a special kind of rule to allow
|
---|
1855 | several independent rules for one target.
|
---|
1856 | * Automatic Prerequisites:: How to automatically generate rules giving
|
---|
1857 | prerequisites from source files themselves.
|
---|
1858 | @end menu
|
---|
1859 |
|
---|
1860 | @ifnottex
|
---|
1861 | @node Rule Example, Rule Syntax, Rules, Rules
|
---|
1862 | @section Rule Example
|
---|
1863 |
|
---|
1864 | Here is an example of a rule:
|
---|
1865 |
|
---|
1866 | @example
|
---|
1867 | foo.o : foo.c defs.h # module for twiddling the frobs
|
---|
1868 | cc -c -g foo.c
|
---|
1869 | @end example
|
---|
1870 |
|
---|
1871 | Its target is @file{foo.o} and its prerequisites are @file{foo.c} and
|
---|
1872 | @file{defs.h}. It has one command, which is @samp{cc -c -g foo.c}.
|
---|
1873 | The command line starts with a tab to identify it as a command.
|
---|
1874 |
|
---|
1875 | This rule says two things:
|
---|
1876 |
|
---|
1877 | @itemize @bullet
|
---|
1878 | @item
|
---|
1879 | How to decide whether @file{foo.o} is out of date: it is out of date
|
---|
1880 | if it does not exist, or if either @file{foo.c} or @file{defs.h} is
|
---|
1881 | more recent than it.
|
---|
1882 |
|
---|
1883 | @item
|
---|
1884 | How to update the file @file{foo.o}: by running @code{cc} as stated.
|
---|
1885 | The command does not explicitly mention @file{defs.h}, but we presume
|
---|
1886 | that @file{foo.c} includes it, and that that is why @file{defs.h} was
|
---|
1887 | added to the prerequisites.
|
---|
1888 | @end itemize
|
---|
1889 | @end ifnottex
|
---|
1890 |
|
---|
1891 | @node Rule Syntax, Prerequisite Types, Rule Example, Rules
|
---|
1892 | @section Rule Syntax
|
---|
1893 |
|
---|
1894 | @cindex rule syntax
|
---|
1895 | @cindex syntax of rules
|
---|
1896 | In general, a rule looks like this:
|
---|
1897 |
|
---|
1898 | @example
|
---|
1899 | @var{targets} : @var{prerequisites}
|
---|
1900 | @var{command}
|
---|
1901 | @dots{}
|
---|
1902 | @end example
|
---|
1903 |
|
---|
1904 | @noindent
|
---|
1905 | or like this:
|
---|
1906 |
|
---|
1907 | @example
|
---|
1908 | @var{targets} : @var{prerequisites} ; @var{command}
|
---|
1909 | @var{command}
|
---|
1910 | @dots{}
|
---|
1911 | @end example
|
---|
1912 |
|
---|
1913 | @cindex targets
|
---|
1914 | @cindex rule targets
|
---|
1915 | The @var{targets} are file names, separated by spaces. Wildcard
|
---|
1916 | characters may be used (@pxref{Wildcards, ,Using Wildcard Characters
|
---|
1917 | in File Names}) and a name of the form @file{@var{a}(@var{m})}
|
---|
1918 | represents member @var{m} in archive file @var{a}
|
---|
1919 | (@pxref{Archive Members, ,Archive Members as Targets}).
|
---|
1920 | Usually there is only one
|
---|
1921 | target per rule, but occasionally there is a reason to have more
|
---|
1922 | (@pxref{Multiple Targets, , Multiple Targets in a Rule}).@refill
|
---|
1923 |
|
---|
1924 | @cindex commands
|
---|
1925 | @cindex tab character (in commands)
|
---|
1926 | The @var{command} lines start with a tab character. The first command may
|
---|
1927 | appear on the line after the prerequisites, with a tab character, or may
|
---|
1928 | appear on the same line, with a semicolon. Either way, the effect is the
|
---|
1929 | same. There are other differences in the syntax of command lines.
|
---|
1930 | @xref{Commands, ,Writing the Commands in Rules}.
|
---|
1931 |
|
---|
1932 | @cindex dollar sign (@code{$}), in rules
|
---|
1933 | @cindex @code{$}, in rules
|
---|
1934 | @cindex rules, and @code{$}
|
---|
1935 | Because dollar signs are used to start @code{make} variable
|
---|
1936 | references, if you really want a dollar sign in a target or
|
---|
1937 | prerequisite you must write two of them, @samp{$$} (@pxref{Using
|
---|
1938 | Variables, ,How to Use Variables}). If you have enabled secondary
|
---|
1939 | expansion (@pxref{Secondary Expansion}) and you want a literal dollar
|
---|
1940 | sign in the prerequisites lise, you must actually write @emph{four}
|
---|
1941 | dollar signs (@samp{$$$$}).
|
---|
1942 |
|
---|
1943 | You may split a long line by inserting a backslash followed by a
|
---|
1944 | newline, but this is not required, as @code{make} places no limit on
|
---|
1945 | the length of a line in a makefile.
|
---|
1946 |
|
---|
1947 | A rule tells @code{make} two things: when the targets are out of date,
|
---|
1948 | and how to update them when necessary.
|
---|
1949 |
|
---|
1950 | @cindex prerequisites
|
---|
1951 | @cindex rule prerequisites
|
---|
1952 | The criterion for being out of date is specified in terms of the
|
---|
1953 | @var{prerequisites}, which consist of file names separated by spaces.
|
---|
1954 | (Wildcards and archive members (@pxref{Archives}) are allowed here too.)
|
---|
1955 | A target is out of date if it does not exist or if it is older than any
|
---|
1956 | of the prerequisites (by comparison of last-modification times). The
|
---|
1957 | idea is that the contents of the target file are computed based on
|
---|
1958 | information in the prerequisites, so if any of the prerequisites changes,
|
---|
1959 | the contents of the existing target file are no longer necessarily
|
---|
1960 | valid.
|
---|
1961 |
|
---|
1962 | How to update is specified by @var{commands}. These are lines to be
|
---|
1963 | executed by the shell (normally @samp{sh}), but with some extra features
|
---|
1964 | (@pxref{Commands, ,Writing the Commands in Rules}).
|
---|
1965 |
|
---|
1966 | @node Prerequisite Types, Wildcards, Rule Syntax, Rules
|
---|
1967 | @comment node-name, next, previous, up
|
---|
1968 | @section Types of Prerequisites
|
---|
1969 | @cindex prerequisite types
|
---|
1970 | @cindex types of prerequisites
|
---|
1971 |
|
---|
1972 | @cindex prerequisites, normal
|
---|
1973 | @cindex normal prerequisites
|
---|
1974 | @cindex prerequisites, order-only
|
---|
1975 | @cindex order-only prerequisites
|
---|
1976 | There are actually two different types of prerequisites understood by
|
---|
1977 | GNU @code{make}: normal prerequisites such as described in the
|
---|
1978 | previous section, and @dfn{order-only} prerequisites. A normal
|
---|
1979 | prerequisite makes two statements: first, it imposes an order of
|
---|
1980 | execution of build commands: any commands necessary to build any of a
|
---|
1981 | target's prerequisites will be fully executed before any commands
|
---|
1982 | necessary to build the target. Second, it imposes a dependency
|
---|
1983 | relationship: if any prerequisite is newer than the target, then the
|
---|
1984 | target is considered out-of-date and must be rebuilt.
|
---|
1985 |
|
---|
1986 | Normally, this is exactly what you want: if a target's prerequisite is
|
---|
1987 | updated, then the target should also be updated.
|
---|
1988 |
|
---|
1989 | Occasionally, however, you have a situation where you want to impose a
|
---|
1990 | specific ordering on the rules to be invoked @emph{without} forcing
|
---|
1991 | the target to be updated if one of those rules is executed. In that
|
---|
1992 | case, you want to define @dfn{order-only} prerequisites. Order-only
|
---|
1993 | prerequisites can be specified by placing a pipe symbol (@code{|})
|
---|
1994 | in the prerequisites list: any prerequisites to the left of the pipe
|
---|
1995 | symbol are normal; any prerequisites to the right are order-only:
|
---|
1996 |
|
---|
1997 | @example
|
---|
1998 | @var{targets} : @var{normal-prerequisites} | @var{order-only-prerequisites}
|
---|
1999 | @end example
|
---|
2000 |
|
---|
2001 | The normal prerequisites section may of course be empty. Also, you
|
---|
2002 | may still declare multiple lines of prerequisites for the same target:
|
---|
2003 | they are appended appropriately. Note that if you declare the same
|
---|
2004 | file to be both a normal and an order-only prerequisite, the normal
|
---|
2005 | prerequisite takes precedence (since they are a strict superset of the
|
---|
2006 | behavior of an order-only prerequisite).
|
---|
2007 |
|
---|
2008 | @node Wildcards, Directory Search, Prerequisite Types, Rules
|
---|
2009 | @section Using Wildcard Characters in File Names
|
---|
2010 | @cindex wildcard
|
---|
2011 | @cindex file name with wildcards
|
---|
2012 | @cindex globbing (wildcards)
|
---|
2013 |
|
---|
2014 | @cindex @code{*} (wildcard character)
|
---|
2015 | @cindex @code{?} (wildcard character)
|
---|
2016 | @cindex @code{[@dots{}]} (wildcard characters)
|
---|
2017 | A single file name can specify many files using @dfn{wildcard characters}.
|
---|
2018 | The wildcard characters in @code{make} are @samp{*}, @samp{?} and
|
---|
2019 | @samp{[@dots{}]}, the same as in the Bourne shell. For example, @file{*.c}
|
---|
2020 | specifies a list of all the files (in the working directory) whose names
|
---|
2021 | end in @samp{.c}.@refill
|
---|
2022 |
|
---|
2023 | @cindex @code{~} (tilde)
|
---|
2024 | @cindex tilde (@code{~})
|
---|
2025 | @cindex home directory
|
---|
2026 | The character @samp{~} at the beginning of a file name also has special
|
---|
2027 | significance. If alone, or followed by a slash, it represents your home
|
---|
2028 | directory. For example @file{~/bin} expands to @file{/home/you/bin}.
|
---|
2029 | If the @samp{~} is followed by a word, the string represents the home
|
---|
2030 | directory of the user named by that word. For example @file{~john/bin}
|
---|
2031 | expands to @file{/home/john/bin}. On systems which don't have a home
|
---|
2032 | directory for each user (such as MS-DOS or MS-Windows), this
|
---|
2033 | functionality can be simulated by setting the environment variable
|
---|
2034 | @var{HOME}.@refill
|
---|
2035 |
|
---|
2036 | Wildcard expansion is performed by @code{make} automatically in
|
---|
2037 | targets and in prerequisites. In commands the shell is responsible
|
---|
2038 | for wildcard expansion. In other contexts, wildcard expansion happens
|
---|
2039 | only if you request it explicitly with the @code{wildcard} function.
|
---|
2040 |
|
---|
2041 | The special significance of a wildcard character can be turned off by
|
---|
2042 | preceding it with a backslash. Thus, @file{foo\*bar} would refer to a
|
---|
2043 | specific file whose name consists of @samp{foo}, an asterisk, and
|
---|
2044 | @samp{bar}.@refill
|
---|
2045 |
|
---|
2046 | @menu
|
---|
2047 | * Wildcard Examples:: Several examples
|
---|
2048 | * Wildcard Pitfall:: Problems to avoid.
|
---|
2049 | * Wildcard Function:: How to cause wildcard expansion where
|
---|
2050 | it does not normally take place.
|
---|
2051 | @end menu
|
---|
2052 |
|
---|
2053 | @node Wildcard Examples, Wildcard Pitfall, Wildcards, Wildcards
|
---|
2054 | @subsection Wildcard Examples
|
---|
2055 |
|
---|
2056 | Wildcards can be used in the commands of a rule, where they are expanded
|
---|
2057 | by the shell. For example, here is a rule to delete all the object files:
|
---|
2058 |
|
---|
2059 | @example
|
---|
2060 | @group
|
---|
2061 | clean:
|
---|
2062 | rm -f *.o
|
---|
2063 | @end group
|
---|
2064 | @end example
|
---|
2065 | @cindex @code{rm} (shell command)
|
---|
2066 |
|
---|
2067 | Wildcards are also useful in the prerequisites of a rule. With the
|
---|
2068 | following rule in the makefile, @samp{make print} will print all the
|
---|
2069 | @samp{.c} files that have changed since the last time you printed them:
|
---|
2070 |
|
---|
2071 | @example
|
---|
2072 | print: *.c
|
---|
2073 | lpr -p $?
|
---|
2074 | touch print
|
---|
2075 | @end example
|
---|
2076 |
|
---|
2077 | @cindex @code{print} target
|
---|
2078 | @cindex @code{lpr} (shell command)
|
---|
2079 | @cindex @code{touch} (shell command)
|
---|
2080 | @noindent
|
---|
2081 | This rule uses @file{print} as an empty target file; see @ref{Empty
|
---|
2082 | Targets, ,Empty Target Files to Record Events}. (The automatic variable
|
---|
2083 | @samp{$?} is used to print only those files that have changed; see
|
---|
2084 | @ref{Automatic Variables}.)@refill
|
---|
2085 |
|
---|
2086 | Wildcard expansion does not happen when you define a variable. Thus, if
|
---|
2087 | you write this:
|
---|
2088 |
|
---|
2089 | @example
|
---|
2090 | objects = *.o
|
---|
2091 | @end example
|
---|
2092 |
|
---|
2093 | @noindent
|
---|
2094 | then the value of the variable @code{objects} is the actual string
|
---|
2095 | @samp{*.o}. However, if you use the value of @code{objects} in a target,
|
---|
2096 | prerequisite or command, wildcard expansion will take place at that time.
|
---|
2097 | To set @code{objects} to the expansion, instead use:
|
---|
2098 |
|
---|
2099 | @example
|
---|
2100 | objects := $(wildcard *.o)
|
---|
2101 | @end example
|
---|
2102 |
|
---|
2103 | @noindent
|
---|
2104 | @xref{Wildcard Function}.
|
---|
2105 |
|
---|
2106 | @node Wildcard Pitfall, Wildcard Function, Wildcard Examples, Wildcards
|
---|
2107 | @subsection Pitfalls of Using Wildcards
|
---|
2108 | @cindex wildcard pitfalls
|
---|
2109 | @cindex pitfalls of wildcards
|
---|
2110 | @cindex mistakes with wildcards
|
---|
2111 | @cindex errors with wildcards
|
---|
2112 | @cindex problems with wildcards
|
---|
2113 |
|
---|
2114 | Now here is an example of a naive way of using wildcard expansion, that
|
---|
2115 | does not do what you would intend. Suppose you would like to say that the
|
---|
2116 | executable file @file{foo} is made from all the object files in the
|
---|
2117 | directory, and you write this:
|
---|
2118 |
|
---|
2119 | @example
|
---|
2120 | objects = *.o
|
---|
2121 |
|
---|
2122 | foo : $(objects)
|
---|
2123 | cc -o foo $(CFLAGS) $(objects)
|
---|
2124 | @end example
|
---|
2125 |
|
---|
2126 | @noindent
|
---|
2127 | The value of @code{objects} is the actual string @samp{*.o}. Wildcard
|
---|
2128 | expansion happens in the rule for @file{foo}, so that each @emph{existing}
|
---|
2129 | @samp{.o} file becomes a prerequisite of @file{foo} and will be recompiled if
|
---|
2130 | necessary.
|
---|
2131 |
|
---|
2132 | But what if you delete all the @samp{.o} files? When a wildcard matches
|
---|
2133 | no files, it is left as it is, so then @file{foo} will depend on the
|
---|
2134 | oddly-named file @file{*.o}. Since no such file is likely to exist,
|
---|
2135 | @code{make} will give you an error saying it cannot figure out how to
|
---|
2136 | make @file{*.o}. This is not what you want!
|
---|
2137 |
|
---|
2138 | Actually it is possible to obtain the desired result with wildcard
|
---|
2139 | expansion, but you need more sophisticated techniques, including the
|
---|
2140 | @code{wildcard} function and string substitution.
|
---|
2141 | @ifnottex
|
---|
2142 | @xref{Wildcard Function, ,The Function @code{wildcard}}.
|
---|
2143 | @end ifnottex
|
---|
2144 | @iftex
|
---|
2145 | These are described in the following section.
|
---|
2146 | @end iftex
|
---|
2147 |
|
---|
2148 | @cindex wildcards and MS-DOS/MS-Windows backslashes
|
---|
2149 | @cindex backslashes in pathnames and wildcard expansion
|
---|
2150 |
|
---|
2151 | Microsoft operating systems (MS-DOS and MS-Windows) use backslashes to
|
---|
2152 | separate directories in pathnames, like so:
|
---|
2153 |
|
---|
2154 | @example
|
---|
2155 | c:\foo\bar\baz.c
|
---|
2156 | @end example
|
---|
2157 |
|
---|
2158 | This is equivalent to the Unix-style @file{c:/foo/bar/baz.c} (the
|
---|
2159 | @file{c:} part is the so-called drive letter). When @code{make} runs on
|
---|
2160 | these systems, it supports backslashes as well as the Unix-style forward
|
---|
2161 | slashes in pathnames. However, this support does @emph{not} include the
|
---|
2162 | wildcard expansion, where backslash is a quote character. Therefore,
|
---|
2163 | you @emph{must} use Unix-style slashes in these cases.
|
---|
2164 |
|
---|
2165 |
|
---|
2166 | @node Wildcard Function, , Wildcard Pitfall, Wildcards
|
---|
2167 | @subsection The Function @code{wildcard}
|
---|
2168 | @findex wildcard
|
---|
2169 |
|
---|
2170 | Wildcard expansion happens automatically in rules. But wildcard expansion
|
---|
2171 | does not normally take place when a variable is set, or inside the
|
---|
2172 | arguments of a function. If you want to do wildcard expansion in such
|
---|
2173 | places, you need to use the @code{wildcard} function, like this:
|
---|
2174 |
|
---|
2175 | @example
|
---|
2176 | $(wildcard @var{pattern}@dots{})
|
---|
2177 | @end example
|
---|
2178 |
|
---|
2179 | @noindent
|
---|
2180 | This string, used anywhere in a makefile, is replaced by a
|
---|
2181 | space-separated list of names of existing files that match one of the
|
---|
2182 | given file name patterns. If no existing file name matches a pattern,
|
---|
2183 | then that pattern is omitted from the output of the @code{wildcard}
|
---|
2184 | function. Note that this is different from how unmatched wildcards
|
---|
2185 | behave in rules, where they are used verbatim rather than ignored
|
---|
2186 | (@pxref{Wildcard Pitfall}).
|
---|
2187 |
|
---|
2188 | One use of the @code{wildcard} function is to get a list of all the C source
|
---|
2189 | files in a directory, like this:
|
---|
2190 |
|
---|
2191 | @example
|
---|
2192 | $(wildcard *.c)
|
---|
2193 | @end example
|
---|
2194 |
|
---|
2195 | We can change the list of C source files into a list of object files by
|
---|
2196 | replacing the @samp{.c} suffix with @samp{.o} in the result, like this:
|
---|
2197 |
|
---|
2198 | @example
|
---|
2199 | $(patsubst %.c,%.o,$(wildcard *.c))
|
---|
2200 | @end example
|
---|
2201 |
|
---|
2202 | @noindent
|
---|
2203 | (Here we have used another function, @code{patsubst}.
|
---|
2204 | @xref{Text Functions, ,Functions for String Substitution and Analysis}.)@refill
|
---|
2205 |
|
---|
2206 | Thus, a makefile to compile all C source files in the directory and then
|
---|
2207 | link them together could be written as follows:
|
---|
2208 |
|
---|
2209 | @example
|
---|
2210 | objects := $(patsubst %.c,%.o,$(wildcard *.c))
|
---|
2211 |
|
---|
2212 | foo : $(objects)
|
---|
2213 | cc -o foo $(objects)
|
---|
2214 | @end example
|
---|
2215 |
|
---|
2216 | @noindent
|
---|
2217 | (This takes advantage of the implicit rule for compiling C programs, so
|
---|
2218 | there is no need to write explicit rules for compiling the files.
|
---|
2219 | @xref{Flavors, ,The Two Flavors of Variables}, for an explanation of
|
---|
2220 | @samp{:=}, which is a variant of @samp{=}.)
|
---|
2221 |
|
---|
2222 | @node Directory Search, Phony Targets, Wildcards, Rules
|
---|
2223 | @section Searching Directories for Prerequisites
|
---|
2224 | @vindex VPATH
|
---|
2225 | @findex vpath
|
---|
2226 | @cindex vpath
|
---|
2227 | @cindex search path for prerequisites (@code{VPATH})
|
---|
2228 | @cindex directory search (@code{VPATH})
|
---|
2229 |
|
---|
2230 | For large systems, it is often desirable to put sources in a separate
|
---|
2231 | directory from the binaries. The @dfn{directory search} features of
|
---|
2232 | @code{make} facilitate this by searching several directories
|
---|
2233 | automatically to find a prerequisite. When you redistribute the files
|
---|
2234 | among directories, you do not need to change the individual rules,
|
---|
2235 | just the search paths.
|
---|
2236 |
|
---|
2237 | @menu
|
---|
2238 | * General Search:: Specifying a search path that applies
|
---|
2239 | to every prerequisite.
|
---|
2240 | * Selective Search:: Specifying a search path
|
---|
2241 | for a specified class of names.
|
---|
2242 | * Search Algorithm:: When and how search paths are applied.
|
---|
2243 | * Commands/Search:: How to write shell commands that work together
|
---|
2244 | with search paths.
|
---|
2245 | * Implicit/Search:: How search paths affect implicit rules.
|
---|
2246 | * Libraries/Search:: Directory search for link libraries.
|
---|
2247 | @end menu
|
---|
2248 |
|
---|
2249 | @node General Search, Selective Search, Directory Search, Directory Search
|
---|
2250 | @subsection @code{VPATH}: Search Path for All Prerequisites
|
---|
2251 | @vindex VPATH
|
---|
2252 |
|
---|
2253 | The value of the @code{make} variable @code{VPATH} specifies a list of
|
---|
2254 | directories that @code{make} should search. Most often, the
|
---|
2255 | directories are expected to contain prerequisite files that are not in the
|
---|
2256 | current directory; however, @code{make} uses @code{VPATH} as a search
|
---|
2257 | list for both prerequisites and targets of rules.
|
---|
2258 |
|
---|
2259 | Thus, if a file that is listed as a target or prerequisite does not exist
|
---|
2260 | in the current directory, @code{make} searches the directories listed in
|
---|
2261 | @code{VPATH} for a file with that name. If a file is found in one of
|
---|
2262 | them, that file may become the prerequisite (see below). Rules may then
|
---|
2263 | specify the names of files in the prerequisite list as if they all
|
---|
2264 | existed in the current directory. @xref{Commands/Search, ,Writing Shell
|
---|
2265 | Commands with Directory Search}.
|
---|
2266 |
|
---|
2267 | In the @code{VPATH} variable, directory names are separated by colons or
|
---|
2268 | blanks. The order in which directories are listed is the order followed
|
---|
2269 | by @code{make} in its search. (On MS-DOS and MS-Windows, semi-colons
|
---|
2270 | are used as separators of directory names in @code{VPATH}, since the
|
---|
2271 | colon can be used in the pathname itself, after the drive letter.)
|
---|
2272 |
|
---|
2273 | For example,
|
---|
2274 |
|
---|
2275 | @example
|
---|
2276 | VPATH = src:../headers
|
---|
2277 | @end example
|
---|
2278 |
|
---|
2279 | @noindent
|
---|
2280 | specifies a path containing two directories, @file{src} and
|
---|
2281 | @file{../headers}, which @code{make} searches in that order.
|
---|
2282 |
|
---|
2283 | With this value of @code{VPATH}, the following rule,
|
---|
2284 |
|
---|
2285 | @example
|
---|
2286 | foo.o : foo.c
|
---|
2287 | @end example
|
---|
2288 |
|
---|
2289 | @noindent
|
---|
2290 | is interpreted as if it were written like this:
|
---|
2291 |
|
---|
2292 | @example
|
---|
2293 | foo.o : src/foo.c
|
---|
2294 | @end example
|
---|
2295 |
|
---|
2296 | @noindent
|
---|
2297 | assuming the file @file{foo.c} does not exist in the current directory but
|
---|
2298 | is found in the directory @file{src}.
|
---|
2299 |
|
---|
2300 | @node Selective Search, Search Algorithm, General Search, Directory Search
|
---|
2301 | @subsection The @code{vpath} Directive
|
---|
2302 | @findex vpath
|
---|
2303 |
|
---|
2304 | Similar to the @code{VPATH} variable, but more selective, is the
|
---|
2305 | @code{vpath} directive (note lower case), which allows you to specify a
|
---|
2306 | search path for a particular class of file names: those that match a
|
---|
2307 | particular pattern. Thus you can supply certain search directories for
|
---|
2308 | one class of file names and other directories (or none) for other file
|
---|
2309 | names.
|
---|
2310 |
|
---|
2311 | There are three forms of the @code{vpath} directive:
|
---|
2312 |
|
---|
2313 | @table @code
|
---|
2314 | @item vpath @var{pattern} @var{directories}
|
---|
2315 | Specify the search path @var{directories} for file names that match
|
---|
2316 | @var{pattern}.
|
---|
2317 |
|
---|
2318 | The search path, @var{directories}, is a list of directories to be
|
---|
2319 | searched, separated by colons (semi-colons on MS-DOS and MS-Windows) or
|
---|
2320 | blanks, just like the search path used in the @code{VPATH} variable.
|
---|
2321 |
|
---|
2322 | @item vpath @var{pattern}
|
---|
2323 | Clear out the search path associated with @var{pattern}.
|
---|
2324 |
|
---|
2325 | @c Extra blank line makes sure this gets two lines.
|
---|
2326 | @item vpath
|
---|
2327 |
|
---|
2328 | Clear all search paths previously specified with @code{vpath} directives.
|
---|
2329 | @end table
|
---|
2330 |
|
---|
2331 | A @code{vpath} pattern is a string containing a @samp{%} character. The
|
---|
2332 | string must match the file name of a prerequisite that is being searched
|
---|
2333 | for, the @samp{%} character matching any sequence of zero or more
|
---|
2334 | characters (as in pattern rules; @pxref{Pattern Rules, ,Defining and
|
---|
2335 | Redefining Pattern Rules}). For example, @code{%.h} matches files that
|
---|
2336 | end in @code{.h}. (If there is no @samp{%}, the pattern must match the
|
---|
2337 | prerequisite exactly, which is not useful very often.)
|
---|
2338 |
|
---|
2339 | @cindex @code{%}, quoting in @code{vpath}
|
---|
2340 | @cindex @code{%}, quoting with @code{\} (backslash)
|
---|
2341 | @cindex @code{\} (backslash), to quote @code{%}
|
---|
2342 | @cindex backslash (@code{\}), to quote @code{%}
|
---|
2343 | @cindex quoting @code{%}, in @code{vpath}
|
---|
2344 | @samp{%} characters in a @code{vpath} directive's pattern can be quoted
|
---|
2345 | with preceding backslashes (@samp{\}). Backslashes that would otherwise
|
---|
2346 | quote @samp{%} characters can be quoted with more backslashes.
|
---|
2347 | Backslashes that quote @samp{%} characters or other backslashes are
|
---|
2348 | removed from the pattern before it is compared to file names. Backslashes
|
---|
2349 | that are not in danger of quoting @samp{%} characters go unmolested.@refill
|
---|
2350 |
|
---|
2351 | When a prerequisite fails to exist in the current directory, if the
|
---|
2352 | @var{pattern} in a @code{vpath} directive matches the name of the
|
---|
2353 | prerequisite file, then the @var{directories} in that directive are searched
|
---|
2354 | just like (and before) the directories in the @code{VPATH} variable.
|
---|
2355 |
|
---|
2356 | For example,
|
---|
2357 |
|
---|
2358 | @example
|
---|
2359 | vpath %.h ../headers
|
---|
2360 | @end example
|
---|
2361 |
|
---|
2362 | @noindent
|
---|
2363 | tells @code{make} to look for any prerequisite whose name ends in @file{.h}
|
---|
2364 | in the directory @file{../headers} if the file is not found in the current
|
---|
2365 | directory.
|
---|
2366 |
|
---|
2367 | If several @code{vpath} patterns match the prerequisite file's name, then
|
---|
2368 | @code{make} processes each matching @code{vpath} directive one by one,
|
---|
2369 | searching all the directories mentioned in each directive. @code{make}
|
---|
2370 | handles multiple @code{vpath} directives in the order in which they
|
---|
2371 | appear in the makefile; multiple directives with the same pattern are
|
---|
2372 | independent of each other.
|
---|
2373 |
|
---|
2374 | @need 750
|
---|
2375 | Thus,
|
---|
2376 |
|
---|
2377 | @example
|
---|
2378 | @group
|
---|
2379 | vpath %.c foo
|
---|
2380 | vpath % blish
|
---|
2381 | vpath %.c bar
|
---|
2382 | @end group
|
---|
2383 | @end example
|
---|
2384 |
|
---|
2385 | @noindent
|
---|
2386 | will look for a file ending in @samp{.c} in @file{foo}, then
|
---|
2387 | @file{blish}, then @file{bar}, while
|
---|
2388 |
|
---|
2389 | @example
|
---|
2390 | @group
|
---|
2391 | vpath %.c foo:bar
|
---|
2392 | vpath % blish
|
---|
2393 | @end group
|
---|
2394 | @end example
|
---|
2395 |
|
---|
2396 | @noindent
|
---|
2397 | will look for a file ending in @samp{.c} in @file{foo}, then
|
---|
2398 | @file{bar}, then @file{blish}.
|
---|
2399 |
|
---|
2400 | @node Search Algorithm, Commands/Search, Selective Search, Directory Search
|
---|
2401 | @subsection How Directory Searches are Performed
|
---|
2402 | @cindex algorithm for directory search
|
---|
2403 | @cindex directory search algorithm
|
---|
2404 |
|
---|
2405 | When a prerequisite is found through directory search, regardless of type
|
---|
2406 | (general or selective), the pathname located may not be the one that
|
---|
2407 | @code{make} actually provides you in the prerequisite list. Sometimes
|
---|
2408 | the path discovered through directory search is thrown away.
|
---|
2409 |
|
---|
2410 | The algorithm @code{make} uses to decide whether to keep or abandon a
|
---|
2411 | path found via directory search is as follows:
|
---|
2412 |
|
---|
2413 | @enumerate
|
---|
2414 | @item
|
---|
2415 | If a target file does not exist at the path specified in the makefile,
|
---|
2416 | directory search is performed.
|
---|
2417 |
|
---|
2418 | @item
|
---|
2419 | If the directory search is successful, that path is kept and this file
|
---|
2420 | is tentatively stored as the target.
|
---|
2421 |
|
---|
2422 | @item
|
---|
2423 | All prerequisites of this target are examined using this same method.
|
---|
2424 |
|
---|
2425 | @item
|
---|
2426 | After processing the prerequisites, the target may or may not need to be
|
---|
2427 | rebuilt:
|
---|
2428 |
|
---|
2429 | @enumerate a
|
---|
2430 | @item
|
---|
2431 | If the target does @emph{not} need to be rebuilt, the path to the file
|
---|
2432 | found during directory search is used for any prerequisite lists which
|
---|
2433 | contain this target. In short, if @code{make} doesn't need to rebuild
|
---|
2434 | the target then you use the path found via directory search.
|
---|
2435 |
|
---|
2436 | @item
|
---|
2437 | If the target @emph{does} need to be rebuilt (is out-of-date), the
|
---|
2438 | pathname found during directory search is @emph{thrown away}, and the
|
---|
2439 | target is rebuilt using the file name specified in the makefile. In
|
---|
2440 | short, if @code{make} must rebuild, then the target is rebuilt locally,
|
---|
2441 | not in the directory found via directory search.
|
---|
2442 | @end enumerate
|
---|
2443 | @end enumerate
|
---|
2444 |
|
---|
2445 | This algorithm may seem complex, but in practice it is quite often
|
---|
2446 | exactly what you want.
|
---|
2447 |
|
---|
2448 | @cindex traditional directory search (GPATH)
|
---|
2449 | @cindex directory search, traditional (GPATH)
|
---|
2450 | Other versions of @code{make} use a simpler algorithm: if the file does
|
---|
2451 | not exist, and it is found via directory search, then that pathname is
|
---|
2452 | always used whether or not the target needs to be built. Thus, if the
|
---|
2453 | target is rebuilt it is created at the pathname discovered during
|
---|
2454 | directory search.
|
---|
2455 |
|
---|
2456 | @vindex GPATH
|
---|
2457 | If, in fact, this is the behavior you want for some or all of your
|
---|
2458 | directories, you can use the @code{GPATH} variable to indicate this to
|
---|
2459 | @code{make}.
|
---|
2460 |
|
---|
2461 | @code{GPATH} has the same syntax and format as @code{VPATH} (that is, a
|
---|
2462 | space- or colon-delimited list of pathnames). If an out-of-date target
|
---|
2463 | is found by directory search in a directory that also appears in
|
---|
2464 | @code{GPATH}, then that pathname is not thrown away. The target is
|
---|
2465 | rebuilt using the expanded path.
|
---|
2466 |
|
---|
2467 | @node Commands/Search, Implicit/Search, Search Algorithm, Directory Search
|
---|
2468 | @subsection Writing Shell Commands with Directory Search
|
---|
2469 | @cindex shell command, and directory search
|
---|
2470 | @cindex directory search (@code{VPATH}), and shell commands
|
---|
2471 |
|
---|
2472 | When a prerequisite is found in another directory through directory search,
|
---|
2473 | this cannot change the commands of the rule; they will execute as written.
|
---|
2474 | Therefore, you must write the commands with care so that they will look for
|
---|
2475 | the prerequisite in the directory where @code{make} finds it.
|
---|
2476 |
|
---|
2477 | This is done with the @dfn{automatic variables} such as @samp{$^}
|
---|
2478 | (@pxref{Automatic Variables}).
|
---|
2479 | For instance, the value of @samp{$^} is a
|
---|
2480 | list of all the prerequisites of the rule, including the names of
|
---|
2481 | the directories in which they were found, and the value of
|
---|
2482 | @samp{$@@} is the target. Thus:@refill
|
---|
2483 |
|
---|
2484 | @example
|
---|
2485 | foo.o : foo.c
|
---|
2486 | cc -c $(CFLAGS) $^ -o $@@
|
---|
2487 | @end example
|
---|
2488 |
|
---|
2489 | @noindent
|
---|
2490 | (The variable @code{CFLAGS} exists so you can specify flags for C
|
---|
2491 | compilation by implicit rules; we use it here for consistency so it will
|
---|
2492 | affect all C compilations uniformly;
|
---|
2493 | @pxref{Implicit Variables, ,Variables Used by Implicit Rules}.)
|
---|
2494 |
|
---|
2495 | Often the prerequisites include header files as well, which you do not
|
---|
2496 | want to mention in the commands. The automatic variable @samp{$<} is
|
---|
2497 | just the first prerequisite:
|
---|
2498 |
|
---|
2499 | @example
|
---|
2500 | VPATH = src:../headers
|
---|
2501 | foo.o : foo.c defs.h hack.h
|
---|
2502 | cc -c $(CFLAGS) $< -o $@@
|
---|
2503 | @end example
|
---|
2504 |
|
---|
2505 | @node Implicit/Search, Libraries/Search, Commands/Search, Directory Search
|
---|
2506 | @subsection Directory Search and Implicit Rules
|
---|
2507 | @cindex @code{VPATH}, and implicit rules
|
---|
2508 | @cindex directory search (@code{VPATH}), and implicit rules
|
---|
2509 | @cindex search path for prerequisites (@code{VPATH}), and implicit rules
|
---|
2510 | @cindex implicit rule, and directory search
|
---|
2511 | @cindex implicit rule, and @code{VPATH}
|
---|
2512 | @cindex rule, implicit, and directory search
|
---|
2513 | @cindex rule, implicit, and @code{VPATH}
|
---|
2514 |
|
---|
2515 | The search through the directories specified in @code{VPATH} or with
|
---|
2516 | @code{vpath} also happens during consideration of implicit rules
|
---|
2517 | (@pxref{Implicit Rules, ,Using Implicit Rules}).
|
---|
2518 |
|
---|
2519 | For example, when a file @file{foo.o} has no explicit rule, @code{make}
|
---|
2520 | considers implicit rules, such as the built-in rule to compile
|
---|
2521 | @file{foo.c} if that file exists. If such a file is lacking in the
|
---|
2522 | current directory, the appropriate directories are searched for it. If
|
---|
2523 | @file{foo.c} exists (or is mentioned in the makefile) in any of the
|
---|
2524 | directories, the implicit rule for C compilation is applied.
|
---|
2525 |
|
---|
2526 | The commands of implicit rules normally use automatic variables as a
|
---|
2527 | matter of necessity; consequently they will use the file names found by
|
---|
2528 | directory search with no extra effort.
|
---|
2529 |
|
---|
2530 | @node Libraries/Search, , Implicit/Search, Directory Search
|
---|
2531 | @subsection Directory Search for Link Libraries
|
---|
2532 | @cindex link libraries, and directory search
|
---|
2533 | @cindex libraries for linking, directory search
|
---|
2534 | @cindex directory search (@code{VPATH}), and link libraries
|
---|
2535 | @cindex @code{VPATH}, and link libraries
|
---|
2536 | @cindex search path for prerequisites (@code{VPATH}), and link libraries
|
---|
2537 | @cindex @code{-l} (library search)
|
---|
2538 | @cindex link libraries, patterns matching
|
---|
2539 | @cindex @code{.LIBPATTERNS}, and link libraries
|
---|
2540 | @vindex .LIBPATTERNS
|
---|
2541 |
|
---|
2542 | Directory search applies in a special way to libraries used with the
|
---|
2543 | linker. This special feature comes into play when you write a prerequisite
|
---|
2544 | whose name is of the form @samp{-l@var{name}}. (You can tell something
|
---|
2545 | strange is going on here because the prerequisite is normally the name of a
|
---|
2546 | file, and the @emph{file name} of a library generally looks like
|
---|
2547 | @file{lib@var{name}.a}, not like @samp{-l@var{name}}.)@refill
|
---|
2548 |
|
---|
2549 | When a prerequisite's name has the form @samp{-l@var{name}}, @code{make}
|
---|
2550 | handles it specially by searching for the file @file{lib@var{name}.so} in
|
---|
2551 | the current directory, in directories specified by matching @code{vpath}
|
---|
2552 | search paths and the @code{VPATH} search path, and then in the
|
---|
2553 | directories @file{/lib}, @file{/usr/lib}, and @file{@var{prefix}/lib}
|
---|
2554 | (normally @file{/usr/local/lib}, but MS-DOS/MS-Windows versions of
|
---|
2555 | @code{make} behave as if @var{prefix} is defined to be the root of the
|
---|
2556 | DJGPP installation tree).
|
---|
2557 |
|
---|
2558 | If that file is not found, then the file @file{lib@var{name}.a} is
|
---|
2559 | searched for, in the same directories as above.
|
---|
2560 |
|
---|
2561 | For example, if there is a @file{/usr/lib/libcurses.a} library on your
|
---|
2562 | system (and no @file{/usr/lib/libcurses.so} file), then
|
---|
2563 |
|
---|
2564 | @example
|
---|
2565 | @group
|
---|
2566 | foo : foo.c -lcurses
|
---|
2567 | cc $^ -o $@@
|
---|
2568 | @end group
|
---|
2569 | @end example
|
---|
2570 |
|
---|
2571 | @noindent
|
---|
2572 | would cause the command @samp{cc foo.c /usr/lib/libcurses.a -o foo} to
|
---|
2573 | be executed when @file{foo} is older than @file{foo.c} or than
|
---|
2574 | @file{/usr/lib/libcurses.a}.@refill
|
---|
2575 |
|
---|
2576 | Although the default set of files to be searched for is
|
---|
2577 | @file{lib@var{name}.so} and @file{lib@var{name}.a}, this is customizable
|
---|
2578 | via the @code{.LIBPATTERNS} variable. Each word in the value of this
|
---|
2579 | variable is a pattern string. When a prerequisite like
|
---|
2580 | @samp{-l@var{name}} is seen, @code{make} will replace the percent in
|
---|
2581 | each pattern in the list with @var{name} and perform the above directory
|
---|
2582 | searches using that library filename. If no library is found, the next
|
---|
2583 | word in the list will be used.
|
---|
2584 |
|
---|
2585 | The default value for @code{.LIBPATTERNS} is @samp{lib%.so lib%.a},
|
---|
2586 | which provides the default behavior described above.
|
---|
2587 |
|
---|
2588 | You can turn off link library expansion completely by setting this
|
---|
2589 | variable to an empty value.
|
---|
2590 |
|
---|
2591 | @node Phony Targets, Force Targets, Directory Search, Rules
|
---|
2592 | @section Phony Targets
|
---|
2593 | @cindex phony targets
|
---|
2594 | @cindex targets, phony
|
---|
2595 | @cindex targets without a file
|
---|
2596 |
|
---|
2597 | A phony target is one that is not really the name of a file. It is just a
|
---|
2598 | name for some commands to be executed when you make an explicit request.
|
---|
2599 | There are two reasons to use a phony target: to avoid a conflict with
|
---|
2600 | a file of the same name, and to improve performance.
|
---|
2601 |
|
---|
2602 | If you write a rule whose commands will not create the target file, the
|
---|
2603 | commands will be executed every time the target comes up for remaking.
|
---|
2604 | Here is an example:
|
---|
2605 |
|
---|
2606 | @example
|
---|
2607 | @group
|
---|
2608 | clean:
|
---|
2609 | rm *.o temp
|
---|
2610 | @end group
|
---|
2611 | @end example
|
---|
2612 |
|
---|
2613 | @noindent
|
---|
2614 | Because the @code{rm} command does not create a file named @file{clean},
|
---|
2615 | probably no such file will ever exist. Therefore, the @code{rm} command
|
---|
2616 | will be executed every time you say @samp{make clean}.
|
---|
2617 | @cindex @code{rm} (shell command)
|
---|
2618 |
|
---|
2619 | @findex .PHONY
|
---|
2620 | The phony target will cease to work if anything ever does create a file
|
---|
2621 | named @file{clean} in this directory. Since it has no prerequisites, the
|
---|
2622 | file @file{clean} would inevitably be considered up to date, and its
|
---|
2623 | commands would not be executed. To avoid this problem, you can explicitly
|
---|
2624 | declare the target to be phony, using the special target @code{.PHONY}
|
---|
2625 | (@pxref{Special Targets, ,Special Built-in Target Names}) as follows:
|
---|
2626 |
|
---|
2627 | @example
|
---|
2628 | .PHONY : clean
|
---|
2629 | @end example
|
---|
2630 |
|
---|
2631 | @noindent
|
---|
2632 | Once this is done, @samp{make clean} will run the commands regardless of
|
---|
2633 | whether there is a file named @file{clean}.
|
---|
2634 |
|
---|
2635 | Since it knows that phony targets do not name actual files that could be
|
---|
2636 | remade from other files, @code{make} skips the implicit rule search for
|
---|
2637 | phony targets (@pxref{Implicit Rules}). This is why declaring a target
|
---|
2638 | phony is good for performance, even if you are not worried about the
|
---|
2639 | actual file existing.
|
---|
2640 |
|
---|
2641 | Thus, you first write the line that states that @code{clean} is a
|
---|
2642 | phony target, then you write the rule, like this:
|
---|
2643 |
|
---|
2644 | @example
|
---|
2645 | @group
|
---|
2646 | .PHONY: clean
|
---|
2647 | clean:
|
---|
2648 | rm *.o temp
|
---|
2649 | @end group
|
---|
2650 | @end example
|
---|
2651 |
|
---|
2652 | Another example of the usefulness of phony targets is in conjunction
|
---|
2653 | with recursive invocations of @code{make} (for more information, see
|
---|
2654 | @ref{Recursion, ,Recursive Use of @code{make}}). In this case the
|
---|
2655 | makefile will often contain a variable which lists a number of
|
---|
2656 | subdirectories to be built. One way to handle this is with one rule
|
---|
2657 | whose command is a shell loop over the subdirectories, like this:
|
---|
2658 |
|
---|
2659 | @example
|
---|
2660 | @group
|
---|
2661 | SUBDIRS = foo bar baz
|
---|
2662 |
|
---|
2663 | subdirs:
|
---|
2664 | for dir in $(SUBDIRS); do \
|
---|
2665 | $(MAKE) -C $$dir; \
|
---|
2666 | done
|
---|
2667 | @end group
|
---|
2668 | @end example
|
---|
2669 |
|
---|
2670 | There are a few problems with this method, however. First, any error
|
---|
2671 | detected in a submake is not noted by this rule, so it will continue to
|
---|
2672 | build the rest of the directories even when one fails. This can be
|
---|
2673 | overcome by adding shell commands to note the error and exit, but then
|
---|
2674 | it will do so even if @code{make} is invoked with the @code{-k} option,
|
---|
2675 | which is unfortunate. Second, and perhaps more importantly, you cannot
|
---|
2676 | take advantage of @code{make}'s ability to build targets in parallel
|
---|
2677 | (@pxref{Parallel, ,Parallel Execution}), since there is only one rule.
|
---|
2678 |
|
---|
2679 | By declaring the subdirectories as phony targets (you must do this as
|
---|
2680 | the subdirectory obviously always exists; otherwise it won't be built)
|
---|
2681 | you can remove these problems:
|
---|
2682 |
|
---|
2683 | @example
|
---|
2684 | @group
|
---|
2685 | SUBDIRS = foo bar baz
|
---|
2686 |
|
---|
2687 | .PHONY: subdirs $(SUBDIRS)
|
---|
2688 |
|
---|
2689 | subdirs: $(SUBDIRS)
|
---|
2690 |
|
---|
2691 | $(SUBDIRS):
|
---|
2692 | $(MAKE) -C $@@
|
---|
2693 |
|
---|
2694 | foo: baz
|
---|
2695 | @end group
|
---|
2696 | @end example
|
---|
2697 |
|
---|
2698 | Here we've also declared that the @file{foo} subdirectory cannot be
|
---|
2699 | built until after the @file{baz} subdirectory is complete; this kind of
|
---|
2700 | relationship declaration is particularly important when attempting
|
---|
2701 | parallel builds.
|
---|
2702 |
|
---|
2703 | A phony target should not be a prerequisite of a real target file; if it
|
---|
2704 | is, its commands are run every time @code{make} goes to update that
|
---|
2705 | file. As long as a phony target is never a prerequisite of a real
|
---|
2706 | target, the phony target commands will be executed only when the phony
|
---|
2707 | target is a specified goal (@pxref{Goals, ,Arguments to Specify the
|
---|
2708 | Goals}).
|
---|
2709 |
|
---|
2710 | Phony targets can have prerequisites. When one directory contains multiple
|
---|
2711 | programs, it is most convenient to describe all of the programs in one
|
---|
2712 | makefile @file{./Makefile}. Since the target remade by default will be the
|
---|
2713 | first one in the makefile, it is common to make this a phony target named
|
---|
2714 | @samp{all} and give it, as prerequisites, all the individual programs. For
|
---|
2715 | example:
|
---|
2716 |
|
---|
2717 | @example
|
---|
2718 | all : prog1 prog2 prog3
|
---|
2719 | .PHONY : all
|
---|
2720 |
|
---|
2721 | prog1 : prog1.o utils.o
|
---|
2722 | cc -o prog1 prog1.o utils.o
|
---|
2723 |
|
---|
2724 | prog2 : prog2.o
|
---|
2725 | cc -o prog2 prog2.o
|
---|
2726 |
|
---|
2727 | prog3 : prog3.o sort.o utils.o
|
---|
2728 | cc -o prog3 prog3.o sort.o utils.o
|
---|
2729 | @end example
|
---|
2730 |
|
---|
2731 | @noindent
|
---|
2732 | Now you can say just @samp{make} to remake all three programs, or
|
---|
2733 | specify as arguments the ones to remake (as in @samp{make prog1
|
---|
2734 | prog3}). Phoniness is not inherited: the prerequisites of a phony
|
---|
2735 | target are not themselves phony, unless explicitly declared to be so.
|
---|
2736 |
|
---|
2737 | When one phony target is a prerequisite of another, it serves as a subroutine
|
---|
2738 | of the other. For example, here @samp{make cleanall} will delete the
|
---|
2739 | object files, the difference files, and the file @file{program}:
|
---|
2740 |
|
---|
2741 | @example
|
---|
2742 | .PHONY: cleanall cleanobj cleandiff
|
---|
2743 |
|
---|
2744 | cleanall : cleanobj cleandiff
|
---|
2745 | rm program
|
---|
2746 |
|
---|
2747 | cleanobj :
|
---|
2748 | rm *.o
|
---|
2749 |
|
---|
2750 | cleandiff :
|
---|
2751 | rm *.diff
|
---|
2752 | @end example
|
---|
2753 |
|
---|
2754 | @node Force Targets, Empty Targets, Phony Targets, Rules
|
---|
2755 | @section Rules without Commands or Prerequisites
|
---|
2756 | @cindex force targets
|
---|
2757 | @cindex targets, force
|
---|
2758 | @cindex @code{FORCE}
|
---|
2759 | @cindex rule, no commands or prerequisites
|
---|
2760 |
|
---|
2761 | If a rule has no prerequisites or commands, and the target of the rule
|
---|
2762 | is a nonexistent file, then @code{make} imagines this target to have
|
---|
2763 | been updated whenever its rule is run. This implies that all targets
|
---|
2764 | depending on this one will always have their commands run.
|
---|
2765 |
|
---|
2766 | An example will illustrate this:
|
---|
2767 |
|
---|
2768 | @example
|
---|
2769 | @group
|
---|
2770 | clean: FORCE
|
---|
2771 | rm $(objects)
|
---|
2772 | FORCE:
|
---|
2773 | @end group
|
---|
2774 | @end example
|
---|
2775 |
|
---|
2776 | Here the target @samp{FORCE} satisfies the special conditions, so the
|
---|
2777 | target @file{clean} that depends on it is forced to run its commands.
|
---|
2778 | There is nothing special about the name @samp{FORCE}, but that is one name
|
---|
2779 | commonly used this way.
|
---|
2780 |
|
---|
2781 | As you can see, using @samp{FORCE} this way has the same results as using
|
---|
2782 | @samp{.PHONY: clean}.
|
---|
2783 |
|
---|
2784 | Using @samp{.PHONY} is more explicit and more efficient. However,
|
---|
2785 | other versions of @code{make} do not support @samp{.PHONY}; thus
|
---|
2786 | @samp{FORCE} appears in many makefiles. @xref{Phony Targets}.
|
---|
2787 |
|
---|
2788 | @node Empty Targets, Special Targets, Force Targets, Rules
|
---|
2789 | @section Empty Target Files to Record Events
|
---|
2790 | @cindex empty targets
|
---|
2791 | @cindex targets, empty
|
---|
2792 | @cindex recording events with empty targets
|
---|
2793 |
|
---|
2794 | The @dfn{empty target} is a variant of the phony target; it is used to hold
|
---|
2795 | commands for an action that you request explicitly from time to time.
|
---|
2796 | Unlike a phony target, this target file can really exist; but the file's
|
---|
2797 | contents do not matter, and usually are empty.
|
---|
2798 |
|
---|
2799 | The purpose of the empty target file is to record, with its
|
---|
2800 | last-modification time, when the rule's commands were last executed. It
|
---|
2801 | does so because one of the commands is a @code{touch} command to update the
|
---|
2802 | target file.
|
---|
2803 |
|
---|
2804 | The empty target file should have some prerequisites (otherwise it
|
---|
2805 | doesn't make sense). When you ask to remake the empty target, the
|
---|
2806 | commands are executed if any prerequisite is more recent than the target;
|
---|
2807 | in other words, if a prerequisite has changed since the last time you
|
---|
2808 | remade the target. Here is an example:
|
---|
2809 |
|
---|
2810 | @example
|
---|
2811 | print: foo.c bar.c
|
---|
2812 | lpr -p $?
|
---|
2813 | touch print
|
---|
2814 | @end example
|
---|
2815 | @cindex @code{print} target
|
---|
2816 | @cindex @code{lpr} (shell command)
|
---|
2817 | @cindex @code{touch} (shell command)
|
---|
2818 |
|
---|
2819 | @noindent
|
---|
2820 | With this rule, @samp{make print} will execute the @code{lpr} command if
|
---|
2821 | either source file has changed since the last @samp{make print}. The
|
---|
2822 | automatic variable @samp{$?} is used to print only those files that have
|
---|
2823 | changed (@pxref{Automatic Variables}).
|
---|
2824 |
|
---|
2825 | @node Special Targets, Multiple Targets, Empty Targets, Rules
|
---|
2826 | @section Special Built-in Target Names
|
---|
2827 | @cindex special targets
|
---|
2828 | @cindex built-in special targets
|
---|
2829 | @cindex targets, built-in special
|
---|
2830 |
|
---|
2831 | Certain names have special meanings if they appear as targets.
|
---|
2832 |
|
---|
2833 | @table @code
|
---|
2834 | @findex .PHONY
|
---|
2835 | @item .PHONY
|
---|
2836 |
|
---|
2837 | The prerequisites of the special target @code{.PHONY} are considered to
|
---|
2838 | be phony targets. When it is time to consider such a target,
|
---|
2839 | @code{make} will run its commands unconditionally, regardless of
|
---|
2840 | whether a file with that name exists or what its last-modification
|
---|
2841 | time is. @xref{Phony Targets, ,Phony Targets}.
|
---|
2842 |
|
---|
2843 | @findex .SUFFIXES
|
---|
2844 | @item .SUFFIXES
|
---|
2845 |
|
---|
2846 | The prerequisites of the special target @code{.SUFFIXES} are the list
|
---|
2847 | of suffixes to be used in checking for suffix rules.
|
---|
2848 | @xref{Suffix Rules, , Old-Fashioned Suffix Rules}.
|
---|
2849 |
|
---|
2850 | @findex .DEFAULT
|
---|
2851 | @item .DEFAULT
|
---|
2852 |
|
---|
2853 | The commands specified for @code{.DEFAULT} are used for any target for
|
---|
2854 | which no rules are found (either explicit rules or implicit rules).
|
---|
2855 | @xref{Last Resort}. If @code{.DEFAULT} commands are specified, every
|
---|
2856 | file mentioned as a prerequisite, but not as a target in a rule, will have
|
---|
2857 | these commands executed on its behalf. @xref{Implicit Rule Search,
|
---|
2858 | ,Implicit Rule Search Algorithm}.
|
---|
2859 |
|
---|
2860 | @findex .PRECIOUS
|
---|
2861 | @item .PRECIOUS
|
---|
2862 | @cindex precious targets
|
---|
2863 | @cindex preserving with @code{.PRECIOUS}
|
---|
2864 |
|
---|
2865 | The targets which @code{.PRECIOUS} depends on are given the following
|
---|
2866 | special treatment: if @code{make} is killed or interrupted during the
|
---|
2867 | execution of their commands, the target is not deleted.
|
---|
2868 | @xref{Interrupts, ,Interrupting or Killing @code{make}}. Also, if the
|
---|
2869 | target is an intermediate file, it will not be deleted after it is no
|
---|
2870 | longer needed, as is normally done. @xref{Chained Rules, ,Chains of
|
---|
2871 | Implicit Rules}. In this latter respect it overlaps with the
|
---|
2872 | @code{.SECONDARY} special target.
|
---|
2873 |
|
---|
2874 | You can also list the target pattern of an implicit rule (such as
|
---|
2875 | @samp{%.o}) as a prerequisite file of the special target @code{.PRECIOUS}
|
---|
2876 | to preserve intermediate files created by rules whose target patterns
|
---|
2877 | match that file's name.
|
---|
2878 |
|
---|
2879 | @findex .INTERMEDIATE
|
---|
2880 | @item .INTERMEDIATE
|
---|
2881 | @cindex intermediate targets, explicit
|
---|
2882 |
|
---|
2883 | The targets which @code{.INTERMEDIATE} depends on are treated as
|
---|
2884 | intermediate files. @xref{Chained Rules, ,Chains of Implicit Rules}.
|
---|
2885 | @code{.INTERMEDIATE} with no prerequisites has no effect.
|
---|
2886 |
|
---|
2887 | @findex .SECONDARY
|
---|
2888 | @item .SECONDARY
|
---|
2889 | @cindex secondary targets
|
---|
2890 | @cindex preserving with @code{.SECONDARY}
|
---|
2891 |
|
---|
2892 | The targets which @code{.SECONDARY} depends on are treated as
|
---|
2893 | intermediate files, except that they are never automatically deleted.
|
---|
2894 | @xref{Chained Rules, ,Chains of Implicit Rules}.
|
---|
2895 |
|
---|
2896 | @code{.SECONDARY} with no prerequisites causes all targets to be treated
|
---|
2897 | as secondary (i.e., no target is removed because it is considered
|
---|
2898 | intermediate).
|
---|
2899 |
|
---|
2900 | @findex .SECONDEXPANSION
|
---|
2901 | @item .SECONDEXPANSION
|
---|
2902 |
|
---|
2903 | If @code{.SECONDEXPANSION} is mentioned as a target anywhere in the
|
---|
2904 | makefile, then all prerequisite lists defined @emph{after} it appears
|
---|
2905 | will be expanded a second time after all makefiles have been read in.
|
---|
2906 | @xref{Secondary Expansion, ,Secondary Expansion}.
|
---|
2907 |
|
---|
2908 | The prerequisites of the special target @code{.SUFFIXES} are the list
|
---|
2909 | of suffixes to be used in checking for suffix rules.
|
---|
2910 | @xref{Suffix Rules, , Old-Fashioned Suffix Rules}.
|
---|
2911 |
|
---|
2912 | @findex .DELETE_ON_ERROR
|
---|
2913 | @item .DELETE_ON_ERROR
|
---|
2914 | @cindex removing targets on failure
|
---|
2915 |
|
---|
2916 | If @code{.DELETE_ON_ERROR} is mentioned as a target anywhere in the
|
---|
2917 | makefile, then @code{make} will delete the target of a rule if it has
|
---|
2918 | changed and its commands exit with a nonzero exit status, just as it
|
---|
2919 | does when it receives a signal. @xref{Errors, ,Errors in Commands}.
|
---|
2920 |
|
---|
2921 | @findex .IGNORE
|
---|
2922 | @item .IGNORE
|
---|
2923 |
|
---|
2924 | If you specify prerequisites for @code{.IGNORE}, then @code{make} will
|
---|
2925 | ignore errors in execution of the commands run for those particular
|
---|
2926 | files. The commands for @code{.IGNORE} are not meaningful.
|
---|
2927 |
|
---|
2928 | If mentioned as a target with no prerequisites, @code{.IGNORE} says to
|
---|
2929 | ignore errors in execution of commands for all files. This usage of
|
---|
2930 | @samp{.IGNORE} is supported only for historical compatibility. Since
|
---|
2931 | this affects every command in the makefile, it is not very useful; we
|
---|
2932 | recommend you use the more selective ways to ignore errors in specific
|
---|
2933 | commands. @xref{Errors, ,Errors in Commands}.
|
---|
2934 |
|
---|
2935 | @findex .LOW_RESOLUTION_TIME
|
---|
2936 | @item .LOW_RESOLUTION_TIME
|
---|
2937 |
|
---|
2938 | If you specify prerequisites for @code{.LOW_RESOLUTION_TIME},
|
---|
2939 | @command{make} assumes that these files are created by commands that
|
---|
2940 | generate low resolution time stamps. The commands for
|
---|
2941 | @code{.LOW_RESOLUTION_TIME} are not meaningful.
|
---|
2942 |
|
---|
2943 | The high resolution file time stamps of many modern hosts lessen the
|
---|
2944 | chance of @command{make} incorrectly concluding that a file is up to
|
---|
2945 | date. Unfortunately, these hosts provide no way to set a high
|
---|
2946 | resolution file time stamp, so commands like @samp{cp -p} that
|
---|
2947 | explicitly set a file's time stamp must discard its subsecond part. If
|
---|
2948 | a file is created by such a command, you should list it as a
|
---|
2949 | prerequisite of @code{.LOW_RESOLUTION_TIME} so that @command{make} does
|
---|
2950 | not mistakenly conclude that the file is out of date. For example:
|
---|
2951 |
|
---|
2952 | @example
|
---|
2953 | @group
|
---|
2954 | .LOW_RESOLUTION_TIME: dst
|
---|
2955 | dst: src
|
---|
2956 | cp -p src dst
|
---|
2957 | @end group
|
---|
2958 | @end example
|
---|
2959 |
|
---|
2960 | Since @samp{cp -p} discards the subsecond part of @file{src}'s time
|
---|
2961 | stamp, @file{dst} is typically slightly older than @file{src} even when
|
---|
2962 | it is up to date. The @code{.LOW_RESOLUTION_TIME} line causes
|
---|
2963 | @command{make} to consider @file{dst} to be up to date if its time stamp
|
---|
2964 | is at the start of the same second that @file{src}'s time stamp is in.
|
---|
2965 |
|
---|
2966 | Due to a limitation of the archive format, archive member time stamps
|
---|
2967 | are always low resolution. You need not list archive members as
|
---|
2968 | prerequisites of @code{.LOW_RESOLUTION_TIME}, as @command{make} does this
|
---|
2969 | automatically.
|
---|
2970 |
|
---|
2971 | @findex .SILENT
|
---|
2972 | @item .SILENT
|
---|
2973 |
|
---|
2974 | If you specify prerequisites for @code{.SILENT}, then @code{make} will
|
---|
2975 | not print the commands to remake those particular files before executing
|
---|
2976 | them. The commands for @code{.SILENT} are not meaningful.
|
---|
2977 |
|
---|
2978 | If mentioned as a target with no prerequisites, @code{.SILENT} says not
|
---|
2979 | to print any commands before executing them. This usage of
|
---|
2980 | @samp{.SILENT} is supported only for historical compatibility. We
|
---|
2981 | recommend you use the more selective ways to silence specific commands.
|
---|
2982 | @xref{Echoing, ,Command Echoing}. If you want to silence all commands
|
---|
2983 | for a particular run of @code{make}, use the @samp{-s} or
|
---|
2984 | @w{@samp{--silent}} option (@pxref{Options Summary}).
|
---|
2985 |
|
---|
2986 | @findex .EXPORT_ALL_VARIABLES
|
---|
2987 | @item .EXPORT_ALL_VARIABLES
|
---|
2988 |
|
---|
2989 | Simply by being mentioned as a target, this tells @code{make} to
|
---|
2990 | export all variables to child processes by default.
|
---|
2991 | @xref{Variables/Recursion, ,Communicating Variables to a
|
---|
2992 | Sub-@code{make}}.
|
---|
2993 |
|
---|
2994 | @findex .NOTPARALLEL
|
---|
2995 | @item .NOTPARALLEL
|
---|
2996 | @cindex parallel execution, overriding
|
---|
2997 |
|
---|
2998 | If @code{.NOTPARALLEL} is mentioned as a target, then this invocation of
|
---|
2999 | @code{make} will be run serially, even if the @samp{-j} option is
|
---|
3000 | given. Any recursively invoked @code{make} command will still be run in
|
---|
3001 | parallel (unless its makefile contains this target). Any prerequisites
|
---|
3002 | on this target are ignored.
|
---|
3003 | @end table
|
---|
3004 |
|
---|
3005 | Any defined implicit rule suffix also counts as a special target if it
|
---|
3006 | appears as a target, and so does the concatenation of two suffixes, such
|
---|
3007 | as @samp{.c.o}. These targets are suffix rules, an obsolete way of
|
---|
3008 | defining implicit rules (but a way still widely used). In principle, any
|
---|
3009 | target name could be special in this way if you break it in two and add
|
---|
3010 | both pieces to the suffix list. In practice, suffixes normally begin with
|
---|
3011 | @samp{.}, so these special target names also begin with @samp{.}.
|
---|
3012 | @xref{Suffix Rules, ,Old-Fashioned Suffix Rules}.
|
---|
3013 |
|
---|
3014 | @node Multiple Targets, Multiple Rules, Special Targets, Rules
|
---|
3015 | @section Multiple Targets in a Rule
|
---|
3016 | @cindex multiple targets
|
---|
3017 | @cindex several targets in a rule
|
---|
3018 | @cindex targets, multiple
|
---|
3019 | @cindex rule, with multiple targets
|
---|
3020 |
|
---|
3021 | A rule with multiple targets is equivalent to writing many rules, each with
|
---|
3022 | one target, and all identical aside from that. The same commands apply to
|
---|
3023 | all the targets, but their effects may vary because you can substitute the
|
---|
3024 | actual target name into the command using @samp{$@@}. The rule contributes
|
---|
3025 | the same prerequisites to all the targets also.
|
---|
3026 |
|
---|
3027 | This is useful in two cases.
|
---|
3028 |
|
---|
3029 | @itemize @bullet
|
---|
3030 | @item
|
---|
3031 | You want just prerequisites, no commands. For example:
|
---|
3032 |
|
---|
3033 | @example
|
---|
3034 | kbd.o command.o files.o: command.h
|
---|
3035 | @end example
|
---|
3036 |
|
---|
3037 | @noindent
|
---|
3038 | gives an additional prerequisite to each of the three object files
|
---|
3039 | mentioned.
|
---|
3040 |
|
---|
3041 | @item
|
---|
3042 | Similar commands work for all the targets. The commands do not need
|
---|
3043 | to be absolutely identical, since the automatic variable @samp{$@@}
|
---|
3044 | can be used to substitute the particular target to be remade into the
|
---|
3045 | commands (@pxref{Automatic Variables}). For example:
|
---|
3046 |
|
---|
3047 | @example
|
---|
3048 | @group
|
---|
3049 | bigoutput littleoutput : text.g
|
---|
3050 | generate text.g -$(subst output,,$@@) > $@@
|
---|
3051 | @end group
|
---|
3052 | @end example
|
---|
3053 | @findex subst
|
---|
3054 |
|
---|
3055 | @noindent
|
---|
3056 | is equivalent to
|
---|
3057 |
|
---|
3058 | @example
|
---|
3059 | bigoutput : text.g
|
---|
3060 | generate text.g -big > bigoutput
|
---|
3061 | littleoutput : text.g
|
---|
3062 | generate text.g -little > littleoutput
|
---|
3063 | @end example
|
---|
3064 |
|
---|
3065 | @noindent
|
---|
3066 | Here we assume the hypothetical program @code{generate} makes two
|
---|
3067 | types of output, one if given @samp{-big} and one if given
|
---|
3068 | @samp{-little}.
|
---|
3069 | @xref{Text Functions, ,Functions for String Substitution and Analysis},
|
---|
3070 | for an explanation of the @code{subst} function.
|
---|
3071 | @end itemize
|
---|
3072 |
|
---|
3073 | Suppose you would like to vary the prerequisites according to the target,
|
---|
3074 | much as the variable @samp{$@@} allows you to vary the commands.
|
---|
3075 | You cannot do this with multiple targets in an ordinary rule, but you can
|
---|
3076 | do it with a @dfn{static pattern rule}.
|
---|
3077 | @xref{Static Pattern, ,Static Pattern Rules}.
|
---|
3078 |
|
---|
3079 | @node Multiple Rules, Static Pattern, Multiple Targets, Rules
|
---|
3080 | @section Multiple Rules for One Target
|
---|
3081 | @cindex multiple rules for one target
|
---|
3082 | @cindex several rules for one target
|
---|
3083 | @cindex rule, multiple for one target
|
---|
3084 | @cindex target, multiple rules for one
|
---|
3085 |
|
---|
3086 | One file can be the target of several rules. All the prerequisites
|
---|
3087 | mentioned in all the rules are merged into one list of prerequisites for
|
---|
3088 | the target. If the target is older than any prerequisite from any rule,
|
---|
3089 | the commands are executed.
|
---|
3090 |
|
---|
3091 | There can only be one set of commands to be executed for a file. If
|
---|
3092 | more than one rule gives commands for the same file, @code{make} uses
|
---|
3093 | the last set given and prints an error message. (As a special case,
|
---|
3094 | if the file's name begins with a dot, no error message is printed.
|
---|
3095 | This odd behavior is only for compatibility with other implementations
|
---|
3096 | of @code{make}... you should avoid using it). Occasionally it is
|
---|
3097 | useful to have the same target invoke multiple commands which are
|
---|
3098 | defined in different parts of your makefile; you can use
|
---|
3099 | @dfn{double-colon rules} (@pxref{Double-Colon}) for this.
|
---|
3100 |
|
---|
3101 | An extra rule with just prerequisites can be used to give a few extra
|
---|
3102 | prerequisites to many files at once. For example, makefiles often
|
---|
3103 | have a variable, such as @code{objects}, containing a list of all the
|
---|
3104 | compiler output files in the system being made. An easy way to say
|
---|
3105 | that all of them must be recompiled if @file{config.h} changes is to
|
---|
3106 | write the following:
|
---|
3107 |
|
---|
3108 | @example
|
---|
3109 | objects = foo.o bar.o
|
---|
3110 | foo.o : defs.h
|
---|
3111 | bar.o : defs.h test.h
|
---|
3112 | $(objects) : config.h
|
---|
3113 | @end example
|
---|
3114 |
|
---|
3115 | This could be inserted or taken out without changing the rules that really
|
---|
3116 | specify how to make the object files, making it a convenient form to use if
|
---|
3117 | you wish to add the additional prerequisite intermittently.
|
---|
3118 |
|
---|
3119 | Another wrinkle is that the additional prerequisites could be specified with
|
---|
3120 | a variable that you set with a command argument to @code{make}
|
---|
3121 | (@pxref{Overriding, ,Overriding Variables}). For example,
|
---|
3122 |
|
---|
3123 | @example
|
---|
3124 | @group
|
---|
3125 | extradeps=
|
---|
3126 | $(objects) : $(extradeps)
|
---|
3127 | @end group
|
---|
3128 | @end example
|
---|
3129 |
|
---|
3130 | @noindent
|
---|
3131 | means that the command @samp{make extradeps=foo.h} will consider
|
---|
3132 | @file{foo.h} as a prerequisite of each object file, but plain @samp{make}
|
---|
3133 | will not.
|
---|
3134 |
|
---|
3135 | If none of the explicit rules for a target has commands, then @code{make}
|
---|
3136 | searches for an applicable implicit rule to find some commands
|
---|
3137 | @pxref{Implicit Rules, ,Using Implicit Rules}).
|
---|
3138 |
|
---|
3139 | @node Static Pattern, Double-Colon, Multiple Rules, Rules
|
---|
3140 | @section Static Pattern Rules
|
---|
3141 | @cindex static pattern rule
|
---|
3142 | @cindex rule, static pattern
|
---|
3143 | @cindex pattern rules, static (not implicit)
|
---|
3144 | @cindex varying prerequisites
|
---|
3145 | @cindex prerequisites, varying (static pattern)
|
---|
3146 |
|
---|
3147 | @dfn{Static pattern rules} are rules which specify multiple targets and
|
---|
3148 | construct the prerequisite names for each target based on the target name.
|
---|
3149 | They are more general than ordinary rules with multiple targets because the
|
---|
3150 | targets do not have to have identical prerequisites. Their prerequisites must
|
---|
3151 | be @emph{analogous}, but not necessarily @emph{identical}.
|
---|
3152 |
|
---|
3153 | @menu
|
---|
3154 | * Static Usage:: The syntax of static pattern rules.
|
---|
3155 | * Static versus Implicit:: When are they better than implicit rules?
|
---|
3156 | @end menu
|
---|
3157 |
|
---|
3158 | @node Static Usage, Static versus Implicit, Static Pattern, Static Pattern
|
---|
3159 | @subsection Syntax of Static Pattern Rules
|
---|
3160 | @cindex static pattern rule, syntax of
|
---|
3161 | @cindex pattern rules, static, syntax of
|
---|
3162 |
|
---|
3163 | Here is the syntax of a static pattern rule:
|
---|
3164 |
|
---|
3165 | @example
|
---|
3166 | @var{targets} @dots{}: @var{target-pattern}: @var{prereq-patterns} @dots{}
|
---|
3167 | @var{commands}
|
---|
3168 | @dots{}
|
---|
3169 | @end example
|
---|
3170 |
|
---|
3171 | @noindent
|
---|
3172 | The @var{targets} list specifies the targets that the rule applies to.
|
---|
3173 | The targets can contain wildcard characters, just like the targets of
|
---|
3174 | ordinary rules (@pxref{Wildcards, ,Using Wildcard Characters in File
|
---|
3175 | Names}).
|
---|
3176 |
|
---|
3177 | @cindex target pattern, static (not implicit)
|
---|
3178 | @cindex stem
|
---|
3179 | The @var{target-pattern} and @var{prereq-patterns} say how to compute the
|
---|
3180 | prerequisites of each target. Each target is matched against the
|
---|
3181 | @var{target-pattern} to extract a part of the target name, called the
|
---|
3182 | @dfn{stem}. This stem is substituted into each of the @var{prereq-patterns}
|
---|
3183 | to make the prerequisite names (one from each @var{prereq-pattern}).
|
---|
3184 |
|
---|
3185 | Each pattern normally contains the character @samp{%} just once. When the
|
---|
3186 | @var{target-pattern} matches a target, the @samp{%} can match any part of
|
---|
3187 | the target name; this part is called the @dfn{stem}. The rest of the
|
---|
3188 | pattern must match exactly. For example, the target @file{foo.o} matches
|
---|
3189 | the pattern @samp{%.o}, with @samp{foo} as the stem. The targets
|
---|
3190 | @file{foo.c} and @file{foo.out} do not match that pattern.@refill
|
---|
3191 |
|
---|
3192 | @cindex prerequisite pattern, static (not implicit)
|
---|
3193 | The prerequisite names for each target are made by substituting the stem
|
---|
3194 | for the @samp{%} in each prerequisite pattern. For example, if one
|
---|
3195 | prerequisite pattern is @file{%.c}, then substitution of the stem
|
---|
3196 | @samp{foo} gives the prerequisite name @file{foo.c}. It is legitimate
|
---|
3197 | to write a prerequisite pattern that does not contain @samp{%}; then this
|
---|
3198 | prerequisite is the same for all targets.
|
---|
3199 |
|
---|
3200 | @cindex @code{%}, quoting in static pattern
|
---|
3201 | @cindex @code{%}, quoting with @code{\} (backslash)
|
---|
3202 | @cindex @code{\} (backslash), to quote @code{%}
|
---|
3203 | @cindex backslash (@code{\}), to quote @code{%}
|
---|
3204 | @cindex quoting @code{%}, in static pattern
|
---|
3205 | @samp{%} characters in pattern rules can be quoted with preceding
|
---|
3206 | backslashes (@samp{\}). Backslashes that would otherwise quote @samp{%}
|
---|
3207 | characters can be quoted with more backslashes. Backslashes that quote
|
---|
3208 | @samp{%} characters or other backslashes are removed from the pattern
|
---|
3209 | before it is compared to file names or has a stem substituted into it.
|
---|
3210 | Backslashes that are not in danger of quoting @samp{%} characters go
|
---|
3211 | unmolested. For example, the pattern @file{the\%weird\\%pattern\\} has
|
---|
3212 | @samp{the%weird\} preceding the operative @samp{%} character, and
|
---|
3213 | @samp{pattern\\} following it. The final two backslashes are left alone
|
---|
3214 | because they cannot affect any @samp{%} character.@refill
|
---|
3215 |
|
---|
3216 | Here is an example, which compiles each of @file{foo.o} and @file{bar.o}
|
---|
3217 | from the corresponding @file{.c} file:
|
---|
3218 |
|
---|
3219 | @example
|
---|
3220 | @group
|
---|
3221 | objects = foo.o bar.o
|
---|
3222 |
|
---|
3223 | all: $(objects)
|
---|
3224 |
|
---|
3225 | $(objects): %.o: %.c
|
---|
3226 | $(CC) -c $(CFLAGS) $< -o $@@
|
---|
3227 | @end group
|
---|
3228 | @end example
|
---|
3229 |
|
---|
3230 | @noindent
|
---|
3231 | Here @samp{$<} is the automatic variable that holds the name of the
|
---|
3232 | prerequisite and @samp{$@@} is the automatic variable that holds the name
|
---|
3233 | of the target; see @ref{Automatic Variables}.
|
---|
3234 |
|
---|
3235 | Each target specified must match the target pattern; a warning is issued
|
---|
3236 | for each target that does not. If you have a list of files, only some of
|
---|
3237 | which will match the pattern, you can use the @code{filter} function to
|
---|
3238 | remove nonmatching file names (@pxref{Text Functions, ,Functions for String Substitution and Analysis}):
|
---|
3239 |
|
---|
3240 | @example
|
---|
3241 | files = foo.elc bar.o lose.o
|
---|
3242 |
|
---|
3243 | $(filter %.o,$(files)): %.o: %.c
|
---|
3244 | $(CC) -c $(CFLAGS) $< -o $@@
|
---|
3245 | $(filter %.elc,$(files)): %.elc: %.el
|
---|
3246 | emacs -f batch-byte-compile $<
|
---|
3247 | @end example
|
---|
3248 |
|
---|
3249 | @noindent
|
---|
3250 | In this example the result of @samp{$(filter %.o,$(files))} is
|
---|
3251 | @file{bar.o lose.o}, and the first static pattern rule causes each of
|
---|
3252 | these object files to be updated by compiling the corresponding C source
|
---|
3253 | file. The result of @w{@samp{$(filter %.elc,$(files))}} is
|
---|
3254 | @file{foo.elc}, so that file is made from @file{foo.el}.@refill
|
---|
3255 |
|
---|
3256 | Another example shows how to use @code{$*} in static pattern rules:
|
---|
3257 | @vindex $*@r{, and static pattern}
|
---|
3258 |
|
---|
3259 | @example
|
---|
3260 | @group
|
---|
3261 | bigoutput littleoutput : %output : text.g
|
---|
3262 | generate text.g -$* > $@@
|
---|
3263 | @end group
|
---|
3264 | @end example
|
---|
3265 |
|
---|
3266 | @noindent
|
---|
3267 | When the @code{generate} command is run, @code{$*} will expand to the
|
---|
3268 | stem, either @samp{big} or @samp{little}.
|
---|
3269 |
|
---|
3270 | @node Static versus Implicit, , Static Usage, Static Pattern
|
---|
3271 | @subsection Static Pattern Rules versus Implicit Rules
|
---|
3272 | @cindex rule, static pattern versus implicit
|
---|
3273 | @cindex static pattern rule, versus implicit
|
---|
3274 |
|
---|
3275 | A static pattern rule has much in common with an implicit rule defined as a
|
---|
3276 | pattern rule (@pxref{Pattern Rules, ,Defining and Redefining Pattern Rules}).
|
---|
3277 | Both have a pattern for the target and patterns for constructing the
|
---|
3278 | names of prerequisites. The difference is in how @code{make} decides
|
---|
3279 | @emph{when} the rule applies.
|
---|
3280 |
|
---|
3281 | An implicit rule @emph{can} apply to any target that matches its pattern,
|
---|
3282 | but it @emph{does} apply only when the target has no commands otherwise
|
---|
3283 | specified, and only when the prerequisites can be found. If more than one
|
---|
3284 | implicit rule appears applicable, only one applies; the choice depends on
|
---|
3285 | the order of rules.
|
---|
3286 |
|
---|
3287 | By contrast, a static pattern rule applies to the precise list of targets
|
---|
3288 | that you specify in the rule. It cannot apply to any other target and it
|
---|
3289 | invariably does apply to each of the targets specified. If two conflicting
|
---|
3290 | rules apply, and both have commands, that's an error.
|
---|
3291 |
|
---|
3292 | The static pattern rule can be better than an implicit rule for these
|
---|
3293 | reasons:
|
---|
3294 |
|
---|
3295 | @itemize @bullet
|
---|
3296 | @item
|
---|
3297 | You may wish to override the usual implicit rule for a few
|
---|
3298 | files whose names cannot be categorized syntactically but
|
---|
3299 | can be given in an explicit list.
|
---|
3300 |
|
---|
3301 | @item
|
---|
3302 | If you cannot be sure of the precise contents of the directories
|
---|
3303 | you are using, you may not be sure which other irrelevant files
|
---|
3304 | might lead @code{make} to use the wrong implicit rule. The choice
|
---|
3305 | might depend on the order in which the implicit rule search is done.
|
---|
3306 | With static pattern rules, there is no uncertainty: each rule applies
|
---|
3307 | to precisely the targets specified.
|
---|
3308 | @end itemize
|
---|
3309 |
|
---|
3310 | @node Double-Colon, Automatic Prerequisites, Static Pattern, Rules
|
---|
3311 | @section Double-Colon Rules
|
---|
3312 | @cindex double-colon rules
|
---|
3313 | @cindex rule, double-colon (@code{::})
|
---|
3314 | @cindex multiple rules for one target (@code{::})
|
---|
3315 | @cindex @code{::} rules (double-colon)
|
---|
3316 |
|
---|
3317 | @dfn{Double-colon} rules are rules written with @samp{::} instead of
|
---|
3318 | @samp{:} after the target names. They are handled differently from
|
---|
3319 | ordinary rules when the same target appears in more than one rule.
|
---|
3320 |
|
---|
3321 | When a target appears in multiple rules, all the rules must be the same
|
---|
3322 | type: all ordinary, or all double-colon. If they are double-colon, each
|
---|
3323 | of them is independent of the others. Each double-colon rule's commands
|
---|
3324 | are executed if the target is older than any prerequisites of that rule.
|
---|
3325 | If there are no prerequisites for that rule, its commands are always
|
---|
3326 | executed (even if the target already exists). This can result in
|
---|
3327 | executing none, any, or all of the double-colon rules.
|
---|
3328 |
|
---|
3329 | Double-colon rules with the same target are in fact completely separate
|
---|
3330 | from one another. Each double-colon rule is processed individually, just
|
---|
3331 | as rules with different targets are processed.
|
---|
3332 |
|
---|
3333 | The double-colon rules for a target are executed in the order they appear
|
---|
3334 | in the makefile. However, the cases where double-colon rules really make
|
---|
3335 | sense are those where the order of executing the commands would not matter.
|
---|
3336 |
|
---|
3337 | Double-colon rules are somewhat obscure and not often very useful; they
|
---|
3338 | provide a mechanism for cases in which the method used to update a target
|
---|
3339 | differs depending on which prerequisite files caused the update, and such
|
---|
3340 | cases are rare.
|
---|
3341 |
|
---|
3342 | Each double-colon rule should specify commands; if it does not, an
|
---|
3343 | implicit rule will be used if one applies.
|
---|
3344 | @xref{Implicit Rules, ,Using Implicit Rules}.
|
---|
3345 |
|
---|
3346 | @node Automatic Prerequisites, , Double-Colon, Rules
|
---|
3347 | @section Generating Prerequisites Automatically
|
---|
3348 | @cindex prerequisites, automatic generation
|
---|
3349 | @cindex automatic generation of prerequisites
|
---|
3350 | @cindex generating prerequisites automatically
|
---|
3351 |
|
---|
3352 | In the makefile for a program, many of the rules you need to write often
|
---|
3353 | say only that some object file depends on some header
|
---|
3354 | file. For example, if @file{main.c} uses @file{defs.h} via an
|
---|
3355 | @code{#include}, you would write:
|
---|
3356 |
|
---|
3357 | @example
|
---|
3358 | main.o: defs.h
|
---|
3359 | @end example
|
---|
3360 |
|
---|
3361 | @noindent
|
---|
3362 | You need this rule so that @code{make} knows that it must remake
|
---|
3363 | @file{main.o} whenever @file{defs.h} changes. You can see that for a
|
---|
3364 | large program you would have to write dozens of such rules in your
|
---|
3365 | makefile. And, you must always be very careful to update the makefile
|
---|
3366 | every time you add or remove an @code{#include}.
|
---|
3367 | @cindex @code{#include}
|
---|
3368 |
|
---|
3369 | @cindex @code{-M} (to compiler)
|
---|
3370 | To avoid this hassle, most modern C compilers can write these rules for
|
---|
3371 | you, by looking at the @code{#include} lines in the source files.
|
---|
3372 | Usually this is done with the @samp{-M} option to the compiler.
|
---|
3373 | For example, the command:
|
---|
3374 |
|
---|
3375 | @example
|
---|
3376 | cc -M main.c
|
---|
3377 | @end example
|
---|
3378 |
|
---|
3379 | @noindent
|
---|
3380 | generates the output:
|
---|
3381 |
|
---|
3382 | @example
|
---|
3383 | main.o : main.c defs.h
|
---|
3384 | @end example
|
---|
3385 |
|
---|
3386 | @noindent
|
---|
3387 | Thus you no longer have to write all those rules yourself.
|
---|
3388 | The compiler will do it for you.
|
---|
3389 |
|
---|
3390 | Note that such a prerequisite constitutes mentioning @file{main.o} in a
|
---|
3391 | makefile, so it can never be considered an intermediate file by implicit
|
---|
3392 | rule search. This means that @code{make} won't ever remove the file
|
---|
3393 | after using it; @pxref{Chained Rules, ,Chains of Implicit Rules}.
|
---|
3394 |
|
---|
3395 | @cindex @code{make depend}
|
---|
3396 | With old @code{make} programs, it was traditional practice to use this
|
---|
3397 | compiler feature to generate prerequisites on demand with a command like
|
---|
3398 | @samp{make depend}. That command would create a file @file{depend}
|
---|
3399 | containing all the automatically-generated prerequisites; then the
|
---|
3400 | makefile could use @code{include} to read them in (@pxref{Include}).
|
---|
3401 |
|
---|
3402 | In GNU @code{make}, the feature of remaking makefiles makes this
|
---|
3403 | practice obsolete---you need never tell @code{make} explicitly to
|
---|
3404 | regenerate the prerequisites, because it always regenerates any makefile
|
---|
3405 | that is out of date. @xref{Remaking Makefiles}.
|
---|
3406 |
|
---|
3407 | The practice we recommend for automatic prerequisite generation is to have
|
---|
3408 | one makefile corresponding to each source file. For each source file
|
---|
3409 | @file{@var{name}.c} there is a makefile @file{@var{name}.d} which lists
|
---|
3410 | what files the object file @file{@var{name}.o} depends on. That way
|
---|
3411 | only the source files that have changed need to be rescanned to produce
|
---|
3412 | the new prerequisites.
|
---|
3413 |
|
---|
3414 | Here is the pattern rule to generate a file of prerequisites (i.e., a makefile)
|
---|
3415 | called @file{@var{name}.d} from a C source file called @file{@var{name}.c}:
|
---|
3416 |
|
---|
3417 | @smallexample
|
---|
3418 | @group
|
---|
3419 | %.d: %.c
|
---|
3420 | @@set -e; rm -f $@@; \
|
---|
3421 | $(CC) -M $(CPPFLAGS) $< > $@@.$$$$; \
|
---|
3422 | sed 's,\($*\)\.o[ :]*,\1.o $@@ : ,g' < $@@.$$$$ > $@@; \
|
---|
3423 | rm -f $@@.$$$$
|
---|
3424 | @end group
|
---|
3425 | @end smallexample
|
---|
3426 |
|
---|
3427 | @noindent
|
---|
3428 | @xref{Pattern Rules}, for information on defining pattern rules. The
|
---|
3429 | @samp{-e} flag to the shell causes it to exit immediately if the
|
---|
3430 | @code{$(CC)} command (or any other command) fails (exits with a
|
---|
3431 | nonzero status).
|
---|
3432 | @cindex @code{-e} (shell flag)
|
---|
3433 |
|
---|
3434 | @cindex @code{-MM} (to GNU compiler)
|
---|
3435 | With the GNU C compiler, you may wish to use the @samp{-MM} flag instead
|
---|
3436 | of @samp{-M}. This omits prerequisites on system header files.
|
---|
3437 | @xref{Preprocessor Options, , Options Controlling the Preprocessor,
|
---|
3438 | gcc.info, Using GNU CC}, for details.
|
---|
3439 |
|
---|
3440 | @cindex @code{sed} (shell command)
|
---|
3441 | The purpose of the @code{sed} command is to translate (for example):
|
---|
3442 |
|
---|
3443 | @example
|
---|
3444 | main.o : main.c defs.h
|
---|
3445 | @end example
|
---|
3446 |
|
---|
3447 | @noindent
|
---|
3448 | into:
|
---|
3449 |
|
---|
3450 | @example
|
---|
3451 | main.o main.d : main.c defs.h
|
---|
3452 | @end example
|
---|
3453 |
|
---|
3454 | @noindent
|
---|
3455 | @cindex @code{.d}
|
---|
3456 | This makes each @samp{.d} file depend on all the source and header files
|
---|
3457 | that the corresponding @samp{.o} file depends on. @code{make} then
|
---|
3458 | knows it must regenerate the prerequisites whenever any of the source or
|
---|
3459 | header files changes.
|
---|
3460 |
|
---|
3461 | Once you've defined the rule to remake the @samp{.d} files,
|
---|
3462 | you then use the @code{include} directive to read them all in.
|
---|
3463 | @xref{Include}. For example:
|
---|
3464 |
|
---|
3465 | @example
|
---|
3466 | @group
|
---|
3467 | sources = foo.c bar.c
|
---|
3468 |
|
---|
3469 | include $(sources:.c=.d)
|
---|
3470 | @end group
|
---|
3471 | @end example
|
---|
3472 |
|
---|
3473 | @noindent
|
---|
3474 | (This example uses a substitution variable reference to translate the
|
---|
3475 | list of source files @samp{foo.c bar.c} into a list of prerequisite
|
---|
3476 | makefiles, @samp{foo.d bar.d}. @xref{Substitution Refs}, for full
|
---|
3477 | information on substitution references.) Since the @samp{.d} files are
|
---|
3478 | makefiles like any others, @code{make} will remake them as necessary
|
---|
3479 | with no further work from you. @xref{Remaking Makefiles}.
|
---|
3480 |
|
---|
3481 | Note that the @samp{.d} files contain target definitions; you should
|
---|
3482 | be sure to place the @code{include} directive @emph{after} the first,
|
---|
3483 | default goal in your makefiles or run the risk of having a random
|
---|
3484 | object file become the default goal.
|
---|
3485 | @xref{How Make Works}.
|
---|
3486 |
|
---|
3487 | @node Commands, Using Variables, Rules, Top
|
---|
3488 | @chapter Writing the Commands in Rules
|
---|
3489 | @cindex commands, how to write
|
---|
3490 | @cindex rule commands
|
---|
3491 | @cindex writing rule commands
|
---|
3492 |
|
---|
3493 | The commands of a rule consist of one or more shell command lines to
|
---|
3494 | be executed, one at a time, in the order they appear. Typically, the
|
---|
3495 | result of executing these commands is that the target of the rule is
|
---|
3496 | brought up to date.
|
---|
3497 |
|
---|
3498 | Users use many different shell programs, but commands in makefiles are
|
---|
3499 | always interpreted by @file{/bin/sh} unless the makefile specifies
|
---|
3500 | otherwise. @xref{Execution, ,Command Execution}.
|
---|
3501 |
|
---|
3502 | @menu
|
---|
3503 | * Command Syntax:: Command syntax features and pitfalls.
|
---|
3504 | * Echoing:: How to control when commands are echoed.
|
---|
3505 | * Execution:: How commands are executed.
|
---|
3506 | * Parallel:: How commands can be executed in parallel.
|
---|
3507 | * Errors:: What happens after a command execution error.
|
---|
3508 | * Interrupts:: What happens when a command is interrupted.
|
---|
3509 | * Recursion:: Invoking @code{make} from makefiles.
|
---|
3510 | * Sequences:: Defining canned sequences of commands.
|
---|
3511 | * Empty Commands:: Defining useful, do-nothing commands.
|
---|
3512 | @end menu
|
---|
3513 |
|
---|
3514 | @node Command Syntax, Echoing, Commands, Commands
|
---|
3515 | @section Command Syntax
|
---|
3516 | @cindex command syntax
|
---|
3517 | @cindex syntax of commands
|
---|
3518 |
|
---|
3519 | Makefiles have the unusual property that there are really two distinct
|
---|
3520 | syntaxes in one file. Most of the makefile uses @code{make} syntax
|
---|
3521 | (@pxref{Makefiles, ,Writing Makefiles}). However, commands are meant to be
|
---|
3522 | interpreted by the shell and so they are written using shell syntax.
|
---|
3523 | The @code{make} program does not try to understand shell syntax: it
|
---|
3524 | performs only a very few specific translations on the content of the
|
---|
3525 | command before handing it to the shell.
|
---|
3526 |
|
---|
3527 | Each command line must start with a tab, except that the first command
|
---|
3528 | line may be attached to the target-and-prerequisites line with a
|
---|
3529 | semicolon in between. @emph{Any} line in the makefile that begins
|
---|
3530 | with a tab and appears in a ``rule context'' (that is, after a rule
|
---|
3531 | has been started until another rule or variable definition) will be
|
---|
3532 | considered a command line for that rule. Blank lines and lines of
|
---|
3533 | just comments may appear among the command lines; they are ignored.
|
---|
3534 |
|
---|
3535 | Some consequences of these rules include:
|
---|
3536 |
|
---|
3537 | @itemize @bullet
|
---|
3538 | @item
|
---|
3539 | A blank line that begins with a tab is not blank: it's an empty
|
---|
3540 | command (@pxref{Empty Commands}).
|
---|
3541 |
|
---|
3542 | @cindex comments, in commands
|
---|
3543 | @cindex commands, comments in
|
---|
3544 | @cindex @code{#} (comments), in commands
|
---|
3545 | @item
|
---|
3546 | A comment in a command line is not a @code{make} comment; it will be
|
---|
3547 | passed to the shell as-is. Whether the shell treats it as a comment
|
---|
3548 | or not depends on your shell.
|
---|
3549 |
|
---|
3550 | @item
|
---|
3551 | A variable definition in a ``rule context'' which is indented by a tab
|
---|
3552 | as the first character on the line, will be considered a command line,
|
---|
3553 | not a @code{make} variable definition, and passed to the shell.
|
---|
3554 |
|
---|
3555 | @item
|
---|
3556 | A conditional expression (@code{ifdef}, @code{ifeq},
|
---|
3557 | etc. @pxref{Conditional Syntax, ,Syntax of Conditionals}) in a ``rule
|
---|
3558 | context'' which is indented by a tab as the first character on the
|
---|
3559 | line, will be considered a command line and be passed to the shell.
|
---|
3560 |
|
---|
3561 | @end itemize
|
---|
3562 |
|
---|
3563 | @menu
|
---|
3564 | * Splitting Lines:: Breaking long command lines for readability.
|
---|
3565 | * Variables in Commands:: Using @code{make} variables in commands.
|
---|
3566 | @end menu
|
---|
3567 |
|
---|
3568 | @node Splitting Lines, Variables in Commands, Command Syntax, Command Syntax
|
---|
3569 | @subsection Splitting Command Lines
|
---|
3570 | @cindex commands, splitting
|
---|
3571 | @cindex splitting commands
|
---|
3572 | @cindex commands, backslash (@code{\}) in
|
---|
3573 | @cindex commands, quoting newlines in
|
---|
3574 | @cindex backslash (@code{\}), in commands
|
---|
3575 | @cindex @code{\} (backslash), in commands
|
---|
3576 | @cindex quoting newline, in commands
|
---|
3577 | @cindex newline, quoting, in commands
|
---|
3578 |
|
---|
3579 | One of the few ways in which @code{make} does interpret command lines
|
---|
3580 | is checking for a backslash just before the newline. As in normal
|
---|
3581 | makefile syntax, a single command can be split into multiple lines in
|
---|
3582 | the makefile by placing a backslash before each newline. A sequence
|
---|
3583 | of lines like this is considered a single command, and one instance of
|
---|
3584 | the shell will be invoked to run it.
|
---|
3585 |
|
---|
3586 | However, in contrast to how they are treated in other places in a
|
---|
3587 | makefile, backslash-newline pairs are @emph{not} removed from the
|
---|
3588 | command. Both the backslash and the newline characters are preserved
|
---|
3589 | and passed to the shell. How the backslash-newline is interpreted
|
---|
3590 | depends on your shell. If the first character of the next line
|
---|
3591 | after the backslash-newline is a tab, then that tab (and only that
|
---|
3592 | tab) is removed. Whitespace is never added to the command.
|
---|
3593 |
|
---|
3594 | For example, this makefile:
|
---|
3595 |
|
---|
3596 | @example
|
---|
3597 | @group
|
---|
3598 | all :
|
---|
3599 | @@echo no\
|
---|
3600 | space
|
---|
3601 | @@echo no\
|
---|
3602 | space
|
---|
3603 | @@echo one \
|
---|
3604 | space
|
---|
3605 | @@echo one\
|
---|
3606 | space
|
---|
3607 | @end group
|
---|
3608 | @end example
|
---|
3609 |
|
---|
3610 | @noindent
|
---|
3611 | consists of four separate shell commands where the output is:
|
---|
3612 |
|
---|
3613 | @example
|
---|
3614 | @group
|
---|
3615 | nospace
|
---|
3616 | nospace
|
---|
3617 | one space
|
---|
3618 | one space
|
---|
3619 | @end group
|
---|
3620 | @end example
|
---|
3621 |
|
---|
3622 | As a more complex example, this makefile:
|
---|
3623 |
|
---|
3624 | @example
|
---|
3625 | @group
|
---|
3626 | all : ; @@echo 'hello \
|
---|
3627 | world' ; echo "hello \
|
---|
3628 | world"
|
---|
3629 | @end group
|
---|
3630 | @end example
|
---|
3631 |
|
---|
3632 | @noindent
|
---|
3633 | will run one shell with a command script of:
|
---|
3634 |
|
---|
3635 | @example
|
---|
3636 | @group
|
---|
3637 | echo 'hello \
|
---|
3638 | world' ; echo "hello \
|
---|
3639 | world"
|
---|
3640 | @end group
|
---|
3641 | @end example
|
---|
3642 |
|
---|
3643 | @noindent
|
---|
3644 | which, according to shell quoting rules, will yield the following output:
|
---|
3645 |
|
---|
3646 | @example
|
---|
3647 | @group
|
---|
3648 | hello \
|
---|
3649 | world
|
---|
3650 | hello world
|
---|
3651 | @end group
|
---|
3652 | @end example
|
---|
3653 |
|
---|
3654 | @noindent
|
---|
3655 | Notice how the backslash/newline pair was removed inside the string quoted
|
---|
3656 | with double quotes (@code{"..."}), but not from the string quoted with single
|
---|
3657 | quotes (@code{'...'}). This is the way the default shell (@file{/bin/sh})
|
---|
3658 | handles backslash/newline pairs. If you specify a different shell in your
|
---|
3659 | makefiles it may treat them differently.
|
---|
3660 |
|
---|
3661 | Sometimes you want to split a long line inside of single quotes, but
|
---|
3662 | you don't want the backslash-newline to appear in the quoted content.
|
---|
3663 | This is often the case when passing scripts to languages such as Perl,
|
---|
3664 | where extraneous backslashes inside the script can change its meaning
|
---|
3665 | or even be a syntax error. One simple way of handling this is to
|
---|
3666 | place the quoted string, or even the entire command, into a
|
---|
3667 | @code{make} variable then use the variable in the command. In this
|
---|
3668 | situation the newline quoting rules for makefiles will be used, and
|
---|
3669 | the backslash-newline will be removed. If we rewrite our example
|
---|
3670 | above using this method:
|
---|
3671 |
|
---|
3672 | @example
|
---|
3673 | @group
|
---|
3674 | HELLO = 'hello \
|
---|
3675 | world'
|
---|
3676 |
|
---|
3677 | all : ; @@echo $(HELLO)
|
---|
3678 | @end group
|
---|
3679 | @end example
|
---|
3680 |
|
---|
3681 | @noindent
|
---|
3682 | we will get output like this:
|
---|
3683 |
|
---|
3684 | @example
|
---|
3685 | @group
|
---|
3686 | hello world
|
---|
3687 | @end group
|
---|
3688 | @end example
|
---|
3689 |
|
---|
3690 | If you like, you can also use target-specific variables
|
---|
3691 | (@pxref{Target-specific, ,Target-specific Variable Values}) to obtain
|
---|
3692 | a tighter correspondence between the variable and the command that
|
---|
3693 | uses it.
|
---|
3694 |
|
---|
3695 | @node Variables in Commands, , Splitting Lines, Command Syntax
|
---|
3696 | @subsection Using Variables in Commands
|
---|
3697 | @cindex variable references in commands
|
---|
3698 | @cindex commands, using variables in
|
---|
3699 |
|
---|
3700 | The other way in which @code{make} processes commands is by expanding
|
---|
3701 | any variable references in them (@pxref{Reference,Basics of Variable
|
---|
3702 | References}). This occurs after make has finished reading all the
|
---|
3703 | makefiles and the target is determined to be out of date; so, the
|
---|
3704 | commands for targets which are not rebuilt are never expanded.
|
---|
3705 |
|
---|
3706 | Variable and function references in commands have identical syntax and
|
---|
3707 | semantics to references elsewhere in the makefile. They also have the
|
---|
3708 | same quoting rules: if you want a dollar sign to appear in your
|
---|
3709 | command, you must double it (@samp{$$}). For shells like the default
|
---|
3710 | shell, that use dollar signs to introduce variables, it's important to
|
---|
3711 | keep clear in your mind whether the variable you want to reference is
|
---|
3712 | a @code{make} variable (use a single dollar sign) or a shell variable
|
---|
3713 | (use two dollar signs). For example:
|
---|
3714 |
|
---|
3715 | @example
|
---|
3716 | @group
|
---|
3717 | LIST = one two three
|
---|
3718 | all:
|
---|
3719 | for i in $(LIST); do \
|
---|
3720 | echo $$i; \
|
---|
3721 | done
|
---|
3722 | @end group
|
---|
3723 | @end example
|
---|
3724 |
|
---|
3725 | @noindent
|
---|
3726 | results in the following command being passed to the shell:
|
---|
3727 |
|
---|
3728 | @example
|
---|
3729 | @group
|
---|
3730 | for i in one two three; do \
|
---|
3731 | echo $i; \
|
---|
3732 | done
|
---|
3733 | @end group
|
---|
3734 | @end example
|
---|
3735 |
|
---|
3736 | @noindent
|
---|
3737 | which generates the expected result:
|
---|
3738 |
|
---|
3739 | @example
|
---|
3740 | @group
|
---|
3741 | one
|
---|
3742 | two
|
---|
3743 | three
|
---|
3744 | @end group
|
---|
3745 | @end example
|
---|
3746 |
|
---|
3747 | @node Echoing, Execution, Command Syntax, Commands
|
---|
3748 | @section Command Echoing
|
---|
3749 | @cindex echoing of commands
|
---|
3750 | @cindex silent operation
|
---|
3751 | @cindex @code{@@} (in commands)
|
---|
3752 | @cindex commands, echoing
|
---|
3753 | @cindex printing of commands
|
---|
3754 |
|
---|
3755 | Normally @code{make} prints each command line before it is executed.
|
---|
3756 | We call this @dfn{echoing} because it gives the appearance that you
|
---|
3757 | are typing the commands yourself.
|
---|
3758 |
|
---|
3759 | When a line starts with @samp{@@}, the echoing of that line is suppressed.
|
---|
3760 | The @samp{@@} is discarded before the command is passed to the shell.
|
---|
3761 | Typically you would use this for a command whose only effect is to print
|
---|
3762 | something, such as an @code{echo} command to indicate progress through
|
---|
3763 | the makefile:
|
---|
3764 |
|
---|
3765 | @example
|
---|
3766 | @@echo About to make distribution files
|
---|
3767 | @end example
|
---|
3768 |
|
---|
3769 | @cindex @code{-n}
|
---|
3770 | @cindex @code{--just-print}
|
---|
3771 | @cindex @code{--dry-run}
|
---|
3772 | @cindex @code{--recon}
|
---|
3773 | When @code{make} is given the flag @samp{-n} or @samp{--just-print}
|
---|
3774 | it only echoes commands, it won't execute them. @xref{Options Summary,
|
---|
3775 | ,Summary of Options}. In this case and only this case, even the
|
---|
3776 | commands starting with @samp{@@} are printed. This flag is useful for
|
---|
3777 | finding out which commands @code{make} thinks are necessary without
|
---|
3778 | actually doing them.
|
---|
3779 |
|
---|
3780 | @cindex @code{-s}
|
---|
3781 | @cindex @code{--silent}
|
---|
3782 | @cindex @code{--quiet}
|
---|
3783 | @findex .SILENT
|
---|
3784 | The @samp{-s} or @samp{--silent}
|
---|
3785 | flag to @code{make} prevents all echoing, as if all commands
|
---|
3786 | started with @samp{@@}. A rule in the makefile for the special target
|
---|
3787 | @code{.SILENT} without prerequisites has the same effect
|
---|
3788 | (@pxref{Special Targets, ,Special Built-in Target Names}).
|
---|
3789 | @code{.SILENT} is essentially obsolete since @samp{@@} is more flexible.@refill
|
---|
3790 |
|
---|
3791 | @node Execution, Parallel, Echoing, Commands
|
---|
3792 | @section Command Execution
|
---|
3793 | @cindex commands, execution
|
---|
3794 | @cindex execution, of commands
|
---|
3795 | @cindex shell command, execution
|
---|
3796 | @vindex @code{SHELL} @r{(command execution)}
|
---|
3797 |
|
---|
3798 | When it is time to execute commands to update a target, they are
|
---|
3799 | executed by invoking a new subshell for each command line. (In
|
---|
3800 | practice, @code{make} may take shortcuts that do not affect the
|
---|
3801 | results.)
|
---|
3802 |
|
---|
3803 | @cindex @code{cd} (shell command)
|
---|
3804 | @cindex shell variables, setting in commands
|
---|
3805 | @cindex commands setting shell variables
|
---|
3806 | @strong{Please note:} this implies that setting shell variables and
|
---|
3807 | invoking shell commands such as @code{cd} that set a context local to
|
---|
3808 | each process will not affect the following command lines.@footnote{On
|
---|
3809 | MS-DOS, the value of current working directory is @strong{global}, so
|
---|
3810 | changing it @emph{will} affect the following command lines on those
|
---|
3811 | systems.} If you want to use @code{cd} to affect the next statement,
|
---|
3812 | put both statements in a single command line. Then @code{make} will
|
---|
3813 | invoke one shell to run the entire line, and the shell will execute
|
---|
3814 | the statements in sequence. For example:
|
---|
3815 |
|
---|
3816 | @example
|
---|
3817 | foo : bar/lose
|
---|
3818 | cd $(@@D) && gobble $(@@F) > ../$@@
|
---|
3819 | @end example
|
---|
3820 |
|
---|
3821 | @noindent
|
---|
3822 | Here we use the shell AND operator (@code{&&}) so that if the
|
---|
3823 | @code{cd} command fails, the script will fail without trying to invoke
|
---|
3824 | the @code{gobble} command in the wrong directory, which could cause
|
---|
3825 | problems (in this case it would certainly cause @file{../foo} to be
|
---|
3826 | truncated, at least).
|
---|
3827 |
|
---|
3828 | @menu
|
---|
3829 | * Choosing the Shell:: How @code{make} chooses the shell used
|
---|
3830 | to run commands.
|
---|
3831 | @end menu
|
---|
3832 |
|
---|
3833 | @node Choosing the Shell, , Execution, Execution
|
---|
3834 | @subsection Choosing the Shell
|
---|
3835 | @cindex shell, choosing the
|
---|
3836 | @cindex @code{SHELL}, value of
|
---|
3837 |
|
---|
3838 | @vindex SHELL
|
---|
3839 | The program used as the shell is taken from the variable @code{SHELL}.
|
---|
3840 | If this variable is not set in your makefile, the program
|
---|
3841 | @file{/bin/sh} is used as the shell.
|
---|
3842 |
|
---|
3843 | @cindex environment, @code{SHELL} in
|
---|
3844 | Unlike most variables, the variable @code{SHELL} is never set from the
|
---|
3845 | environment. This is because the @code{SHELL} environment variable is
|
---|
3846 | used to specify your personal choice of shell program for interactive
|
---|
3847 | use. It would be very bad for personal choices like this to affect the
|
---|
3848 | functioning of makefiles. @xref{Environment, ,Variables from the
|
---|
3849 | Environment}.
|
---|
3850 |
|
---|
3851 | Furthermore, when you do set @code{SHELL} in your makefile that value
|
---|
3852 | is @emph{not} exported in the environment to commands that @code{make}
|
---|
3853 | invokes. Instead, the value inherited from the user's environment, if
|
---|
3854 | any, is exported. You can override this behavior by explicitly
|
---|
3855 | exporting @code{SHELL} (@pxref{Variables/Recursion, ,Communicating
|
---|
3856 | Variables to a Sub-@code{make}}), forcing it to be passed in the
|
---|
3857 | environment to commands.
|
---|
3858 |
|
---|
3859 | @vindex @code{MAKESHELL} @r{(MS-DOS alternative to @code{SHELL})}
|
---|
3860 | However, on MS-DOS and MS-Windows the value of @code{SHELL} in the
|
---|
3861 | environment @strong{is} used, since on those systems most users do not
|
---|
3862 | set this variable, and therefore it is most likely set specifically to
|
---|
3863 | be used by @code{make}. On MS-DOS, if the setting of @code{SHELL} is
|
---|
3864 | not suitable for @code{make}, you can set the variable
|
---|
3865 | @code{MAKESHELL} to the shell that @code{make} should use; if set it
|
---|
3866 | will be used as the shell instead of the value of @code{SHELL}.
|
---|
3867 |
|
---|
3868 | @subsubheading Choosing a Shell in DOS and Windows
|
---|
3869 | @cindex shell, in DOS and Windows
|
---|
3870 | @cindex DOS, choosing a shell in
|
---|
3871 | @cindex Windows, choosing a shell in
|
---|
3872 |
|
---|
3873 | Choosing a shell in MS-DOS and MS-Windows is much more complex than on
|
---|
3874 | other systems.
|
---|
3875 |
|
---|
3876 | @vindex COMSPEC
|
---|
3877 | On MS-DOS, if @code{SHELL} is not set, the value of the variable
|
---|
3878 | @code{COMSPEC} (which is always set) is used instead.
|
---|
3879 |
|
---|
3880 | @cindex @code{SHELL}, MS-DOS specifics
|
---|
3881 | The processing of lines that set the variable @code{SHELL} in Makefiles
|
---|
3882 | is different on MS-DOS. The stock shell, @file{command.com}, is
|
---|
3883 | ridiculously limited in its functionality and many users of @code{make}
|
---|
3884 | tend to install a replacement shell. Therefore, on MS-DOS, @code{make}
|
---|
3885 | examines the value of @code{SHELL}, and changes its behavior based on
|
---|
3886 | whether it points to a Unix-style or DOS-style shell. This allows
|
---|
3887 | reasonable functionality even if @code{SHELL} points to
|
---|
3888 | @file{command.com}.
|
---|
3889 |
|
---|
3890 | If @code{SHELL} points to a Unix-style shell, @code{make} on MS-DOS
|
---|
3891 | additionally checks whether that shell can indeed be found; if not, it
|
---|
3892 | ignores the line that sets @code{SHELL}. In MS-DOS, GNU @code{make}
|
---|
3893 | searches for the shell in the following places:
|
---|
3894 |
|
---|
3895 | @enumerate
|
---|
3896 | @item
|
---|
3897 | In the precise place pointed to by the value of @code{SHELL}. For
|
---|
3898 | example, if the makefile specifies @samp{SHELL = /bin/sh}, @code{make}
|
---|
3899 | will look in the directory @file{/bin} on the current drive.
|
---|
3900 |
|
---|
3901 | @item
|
---|
3902 | In the current directory.
|
---|
3903 |
|
---|
3904 | @item
|
---|
3905 | In each of the directories in the @code{PATH} variable, in order.
|
---|
3906 |
|
---|
3907 | @end enumerate
|
---|
3908 |
|
---|
3909 | In every directory it examines, @code{make} will first look for the
|
---|
3910 | specific file (@file{sh} in the example above). If this is not found,
|
---|
3911 | it will also look in that directory for that file with one of the known
|
---|
3912 | extensions which identify executable files. For example @file{.exe},
|
---|
3913 | @file{.com}, @file{.bat}, @file{.btm}, @file{.sh}, and some others.
|
---|
3914 |
|
---|
3915 | If any of these attempts is successful, the value of @code{SHELL} will
|
---|
3916 | be set to the full pathname of the shell as found. However, if none of
|
---|
3917 | these is found, the value of @code{SHELL} will not be changed, and thus
|
---|
3918 | the line that sets it will be effectively ignored. This is so
|
---|
3919 | @code{make} will only support features specific to a Unix-style shell if
|
---|
3920 | such a shell is actually installed on the system where @code{make} runs.
|
---|
3921 |
|
---|
3922 | Note that this extended search for the shell is limited to the cases
|
---|
3923 | where @code{SHELL} is set from the Makefile; if it is set in the
|
---|
3924 | environment or command line, you are expected to set it to the full
|
---|
3925 | pathname of the shell, exactly as things are on Unix.
|
---|
3926 |
|
---|
3927 | The effect of the above DOS-specific processing is that a Makefile that
|
---|
3928 | contains @samp{SHELL = /bin/sh} (as many Unix makefiles do), will work
|
---|
3929 | on MS-DOS unaltered if you have e.g.@: @file{sh.exe} installed in some
|
---|
3930 | directory along your @code{PATH}.
|
---|
3931 |
|
---|
3932 | @node Parallel, Errors, Execution, Commands
|
---|
3933 | @section Parallel Execution
|
---|
3934 | @cindex commands, execution in parallel
|
---|
3935 | @cindex parallel execution
|
---|
3936 | @cindex execution, in parallel
|
---|
3937 | @cindex job slots
|
---|
3938 | @cindex @code{-j}
|
---|
3939 | @cindex @code{--jobs}
|
---|
3940 |
|
---|
3941 | GNU @code{make} knows how to execute several commands at once.
|
---|
3942 | Normally, @code{make} will execute only one command at a time, waiting
|
---|
3943 | for it to finish before executing the next. However, the @samp{-j} or
|
---|
3944 | @samp{--jobs} option tells @code{make} to execute many commands
|
---|
3945 | simultaneously.@refill
|
---|
3946 |
|
---|
3947 | On MS-DOS, the @samp{-j} option has no effect, since that system doesn't
|
---|
3948 | support multi-processing.
|
---|
3949 |
|
---|
3950 | If the @samp{-j} option is followed by an integer, this is the number of
|
---|
3951 | commands to execute at once; this is called the number of @dfn{job slots}.
|
---|
3952 | If there is nothing looking like an integer after the @samp{-j} option,
|
---|
3953 | there is no limit on the number of job slots. The default number of job
|
---|
3954 | slots is one, which means serial execution (one thing at a time).
|
---|
3955 |
|
---|
3956 | One unpleasant consequence of running several commands simultaneously is
|
---|
3957 | that output generated by the commands appears whenever each command
|
---|
3958 | sends it, so messages from different commands may be interspersed.
|
---|
3959 |
|
---|
3960 | Another problem is that two processes cannot both take input from the
|
---|
3961 | same device; so to make sure that only one command tries to take input
|
---|
3962 | from the terminal at once, @code{make} will invalidate the standard
|
---|
3963 | input streams of all but one running command. This means that
|
---|
3964 | attempting to read from standard input will usually be a fatal error (a
|
---|
3965 | @samp{Broken pipe} signal) for most child processes if there are
|
---|
3966 | several.
|
---|
3967 | @cindex broken pipe
|
---|
3968 | @cindex standard input
|
---|
3969 |
|
---|
3970 | It is unpredictable which command will have a valid standard input stream
|
---|
3971 | (which will come from the terminal, or wherever you redirect the standard
|
---|
3972 | input of @code{make}). The first command run will always get it first, and
|
---|
3973 | the first command started after that one finishes will get it next, and so
|
---|
3974 | on.
|
---|
3975 |
|
---|
3976 | We will change how this aspect of @code{make} works if we find a better
|
---|
3977 | alternative. In the mean time, you should not rely on any command using
|
---|
3978 | standard input at all if you are using the parallel execution feature; but
|
---|
3979 | if you are not using this feature, then standard input works normally in
|
---|
3980 | all commands.
|
---|
3981 |
|
---|
3982 | Finally, handling recursive @code{make} invocations raises issues. For
|
---|
3983 | more information on this, see
|
---|
3984 | @ref{Options/Recursion, ,Communicating Options to a Sub-@code{make}}.
|
---|
3985 |
|
---|
3986 | If a command fails (is killed by a signal or exits with a nonzero
|
---|
3987 | status), and errors are not ignored for that command
|
---|
3988 | (@pxref{Errors, ,Errors in Commands}),
|
---|
3989 | the remaining command lines to remake the same target will not be run.
|
---|
3990 | If a command fails and the @samp{-k} or @samp{--keep-going}
|
---|
3991 | option was not given
|
---|
3992 | (@pxref{Options Summary, ,Summary of Options}),
|
---|
3993 | @code{make} aborts execution. If make
|
---|
3994 | terminates for any reason (including a signal) with child processes
|
---|
3995 | running, it waits for them to finish before actually exiting.@refill
|
---|
3996 |
|
---|
3997 | @cindex load average
|
---|
3998 | @cindex limiting jobs based on load
|
---|
3999 | @cindex jobs, limiting based on load
|
---|
4000 | @cindex @code{-l} (load average)
|
---|
4001 | @cindex @code{--max-load}
|
---|
4002 | @cindex @code{--load-average}
|
---|
4003 | When the system is heavily loaded, you will probably want to run fewer jobs
|
---|
4004 | than when it is lightly loaded. You can use the @samp{-l} option to tell
|
---|
4005 | @code{make} to limit the number of jobs to run at once, based on the load
|
---|
4006 | average. The @samp{-l} or @samp{--max-load}
|
---|
4007 | option is followed by a floating-point number. For
|
---|
4008 | example,
|
---|
4009 |
|
---|
4010 | @example
|
---|
4011 | -l 2.5
|
---|
4012 | @end example
|
---|
4013 |
|
---|
4014 | @noindent
|
---|
4015 | will not let @code{make} start more than one job if the load average is
|
---|
4016 | above 2.5. The @samp{-l} option with no following number removes the
|
---|
4017 | load limit, if one was given with a previous @samp{-l} option.@refill
|
---|
4018 |
|
---|
4019 | More precisely, when @code{make} goes to start up a job, and it already has
|
---|
4020 | at least one job running, it checks the current load average; if it is not
|
---|
4021 | lower than the limit given with @samp{-l}, @code{make} waits until the load
|
---|
4022 | average goes below that limit, or until all the other jobs finish.
|
---|
4023 |
|
---|
4024 | By default, there is no load limit.
|
---|
4025 |
|
---|
4026 | @node Errors, Interrupts, Parallel, Commands
|
---|
4027 | @section Errors in Commands
|
---|
4028 | @cindex errors (in commands)
|
---|
4029 | @cindex commands, errors in
|
---|
4030 | @cindex exit status (errors)
|
---|
4031 |
|
---|
4032 | After each shell command returns, @code{make} looks at its exit status.
|
---|
4033 | If the command completed successfully, the next command line is executed
|
---|
4034 | in a new shell; after the last command line is finished, the rule is
|
---|
4035 | finished.
|
---|
4036 |
|
---|
4037 | If there is an error (the exit status is nonzero), @code{make} gives up on
|
---|
4038 | the current rule, and perhaps on all rules.
|
---|
4039 |
|
---|
4040 | Sometimes the failure of a certain command does not indicate a problem.
|
---|
4041 | For example, you may use the @code{mkdir} command to ensure that a
|
---|
4042 | directory exists. If the directory already exists, @code{mkdir} will
|
---|
4043 | report an error, but you probably want @code{make} to continue regardless.
|
---|
4044 |
|
---|
4045 | @cindex @code{-} (in commands)
|
---|
4046 | To ignore errors in a command line, write a @samp{-} at the beginning of
|
---|
4047 | the line's text (after the initial tab). The @samp{-} is discarded before
|
---|
4048 | the command is passed to the shell for execution.
|
---|
4049 |
|
---|
4050 | For example,
|
---|
4051 |
|
---|
4052 | @example
|
---|
4053 | @group
|
---|
4054 | clean:
|
---|
4055 | -rm -f *.o
|
---|
4056 | @end group
|
---|
4057 | @end example
|
---|
4058 | @cindex @code{rm} (shell command)
|
---|
4059 |
|
---|
4060 | @noindent
|
---|
4061 | This causes @code{rm} to continue even if it is unable to remove a file.
|
---|
4062 |
|
---|
4063 | @cindex @code{-i}
|
---|
4064 | @cindex @code{--ignore-errors}
|
---|
4065 | @findex .IGNORE
|
---|
4066 | When you run @code{make} with the @samp{-i} or @samp{--ignore-errors}
|
---|
4067 | flag, errors are ignored in all commands of all rules. A rule in the
|
---|
4068 | makefile for the special target @code{.IGNORE} has the same effect, if
|
---|
4069 | there are no prerequisites. These ways of ignoring errors are obsolete
|
---|
4070 | because @samp{-} is more flexible.
|
---|
4071 |
|
---|
4072 | When errors are to be ignored, because of either a @samp{-} or the
|
---|
4073 | @samp{-i} flag, @code{make} treats an error return just like success,
|
---|
4074 | except that it prints out a message that tells you the status code
|
---|
4075 | the command exited with, and says that the error has been ignored.
|
---|
4076 |
|
---|
4077 | When an error happens that @code{make} has not been told to ignore,
|
---|
4078 | it implies that the current target cannot be correctly remade, and neither
|
---|
4079 | can any other that depends on it either directly or indirectly. No further
|
---|
4080 | commands will be executed for these targets, since their preconditions
|
---|
4081 | have not been achieved.
|
---|
4082 |
|
---|
4083 |
|
---|
4084 | @cindex @code{-k}
|
---|
4085 | @cindex @code{--keep-going}
|
---|
4086 | Normally @code{make} gives up immediately in this circumstance, returning a
|
---|
4087 | nonzero status. However, if the @samp{-k} or @samp{--keep-going}
|
---|
4088 | flag is specified, @code{make}
|
---|
4089 | continues to consider the other prerequisites of the pending targets,
|
---|
4090 | remaking them if necessary, before it gives up and returns nonzero status.
|
---|
4091 | For example, after an error in compiling one object file, @samp{make -k}
|
---|
4092 | will continue compiling other object files even though it already knows
|
---|
4093 | that linking them will be impossible. @xref{Options Summary, ,Summary of Options}.
|
---|
4094 |
|
---|
4095 | The usual behavior assumes that your purpose is to get the specified
|
---|
4096 | targets up to date; once @code{make} learns that this is impossible, it
|
---|
4097 | might as well report the failure immediately. The @samp{-k} option says
|
---|
4098 | that the real purpose is to test as many of the changes made in the
|
---|
4099 | program as possible, perhaps to find several independent problems so
|
---|
4100 | that you can correct them all before the next attempt to compile. This
|
---|
4101 | is why Emacs' @code{compile} command passes the @samp{-k} flag by
|
---|
4102 | default.
|
---|
4103 | @cindex Emacs (@code{M-x compile})
|
---|
4104 |
|
---|
4105 | @findex .DELETE_ON_ERROR
|
---|
4106 | @cindex deletion of target files
|
---|
4107 | @cindex removal of target files
|
---|
4108 | @cindex target, deleting on error
|
---|
4109 | Usually when a command fails, if it has changed the target file at all,
|
---|
4110 | the file is corrupted and cannot be used---or at least it is not
|
---|
4111 | completely updated. Yet the file's time stamp says that it is now up to
|
---|
4112 | date, so the next time @code{make} runs, it will not try to update that
|
---|
4113 | file. The situation is just the same as when the command is killed by a
|
---|
4114 | signal; @pxref{Interrupts}. So generally the right thing to do is to
|
---|
4115 | delete the target file if the command fails after beginning to change
|
---|
4116 | the file. @code{make} will do this if @code{.DELETE_ON_ERROR} appears
|
---|
4117 | as a target. This is almost always what you want @code{make} to do, but
|
---|
4118 | it is not historical practice; so for compatibility, you must explicitly
|
---|
4119 | request it.
|
---|
4120 |
|
---|
4121 | @node Interrupts, Recursion, Errors, Commands
|
---|
4122 | @section Interrupting or Killing @code{make}
|
---|
4123 | @cindex interrupt
|
---|
4124 | @cindex signal
|
---|
4125 | @cindex deletion of target files
|
---|
4126 | @cindex removal of target files
|
---|
4127 | @cindex target, deleting on interrupt
|
---|
4128 | @cindex killing (interruption)
|
---|
4129 |
|
---|
4130 | If @code{make} gets a fatal signal while a command is executing, it may
|
---|
4131 | delete the target file that the command was supposed to update. This is
|
---|
4132 | done if the target file's last-modification time has changed since
|
---|
4133 | @code{make} first checked it.
|
---|
4134 |
|
---|
4135 | The purpose of deleting the target is to make sure that it is remade from
|
---|
4136 | scratch when @code{make} is next run. Why is this? Suppose you type
|
---|
4137 | @kbd{Ctrl-c} while a compiler is running, and it has begun to write an
|
---|
4138 | object file @file{foo.o}. The @kbd{Ctrl-c} kills the compiler, resulting
|
---|
4139 | in an incomplete file whose last-modification time is newer than the source
|
---|
4140 | file @file{foo.c}. But @code{make} also receives the @kbd{Ctrl-c} signal
|
---|
4141 | and deletes this incomplete file. If @code{make} did not do this, the next
|
---|
4142 | invocation of @code{make} would think that @file{foo.o} did not require
|
---|
4143 | updating---resulting in a strange error message from the linker when it
|
---|
4144 | tries to link an object file half of which is missing.
|
---|
4145 |
|
---|
4146 | @findex .PRECIOUS
|
---|
4147 | You can prevent the deletion of a target file in this way by making the
|
---|
4148 | special target @code{.PRECIOUS} depend on it. Before remaking a target,
|
---|
4149 | @code{make} checks to see whether it appears on the prerequisites of
|
---|
4150 | @code{.PRECIOUS}, and thereby decides whether the target should be deleted
|
---|
4151 | if a signal happens. Some reasons why you might do this are that the
|
---|
4152 | target is updated in some atomic fashion, or exists only to record a
|
---|
4153 | modification-time (its contents do not matter), or must exist at all
|
---|
4154 | times to prevent other sorts of trouble.
|
---|
4155 |
|
---|
4156 | @node Recursion, Sequences, Interrupts, Commands
|
---|
4157 | @section Recursive Use of @code{make}
|
---|
4158 | @cindex recursion
|
---|
4159 | @cindex subdirectories, recursion for
|
---|
4160 |
|
---|
4161 | Recursive use of @code{make} means using @code{make} as a command in a
|
---|
4162 | makefile. This technique is useful when you want separate makefiles for
|
---|
4163 | various subsystems that compose a larger system. For example, suppose you
|
---|
4164 | have a subdirectory @file{subdir} which has its own makefile, and you would
|
---|
4165 | like the containing directory's makefile to run @code{make} on the
|
---|
4166 | subdirectory. You can do it by writing this:
|
---|
4167 |
|
---|
4168 | @example
|
---|
4169 | subsystem:
|
---|
4170 | cd subdir && $(MAKE)
|
---|
4171 | @end example
|
---|
4172 |
|
---|
4173 | @noindent
|
---|
4174 | or, equivalently, this (@pxref{Options Summary, ,Summary of Options}):
|
---|
4175 |
|
---|
4176 | @example
|
---|
4177 | subsystem:
|
---|
4178 | $(MAKE) -C subdir
|
---|
4179 | @end example
|
---|
4180 | @cindex @code{-C}
|
---|
4181 | @cindex @code{--directory}
|
---|
4182 |
|
---|
4183 | You can write recursive @code{make} commands just by copying this example,
|
---|
4184 | but there are many things to know about how they work and why, and about
|
---|
4185 | how the sub-@code{make} relates to the top-level @code{make}. You may
|
---|
4186 | also find it useful to declare targets that invoke recursive
|
---|
4187 | @code{make} commands as @samp{.PHONY} (for more discussion on when
|
---|
4188 | this is useful, see @ref{Phony Targets}).
|
---|
4189 |
|
---|
4190 | @vindex @code{CURDIR}
|
---|
4191 | For your convenience, when GNU @code{make} starts (after it has
|
---|
4192 | processed any @code{-C} options) it sets the variable @code{CURDIR} to
|
---|
4193 | the pathname of the current working directory. This value is never
|
---|
4194 | touched by @code{make} again: in particular note that if you include
|
---|
4195 | files from other directories the value of @code{CURDIR} does not
|
---|
4196 | change. The value has the same precedence it would have if it were
|
---|
4197 | set in the makefile (by default, an environment variable @code{CURDIR}
|
---|
4198 | will not override this value). Note that setting this variable has no
|
---|
4199 | impact on the operation of @code{make} (it does not cause @code{make}
|
---|
4200 | to change its working directory, for example).
|
---|
4201 |
|
---|
4202 | @menu
|
---|
4203 | * MAKE Variable:: The special effects of using @samp{$(MAKE)}.
|
---|
4204 | * Variables/Recursion:: How to communicate variables to a sub-@code{make}.
|
---|
4205 | * Options/Recursion:: How to communicate options to a sub-@code{make}.
|
---|
4206 | * -w Option:: How the @samp{-w} or @samp{--print-directory} option
|
---|
4207 | helps debug use of recursive @code{make} commands.
|
---|
4208 | @end menu
|
---|
4209 |
|
---|
4210 | @node MAKE Variable, Variables/Recursion, Recursion, Recursion
|
---|
4211 | @subsection How the @code{MAKE} Variable Works
|
---|
4212 | @vindex MAKE
|
---|
4213 | @cindex recursion, and @code{MAKE} variable
|
---|
4214 |
|
---|
4215 | Recursive @code{make} commands should always use the variable @code{MAKE},
|
---|
4216 | not the explicit command name @samp{make}, as shown here:
|
---|
4217 |
|
---|
4218 | @example
|
---|
4219 | @group
|
---|
4220 | subsystem:
|
---|
4221 | cd subdir && $(MAKE)
|
---|
4222 | @end group
|
---|
4223 | @end example
|
---|
4224 |
|
---|
4225 | The value of this variable is the file name with which @code{make} was
|
---|
4226 | invoked. If this file name was @file{/bin/make}, then the command executed
|
---|
4227 | is @samp{cd subdir && /bin/make}. If you use a special version of
|
---|
4228 | @code{make} to run the top-level makefile, the same special version will be
|
---|
4229 | executed for recursive invocations.
|
---|
4230 | @cindex @code{cd} (shell command)
|
---|
4231 |
|
---|
4232 | @cindex +, and commands
|
---|
4233 | As a special feature, using the variable @code{MAKE} in the commands of
|
---|
4234 | a rule alters the effects of the @samp{-t} (@samp{--touch}), @samp{-n}
|
---|
4235 | (@samp{--just-print}), or @samp{-q} (@w{@samp{--question}}) option.
|
---|
4236 | Using the @code{MAKE} variable has the same effect as using a @samp{+}
|
---|
4237 | character at the beginning of the command line. @xref{Instead of
|
---|
4238 | Execution, ,Instead of Executing the Commands}. This special feature
|
---|
4239 | is only enabled if the @code{MAKE} variable appears directly in the
|
---|
4240 | command script: it does not apply if the @code{MAKE} variable is
|
---|
4241 | referenced through expansion of another variable. In the latter case
|
---|
4242 | you must use the @samp{+} token to get these special effects.@refill
|
---|
4243 |
|
---|
4244 | Consider the command @samp{make -t} in the above example. (The
|
---|
4245 | @samp{-t} option marks targets as up to date without actually running
|
---|
4246 | any commands; see @ref{Instead of Execution}.) Following the usual
|
---|
4247 | definition of @samp{-t}, a @samp{make -t} command in the example would
|
---|
4248 | create a file named @file{subsystem} and do nothing else. What you
|
---|
4249 | really want it to do is run @samp{@w{cd subdir &&} @w{make -t}}; but that would
|
---|
4250 | require executing the command, and @samp{-t} says not to execute
|
---|
4251 | commands.@refill
|
---|
4252 | @cindex @code{-t}, and recursion
|
---|
4253 | @cindex recursion, and @code{-t}
|
---|
4254 | @cindex @code{--touch}, and recursion
|
---|
4255 |
|
---|
4256 | The special feature makes this do what you want: whenever a command
|
---|
4257 | line of a rule contains the variable @code{MAKE}, the flags @samp{-t},
|
---|
4258 | @samp{-n} and @samp{-q} do not apply to that line. Command lines
|
---|
4259 | containing @code{MAKE} are executed normally despite the presence of a
|
---|
4260 | flag that causes most commands not to be run. The usual
|
---|
4261 | @code{MAKEFLAGS} mechanism passes the flags to the sub-@code{make}
|
---|
4262 | (@pxref{Options/Recursion, ,Communicating Options to a
|
---|
4263 | Sub-@code{make}}), so your request to touch the files, or print the
|
---|
4264 | commands, is propagated to the subsystem.@refill
|
---|
4265 |
|
---|
4266 | @node Variables/Recursion, Options/Recursion, MAKE Variable, Recursion
|
---|
4267 | @subsection Communicating Variables to a Sub-@code{make}
|
---|
4268 | @cindex sub-@code{make}
|
---|
4269 | @cindex environment, and recursion
|
---|
4270 | @cindex exporting variables
|
---|
4271 | @cindex variables, environment
|
---|
4272 | @cindex variables, exporting
|
---|
4273 | @cindex recursion, and environment
|
---|
4274 | @cindex recursion, and variables
|
---|
4275 |
|
---|
4276 | Variable values of the top-level @code{make} can be passed to the
|
---|
4277 | sub-@code{make} through the environment by explicit request. These
|
---|
4278 | variables are defined in the sub-@code{make} as defaults, but do not
|
---|
4279 | override what is specified in the makefile used by the sub-@code{make}
|
---|
4280 | makefile unless you use the @samp{-e} switch (@pxref{Options Summary,
|
---|
4281 | ,Summary of Options}).@refill
|
---|
4282 |
|
---|
4283 | To pass down, or @dfn{export}, a variable, @code{make} adds the variable
|
---|
4284 | and its value to the environment for running each command. The
|
---|
4285 | sub-@code{make}, in turn, uses the environment to initialize its table
|
---|
4286 | of variable values. @xref{Environment, ,Variables from the
|
---|
4287 | Environment}.
|
---|
4288 |
|
---|
4289 | Except by explicit request, @code{make} exports a variable only if it
|
---|
4290 | is either defined in the environment initially or set on the command
|
---|
4291 | line, and if its name consists only of letters, numbers, and underscores.
|
---|
4292 | Some shells cannot cope with environment variable names consisting of
|
---|
4293 | characters other than letters, numbers, and underscores.
|
---|
4294 |
|
---|
4295 | @cindex SHELL, exported value
|
---|
4296 | The value of the @code{make} variable @code{SHELL} is not exported.
|
---|
4297 | Instead, the value of the @code{SHELL} variable from the invoking
|
---|
4298 | environment is passed to the sub-@code{make}. You can force
|
---|
4299 | @code{make} to export its value for @code{SHELL} by using the
|
---|
4300 | @code{export} directive, described below. @xref{Choosing the Shell}.
|
---|
4301 |
|
---|
4302 | The special variable @code{MAKEFLAGS} is always exported (unless you
|
---|
4303 | unexport it). @code{MAKEFILES} is exported if you set it to anything.
|
---|
4304 |
|
---|
4305 | @code{make} automatically passes down variable values that were defined
|
---|
4306 | on the command line, by putting them in the @code{MAKEFLAGS} variable.
|
---|
4307 | @iftex
|
---|
4308 | See the next section.
|
---|
4309 | @end iftex
|
---|
4310 | @ifnottex
|
---|
4311 | @xref{Options/Recursion}.
|
---|
4312 | @end ifnottex
|
---|
4313 |
|
---|
4314 | Variables are @emph{not} normally passed down if they were created by
|
---|
4315 | default by @code{make} (@pxref{Implicit Variables, ,Variables Used by
|
---|
4316 | Implicit Rules}). The sub-@code{make} will define these for
|
---|
4317 | itself.@refill
|
---|
4318 |
|
---|
4319 | @findex export
|
---|
4320 | If you want to export specific variables to a sub-@code{make}, use the
|
---|
4321 | @code{export} directive, like this:
|
---|
4322 |
|
---|
4323 | @example
|
---|
4324 | export @var{variable} @dots{}
|
---|
4325 | @end example
|
---|
4326 |
|
---|
4327 | @noindent
|
---|
4328 | @findex unexport
|
---|
4329 | If you want to @emph{prevent} a variable from being exported, use the
|
---|
4330 | @code{unexport} directive, like this:
|
---|
4331 |
|
---|
4332 | @example
|
---|
4333 | unexport @var{variable} @dots{}
|
---|
4334 | @end example
|
---|
4335 |
|
---|
4336 | @noindent
|
---|
4337 | In both of these forms, the arguments to @code{export} and
|
---|
4338 | @code{unexport} are expanded, and so could be variables or functions
|
---|
4339 | which expand to a (list of) variable names to be (un)exported.
|
---|
4340 |
|
---|
4341 | As a convenience, you can define a variable and export it at the same
|
---|
4342 | time by doing:
|
---|
4343 |
|
---|
4344 | @example
|
---|
4345 | export @var{variable} = value
|
---|
4346 | @end example
|
---|
4347 |
|
---|
4348 | @noindent
|
---|
4349 | has the same result as:
|
---|
4350 |
|
---|
4351 | @example
|
---|
4352 | @var{variable} = value
|
---|
4353 | export @var{variable}
|
---|
4354 | @end example
|
---|
4355 |
|
---|
4356 | @noindent
|
---|
4357 | and
|
---|
4358 |
|
---|
4359 | @example
|
---|
4360 | export @var{variable} := value
|
---|
4361 | @end example
|
---|
4362 |
|
---|
4363 | @noindent
|
---|
4364 | has the same result as:
|
---|
4365 |
|
---|
4366 | @example
|
---|
4367 | @var{variable} := value
|
---|
4368 | export @var{variable}
|
---|
4369 | @end example
|
---|
4370 |
|
---|
4371 | Likewise,
|
---|
4372 |
|
---|
4373 | @example
|
---|
4374 | export @var{variable} += value
|
---|
4375 | @end example
|
---|
4376 |
|
---|
4377 | @noindent
|
---|
4378 | is just like:
|
---|
4379 |
|
---|
4380 | @example
|
---|
4381 | @var{variable} += value
|
---|
4382 | export @var{variable}
|
---|
4383 | @end example
|
---|
4384 |
|
---|
4385 | @noindent
|
---|
4386 | @xref{Appending, ,Appending More Text to Variables}.
|
---|
4387 |
|
---|
4388 | You may notice that the @code{export} and @code{unexport} directives
|
---|
4389 | work in @code{make} in the same way they work in the shell, @code{sh}.
|
---|
4390 |
|
---|
4391 | If you want all variables to be exported by default, you can use
|
---|
4392 | @code{export} by itself:
|
---|
4393 |
|
---|
4394 | @example
|
---|
4395 | export
|
---|
4396 | @end example
|
---|
4397 |
|
---|
4398 | @noindent
|
---|
4399 | This tells @code{make} that variables which are not explicitly mentioned
|
---|
4400 | in an @code{export} or @code{unexport} directive should be exported.
|
---|
4401 | Any variable given in an @code{unexport} directive will still @emph{not}
|
---|
4402 | be exported. If you use @code{export} by itself to export variables by
|
---|
4403 | default, variables whose names contain characters other than
|
---|
4404 | alphanumerics and underscores will not be exported unless specifically
|
---|
4405 | mentioned in an @code{export} directive.@refill
|
---|
4406 |
|
---|
4407 | @findex .EXPORT_ALL_VARIABLES
|
---|
4408 | The behavior elicited by an @code{export} directive by itself was the
|
---|
4409 | default in older versions of GNU @code{make}. If your makefiles depend
|
---|
4410 | on this behavior and you want to be compatible with old versions of
|
---|
4411 | @code{make}, you can write a rule for the special target
|
---|
4412 | @code{.EXPORT_ALL_VARIABLES} instead of using the @code{export} directive.
|
---|
4413 | This will be ignored by old @code{make}s, while the @code{export}
|
---|
4414 | directive will cause a syntax error.@refill
|
---|
4415 | @cindex compatibility in exporting
|
---|
4416 |
|
---|
4417 | Likewise, you can use @code{unexport} by itself to tell @code{make}
|
---|
4418 | @emph{not} to export variables by default. Since this is the default
|
---|
4419 | behavior, you would only need to do this if @code{export} had been used
|
---|
4420 | by itself earlier (in an included makefile, perhaps). You
|
---|
4421 | @strong{cannot} use @code{export} and @code{unexport} by themselves to
|
---|
4422 | have variables exported for some commands and not for others. The last
|
---|
4423 | @code{export} or @code{unexport} directive that appears by itself
|
---|
4424 | determines the behavior for the entire run of @code{make}.@refill
|
---|
4425 |
|
---|
4426 | @vindex MAKELEVEL
|
---|
4427 | @cindex recursion, level of
|
---|
4428 | As a special feature, the variable @code{MAKELEVEL} is changed when it
|
---|
4429 | is passed down from level to level. This variable's value is a string
|
---|
4430 | which is the depth of the level as a decimal number. The value is
|
---|
4431 | @samp{0} for the top-level @code{make}; @samp{1} for a sub-@code{make},
|
---|
4432 | @samp{2} for a sub-sub-@code{make}, and so on. The incrementation
|
---|
4433 | happens when @code{make} sets up the environment for a command.@refill
|
---|
4434 |
|
---|
4435 | The main use of @code{MAKELEVEL} is to test it in a conditional
|
---|
4436 | directive (@pxref{Conditionals, ,Conditional Parts of Makefiles}); this
|
---|
4437 | way you can write a makefile that behaves one way if run recursively and
|
---|
4438 | another way if run directly by you.@refill
|
---|
4439 |
|
---|
4440 | @vindex MAKEFILES
|
---|
4441 | You can use the variable @code{MAKEFILES} to cause all sub-@code{make}
|
---|
4442 | commands to use additional makefiles. The value of @code{MAKEFILES} is
|
---|
4443 | a whitespace-separated list of file names. This variable, if defined in
|
---|
4444 | the outer-level makefile, is passed down through the environment; then
|
---|
4445 | it serves as a list of extra makefiles for the sub-@code{make} to read
|
---|
4446 | before the usual or specified ones. @xref{MAKEFILES Variable, ,The
|
---|
4447 | Variable @code{MAKEFILES}}.@refill
|
---|
4448 |
|
---|
4449 | @node Options/Recursion, -w Option, Variables/Recursion, Recursion
|
---|
4450 | @subsection Communicating Options to a Sub-@code{make}
|
---|
4451 | @cindex options, and recursion
|
---|
4452 | @cindex recursion, and options
|
---|
4453 |
|
---|
4454 | @vindex MAKEFLAGS
|
---|
4455 | Flags such as @samp{-s} and @samp{-k} are passed automatically to the
|
---|
4456 | sub-@code{make} through the variable @code{MAKEFLAGS}. This variable is
|
---|
4457 | set up automatically by @code{make} to contain the flag letters that
|
---|
4458 | @code{make} received. Thus, if you do @w{@samp{make -ks}} then
|
---|
4459 | @code{MAKEFLAGS} gets the value @samp{ks}.@refill
|
---|
4460 |
|
---|
4461 | As a consequence, every sub-@code{make} gets a value for @code{MAKEFLAGS}
|
---|
4462 | in its environment. In response, it takes the flags from that value and
|
---|
4463 | processes them as if they had been given as arguments.
|
---|
4464 | @xref{Options Summary, ,Summary of Options}.
|
---|
4465 |
|
---|
4466 | @cindex command line variable definitions, and recursion
|
---|
4467 | @cindex variables, command line, and recursion
|
---|
4468 | @cindex recursion, and command line variable definitions
|
---|
4469 | Likewise variables defined on the command line are passed to the
|
---|
4470 | sub-@code{make} through @code{MAKEFLAGS}. Words in the value of
|
---|
4471 | @code{MAKEFLAGS} that contain @samp{=}, @code{make} treats as variable
|
---|
4472 | definitions just as if they appeared on the command line.
|
---|
4473 | @xref{Overriding, ,Overriding Variables}.
|
---|
4474 |
|
---|
4475 | @cindex @code{-C}, and recursion
|
---|
4476 | @cindex @code{-f}, and recursion
|
---|
4477 | @cindex @code{-o}, and recursion
|
---|
4478 | @cindex @code{-W}, and recursion
|
---|
4479 | @cindex @code{--directory}, and recursion
|
---|
4480 | @cindex @code{--file}, and recursion
|
---|
4481 | @cindex @code{--old-file}, and recursion
|
---|
4482 | @cindex @code{--assume-old}, and recursion
|
---|
4483 | @cindex @code{--assume-new}, and recursion
|
---|
4484 | @cindex @code{--new-file}, and recursion
|
---|
4485 | @cindex recursion, and @code{-C}
|
---|
4486 | @cindex recursion, and @code{-f}
|
---|
4487 | @cindex recursion, and @code{-o}
|
---|
4488 | @cindex recursion, and @code{-W}
|
---|
4489 | The options @samp{-C}, @samp{-f}, @samp{-o}, and @samp{-W} are not put
|
---|
4490 | into @code{MAKEFLAGS}; these options are not passed down.@refill
|
---|
4491 |
|
---|
4492 | @cindex @code{-j}, and recursion
|
---|
4493 | @cindex @code{--jobs}, and recursion
|
---|
4494 | @cindex recursion, and @code{-j}
|
---|
4495 | @cindex job slots, and recursion
|
---|
4496 | The @samp{-j} option is a special case (@pxref{Parallel, ,Parallel Execution}).
|
---|
4497 | If you set it to some numeric value @samp{N} and your operating system
|
---|
4498 | supports it (most any UNIX system will; others typically won't), the
|
---|
4499 | parent @code{make} and all the sub-@code{make}s will communicate to
|
---|
4500 | ensure that there are only @samp{N} jobs running at the same time
|
---|
4501 | between them all. Note that any job that is marked recursive
|
---|
4502 | (@pxref{Instead of Execution, ,Instead of Executing the Commands})
|
---|
4503 | doesn't count against the total jobs (otherwise we could get @samp{N}
|
---|
4504 | sub-@code{make}s running and have no slots left over for any real work!)
|
---|
4505 |
|
---|
4506 | If your operating system doesn't support the above communication, then
|
---|
4507 | @samp{-j 1} is always put into @code{MAKEFLAGS} instead of the value you
|
---|
4508 | specified. This is because if the @w{@samp{-j}} option were passed down
|
---|
4509 | to sub-@code{make}s, you would get many more jobs running in parallel
|
---|
4510 | than you asked for. If you give @samp{-j} with no numeric argument,
|
---|
4511 | meaning to run as many jobs as possible in parallel, this is passed
|
---|
4512 | down, since multiple infinities are no more than one.@refill
|
---|
4513 |
|
---|
4514 | If you do not want to pass the other flags down, you must change the
|
---|
4515 | value of @code{MAKEFLAGS}, like this:
|
---|
4516 |
|
---|
4517 | @example
|
---|
4518 | subsystem:
|
---|
4519 | cd subdir && $(MAKE) MAKEFLAGS=
|
---|
4520 | @end example
|
---|
4521 |
|
---|
4522 | @vindex MAKEOVERRIDES
|
---|
4523 | The command line variable definitions really appear in the variable
|
---|
4524 | @code{MAKEOVERRIDES}, and @code{MAKEFLAGS} contains a reference to this
|
---|
4525 | variable. If you do want to pass flags down normally, but don't want to
|
---|
4526 | pass down the command line variable definitions, you can reset
|
---|
4527 | @code{MAKEOVERRIDES} to empty, like this:
|
---|
4528 |
|
---|
4529 | @example
|
---|
4530 | MAKEOVERRIDES =
|
---|
4531 | @end example
|
---|
4532 |
|
---|
4533 | @noindent
|
---|
4534 | @cindex Arg list too long
|
---|
4535 | @cindex E2BIG
|
---|
4536 | This is not usually useful to do. However, some systems have a small
|
---|
4537 | fixed limit on the size of the environment, and putting so much
|
---|
4538 | information into the value of @code{MAKEFLAGS} can exceed it. If you
|
---|
4539 | see the error message @samp{Arg list too long}, this may be the problem.
|
---|
4540 | @findex .POSIX
|
---|
4541 | @cindex POSIX.2
|
---|
4542 | (For strict compliance with POSIX.2, changing @code{MAKEOVERRIDES} does
|
---|
4543 | not affect @code{MAKEFLAGS} if the special target @samp{.POSIX} appears
|
---|
4544 | in the makefile. You probably do not care about this.)
|
---|
4545 |
|
---|
4546 | @vindex MFLAGS
|
---|
4547 | A similar variable @code{MFLAGS} exists also, for historical
|
---|
4548 | compatibility. It has the same value as @code{MAKEFLAGS} except that it
|
---|
4549 | does not contain the command line variable definitions, and it always
|
---|
4550 | begins with a hyphen unless it is empty (@code{MAKEFLAGS} begins with a
|
---|
4551 | hyphen only when it begins with an option that has no single-letter
|
---|
4552 | version, such as @samp{--warn-undefined-variables}). @code{MFLAGS} was
|
---|
4553 | traditionally used explicitly in the recursive @code{make} command, like
|
---|
4554 | this:
|
---|
4555 |
|
---|
4556 | @example
|
---|
4557 | subsystem:
|
---|
4558 | cd subdir && $(MAKE) $(MFLAGS)
|
---|
4559 | @end example
|
---|
4560 |
|
---|
4561 | @noindent
|
---|
4562 | but now @code{MAKEFLAGS} makes this usage redundant. If you want your
|
---|
4563 | makefiles to be compatible with old @code{make} programs, use this
|
---|
4564 | technique; it will work fine with more modern @code{make} versions too.
|
---|
4565 |
|
---|
4566 | @cindex setting options from environment
|
---|
4567 | @cindex options, setting from environment
|
---|
4568 | @cindex setting options in makefiles
|
---|
4569 | @cindex options, setting in makefiles
|
---|
4570 | The @code{MAKEFLAGS} variable can also be useful if you want to have
|
---|
4571 | certain options, such as @samp{-k} (@pxref{Options Summary, ,Summary of
|
---|
4572 | Options}), set each time you run @code{make}. You simply put a value for
|
---|
4573 | @code{MAKEFLAGS} in your environment. You can also set @code{MAKEFLAGS} in
|
---|
4574 | a makefile, to specify additional flags that should also be in effect for
|
---|
4575 | that makefile. (Note that you cannot use @code{MFLAGS} this way. That
|
---|
4576 | variable is set only for compatibility; @code{make} does not interpret a
|
---|
4577 | value you set for it in any way.)
|
---|
4578 |
|
---|
4579 | When @code{make} interprets the value of @code{MAKEFLAGS} (either from the
|
---|
4580 | environment or from a makefile), it first prepends a hyphen if the value
|
---|
4581 | does not already begin with one. Then it chops the value into words
|
---|
4582 | separated by blanks, and parses these words as if they were options given
|
---|
4583 | on the command line (except that @samp{-C}, @samp{-f}, @samp{-h},
|
---|
4584 | @samp{-o}, @samp{-W}, and their long-named versions are ignored; and there
|
---|
4585 | is no error for an invalid option).
|
---|
4586 |
|
---|
4587 | If you do put @code{MAKEFLAGS} in your environment, you should be sure not
|
---|
4588 | to include any options that will drastically affect the actions of
|
---|
4589 | @code{make} and undermine the purpose of makefiles and of @code{make}
|
---|
4590 | itself. For instance, the @samp{-t}, @samp{-n}, and @samp{-q} options, if
|
---|
4591 | put in one of these variables, could have disastrous consequences and would
|
---|
4592 | certainly have at least surprising and probably annoying effects.@refill
|
---|
4593 |
|
---|
4594 | @node -w Option, , Options/Recursion, Recursion
|
---|
4595 | @subsection The @samp{--print-directory} Option
|
---|
4596 | @cindex directories, printing them
|
---|
4597 | @cindex printing directories
|
---|
4598 | @cindex recursion, and printing directories
|
---|
4599 |
|
---|
4600 | If you use several levels of recursive @code{make} invocations, the
|
---|
4601 | @samp{-w} or @w{@samp{--print-directory}} option can make the output a
|
---|
4602 | lot easier to understand by showing each directory as @code{make}
|
---|
4603 | starts processing it and as @code{make} finishes processing it. For
|
---|
4604 | example, if @samp{make -w} is run in the directory @file{/u/gnu/make},
|
---|
4605 | @code{make} will print a line of the form:@refill
|
---|
4606 |
|
---|
4607 | @example
|
---|
4608 | make: Entering directory `/u/gnu/make'.
|
---|
4609 | @end example
|
---|
4610 |
|
---|
4611 | @noindent
|
---|
4612 | before doing anything else, and a line of the form:
|
---|
4613 |
|
---|
4614 | @example
|
---|
4615 | make: Leaving directory `/u/gnu/make'.
|
---|
4616 | @end example
|
---|
4617 |
|
---|
4618 | @noindent
|
---|
4619 | when processing is completed.
|
---|
4620 |
|
---|
4621 | @cindex @code{-C}, and @code{-w}
|
---|
4622 | @cindex @code{--directory}, and @code{--print-directory}
|
---|
4623 | @cindex recursion, and @code{-w}
|
---|
4624 | @cindex @code{-w}, and @code{-C}
|
---|
4625 | @cindex @code{-w}, and recursion
|
---|
4626 | @cindex @code{--print-directory}, and @code{--directory}
|
---|
4627 | @cindex @code{--print-directory}, and recursion
|
---|
4628 | @cindex @code{--no-print-directory}
|
---|
4629 | @cindex @code{--print-directory}, disabling
|
---|
4630 | @cindex @code{-w}, disabling
|
---|
4631 | Normally, you do not need to specify this option because @samp{make}
|
---|
4632 | does it for you: @samp{-w} is turned on automatically when you use the
|
---|
4633 | @samp{-C} option, and in sub-@code{make}s. @code{make} will not
|
---|
4634 | automatically turn on @samp{-w} if you also use @samp{-s}, which says to
|
---|
4635 | be silent, or if you use @samp{--no-print-directory} to explicitly
|
---|
4636 | disable it.
|
---|
4637 |
|
---|
4638 | @node Sequences, Empty Commands, Recursion, Commands
|
---|
4639 | @section Defining Canned Command Sequences
|
---|
4640 | @cindex sequences of commands
|
---|
4641 | @cindex commands, sequences of
|
---|
4642 |
|
---|
4643 | When the same sequence of commands is useful in making various targets, you
|
---|
4644 | can define it as a canned sequence with the @code{define} directive, and
|
---|
4645 | refer to the canned sequence from the rules for those targets. The canned
|
---|
4646 | sequence is actually a variable, so the name must not conflict with other
|
---|
4647 | variable names.
|
---|
4648 |
|
---|
4649 | Here is an example of defining a canned sequence of commands:
|
---|
4650 |
|
---|
4651 | @example
|
---|
4652 | define run-yacc
|
---|
4653 | yacc $(firstword $^)
|
---|
4654 | mv y.tab.c $@@
|
---|
4655 | endef
|
---|
4656 | @end example
|
---|
4657 | @cindex @code{yacc}
|
---|
4658 |
|
---|
4659 | @noindent
|
---|
4660 | Here @code{run-yacc} is the name of the variable being defined;
|
---|
4661 | @code{endef} marks the end of the definition; the lines in between are the
|
---|
4662 | commands. The @code{define} directive does not expand variable references
|
---|
4663 | and function calls in the canned sequence; the @samp{$} characters,
|
---|
4664 | parentheses, variable names, and so on, all become part of the value of the
|
---|
4665 | variable you are defining.
|
---|
4666 | @xref{Defining, ,Defining Variables Verbatim},
|
---|
4667 | for a complete explanation of @code{define}.
|
---|
4668 |
|
---|
4669 | The first command in this example runs Yacc on the first prerequisite of
|
---|
4670 | whichever rule uses the canned sequence. The output file from Yacc is
|
---|
4671 | always named @file{y.tab.c}. The second command moves the output to the
|
---|
4672 | rule's target file name.
|
---|
4673 |
|
---|
4674 | To use the canned sequence, substitute the variable into the commands of a
|
---|
4675 | rule. You can substitute it like any other variable
|
---|
4676 | (@pxref{Reference, ,Basics of Variable References}).
|
---|
4677 | Because variables defined by @code{define} are recursively expanded
|
---|
4678 | variables, all the variable references you wrote inside the @code{define}
|
---|
4679 | are expanded now. For example:
|
---|
4680 |
|
---|
4681 | @example
|
---|
4682 | foo.c : foo.y
|
---|
4683 | $(run-yacc)
|
---|
4684 | @end example
|
---|
4685 |
|
---|
4686 | @noindent
|
---|
4687 | @samp{foo.y} will be substituted for the variable @samp{$^} when it occurs in
|
---|
4688 | @code{run-yacc}'s value, and @samp{foo.c} for @samp{$@@}.@refill
|
---|
4689 |
|
---|
4690 | This is a realistic example, but this particular one is not needed in
|
---|
4691 | practice because @code{make} has an implicit rule to figure out these
|
---|
4692 | commands based on the file names involved
|
---|
4693 | (@pxref{Implicit Rules, ,Using Implicit Rules}).
|
---|
4694 |
|
---|
4695 | @cindex @@, and @code{define}
|
---|
4696 | @cindex -, and @code{define}
|
---|
4697 | @cindex +, and @code{define}
|
---|
4698 | In command execution, each line of a canned sequence is treated just as
|
---|
4699 | if the line appeared on its own in the rule, preceded by a tab. In
|
---|
4700 | particular, @code{make} invokes a separate subshell for each line. You
|
---|
4701 | can use the special prefix characters that affect command lines
|
---|
4702 | (@samp{@@}, @samp{-}, and @samp{+}) on each line of a canned sequence.
|
---|
4703 | @xref{Commands, ,Writing the Commands in Rules}.
|
---|
4704 | For example, using this canned sequence:
|
---|
4705 |
|
---|
4706 | @example
|
---|
4707 | define frobnicate
|
---|
4708 | @@echo "frobnicating target $@@"
|
---|
4709 | frob-step-1 $< -o $@@-step-1
|
---|
4710 | frob-step-2 $@@-step-1 -o $@@
|
---|
4711 | endef
|
---|
4712 | @end example
|
---|
4713 |
|
---|
4714 | @noindent
|
---|
4715 | @code{make} will not echo the first line, the @code{echo} command.
|
---|
4716 | But it @emph{will} echo the following two command lines.
|
---|
4717 |
|
---|
4718 | On the other hand, prefix characters on the command line that refers to
|
---|
4719 | a canned sequence apply to every line in the sequence. So the rule:
|
---|
4720 |
|
---|
4721 | @example
|
---|
4722 | frob.out: frob.in
|
---|
4723 | @@$(frobnicate)
|
---|
4724 | @end example
|
---|
4725 |
|
---|
4726 | @noindent
|
---|
4727 | does not echo @emph{any} commands.
|
---|
4728 | (@xref{Echoing, ,Command Echoing}, for a full explanation of @samp{@@}.)
|
---|
4729 |
|
---|
4730 | @node Empty Commands, , Sequences, Commands
|
---|
4731 | @section Using Empty Commands
|
---|
4732 | @cindex empty commands
|
---|
4733 | @cindex commands, empty
|
---|
4734 |
|
---|
4735 | It is sometimes useful to define commands which do nothing. This is done
|
---|
4736 | simply by giving a command that consists of nothing but whitespace. For
|
---|
4737 | example:
|
---|
4738 |
|
---|
4739 | @example
|
---|
4740 | target: ;
|
---|
4741 | @end example
|
---|
4742 |
|
---|
4743 | @noindent
|
---|
4744 | defines an empty command string for @file{target}. You could also use a
|
---|
4745 | line beginning with a tab character to define an empty command string,
|
---|
4746 | but this would be confusing because such a line looks empty.
|
---|
4747 |
|
---|
4748 | @findex .DEFAULT@r{, and empty commands}
|
---|
4749 | You may be wondering why you would want to define a command string that
|
---|
4750 | does nothing. The only reason this is useful is to prevent a target
|
---|
4751 | from getting implicit commands (from implicit rules or the
|
---|
4752 | @code{.DEFAULT} special target; @pxref{Implicit Rules} and
|
---|
4753 | @pxref{Last Resort, ,Defining Last-Resort Default Rules}).@refill
|
---|
4754 |
|
---|
4755 | @c !!! another reason is for canonical stamp files:
|
---|
4756 | @ignore
|
---|
4757 | @example
|
---|
4758 | foo: stamp-foo ;
|
---|
4759 | stamp-foo: foo.in
|
---|
4760 | create foo frm foo.in
|
---|
4761 | touch $@
|
---|
4762 | @end example
|
---|
4763 | @end ignore
|
---|
4764 |
|
---|
4765 | You may be inclined to define empty command strings for targets that are
|
---|
4766 | not actual files, but only exist so that their prerequisites can be
|
---|
4767 | remade. However, this is not the best way to do that, because the
|
---|
4768 | prerequisites may not be remade properly if the target file actually does exist.
|
---|
4769 | @xref{Phony Targets, ,Phony Targets}, for a better way to do this.
|
---|
4770 |
|
---|
4771 | @node Using Variables, Conditionals, Commands, Top
|
---|
4772 | @chapter How to Use Variables
|
---|
4773 | @cindex variable
|
---|
4774 | @cindex value
|
---|
4775 | @cindex recursive variable expansion
|
---|
4776 | @cindex simple variable expansion
|
---|
4777 |
|
---|
4778 | A @dfn{variable} is a name defined in a makefile to represent a string
|
---|
4779 | of text, called the variable's @dfn{value}. These values are
|
---|
4780 | substituted by explicit request into targets, prerequisites, commands,
|
---|
4781 | and other parts of the makefile. (In some other versions of @code{make},
|
---|
4782 | variables are called @dfn{macros}.)
|
---|
4783 | @cindex macro
|
---|
4784 |
|
---|
4785 | Variables and functions in all parts of a makefile are expanded when
|
---|
4786 | read, except for the shell commands in rules, the right-hand sides of
|
---|
4787 | variable definitions using @samp{=}, and the bodies of variable
|
---|
4788 | definitions using the @code{define} directive.@refill
|
---|
4789 |
|
---|
4790 | Variables can represent lists of file names, options to pass to compilers,
|
---|
4791 | programs to run, directories to look in for source files, directories to
|
---|
4792 | write output in, or anything else you can imagine.
|
---|
4793 |
|
---|
4794 | A variable name may be any sequence of characters not containing @samp{:},
|
---|
4795 | @samp{#}, @samp{=}, or leading or trailing whitespace. However,
|
---|
4796 | variable names containing characters other than letters, numbers, and
|
---|
4797 | underscores should be avoided, as they may be given special meanings in the
|
---|
4798 | future, and with some shells they cannot be passed through the environment to a
|
---|
4799 | sub-@code{make}
|
---|
4800 | (@pxref{Variables/Recursion, ,Communicating Variables to a Sub-@code{make}}).
|
---|
4801 |
|
---|
4802 | Variable names are case-sensitive. The names @samp{foo}, @samp{FOO},
|
---|
4803 | and @samp{Foo} all refer to different variables.
|
---|
4804 |
|
---|
4805 | It is traditional to use upper case letters in variable names, but we
|
---|
4806 | recommend using lower case letters for variable names that serve internal
|
---|
4807 | purposes in the makefile, and reserving upper case for parameters that
|
---|
4808 | control implicit rules or for parameters that the user should override with
|
---|
4809 | command options (@pxref{Overriding, ,Overriding Variables}).
|
---|
4810 |
|
---|
4811 | A few variables have names that are a single punctuation character or
|
---|
4812 | just a few characters. These are the @dfn{automatic variables}, and
|
---|
4813 | they have particular specialized uses. @xref{Automatic Variables}.
|
---|
4814 |
|
---|
4815 | @menu
|
---|
4816 | * Reference:: How to use the value of a variable.
|
---|
4817 | * Flavors:: Variables come in two flavors.
|
---|
4818 | * Advanced:: Advanced features for referencing a variable.
|
---|
4819 | * Values:: All the ways variables get their values.
|
---|
4820 | * Setting:: How to set a variable in the makefile.
|
---|
4821 | * Appending:: How to append more text to the old value
|
---|
4822 | of a variable.
|
---|
4823 | * Override Directive:: How to set a variable in the makefile even if
|
---|
4824 | the user has set it with a command argument.
|
---|
4825 | * Defining:: An alternate way to set a variable
|
---|
4826 | to a verbatim string.
|
---|
4827 | * Environment:: Variable values can come from the environment.
|
---|
4828 | * Target-specific:: Variable values can be defined on a per-target
|
---|
4829 | basis.
|
---|
4830 | * Pattern-specific:: Target-specific variable values can be applied
|
---|
4831 | to a group of targets that match a pattern.
|
---|
4832 | @end menu
|
---|
4833 |
|
---|
4834 | @node Reference, Flavors, Using Variables, Using Variables
|
---|
4835 | @section Basics of Variable References
|
---|
4836 | @cindex variables, how to reference
|
---|
4837 | @cindex reference to variables
|
---|
4838 | @cindex @code{$}, in variable reference
|
---|
4839 | @cindex dollar sign (@code{$}), in variable reference
|
---|
4840 |
|
---|
4841 | To substitute a variable's value, write a dollar sign followed by the name
|
---|
4842 | of the variable in parentheses or braces: either @samp{$(foo)} or
|
---|
4843 | @samp{$@{foo@}} is a valid reference to the variable @code{foo}. This
|
---|
4844 | special significance of @samp{$} is why you must write @samp{$$} to have
|
---|
4845 | the effect of a single dollar sign in a file name or command.
|
---|
4846 |
|
---|
4847 | Variable references can be used in any context: targets, prerequisites,
|
---|
4848 | commands, most directives, and new variable values. Here is an
|
---|
4849 | example of a common case, where a variable holds the names of all the
|
---|
4850 | object files in a program:
|
---|
4851 |
|
---|
4852 | @example
|
---|
4853 | @group
|
---|
4854 | objects = program.o foo.o utils.o
|
---|
4855 | program : $(objects)
|
---|
4856 | cc -o program $(objects)
|
---|
4857 |
|
---|
4858 | $(objects) : defs.h
|
---|
4859 | @end group
|
---|
4860 | @end example
|
---|
4861 |
|
---|
4862 | Variable references work by strict textual substitution. Thus, the rule
|
---|
4863 |
|
---|
4864 | @example
|
---|
4865 | @group
|
---|
4866 | foo = c
|
---|
4867 | prog.o : prog.$(foo)
|
---|
4868 | $(foo)$(foo) -$(foo) prog.$(foo)
|
---|
4869 | @end group
|
---|
4870 | @end example
|
---|
4871 |
|
---|
4872 | @noindent
|
---|
4873 | could be used to compile a C program @file{prog.c}. Since spaces before
|
---|
4874 | the variable value are ignored in variable assignments, the value of
|
---|
4875 | @code{foo} is precisely @samp{c}. (Don't actually write your makefiles
|
---|
4876 | this way!)
|
---|
4877 |
|
---|
4878 | A dollar sign followed by a character other than a dollar sign,
|
---|
4879 | open-parenthesis or open-brace treats that single character as the
|
---|
4880 | variable name. Thus, you could reference the variable @code{x} with
|
---|
4881 | @samp{$x}. However, this practice is strongly discouraged, except in
|
---|
4882 | the case of the automatic variables (@pxref{Automatic Variables}).
|
---|
4883 |
|
---|
4884 | @node Flavors, Advanced, Reference, Using Variables
|
---|
4885 | @section The Two Flavors of Variables
|
---|
4886 | @cindex flavors of variables
|
---|
4887 | @cindex recursive variable expansion
|
---|
4888 | @cindex variables, flavors
|
---|
4889 | @cindex recursively expanded variables
|
---|
4890 | @cindex variables, recursively expanded
|
---|
4891 |
|
---|
4892 | There are two ways that a variable in GNU @code{make} can have a value;
|
---|
4893 | we call them the two @dfn{flavors} of variables. The two flavors are
|
---|
4894 | distinguished in how they are defined and in what they do when expanded.
|
---|
4895 |
|
---|
4896 | @cindex =
|
---|
4897 | The first flavor of variable is a @dfn{recursively expanded} variable.
|
---|
4898 | Variables of this sort are defined by lines using @samp{=}
|
---|
4899 | (@pxref{Setting, ,Setting Variables}) or by the @code{define} directive
|
---|
4900 | (@pxref{Defining, ,Defining Variables Verbatim}). The value you specify
|
---|
4901 | is installed verbatim; if it contains references to other variables,
|
---|
4902 | these references are expanded whenever this variable is substituted (in
|
---|
4903 | the course of expanding some other string). When this happens, it is
|
---|
4904 | called @dfn{recursive expansion}.@refill
|
---|
4905 |
|
---|
4906 | For example,
|
---|
4907 |
|
---|
4908 | @example
|
---|
4909 | foo = $(bar)
|
---|
4910 | bar = $(ugh)
|
---|
4911 | ugh = Huh?
|
---|
4912 |
|
---|
4913 | all:;echo $(foo)
|
---|
4914 | @end example
|
---|
4915 |
|
---|
4916 | @noindent
|
---|
4917 | will echo @samp{Huh?}: @samp{$(foo)} expands to @samp{$(bar)} which
|
---|
4918 | expands to @samp{$(ugh)} which finally expands to @samp{Huh?}.@refill
|
---|
4919 |
|
---|
4920 | This flavor of variable is the only sort supported by other versions of
|
---|
4921 | @code{make}. It has its advantages and its disadvantages. An advantage
|
---|
4922 | (most would say) is that:
|
---|
4923 |
|
---|
4924 | @example
|
---|
4925 | CFLAGS = $(include_dirs) -O
|
---|
4926 | include_dirs = -Ifoo -Ibar
|
---|
4927 | @end example
|
---|
4928 |
|
---|
4929 | @noindent
|
---|
4930 | will do what was intended: when @samp{CFLAGS} is expanded in a command,
|
---|
4931 | it will expand to @samp{-Ifoo -Ibar -O}. A major disadvantage is that you
|
---|
4932 | cannot append something on the end of a variable, as in
|
---|
4933 |
|
---|
4934 | @example
|
---|
4935 | CFLAGS = $(CFLAGS) -O
|
---|
4936 | @end example
|
---|
4937 |
|
---|
4938 | @noindent
|
---|
4939 | because it will cause an infinite loop in the variable expansion.
|
---|
4940 | (Actually @code{make} detects the infinite loop and reports an error.)
|
---|
4941 | @cindex loops in variable expansion
|
---|
4942 | @cindex variables, loops in expansion
|
---|
4943 |
|
---|
4944 | Another disadvantage is that any functions
|
---|
4945 | (@pxref{Functions, ,Functions for Transforming Text})
|
---|
4946 | referenced in the definition will be executed every time the variable is
|
---|
4947 | expanded. This makes @code{make} run slower; worse, it causes the
|
---|
4948 | @code{wildcard} and @code{shell} functions to give unpredictable results
|
---|
4949 | because you cannot easily control when they are called, or even how many
|
---|
4950 | times.
|
---|
4951 |
|
---|
4952 | To avoid all the problems and inconveniences of recursively expanded
|
---|
4953 | variables, there is another flavor: simply expanded variables.
|
---|
4954 |
|
---|
4955 | @cindex simply expanded variables
|
---|
4956 | @cindex variables, simply expanded
|
---|
4957 | @cindex :=
|
---|
4958 | @dfn{Simply expanded variables} are defined by lines using @samp{:=}
|
---|
4959 | (@pxref{Setting, ,Setting Variables}).
|
---|
4960 | The value of a simply expanded variable is scanned
|
---|
4961 | once and for all, expanding any references to other variables and
|
---|
4962 | functions, when the variable is defined. The actual value of the simply
|
---|
4963 | expanded variable is the result of expanding the text that you write.
|
---|
4964 | It does not contain any references to other variables; it contains their
|
---|
4965 | values @emph{as of the time this variable was defined}. Therefore,
|
---|
4966 |
|
---|
4967 | @example
|
---|
4968 | x := foo
|
---|
4969 | y := $(x) bar
|
---|
4970 | x := later
|
---|
4971 | @end example
|
---|
4972 |
|
---|
4973 | @noindent
|
---|
4974 | is equivalent to
|
---|
4975 |
|
---|
4976 | @example
|
---|
4977 | y := foo bar
|
---|
4978 | x := later
|
---|
4979 | @end example
|
---|
4980 |
|
---|
4981 | When a simply expanded variable is referenced, its value is substituted
|
---|
4982 | verbatim.
|
---|
4983 |
|
---|
4984 | Here is a somewhat more complicated example, illustrating the use of
|
---|
4985 | @samp{:=} in conjunction with the @code{shell} function.
|
---|
4986 | (@xref{Shell Function, , The @code{shell} Function}.) This example
|
---|
4987 | also shows use of the variable @code{MAKELEVEL}, which is changed
|
---|
4988 | when it is passed down from level to level.
|
---|
4989 | (@xref{Variables/Recursion, , Communicating Variables to a
|
---|
4990 | Sub-@code{make}}, for information about @code{MAKELEVEL}.)
|
---|
4991 |
|
---|
4992 | @vindex MAKELEVEL
|
---|
4993 | @vindex MAKE
|
---|
4994 | @example
|
---|
4995 | @group
|
---|
4996 | ifeq (0,$@{MAKELEVEL@})
|
---|
4997 | whoami := $(shell whoami)
|
---|
4998 | host-type := $(shell arch)
|
---|
4999 | MAKE := $@{MAKE@} host-type=$@{host-type@} whoami=$@{whoami@}
|
---|
5000 | endif
|
---|
5001 | @end group
|
---|
5002 | @end example
|
---|
5003 |
|
---|
5004 | @noindent
|
---|
5005 | An advantage of this use of @samp{:=} is that a typical
|
---|
5006 | `descend into a directory' command then looks like this:
|
---|
5007 |
|
---|
5008 | @example
|
---|
5009 | @group
|
---|
5010 | $@{subdirs@}:
|
---|
5011 | $@{MAKE@} -C $@@ all
|
---|
5012 | @end group
|
---|
5013 | @end example
|
---|
5014 |
|
---|
5015 | Simply expanded variables generally make complicated makefile programming
|
---|
5016 | more predictable because they work like variables in most programming
|
---|
5017 | languages. They allow you to redefine a variable using its own value (or
|
---|
5018 | its value processed in some way by one of the expansion functions) and to
|
---|
5019 | use the expansion functions much more efficiently
|
---|
5020 | (@pxref{Functions, ,Functions for Transforming Text}).
|
---|
5021 |
|
---|
5022 | @cindex spaces, in variable values
|
---|
5023 | @cindex whitespace, in variable values
|
---|
5024 | @cindex variables, spaces in values
|
---|
5025 | You can also use them to introduce controlled leading whitespace into
|
---|
5026 | variable values. Leading whitespace characters are discarded from your
|
---|
5027 | input before substitution of variable references and function calls;
|
---|
5028 | this means you can include leading spaces in a variable value by
|
---|
5029 | protecting them with variable references, like this:
|
---|
5030 |
|
---|
5031 | @example
|
---|
5032 | nullstring :=
|
---|
5033 | space := $(nullstring) # end of the line
|
---|
5034 | @end example
|
---|
5035 |
|
---|
5036 | @noindent
|
---|
5037 | Here the value of the variable @code{space} is precisely one space. The
|
---|
5038 | comment @w{@samp{# end of the line}} is included here just for clarity.
|
---|
5039 | Since trailing space characters are @emph{not} stripped from variable
|
---|
5040 | values, just a space at the end of the line would have the same effect
|
---|
5041 | (but be rather hard to read). If you put whitespace at the end of a
|
---|
5042 | variable value, it is a good idea to put a comment like that at the end
|
---|
5043 | of the line to make your intent clear. Conversely, if you do @emph{not}
|
---|
5044 | want any whitespace characters at the end of your variable value, you
|
---|
5045 | must remember not to put a random comment on the end of the line after
|
---|
5046 | some whitespace, such as this:
|
---|
5047 |
|
---|
5048 | @example
|
---|
5049 | dir := /foo/bar # directory to put the frobs in
|
---|
5050 | @end example
|
---|
5051 |
|
---|
5052 | @noindent
|
---|
5053 | Here the value of the variable @code{dir} is @w{@samp{/foo/bar }}
|
---|
5054 | (with four trailing spaces), which was probably not the intention.
|
---|
5055 | (Imagine something like @w{@samp{$(dir)/file}} with this definition!)
|
---|
5056 |
|
---|
5057 | @cindex conditional variable assignment
|
---|
5058 | @cindex variables, conditional assignment
|
---|
5059 | @cindex ?=
|
---|
5060 | There is another assignment operator for variables, @samp{?=}. This
|
---|
5061 | is called a conditional variable assignment operator, because it only
|
---|
5062 | has an effect if the variable is not yet defined. This statement:
|
---|
5063 |
|
---|
5064 | @example
|
---|
5065 | FOO ?= bar
|
---|
5066 | @end example
|
---|
5067 |
|
---|
5068 | @noindent
|
---|
5069 | is exactly equivalent to this
|
---|
5070 | (@pxref{Origin Function, ,The @code{origin} Function}):
|
---|
5071 |
|
---|
5072 | @example
|
---|
5073 | ifeq ($(origin FOO), undefined)
|
---|
5074 | FOO = bar
|
---|
5075 | endif
|
---|
5076 | @end example
|
---|
5077 |
|
---|
5078 | Note that a variable set to an empty value is still defined, so
|
---|
5079 | @samp{?=} will not set that variable.
|
---|
5080 |
|
---|
5081 | @node Advanced, Values, Flavors, Using Variables
|
---|
5082 | @section Advanced Features for Reference to Variables
|
---|
5083 | @cindex reference to variables
|
---|
5084 |
|
---|
5085 | This section describes some advanced features you can use to reference
|
---|
5086 | variables in more flexible ways.
|
---|
5087 |
|
---|
5088 | @menu
|
---|
5089 | * Substitution Refs:: Referencing a variable with
|
---|
5090 | substitutions on the value.
|
---|
5091 | * Computed Names:: Computing the name of the variable to refer to.
|
---|
5092 | @end menu
|
---|
5093 |
|
---|
5094 | @node Substitution Refs, Computed Names, Advanced, Advanced
|
---|
5095 | @subsection Substitution References
|
---|
5096 | @cindex modified variable reference
|
---|
5097 | @cindex substitution variable reference
|
---|
5098 | @cindex variables, modified reference
|
---|
5099 | @cindex variables, substitution reference
|
---|
5100 |
|
---|
5101 | @cindex variables, substituting suffix in
|
---|
5102 | @cindex suffix, substituting in variables
|
---|
5103 | A @dfn{substitution reference} substitutes the value of a variable with
|
---|
5104 | alterations that you specify. It has the form
|
---|
5105 | @samp{$(@var{var}:@var{a}=@var{b})} (or
|
---|
5106 | @samp{$@{@var{var}:@var{a}=@var{b}@}}) and its meaning is to take the value
|
---|
5107 | of the variable @var{var}, replace every @var{a} at the end of a word with
|
---|
5108 | @var{b} in that value, and substitute the resulting string.
|
---|
5109 |
|
---|
5110 | When we say ``at the end of a word'', we mean that @var{a} must appear
|
---|
5111 | either followed by whitespace or at the end of the value in order to be
|
---|
5112 | replaced; other occurrences of @var{a} in the value are unaltered. For
|
---|
5113 | example:@refill
|
---|
5114 |
|
---|
5115 | @example
|
---|
5116 | foo := a.o b.o c.o
|
---|
5117 | bar := $(foo:.o=.c)
|
---|
5118 | @end example
|
---|
5119 |
|
---|
5120 | @noindent
|
---|
5121 | sets @samp{bar} to @samp{a.c b.c c.c}. @xref{Setting, ,Setting Variables}.
|
---|
5122 |
|
---|
5123 | A substitution reference is actually an abbreviation for use of the
|
---|
5124 | @code{patsubst} expansion function (@pxref{Text Functions, ,Functions for String Substitution and Analysis}). We provide
|
---|
5125 | substitution references as well as @code{patsubst} for compatibility with
|
---|
5126 | other implementations of @code{make}.
|
---|
5127 |
|
---|
5128 | @findex patsubst
|
---|
5129 | Another type of substitution reference lets you use the full power of
|
---|
5130 | the @code{patsubst} function. It has the same form
|
---|
5131 | @samp{$(@var{var}:@var{a}=@var{b})} described above, except that now
|
---|
5132 | @var{a} must contain a single @samp{%} character. This case is
|
---|
5133 | equivalent to @samp{$(patsubst @var{a},@var{b},$(@var{var}))}.
|
---|
5134 | @xref{Text Functions, ,Functions for String Substitution and Analysis},
|
---|
5135 | for a description of the @code{patsubst} function.@refill
|
---|
5136 |
|
---|
5137 | @example
|
---|
5138 | @group
|
---|
5139 | @exdent For example:
|
---|
5140 |
|
---|
5141 | foo := a.o b.o c.o
|
---|
5142 | bar := $(foo:%.o=%.c)
|
---|
5143 | @end group
|
---|
5144 | @end example
|
---|
5145 |
|
---|
5146 | @noindent
|
---|
5147 | sets @samp{bar} to @samp{a.c b.c c.c}.
|
---|
5148 |
|
---|
5149 | @node Computed Names, , Substitution Refs, Advanced
|
---|
5150 | @subsection Computed Variable Names
|
---|
5151 | @cindex nested variable reference
|
---|
5152 | @cindex computed variable name
|
---|
5153 | @cindex variables, computed names
|
---|
5154 | @cindex variables, nested references
|
---|
5155 | @cindex variables, @samp{$} in name
|
---|
5156 | @cindex @code{$}, in variable name
|
---|
5157 | @cindex dollar sign (@code{$}), in variable name
|
---|
5158 |
|
---|
5159 | Computed variable names are a complicated concept needed only for
|
---|
5160 | sophisticated makefile programming. For most purposes you need not
|
---|
5161 | consider them, except to know that making a variable with a dollar sign
|
---|
5162 | in its name might have strange results. However, if you are the type
|
---|
5163 | that wants to understand everything, or you are actually interested in
|
---|
5164 | what they do, read on.
|
---|
5165 |
|
---|
5166 | Variables may be referenced inside the name of a variable. This is
|
---|
5167 | called a @dfn{computed variable name} or a @dfn{nested variable
|
---|
5168 | reference}. For example,
|
---|
5169 |
|
---|
5170 | @example
|
---|
5171 | x = y
|
---|
5172 | y = z
|
---|
5173 | a := $($(x))
|
---|
5174 | @end example
|
---|
5175 |
|
---|
5176 | @noindent
|
---|
5177 | defines @code{a} as @samp{z}: the @samp{$(x)} inside @samp{$($(x))} expands
|
---|
5178 | to @samp{y}, so @samp{$($(x))} expands to @samp{$(y)} which in turn expands
|
---|
5179 | to @samp{z}. Here the name of the variable to reference is not stated
|
---|
5180 | explicitly; it is computed by expansion of @samp{$(x)}. The reference
|
---|
5181 | @samp{$(x)} here is nested within the outer variable reference.
|
---|
5182 |
|
---|
5183 | The previous example shows two levels of nesting, but any number of levels
|
---|
5184 | is possible. For example, here are three levels:
|
---|
5185 |
|
---|
5186 | @example
|
---|
5187 | x = y
|
---|
5188 | y = z
|
---|
5189 | z = u
|
---|
5190 | a := $($($(x)))
|
---|
5191 | @end example
|
---|
5192 |
|
---|
5193 | @noindent
|
---|
5194 | Here the innermost @samp{$(x)} expands to @samp{y}, so @samp{$($(x))}
|
---|
5195 | expands to @samp{$(y)} which in turn expands to @samp{z}; now we have
|
---|
5196 | @samp{$(z)}, which becomes @samp{u}.
|
---|
5197 |
|
---|
5198 | References to recursively-expanded variables within a variable name are
|
---|
5199 | reexpanded in the usual fashion. For example:
|
---|
5200 |
|
---|
5201 | @example
|
---|
5202 | x = $(y)
|
---|
5203 | y = z
|
---|
5204 | z = Hello
|
---|
5205 | a := $($(x))
|
---|
5206 | @end example
|
---|
5207 |
|
---|
5208 | @noindent
|
---|
5209 | defines @code{a} as @samp{Hello}: @samp{$($(x))} becomes @samp{$($(y))}
|
---|
5210 | which becomes @samp{$(z)} which becomes @samp{Hello}.
|
---|
5211 |
|
---|
5212 | Nested variable references can also contain modified references and
|
---|
5213 | function invocations (@pxref{Functions, ,Functions for Transforming Text}),
|
---|
5214 | just like any other reference.
|
---|
5215 | For example, using the @code{subst} function
|
---|
5216 | (@pxref{Text Functions, ,Functions for String Substitution and Analysis}):
|
---|
5217 |
|
---|
5218 | @example
|
---|
5219 | @group
|
---|
5220 | x = variable1
|
---|
5221 | variable2 := Hello
|
---|
5222 | y = $(subst 1,2,$(x))
|
---|
5223 | z = y
|
---|
5224 | a := $($($(z)))
|
---|
5225 | @end group
|
---|
5226 | @end example
|
---|
5227 |
|
---|
5228 | @noindent
|
---|
5229 | eventually defines @code{a} as @samp{Hello}. It is doubtful that anyone
|
---|
5230 | would ever want to write a nested reference as convoluted as this one, but
|
---|
5231 | it works: @samp{$($($(z)))} expands to @samp{$($(y))} which becomes
|
---|
5232 | @samp{$($(subst 1,2,$(x)))}. This gets the value @samp{variable1} from
|
---|
5233 | @code{x} and changes it by substitution to @samp{variable2}, so that the
|
---|
5234 | entire string becomes @samp{$(variable2)}, a simple variable reference
|
---|
5235 | whose value is @samp{Hello}.@refill
|
---|
5236 |
|
---|
5237 | A computed variable name need not consist entirely of a single variable
|
---|
5238 | reference. It can contain several variable references, as well as some
|
---|
5239 | invariant text. For example,
|
---|
5240 |
|
---|
5241 | @example
|
---|
5242 | @group
|
---|
5243 | a_dirs := dira dirb
|
---|
5244 | 1_dirs := dir1 dir2
|
---|
5245 | @end group
|
---|
5246 |
|
---|
5247 | @group
|
---|
5248 | a_files := filea fileb
|
---|
5249 | 1_files := file1 file2
|
---|
5250 | @end group
|
---|
5251 |
|
---|
5252 | @group
|
---|
5253 | ifeq "$(use_a)" "yes"
|
---|
5254 | a1 := a
|
---|
5255 | else
|
---|
5256 | a1 := 1
|
---|
5257 | endif
|
---|
5258 | @end group
|
---|
5259 |
|
---|
5260 | @group
|
---|
5261 | ifeq "$(use_dirs)" "yes"
|
---|
5262 | df := dirs
|
---|
5263 | else
|
---|
5264 | df := files
|
---|
5265 | endif
|
---|
5266 |
|
---|
5267 | dirs := $($(a1)_$(df))
|
---|
5268 | @end group
|
---|
5269 | @end example
|
---|
5270 |
|
---|
5271 | @noindent
|
---|
5272 | will give @code{dirs} the same value as @code{a_dirs}, @code{1_dirs},
|
---|
5273 | @code{a_files} or @code{1_files} depending on the settings of @code{use_a}
|
---|
5274 | and @code{use_dirs}.@refill
|
---|
5275 |
|
---|
5276 | Computed variable names can also be used in substitution references:
|
---|
5277 |
|
---|
5278 | @example
|
---|
5279 | @group
|
---|
5280 | a_objects := a.o b.o c.o
|
---|
5281 | 1_objects := 1.o 2.o 3.o
|
---|
5282 |
|
---|
5283 | sources := $($(a1)_objects:.o=.c)
|
---|
5284 | @end group
|
---|
5285 | @end example
|
---|
5286 |
|
---|
5287 | @noindent
|
---|
5288 | defines @code{sources} as either @samp{a.c b.c c.c} or @samp{1.c 2.c 3.c},
|
---|
5289 | depending on the value of @code{a1}.
|
---|
5290 |
|
---|
5291 | The only restriction on this sort of use of nested variable references
|
---|
5292 | is that they cannot specify part of the name of a function to be called.
|
---|
5293 | This is because the test for a recognized function name is done before
|
---|
5294 | the expansion of nested references. For example,
|
---|
5295 |
|
---|
5296 | @example
|
---|
5297 | @group
|
---|
5298 | ifdef do_sort
|
---|
5299 | func := sort
|
---|
5300 | else
|
---|
5301 | func := strip
|
---|
5302 | endif
|
---|
5303 | @end group
|
---|
5304 |
|
---|
5305 | @group
|
---|
5306 | bar := a d b g q c
|
---|
5307 | @end group
|
---|
5308 |
|
---|
5309 | @group
|
---|
5310 | foo := $($(func) $(bar))
|
---|
5311 | @end group
|
---|
5312 | @end example
|
---|
5313 |
|
---|
5314 | @noindent
|
---|
5315 | attempts to give @samp{foo} the value of the variable @samp{sort a d b g
|
---|
5316 | q c} or @samp{strip a d b g q c}, rather than giving @samp{a d b g q c}
|
---|
5317 | as the argument to either the @code{sort} or the @code{strip} function.
|
---|
5318 | This restriction could be removed in the future if that change is shown
|
---|
5319 | to be a good idea.
|
---|
5320 |
|
---|
5321 | You can also use computed variable names in the left-hand side of a
|
---|
5322 | variable assignment, or in a @code{define} directive, as in:
|
---|
5323 |
|
---|
5324 | @example
|
---|
5325 | dir = foo
|
---|
5326 | $(dir)_sources := $(wildcard $(dir)/*.c)
|
---|
5327 | define $(dir)_print
|
---|
5328 | lpr $($(dir)_sources)
|
---|
5329 | endef
|
---|
5330 | @end example
|
---|
5331 |
|
---|
5332 | @noindent
|
---|
5333 | This example defines the variables @samp{dir}, @samp{foo_sources}, and
|
---|
5334 | @samp{foo_print}.
|
---|
5335 |
|
---|
5336 | Note that @dfn{nested variable references} are quite different from
|
---|
5337 | @dfn{recursively expanded variables}
|
---|
5338 | (@pxref{Flavors, ,The Two Flavors of Variables}), though both are
|
---|
5339 | used together in complex ways when doing makefile programming.@refill
|
---|
5340 |
|
---|
5341 | @node Values, Setting, Advanced, Using Variables
|
---|
5342 | @section How Variables Get Their Values
|
---|
5343 | @cindex variables, how they get their values
|
---|
5344 | @cindex value, how a variable gets it
|
---|
5345 |
|
---|
5346 | Variables can get values in several different ways:
|
---|
5347 |
|
---|
5348 | @itemize @bullet
|
---|
5349 | @item
|
---|
5350 | You can specify an overriding value when you run @code{make}.
|
---|
5351 | @xref{Overriding, ,Overriding Variables}.
|
---|
5352 |
|
---|
5353 | @item
|
---|
5354 | You can specify a value in the makefile, either
|
---|
5355 | with an assignment (@pxref{Setting, ,Setting Variables}) or with a
|
---|
5356 | verbatim definition (@pxref{Defining, ,Defining Variables Verbatim}).@refill
|
---|
5357 |
|
---|
5358 | @item
|
---|
5359 | Variables in the environment become @code{make} variables.
|
---|
5360 | @xref{Environment, ,Variables from the Environment}.
|
---|
5361 |
|
---|
5362 | @item
|
---|
5363 | Several @dfn{automatic} variables are given new values for each rule.
|
---|
5364 | Each of these has a single conventional use.
|
---|
5365 | @xref{Automatic Variables}.
|
---|
5366 |
|
---|
5367 | @item
|
---|
5368 | Several variables have constant initial values.
|
---|
5369 | @xref{Implicit Variables, ,Variables Used by Implicit Rules}.
|
---|
5370 | @end itemize
|
---|
5371 |
|
---|
5372 | @node Setting, Appending, Values, Using Variables
|
---|
5373 | @section Setting Variables
|
---|
5374 | @cindex setting variables
|
---|
5375 | @cindex variables, setting
|
---|
5376 | @cindex =
|
---|
5377 | @cindex :=
|
---|
5378 | @cindex ?=
|
---|
5379 |
|
---|
5380 | To set a variable from the makefile, write a line starting with the
|
---|
5381 | variable name followed by @samp{=} or @samp{:=}. Whatever follows the
|
---|
5382 | @samp{=} or @samp{:=} on the line becomes the value. For example,
|
---|
5383 |
|
---|
5384 | @example
|
---|
5385 | objects = main.o foo.o bar.o utils.o
|
---|
5386 | @end example
|
---|
5387 |
|
---|
5388 | @noindent
|
---|
5389 | defines a variable named @code{objects}. Whitespace around the variable
|
---|
5390 | name and immediately after the @samp{=} is ignored.
|
---|
5391 |
|
---|
5392 | Variables defined with @samp{=} are @dfn{recursively expanded} variables.
|
---|
5393 | Variables defined with @samp{:=} are @dfn{simply expanded} variables; these
|
---|
5394 | definitions can contain variable references which will be expanded before
|
---|
5395 | the definition is made. @xref{Flavors, ,The Two Flavors of Variables}.
|
---|
5396 |
|
---|
5397 | The variable name may contain function and variable references, which
|
---|
5398 | are expanded when the line is read to find the actual variable name to use.
|
---|
5399 |
|
---|
5400 | There is no limit on the length of the value of a variable except the
|
---|
5401 | amount of swapping space on the computer. When a variable definition is
|
---|
5402 | long, it is a good idea to break it into several lines by inserting
|
---|
5403 | backslash-newline at convenient places in the definition. This will not
|
---|
5404 | affect the functioning of @code{make}, but it will make the makefile easier
|
---|
5405 | to read.
|
---|
5406 |
|
---|
5407 | Most variable names are considered to have the empty string as a value if
|
---|
5408 | you have never set them. Several variables have built-in initial values
|
---|
5409 | that are not empty, but you can set them in the usual ways
|
---|
5410 | (@pxref{Implicit Variables, ,Variables Used by Implicit Rules}).
|
---|
5411 | Several special variables are set
|
---|
5412 | automatically to a new value for each rule; these are called the
|
---|
5413 | @dfn{automatic} variables (@pxref{Automatic Variables}).
|
---|
5414 |
|
---|
5415 | If you'd like a variable to be set to a value only if it's not already
|
---|
5416 | set, then you can use the shorthand operator @samp{?=} instead of
|
---|
5417 | @samp{=}. These two settings of the variable @samp{FOO} are identical
|
---|
5418 | (@pxref{Origin Function, ,The @code{origin} Function}):
|
---|
5419 |
|
---|
5420 | @example
|
---|
5421 | FOO ?= bar
|
---|
5422 | @end example
|
---|
5423 |
|
---|
5424 | @noindent
|
---|
5425 | and
|
---|
5426 |
|
---|
5427 | @example
|
---|
5428 | ifeq ($(origin FOO), undefined)
|
---|
5429 | FOO = bar
|
---|
5430 | endif
|
---|
5431 | @end example
|
---|
5432 |
|
---|
5433 | @node Appending, Override Directive, Setting, Using Variables
|
---|
5434 | @section Appending More Text to Variables
|
---|
5435 | @cindex +=
|
---|
5436 | @cindex appending to variables
|
---|
5437 | @cindex variables, appending to
|
---|
5438 |
|
---|
5439 | Often it is useful to add more text to the value of a variable already defined.
|
---|
5440 | You do this with a line containing @samp{+=}, like this:
|
---|
5441 |
|
---|
5442 | @example
|
---|
5443 | objects += another.o
|
---|
5444 | @end example
|
---|
5445 |
|
---|
5446 | @noindent
|
---|
5447 | This takes the value of the variable @code{objects}, and adds the text
|
---|
5448 | @samp{another.o} to it (preceded by a single space). Thus:
|
---|
5449 |
|
---|
5450 | @example
|
---|
5451 | objects = main.o foo.o bar.o utils.o
|
---|
5452 | objects += another.o
|
---|
5453 | @end example
|
---|
5454 |
|
---|
5455 | @noindent
|
---|
5456 | sets @code{objects} to @samp{main.o foo.o bar.o utils.o another.o}.
|
---|
5457 |
|
---|
5458 | Using @samp{+=} is similar to:
|
---|
5459 |
|
---|
5460 | @example
|
---|
5461 | objects = main.o foo.o bar.o utils.o
|
---|
5462 | objects := $(objects) another.o
|
---|
5463 | @end example
|
---|
5464 |
|
---|
5465 | @noindent
|
---|
5466 | but differs in ways that become important when you use more complex values.
|
---|
5467 |
|
---|
5468 | When the variable in question has not been defined before, @samp{+=}
|
---|
5469 | acts just like normal @samp{=}: it defines a recursively-expanded
|
---|
5470 | variable. However, when there @emph{is} a previous definition, exactly
|
---|
5471 | what @samp{+=} does depends on what flavor of variable you defined
|
---|
5472 | originally. @xref{Flavors, ,The Two Flavors of Variables}, for an
|
---|
5473 | explanation of the two flavors of variables.
|
---|
5474 |
|
---|
5475 | When you add to a variable's value with @samp{+=}, @code{make} acts
|
---|
5476 | essentially as if you had included the extra text in the initial
|
---|
5477 | definition of the variable. If you defined it first with @samp{:=},
|
---|
5478 | making it a simply-expanded variable, @samp{+=} adds to that
|
---|
5479 | simply-expanded definition, and expands the new text before appending it
|
---|
5480 | to the old value just as @samp{:=} does
|
---|
5481 | (see @ref{Setting, ,Setting Variables}, for a full explanation of @samp{:=}).
|
---|
5482 | In fact,
|
---|
5483 |
|
---|
5484 | @example
|
---|
5485 | variable := value
|
---|
5486 | variable += more
|
---|
5487 | @end example
|
---|
5488 |
|
---|
5489 | @noindent
|
---|
5490 | is exactly equivalent to:
|
---|
5491 |
|
---|
5492 | @noindent
|
---|
5493 | @example
|
---|
5494 | variable := value
|
---|
5495 | variable := $(variable) more
|
---|
5496 | @end example
|
---|
5497 |
|
---|
5498 | On the other hand, when you use @samp{+=} with a variable that you defined
|
---|
5499 | first to be recursively-expanded using plain @samp{=}, @code{make} does
|
---|
5500 | something a bit different. Recall that when you define a
|
---|
5501 | recursively-expanded variable, @code{make} does not expand the value you set
|
---|
5502 | for variable and function references immediately. Instead it stores the text
|
---|
5503 | verbatim, and saves these variable and function references to be expanded
|
---|
5504 | later, when you refer to the new variable (@pxref{Flavors, ,The Two Flavors
|
---|
5505 | of Variables}). When you use @samp{+=} on a recursively-expanded variable,
|
---|
5506 | it is this unexpanded text to which @code{make} appends the new text you
|
---|
5507 | specify.
|
---|
5508 |
|
---|
5509 | @example
|
---|
5510 | @group
|
---|
5511 | variable = value
|
---|
5512 | variable += more
|
---|
5513 | @end group
|
---|
5514 | @end example
|
---|
5515 |
|
---|
5516 | @noindent
|
---|
5517 | is roughly equivalent to:
|
---|
5518 |
|
---|
5519 | @example
|
---|
5520 | @group
|
---|
5521 | temp = value
|
---|
5522 | variable = $(temp) more
|
---|
5523 | @end group
|
---|
5524 | @end example
|
---|
5525 |
|
---|
5526 | @noindent
|
---|
5527 | except that of course it never defines a variable called @code{temp}.
|
---|
5528 | The importance of this comes when the variable's old value contains
|
---|
5529 | variable references. Take this common example:
|
---|
5530 |
|
---|
5531 | @example
|
---|
5532 | CFLAGS = $(includes) -O
|
---|
5533 | @dots{}
|
---|
5534 | CFLAGS += -pg # enable profiling
|
---|
5535 | @end example
|
---|
5536 |
|
---|
5537 | @noindent
|
---|
5538 | The first line defines the @code{CFLAGS} variable with a reference to another
|
---|
5539 | variable, @code{includes}. (@code{CFLAGS} is used by the rules for C
|
---|
5540 | compilation; @pxref{Catalogue of Rules, ,Catalogue of Implicit Rules}.)
|
---|
5541 | Using @samp{=} for the definition makes @code{CFLAGS} a recursively-expanded
|
---|
5542 | variable, meaning @w{@samp{$(includes) -O}} is @emph{not} expanded when
|
---|
5543 | @code{make} processes the definition of @code{CFLAGS}. Thus, @code{includes}
|
---|
5544 | need not be defined yet for its value to take effect. It only has to be
|
---|
5545 | defined before any reference to @code{CFLAGS}. If we tried to append to the
|
---|
5546 | value of @code{CFLAGS} without using @samp{+=}, we might do it like this:
|
---|
5547 |
|
---|
5548 | @example
|
---|
5549 | CFLAGS := $(CFLAGS) -pg # enable profiling
|
---|
5550 | @end example
|
---|
5551 |
|
---|
5552 | @noindent
|
---|
5553 | This is pretty close, but not quite what we want. Using @samp{:=}
|
---|
5554 | redefines @code{CFLAGS} as a simply-expanded variable; this means
|
---|
5555 | @code{make} expands the text @w{@samp{$(CFLAGS) -pg}} before setting the
|
---|
5556 | variable. If @code{includes} is not yet defined, we get @w{@samp{ -O
|
---|
5557 | -pg}}, and a later definition of @code{includes} will have no effect.
|
---|
5558 | Conversely, by using @samp{+=} we set @code{CFLAGS} to the
|
---|
5559 | @emph{unexpanded} value @w{@samp{$(includes) -O -pg}}. Thus we preserve
|
---|
5560 | the reference to @code{includes}, so if that variable gets defined at
|
---|
5561 | any later point, a reference like @samp{$(CFLAGS)} still uses its
|
---|
5562 | value.
|
---|
5563 |
|
---|
5564 | @node Override Directive, Defining, Appending, Using Variables
|
---|
5565 | @section The @code{override} Directive
|
---|
5566 | @findex override
|
---|
5567 | @cindex overriding with @code{override}
|
---|
5568 | @cindex variables, overriding
|
---|
5569 |
|
---|
5570 | If a variable has been set with a command argument
|
---|
5571 | (@pxref{Overriding, ,Overriding Variables}),
|
---|
5572 | then ordinary assignments in the makefile are ignored. If you want to set
|
---|
5573 | the variable in the makefile even though it was set with a command
|
---|
5574 | argument, you can use an @code{override} directive, which is a line that
|
---|
5575 | looks like this:@refill
|
---|
5576 |
|
---|
5577 | @example
|
---|
5578 | override @var{variable} = @var{value}
|
---|
5579 | @end example
|
---|
5580 |
|
---|
5581 | @noindent
|
---|
5582 | or
|
---|
5583 |
|
---|
5584 | @example
|
---|
5585 | override @var{variable} := @var{value}
|
---|
5586 | @end example
|
---|
5587 |
|
---|
5588 | To append more text to a variable defined on the command line, use:
|
---|
5589 |
|
---|
5590 | @example
|
---|
5591 | override @var{variable} += @var{more text}
|
---|
5592 | @end example
|
---|
5593 |
|
---|
5594 | @noindent
|
---|
5595 | @xref{Appending, ,Appending More Text to Variables}.
|
---|
5596 |
|
---|
5597 | The @code{override} directive was not invented for escalation in the war
|
---|
5598 | between makefiles and command arguments. It was invented so you can alter
|
---|
5599 | and add to values that the user specifies with command arguments.
|
---|
5600 |
|
---|
5601 | For example, suppose you always want the @samp{-g} switch when you run the
|
---|
5602 | C compiler, but you would like to allow the user to specify the other
|
---|
5603 | switches with a command argument just as usual. You could use this
|
---|
5604 | @code{override} directive:
|
---|
5605 |
|
---|
5606 | @example
|
---|
5607 | override CFLAGS += -g
|
---|
5608 | @end example
|
---|
5609 |
|
---|
5610 | You can also use @code{override} directives with @code{define} directives.
|
---|
5611 | This is done as you might expect:
|
---|
5612 |
|
---|
5613 | @example
|
---|
5614 | override define foo
|
---|
5615 | bar
|
---|
5616 | endef
|
---|
5617 | @end example
|
---|
5618 |
|
---|
5619 | @noindent
|
---|
5620 | @iftex
|
---|
5621 | See the next section for information about @code{define}.
|
---|
5622 | @end iftex
|
---|
5623 | @ifnottex
|
---|
5624 | @xref{Defining, ,Defining Variables Verbatim}.
|
---|
5625 | @end ifnottex
|
---|
5626 |
|
---|
5627 | @node Defining, Environment, Override Directive, Using Variables
|
---|
5628 | @section Defining Variables Verbatim
|
---|
5629 | @findex define
|
---|
5630 | @findex endef
|
---|
5631 | @cindex verbatim variable definition
|
---|
5632 | @cindex defining variables verbatim
|
---|
5633 | @cindex variables, defining verbatim
|
---|
5634 |
|
---|
5635 | Another way to set the value of a variable is to use the @code{define}
|
---|
5636 | directive. This directive has an unusual syntax which allows newline
|
---|
5637 | characters to be included in the value, which is convenient for defining
|
---|
5638 | both canned sequences of commands
|
---|
5639 | (@pxref{Sequences, ,Defining Canned Command Sequences}), and also
|
---|
5640 | sections of makefile syntax to use with @code{eval} (@pxref{Eval Function}).
|
---|
5641 |
|
---|
5642 | The @code{define} directive is followed on the same line by the name of the
|
---|
5643 | variable and nothing more. The value to give the variable appears on the
|
---|
5644 | following lines. The end of the value is marked by a line containing just
|
---|
5645 | the word @code{endef}. Aside from this difference in syntax, @code{define}
|
---|
5646 | works just like @samp{=}: it creates a recursively-expanded variable
|
---|
5647 | (@pxref{Flavors, ,The Two Flavors of Variables}).
|
---|
5648 | The variable name may contain function and variable references, which
|
---|
5649 | are expanded when the directive is read to find the actual variable name
|
---|
5650 | to use.
|
---|
5651 |
|
---|
5652 | You may nest @code{define} directives: @code{make} will keep track of
|
---|
5653 | nested directives and report an error if they are not all properly
|
---|
5654 | closed with @code{endef}. Note that lines beginning with tab
|
---|
5655 | characters are considered part of a command script, so any
|
---|
5656 | @code{define} or @code{endef} strings appearing on such a line will
|
---|
5657 | not be considered @code{make} operators.
|
---|
5658 |
|
---|
5659 | @example
|
---|
5660 | define two-lines
|
---|
5661 | echo foo
|
---|
5662 | echo $(bar)
|
---|
5663 | endef
|
---|
5664 | @end example
|
---|
5665 |
|
---|
5666 | The value in an ordinary assignment cannot contain a newline; but the
|
---|
5667 | newlines that separate the lines of the value in a @code{define} become
|
---|
5668 | part of the variable's value (except for the final newline which precedes
|
---|
5669 | the @code{endef} and is not considered part of the value).@refill
|
---|
5670 |
|
---|
5671 | @need 800
|
---|
5672 | When used in a command script, the previous example is functionally
|
---|
5673 | equivalent to this:
|
---|
5674 |
|
---|
5675 | @example
|
---|
5676 | two-lines = echo foo; echo $(bar)
|
---|
5677 | @end example
|
---|
5678 |
|
---|
5679 | @noindent
|
---|
5680 | since two commands separated by semicolon behave much like two separate
|
---|
5681 | shell commands. However, note that using two separate lines means
|
---|
5682 | @code{make} will invoke the shell twice, running an independent subshell
|
---|
5683 | for each line. @xref{Execution, ,Command Execution}.
|
---|
5684 |
|
---|
5685 | If you want variable definitions made with @code{define} to take
|
---|
5686 | precedence over command-line variable definitions, you can use the
|
---|
5687 | @code{override} directive together with @code{define}:
|
---|
5688 |
|
---|
5689 | @example
|
---|
5690 | override define two-lines
|
---|
5691 | foo
|
---|
5692 | $(bar)
|
---|
5693 | endef
|
---|
5694 | @end example
|
---|
5695 |
|
---|
5696 | @noindent
|
---|
5697 | @xref{Override Directive, ,The @code{override} Directive}.
|
---|
5698 |
|
---|
5699 | @node Environment, Target-specific, Defining, Using Variables
|
---|
5700 | @section Variables from the Environment
|
---|
5701 |
|
---|
5702 | @cindex variables, environment
|
---|
5703 | @cindex environment
|
---|
5704 | Variables in @code{make} can come from the environment in which
|
---|
5705 | @code{make} is run. Every environment variable that @code{make} sees
|
---|
5706 | when it starts up is transformed into a @code{make} variable with the
|
---|
5707 | same name and value. However, an explicit assignment in the makefile,
|
---|
5708 | or with a command argument, overrides the environment. (If the
|
---|
5709 | @samp{-e} flag is specified, then values from the environment override
|
---|
5710 | assignments in the makefile. @xref{Options Summary, ,Summary of
|
---|
5711 | Options}. But this is not recommended practice.)
|
---|
5712 |
|
---|
5713 | Thus, by setting the variable @code{CFLAGS} in your environment, you can
|
---|
5714 | cause all C compilations in most makefiles to use the compiler switches you
|
---|
5715 | prefer. This is safe for variables with standard or conventional meanings
|
---|
5716 | because you know that no makefile will use them for other things. (Note
|
---|
5717 | this is not totally reliable; some makefiles set @code{CFLAGS} explicitly
|
---|
5718 | and therefore are not affected by the value in the environment.)
|
---|
5719 |
|
---|
5720 | When @code{make} runs a command script, variables defined in the
|
---|
5721 | makefile are placed into the environment of that command. This allows
|
---|
5722 | you to pass values to sub-@code{make} invocations (@pxref{Recursion,
|
---|
5723 | ,Recursive Use of @code{make}}). By default, only variables that came
|
---|
5724 | from the environment or the command line are passed to recursive
|
---|
5725 | invocations. You can use the @code{export} directive to pass other
|
---|
5726 | variables. @xref{Variables/Recursion, , Communicating Variables to a
|
---|
5727 | Sub-@code{make}}, for full details.
|
---|
5728 |
|
---|
5729 | Other use of variables from the environment is not recommended. It is not
|
---|
5730 | wise for makefiles to depend for their functioning on environment variables
|
---|
5731 | set up outside their control, since this would cause different users to get
|
---|
5732 | different results from the same makefile. This is against the whole
|
---|
5733 | purpose of most makefiles.
|
---|
5734 |
|
---|
5735 | @cindex SHELL, import from environment
|
---|
5736 | Such problems would be especially likely with the variable
|
---|
5737 | @code{SHELL}, which is normally present in the environment to specify
|
---|
5738 | the user's choice of interactive shell. It would be very undesirable
|
---|
5739 | for this choice to affect @code{make}; so, @code{make} handles the
|
---|
5740 | @code{SHELL} environment variable in a special way; see @ref{Choosing
|
---|
5741 | the Shell}.@refill
|
---|
5742 |
|
---|
5743 | @node Target-specific, Pattern-specific, Environment, Using Variables
|
---|
5744 | @section Target-specific Variable Values
|
---|
5745 | @cindex target-specific variables
|
---|
5746 | @cindex variables, target-specific
|
---|
5747 |
|
---|
5748 | Variable values in @code{make} are usually global; that is, they are the
|
---|
5749 | same regardless of where they are evaluated (unless they're reset, of
|
---|
5750 | course). One exception to that is automatic variables
|
---|
5751 | (@pxref{Automatic Variables}).
|
---|
5752 |
|
---|
5753 | The other exception is @dfn{target-specific variable values}. This
|
---|
5754 | feature allows you to define different values for the same variable,
|
---|
5755 | based on the target that @code{make} is currently building. As with
|
---|
5756 | automatic variables, these values are only available within the context
|
---|
5757 | of a target's command script (and in other target-specific assignments).
|
---|
5758 |
|
---|
5759 | Set a target-specific variable value like this:
|
---|
5760 |
|
---|
5761 | @example
|
---|
5762 | @var{target} @dots{} : @var{variable-assignment}
|
---|
5763 | @end example
|
---|
5764 |
|
---|
5765 | @noindent
|
---|
5766 | or like this:
|
---|
5767 |
|
---|
5768 | @example
|
---|
5769 | @var{target} @dots{} : override @var{variable-assignment}
|
---|
5770 | @end example
|
---|
5771 |
|
---|
5772 | @noindent
|
---|
5773 | or like this:
|
---|
5774 |
|
---|
5775 | @example
|
---|
5776 | @var{target} @dots{} : export @var{variable-assignment}
|
---|
5777 | @end example
|
---|
5778 |
|
---|
5779 | Multiple @var{target} values create a target-specific variable value for
|
---|
5780 | each member of the target list individually.
|
---|
5781 |
|
---|
5782 | The @var{variable-assignment} can be any valid form of assignment;
|
---|
5783 | recursive (@samp{=}), static (@samp{:=}), appending (@samp{+=}), or
|
---|
5784 | conditional (@samp{?=}). All variables that appear within the
|
---|
5785 | @var{variable-assignment} are evaluated within the context of the
|
---|
5786 | target: thus, any previously-defined target-specific variable values
|
---|
5787 | will be in effect. Note that this variable is actually distinct from
|
---|
5788 | any ``global'' value: the two variables do not have to have the same
|
---|
5789 | flavor (recursive vs.@: static).
|
---|
5790 |
|
---|
5791 | Target-specific variables have the same priority as any other makefile
|
---|
5792 | variable. Variables provided on the command-line (and in the
|
---|
5793 | environment if the @samp{-e} option is in force) will take precedence.
|
---|
5794 | Specifying the @code{override} directive will allow the target-specific
|
---|
5795 | variable value to be preferred.
|
---|
5796 |
|
---|
5797 | There is one more special feature of target-specific variables: when
|
---|
5798 | you define a target-specific variable that variable value is also in
|
---|
5799 | effect for all prerequisites of this target, and all their
|
---|
5800 | prerequisites, etc.@: (unless those prerequisites override that variable
|
---|
5801 | with their own target-specific variable value). So, for example, a
|
---|
5802 | statement like this:
|
---|
5803 |
|
---|
5804 | @example
|
---|
5805 | prog : CFLAGS = -g
|
---|
5806 | prog : prog.o foo.o bar.o
|
---|
5807 | @end example
|
---|
5808 |
|
---|
5809 | @noindent
|
---|
5810 | will set @code{CFLAGS} to @samp{-g} in the command script for
|
---|
5811 | @file{prog}, but it will also set @code{CFLAGS} to @samp{-g} in the
|
---|
5812 | command scripts that create @file{prog.o}, @file{foo.o}, and
|
---|
5813 | @file{bar.o}, and any command scripts which create their
|
---|
5814 | prerequisites.
|
---|
5815 |
|
---|
5816 | Be aware that a given prerequisite will only be built once per
|
---|
5817 | invocation of make, at most. If the same file is a prerequisite of
|
---|
5818 | multiple targets, and each of those targets has a different value for
|
---|
5819 | the same target-specific variable, then the first target to be built
|
---|
5820 | will cause that prerequisite to be built and the prerequisite will
|
---|
5821 | inherit the target-specific value from the first target. It will
|
---|
5822 | ignore the target-specific values from any other targets.
|
---|
5823 |
|
---|
5824 | @node Pattern-specific, , Target-specific, Using Variables
|
---|
5825 | @section Pattern-specific Variable Values
|
---|
5826 | @cindex pattern-specific variables
|
---|
5827 | @cindex variables, pattern-specific
|
---|
5828 |
|
---|
5829 | In addition to target-specific variable values
|
---|
5830 | (@pxref{Target-specific, ,Target-specific Variable Values}), GNU
|
---|
5831 | @code{make} supports pattern-specific variable values. In this form,
|
---|
5832 | the variable is defined for any target that matches the pattern
|
---|
5833 | specified. If a target matches more than one pattern, all the
|
---|
5834 | matching pattern-specific variables are interpreted in the order in
|
---|
5835 | which they were defined in the makefile, and collected together into
|
---|
5836 | one set. Variables defined in this way are searched after any
|
---|
5837 | target-specific variables defined explicitly for that target, and
|
---|
5838 | before target-specific variables defined for the parent target.
|
---|
5839 |
|
---|
5840 | Set a pattern-specific variable value like this:
|
---|
5841 |
|
---|
5842 | @example
|
---|
5843 | @var{pattern} @dots{} : @var{variable-assignment}
|
---|
5844 | @end example
|
---|
5845 |
|
---|
5846 | @noindent
|
---|
5847 | or like this:
|
---|
5848 |
|
---|
5849 | @example
|
---|
5850 | @var{pattern} @dots{} : override @var{variable-assignment}
|
---|
5851 | @end example
|
---|
5852 |
|
---|
5853 | @noindent
|
---|
5854 | where @var{pattern} is a %-pattern. As with target-specific variable
|
---|
5855 | values, multiple @var{pattern} values create a pattern-specific variable
|
---|
5856 | value for each pattern individually. The @var{variable-assignment} can
|
---|
5857 | be any valid form of assignment. Any command-line variable setting will
|
---|
5858 | take precedence, unless @code{override} is specified.
|
---|
5859 |
|
---|
5860 | For example:
|
---|
5861 |
|
---|
5862 | @example
|
---|
5863 | %.o : CFLAGS = -O
|
---|
5864 | @end example
|
---|
5865 |
|
---|
5866 | @noindent
|
---|
5867 | will assign @code{CFLAGS} the value of @samp{-O} for all targets
|
---|
5868 | matching the pattern @code{%.o}.
|
---|
5869 |
|
---|
5870 | @node Conditionals, Functions, Using Variables, Top
|
---|
5871 | @chapter Conditional Parts of Makefiles
|
---|
5872 |
|
---|
5873 | @cindex conditionals
|
---|
5874 | A @dfn{conditional} causes part of a makefile to be obeyed or ignored
|
---|
5875 | depending on the values of variables. Conditionals can compare the
|
---|
5876 | value of one variable to another, or the value of a variable to
|
---|
5877 | a constant string. Conditionals control what @code{make} actually
|
---|
5878 | ``sees'' in the makefile, so they @emph{cannot} be used to control shell
|
---|
5879 | commands at the time of execution.@refill
|
---|
5880 |
|
---|
5881 | @menu
|
---|
5882 | * Conditional Example:: Example of a conditional
|
---|
5883 | * Conditional Syntax:: The syntax of conditionals.
|
---|
5884 | * Testing Flags:: Conditionals that test flags.
|
---|
5885 | @end menu
|
---|
5886 |
|
---|
5887 | @node Conditional Example, Conditional Syntax, Conditionals, Conditionals
|
---|
5888 | @section Example of a Conditional
|
---|
5889 |
|
---|
5890 | The following example of a conditional tells @code{make} to use one set
|
---|
5891 | of libraries if the @code{CC} variable is @samp{gcc}, and a different
|
---|
5892 | set of libraries otherwise. It works by controlling which of two
|
---|
5893 | command lines will be used as the command for a rule. The result is
|
---|
5894 | that @samp{CC=gcc} as an argument to @code{make} changes not only which
|
---|
5895 | compiler is used but also which libraries are linked.
|
---|
5896 |
|
---|
5897 | @example
|
---|
5898 | libs_for_gcc = -lgnu
|
---|
5899 | normal_libs =
|
---|
5900 |
|
---|
5901 | foo: $(objects)
|
---|
5902 | ifeq ($(CC),gcc)
|
---|
5903 | $(CC) -o foo $(objects) $(libs_for_gcc)
|
---|
5904 | else
|
---|
5905 | $(CC) -o foo $(objects) $(normal_libs)
|
---|
5906 | endif
|
---|
5907 | @end example
|
---|
5908 |
|
---|
5909 | This conditional uses three directives: one @code{ifeq}, one @code{else}
|
---|
5910 | and one @code{endif}.
|
---|
5911 |
|
---|
5912 | The @code{ifeq} directive begins the conditional, and specifies the
|
---|
5913 | condition. It contains two arguments, separated by a comma and surrounded
|
---|
5914 | by parentheses. Variable substitution is performed on both arguments and
|
---|
5915 | then they are compared. The lines of the makefile following the
|
---|
5916 | @code{ifeq} are obeyed if the two arguments match; otherwise they are
|
---|
5917 | ignored.
|
---|
5918 |
|
---|
5919 | The @code{else} directive causes the following lines to be obeyed if the
|
---|
5920 | previous conditional failed. In the example above, this means that the
|
---|
5921 | second alternative linking command is used whenever the first alternative
|
---|
5922 | is not used. It is optional to have an @code{else} in a conditional.
|
---|
5923 |
|
---|
5924 | The @code{endif} directive ends the conditional. Every conditional must
|
---|
5925 | end with an @code{endif}. Unconditional makefile text follows.
|
---|
5926 |
|
---|
5927 | As this example illustrates, conditionals work at the textual level:
|
---|
5928 | the lines of the conditional are treated as part of the makefile, or
|
---|
5929 | ignored, according to the condition. This is why the larger syntactic
|
---|
5930 | units of the makefile, such as rules, may cross the beginning or the
|
---|
5931 | end of the conditional.
|
---|
5932 |
|
---|
5933 | When the variable @code{CC} has the value @samp{gcc}, the above example has
|
---|
5934 | this effect:
|
---|
5935 |
|
---|
5936 | @example
|
---|
5937 | foo: $(objects)
|
---|
5938 | $(CC) -o foo $(objects) $(libs_for_gcc)
|
---|
5939 | @end example
|
---|
5940 |
|
---|
5941 | @noindent
|
---|
5942 | When the variable @code{CC} has any other value, the effect is this:
|
---|
5943 |
|
---|
5944 | @example
|
---|
5945 | foo: $(objects)
|
---|
5946 | $(CC) -o foo $(objects) $(normal_libs)
|
---|
5947 | @end example
|
---|
5948 |
|
---|
5949 | Equivalent results can be obtained in another way by conditionalizing a
|
---|
5950 | variable assignment and then using the variable unconditionally:
|
---|
5951 |
|
---|
5952 | @example
|
---|
5953 | libs_for_gcc = -lgnu
|
---|
5954 | normal_libs =
|
---|
5955 |
|
---|
5956 | ifeq ($(CC),gcc)
|
---|
5957 | libs=$(libs_for_gcc)
|
---|
5958 | else
|
---|
5959 | libs=$(normal_libs)
|
---|
5960 | endif
|
---|
5961 |
|
---|
5962 | foo: $(objects)
|
---|
5963 | $(CC) -o foo $(objects) $(libs)
|
---|
5964 | @end example
|
---|
5965 |
|
---|
5966 | @node Conditional Syntax, Testing Flags, Conditional Example, Conditionals
|
---|
5967 | @section Syntax of Conditionals
|
---|
5968 | @findex ifdef
|
---|
5969 | @findex ifeq
|
---|
5970 | @findex ifndef
|
---|
5971 | @findex ifneq
|
---|
5972 | @findex else
|
---|
5973 | @findex endif
|
---|
5974 |
|
---|
5975 | The syntax of a simple conditional with no @code{else} is as follows:
|
---|
5976 |
|
---|
5977 | @example
|
---|
5978 | @var{conditional-directive}
|
---|
5979 | @var{text-if-true}
|
---|
5980 | endif
|
---|
5981 | @end example
|
---|
5982 |
|
---|
5983 | @noindent
|
---|
5984 | The @var{text-if-true} may be any lines of text, to be considered as part
|
---|
5985 | of the makefile if the condition is true. If the condition is false, no
|
---|
5986 | text is used instead.
|
---|
5987 |
|
---|
5988 | The syntax of a complex conditional is as follows:
|
---|
5989 |
|
---|
5990 | @example
|
---|
5991 | @var{conditional-directive}
|
---|
5992 | @var{text-if-true}
|
---|
5993 | else
|
---|
5994 | @var{text-if-false}
|
---|
5995 | endif
|
---|
5996 | @end example
|
---|
5997 |
|
---|
5998 | or:
|
---|
5999 |
|
---|
6000 | @example
|
---|
6001 | @var{conditional-directive}
|
---|
6002 | @var{text-if-one-is-true}
|
---|
6003 | else @var{conditional-directive}
|
---|
6004 | @var{text-if-true}
|
---|
6005 | else
|
---|
6006 | @var{text-if-false}
|
---|
6007 | endif
|
---|
6008 | @end example
|
---|
6009 |
|
---|
6010 | @noindent
|
---|
6011 | There can be as many ``@code{else} @var{conditional-directive}''
|
---|
6012 | clauses as necessary. Once a given condition is true,
|
---|
6013 | @var{text-if-true} is used and no other clause is used; if no
|
---|
6014 | condition is true then @var{text-if-false} is used. The
|
---|
6015 | @var{text-if-true} and @var{text-if-false} can be any number of lines
|
---|
6016 | of text.
|
---|
6017 |
|
---|
6018 | The syntax of the @var{conditional-directive} is the same whether the
|
---|
6019 | conditional is simple or complex; after an @code{else} or not. There
|
---|
6020 | are four different directives that test different conditions. Here is
|
---|
6021 | a table of them:
|
---|
6022 |
|
---|
6023 | @table @code
|
---|
6024 | @item ifeq (@var{arg1}, @var{arg2})
|
---|
6025 | @itemx ifeq '@var{arg1}' '@var{arg2}'
|
---|
6026 | @itemx ifeq "@var{arg1}" "@var{arg2}"
|
---|
6027 | @itemx ifeq "@var{arg1}" '@var{arg2}'
|
---|
6028 | @itemx ifeq '@var{arg1}' "@var{arg2}"
|
---|
6029 | Expand all variable references in @var{arg1} and @var{arg2} and
|
---|
6030 | compare them. If they are identical, the @var{text-if-true} is
|
---|
6031 | effective; otherwise, the @var{text-if-false}, if any, is effective.
|
---|
6032 |
|
---|
6033 | Often you want to test if a variable has a non-empty value. When the
|
---|
6034 | value results from complex expansions of variables and functions,
|
---|
6035 | expansions you would consider empty may actually contain whitespace
|
---|
6036 | characters and thus are not seen as empty. However, you can use the
|
---|
6037 | @code{strip} function (@pxref{Text Functions}) to avoid interpreting
|
---|
6038 | whitespace as a non-empty value. For example:
|
---|
6039 |
|
---|
6040 | @example
|
---|
6041 | @group
|
---|
6042 | ifeq ($(strip $(foo)),)
|
---|
6043 | @var{text-if-empty}
|
---|
6044 | endif
|
---|
6045 | @end group
|
---|
6046 | @end example
|
---|
6047 |
|
---|
6048 | @noindent
|
---|
6049 | will evaluate @var{text-if-empty} even if the expansion of
|
---|
6050 | @code{$(foo)} contains whitespace characters.
|
---|
6051 |
|
---|
6052 | @item ifneq (@var{arg1}, @var{arg2})
|
---|
6053 | @itemx ifneq '@var{arg1}' '@var{arg2}'
|
---|
6054 | @itemx ifneq "@var{arg1}" "@var{arg2}"
|
---|
6055 | @itemx ifneq "@var{arg1}" '@var{arg2}'
|
---|
6056 | @itemx ifneq '@var{arg1}' "@var{arg2}"
|
---|
6057 | Expand all variable references in @var{arg1} and @var{arg2} and
|
---|
6058 | compare them. If they are different, the @var{text-if-true} is
|
---|
6059 | effective; otherwise, the @var{text-if-false}, if any, is effective.
|
---|
6060 |
|
---|
6061 | @item ifdef @var{variable-name}
|
---|
6062 | The @code{ifdef} form takes the @emph{name} of a variable as its
|
---|
6063 | argument, not a reference to a variable. The value of that variable
|
---|
6064 | has a non-empty value, the @var{text-if-true} is effective; otherwise,
|
---|
6065 | the @var{text-if-false}, if any, is effective. Variables that have
|
---|
6066 | never been defined have an empty value. The text @var{variable-name}
|
---|
6067 | is expanded, so it could be a variable or function that expands
|
---|
6068 | to the name of a variable. For example:
|
---|
6069 |
|
---|
6070 | @example
|
---|
6071 | bar = true
|
---|
6072 | foo = bar
|
---|
6073 | ifdef $(foo)
|
---|
6074 | frobozz = yes
|
---|
6075 | endif
|
---|
6076 | @end example
|
---|
6077 |
|
---|
6078 | The variable reference @code{$(foo)} is expanded, yielding @code{bar},
|
---|
6079 | which is considered to be the name of a variable. The variable
|
---|
6080 | @code{bar} is not expanded, but its value is examined to determine if
|
---|
6081 | it is non-empty.
|
---|
6082 |
|
---|
6083 | Note that @code{ifdef} only tests whether a variable has a value. It
|
---|
6084 | does not expand the variable to see if that value is nonempty.
|
---|
6085 | Consequently, tests using @code{ifdef} return true for all definitions
|
---|
6086 | except those like @code{foo =}. To test for an empty value, use
|
---|
6087 | @w{@code{ifeq ($(foo),)}}. For example,
|
---|
6088 |
|
---|
6089 | @example
|
---|
6090 | bar =
|
---|
6091 | foo = $(bar)
|
---|
6092 | ifdef foo
|
---|
6093 | frobozz = yes
|
---|
6094 | else
|
---|
6095 | frobozz = no
|
---|
6096 | endif
|
---|
6097 | @end example
|
---|
6098 |
|
---|
6099 | @noindent
|
---|
6100 | sets @samp{frobozz} to @samp{yes}, while:
|
---|
6101 |
|
---|
6102 | @example
|
---|
6103 | foo =
|
---|
6104 | ifdef foo
|
---|
6105 | frobozz = yes
|
---|
6106 | else
|
---|
6107 | frobozz = no
|
---|
6108 | endif
|
---|
6109 | @end example
|
---|
6110 |
|
---|
6111 | @noindent
|
---|
6112 | sets @samp{frobozz} to @samp{no}.
|
---|
6113 |
|
---|
6114 | @item ifndef @var{variable-name}
|
---|
6115 | If the variable @var{variable-name} has an empty value, the
|
---|
6116 | @var{text-if-true} is effective; otherwise, the @var{text-if-false},
|
---|
6117 | if any, is effective. The rules for expansion and testing of
|
---|
6118 | @var{variable-name} are identical to the @code{ifdef} directive.
|
---|
6119 | @end table
|
---|
6120 |
|
---|
6121 | Extra spaces are allowed and ignored at the beginning of the conditional
|
---|
6122 | directive line, but a tab is not allowed. (If the line begins with a tab,
|
---|
6123 | it will be considered a command for a rule.) Aside from this, extra spaces
|
---|
6124 | or tabs may be inserted with no effect anywhere except within the directive
|
---|
6125 | name or within an argument. A comment starting with @samp{#} may appear at
|
---|
6126 | the end of the line.
|
---|
6127 |
|
---|
6128 | The other two directives that play a part in a conditional are @code{else}
|
---|
6129 | and @code{endif}. Each of these directives is written as one word, with no
|
---|
6130 | arguments. Extra spaces are allowed and ignored at the beginning of the
|
---|
6131 | line, and spaces or tabs at the end. A comment starting with @samp{#} may
|
---|
6132 | appear at the end of the line.
|
---|
6133 |
|
---|
6134 | Conditionals affect which lines of the makefile @code{make} uses. If
|
---|
6135 | the condition is true, @code{make} reads the lines of the
|
---|
6136 | @var{text-if-true} as part of the makefile; if the condition is false,
|
---|
6137 | @code{make} ignores those lines completely. It follows that syntactic
|
---|
6138 | units of the makefile, such as rules, may safely be split across the
|
---|
6139 | beginning or the end of the conditional.@refill
|
---|
6140 |
|
---|
6141 | @code{make} evaluates conditionals when it reads a makefile.
|
---|
6142 | Consequently, you cannot use automatic variables in the tests of
|
---|
6143 | conditionals because they are not defined until commands are run
|
---|
6144 | (@pxref{Automatic Variables}).
|
---|
6145 |
|
---|
6146 | To prevent intolerable confusion, it is not permitted to start a
|
---|
6147 | conditional in one makefile and end it in another. However, you may
|
---|
6148 | write an @code{include} directive within a conditional, provided you do
|
---|
6149 | not attempt to terminate the conditional inside the included file.
|
---|
6150 |
|
---|
6151 | @node Testing Flags, , Conditional Syntax, Conditionals
|
---|
6152 | @section Conditionals that Test Flags
|
---|
6153 |
|
---|
6154 | You can write a conditional that tests @code{make} command flags such as
|
---|
6155 | @samp{-t} by using the variable @code{MAKEFLAGS} together with the
|
---|
6156 | @code{findstring} function
|
---|
6157 | (@pxref{Text Functions, , Functions for String Substitution and Analysis}).
|
---|
6158 | This is useful when @code{touch} is not enough to make a file appear up
|
---|
6159 | to date.
|
---|
6160 |
|
---|
6161 | The @code{findstring} function determines whether one string appears as a
|
---|
6162 | substring of another. If you want to test for the @samp{-t} flag,
|
---|
6163 | use @samp{t} as the first string and the value of @code{MAKEFLAGS} as
|
---|
6164 | the other.
|
---|
6165 |
|
---|
6166 | For example, here is how to arrange to use @samp{ranlib -t} to finish
|
---|
6167 | marking an archive file up to date:
|
---|
6168 |
|
---|
6169 | @example
|
---|
6170 | archive.a: @dots{}
|
---|
6171 | ifneq (,$(findstring t,$(MAKEFLAGS)))
|
---|
6172 | +touch archive.a
|
---|
6173 | +ranlib -t archive.a
|
---|
6174 | else
|
---|
6175 | ranlib archive.a
|
---|
6176 | endif
|
---|
6177 | @end example
|
---|
6178 |
|
---|
6179 | @noindent
|
---|
6180 | The @samp{+} prefix marks those command lines as ``recursive'' so
|
---|
6181 | that they will be executed despite use of the @samp{-t} flag.
|
---|
6182 | @xref{Recursion, ,Recursive Use of @code{make}}.
|
---|
6183 |
|
---|
6184 | @node Functions, Running, Conditionals, Top
|
---|
6185 | @chapter Functions for Transforming Text
|
---|
6186 | @cindex functions
|
---|
6187 |
|
---|
6188 | @dfn{Functions} allow you to do text processing in the makefile to compute
|
---|
6189 | the files to operate on or the commands to use. You use a function in a
|
---|
6190 | @dfn{function call}, where you give the name of the function and some text
|
---|
6191 | (the @dfn{arguments}) for the function to operate on. The result of the
|
---|
6192 | function's processing is substituted into the makefile at the point of the
|
---|
6193 | call, just as a variable might be substituted.
|
---|
6194 |
|
---|
6195 | @menu
|
---|
6196 | * Syntax of Functions:: How to write a function call.
|
---|
6197 | * Text Functions:: General-purpose text manipulation functions.
|
---|
6198 | * File Name Functions:: Functions for manipulating file names.
|
---|
6199 | * Conditional Functions:: Functions that implement conditions.
|
---|
6200 | * Foreach Function:: Repeat some text with controlled variation.
|
---|
6201 | * Call Function:: Expand a user-defined function.
|
---|
6202 | * Value Function:: Return the un-expanded value of a variable.
|
---|
6203 | * Eval Function:: Evaluate the arguments as makefile syntax.
|
---|
6204 | * Origin Function:: Find where a variable got its value.
|
---|
6205 | * Flavor Function:: Find out the flavor of a variable.
|
---|
6206 | * Shell Function:: Substitute the output of a shell command.
|
---|
6207 | * Make Control Functions:: Functions that control how make runs.
|
---|
6208 | @end menu
|
---|
6209 |
|
---|
6210 | @node Syntax of Functions, Text Functions, Functions, Functions
|
---|
6211 | @section Function Call Syntax
|
---|
6212 | @cindex @code{$}, in function call
|
---|
6213 | @cindex dollar sign (@code{$}), in function call
|
---|
6214 | @cindex arguments of functions
|
---|
6215 | @cindex functions, syntax of
|
---|
6216 |
|
---|
6217 | A function call resembles a variable reference. It looks like this:
|
---|
6218 |
|
---|
6219 | @example
|
---|
6220 | $(@var{function} @var{arguments})
|
---|
6221 | @end example
|
---|
6222 |
|
---|
6223 | @noindent
|
---|
6224 | or like this:
|
---|
6225 |
|
---|
6226 | @example
|
---|
6227 | $@{@var{function} @var{arguments}@}
|
---|
6228 | @end example
|
---|
6229 |
|
---|
6230 | Here @var{function} is a function name; one of a short list of names
|
---|
6231 | that are part of @code{make}. You can also essentially create your own
|
---|
6232 | functions by using the @code{call} builtin function.
|
---|
6233 |
|
---|
6234 | The @var{arguments} are the arguments of the function. They are
|
---|
6235 | separated from the function name by one or more spaces or tabs, and if
|
---|
6236 | there is more than one argument, then they are separated by commas.
|
---|
6237 | Such whitespace and commas are not part of an argument's value. The
|
---|
6238 | delimiters which you use to surround the function call, whether
|
---|
6239 | parentheses or braces, can appear in an argument only in matching pairs;
|
---|
6240 | the other kind of delimiters may appear singly. If the arguments
|
---|
6241 | themselves contain other function calls or variable references, it is
|
---|
6242 | wisest to use the same kind of delimiters for all the references; write
|
---|
6243 | @w{@samp{$(subst a,b,$(x))}}, not @w{@samp{$(subst a,b,$@{x@})}}. This
|
---|
6244 | is because it is clearer, and because only one type of delimiter is
|
---|
6245 | matched to find the end of the reference.
|
---|
6246 |
|
---|
6247 | The text written for each argument is processed by substitution of
|
---|
6248 | variables and function calls to produce the argument value, which
|
---|
6249 | is the text on which the function acts. The substitution is done in the
|
---|
6250 | order in which the arguments appear.
|
---|
6251 |
|
---|
6252 | Commas and unmatched parentheses or braces cannot appear in the text of an
|
---|
6253 | argument as written; leading spaces cannot appear in the text of the first
|
---|
6254 | argument as written. These characters can be put into the argument value
|
---|
6255 | by variable substitution. First define variables @code{comma} and
|
---|
6256 | @code{space} whose values are isolated comma and space characters, then
|
---|
6257 | substitute these variables where such characters are wanted, like this:
|
---|
6258 |
|
---|
6259 | @example
|
---|
6260 | @group
|
---|
6261 | comma:= ,
|
---|
6262 | empty:=
|
---|
6263 | space:= $(empty) $(empty)
|
---|
6264 | foo:= a b c
|
---|
6265 | bar:= $(subst $(space),$(comma),$(foo))
|
---|
6266 | # @r{bar is now `a,b,c'.}
|
---|
6267 | @end group
|
---|
6268 | @end example
|
---|
6269 |
|
---|
6270 | @noindent
|
---|
6271 | Here the @code{subst} function replaces each space with a comma, through
|
---|
6272 | the value of @code{foo}, and substitutes the result.
|
---|
6273 |
|
---|
6274 | @node Text Functions, File Name Functions, Syntax of Functions, Functions
|
---|
6275 | @section Functions for String Substitution and Analysis
|
---|
6276 | @cindex functions, for text
|
---|
6277 |
|
---|
6278 | Here are some functions that operate on strings:
|
---|
6279 |
|
---|
6280 | @table @code
|
---|
6281 | @item $(subst @var{from},@var{to},@var{text})
|
---|
6282 | @findex subst
|
---|
6283 | Performs a textual replacement on the text @var{text}: each occurrence
|
---|
6284 | of @var{from} is replaced by @var{to}. The result is substituted for
|
---|
6285 | the function call. For example,
|
---|
6286 |
|
---|
6287 | @example
|
---|
6288 | $(subst ee,EE,feet on the street)
|
---|
6289 | @end example
|
---|
6290 |
|
---|
6291 | substitutes the string @samp{fEEt on the strEEt}.
|
---|
6292 |
|
---|
6293 | @item $(patsubst @var{pattern},@var{replacement},@var{text})
|
---|
6294 | @findex patsubst
|
---|
6295 | Finds whitespace-separated words in @var{text} that match
|
---|
6296 | @var{pattern} and replaces them with @var{replacement}. Here
|
---|
6297 | @var{pattern} may contain a @samp{%} which acts as a wildcard,
|
---|
6298 | matching any number of any characters within a word. If
|
---|
6299 | @var{replacement} also contains a @samp{%}, the @samp{%} is replaced
|
---|
6300 | by the text that matched the @samp{%} in @var{pattern}. Only the first
|
---|
6301 | @samp{%} in the @var{pattern} and @var{replacement} is treated this
|
---|
6302 | way; any subsequent @samp{%} is unchanged.@refill
|
---|
6303 |
|
---|
6304 | @cindex @code{%}, quoting in @code{patsubst}
|
---|
6305 | @cindex @code{%}, quoting with @code{\} (backslash)
|
---|
6306 | @cindex @code{\} (backslash), to quote @code{%}
|
---|
6307 | @cindex backslash (@code{\}), to quote @code{%}
|
---|
6308 | @cindex quoting @code{%}, in @code{patsubst}
|
---|
6309 | @samp{%} characters in @code{patsubst} function invocations can be
|
---|
6310 | quoted with preceding backslashes (@samp{\}). Backslashes that would
|
---|
6311 | otherwise quote @samp{%} characters can be quoted with more backslashes.
|
---|
6312 | Backslashes that quote @samp{%} characters or other backslashes are
|
---|
6313 | removed from the pattern before it is compared file names or has a stem
|
---|
6314 | substituted into it. Backslashes that are not in danger of quoting
|
---|
6315 | @samp{%} characters go unmolested. For example, the pattern
|
---|
6316 | @file{the\%weird\\%pattern\\} has @samp{the%weird\} preceding the
|
---|
6317 | operative @samp{%} character, and @samp{pattern\\} following it. The
|
---|
6318 | final two backslashes are left alone because they cannot affect any
|
---|
6319 | @samp{%} character.@refill
|
---|
6320 |
|
---|
6321 | Whitespace between words is folded into single space characters;
|
---|
6322 | leading and trailing whitespace is discarded.
|
---|
6323 |
|
---|
6324 | For example,
|
---|
6325 |
|
---|
6326 | @example
|
---|
6327 | $(patsubst %.c,%.o,x.c.c bar.c)
|
---|
6328 | @end example
|
---|
6329 |
|
---|
6330 | @noindent
|
---|
6331 | produces the value @samp{x.c.o bar.o}.
|
---|
6332 |
|
---|
6333 | Substitution references (@pxref{Substitution Refs, ,Substitution
|
---|
6334 | References}) are a simpler way to get the effect of the @code{patsubst}
|
---|
6335 | function:
|
---|
6336 |
|
---|
6337 | @example
|
---|
6338 | $(@var{var}:@var{pattern}=@var{replacement})
|
---|
6339 | @end example
|
---|
6340 |
|
---|
6341 | @noindent
|
---|
6342 | is equivalent to
|
---|
6343 |
|
---|
6344 | @example
|
---|
6345 | $(patsubst @var{pattern},@var{replacement},$(@var{var}))
|
---|
6346 | @end example
|
---|
6347 |
|
---|
6348 | The second shorthand simplifies one of the most common uses of
|
---|
6349 | @code{patsubst}: replacing the suffix at the end of file names.
|
---|
6350 |
|
---|
6351 | @example
|
---|
6352 | $(@var{var}:@var{suffix}=@var{replacement})
|
---|
6353 | @end example
|
---|
6354 |
|
---|
6355 | @noindent
|
---|
6356 | is equivalent to
|
---|
6357 |
|
---|
6358 | @example
|
---|
6359 | $(patsubst %@var{suffix},%@var{replacement},$(@var{var}))
|
---|
6360 | @end example
|
---|
6361 |
|
---|
6362 | @noindent
|
---|
6363 | For example, you might have a list of object files:
|
---|
6364 |
|
---|
6365 | @example
|
---|
6366 | objects = foo.o bar.o baz.o
|
---|
6367 | @end example
|
---|
6368 |
|
---|
6369 | @noindent
|
---|
6370 | To get the list of corresponding source files, you could simply write:
|
---|
6371 |
|
---|
6372 | @example
|
---|
6373 | $(objects:.o=.c)
|
---|
6374 | @end example
|
---|
6375 |
|
---|
6376 | @noindent
|
---|
6377 | instead of using the general form:
|
---|
6378 |
|
---|
6379 | @example
|
---|
6380 | $(patsubst %.o,%.c,$(objects))
|
---|
6381 | @end example
|
---|
6382 |
|
---|
6383 | @item $(strip @var{string})
|
---|
6384 | @cindex stripping whitespace
|
---|
6385 | @cindex whitespace, stripping
|
---|
6386 | @cindex spaces, stripping
|
---|
6387 | @findex strip
|
---|
6388 | Removes leading and trailing whitespace from @var{string} and replaces
|
---|
6389 | each internal sequence of one or more whitespace characters with a
|
---|
6390 | single space. Thus, @samp{$(strip a b c )} results in @w{@samp{a b c}}.
|
---|
6391 |
|
---|
6392 | The function @code{strip} can be very useful when used in conjunction
|
---|
6393 | with conditionals. When comparing something with the empty string
|
---|
6394 | @samp{} using @code{ifeq} or @code{ifneq}, you usually want a string of
|
---|
6395 | just whitespace to match the empty string (@pxref{Conditionals}).
|
---|
6396 |
|
---|
6397 | Thus, the following may fail to have the desired results:
|
---|
6398 |
|
---|
6399 | @example
|
---|
6400 | .PHONY: all
|
---|
6401 | ifneq "$(needs_made)" ""
|
---|
6402 | all: $(needs_made)
|
---|
6403 | else
|
---|
6404 | all:;@@echo 'Nothing to make!'
|
---|
6405 | endif
|
---|
6406 | @end example
|
---|
6407 |
|
---|
6408 | @noindent
|
---|
6409 | Replacing the variable reference @w{@samp{$(needs_made)}} with the
|
---|
6410 | function call @w{@samp{$(strip $(needs_made))}} in the @code{ifneq}
|
---|
6411 | directive would make it more robust.@refill
|
---|
6412 |
|
---|
6413 | @item $(findstring @var{find},@var{in})
|
---|
6414 | @findex findstring
|
---|
6415 | @cindex searching for strings
|
---|
6416 | @cindex finding strings
|
---|
6417 | @cindex strings, searching for
|
---|
6418 | Searches @var{in} for an occurrence of @var{find}. If it occurs, the
|
---|
6419 | value is @var{find}; otherwise, the value is empty. You can use this
|
---|
6420 | function in a conditional to test for the presence of a specific
|
---|
6421 | substring in a given string. Thus, the two examples,
|
---|
6422 |
|
---|
6423 | @example
|
---|
6424 | $(findstring a,a b c)
|
---|
6425 | $(findstring a,b c)
|
---|
6426 | @end example
|
---|
6427 |
|
---|
6428 | @noindent
|
---|
6429 | produce the values @samp{a} and @samp{} (the empty string),
|
---|
6430 | respectively. @xref{Testing Flags}, for a practical application of
|
---|
6431 | @code{findstring}.@refill
|
---|
6432 |
|
---|
6433 | @need 750
|
---|
6434 | @findex filter
|
---|
6435 | @cindex filtering words
|
---|
6436 | @cindex words, filtering
|
---|
6437 | @item $(filter @var{pattern}@dots{},@var{text})
|
---|
6438 | Returns all whitespace-separated words in @var{text} that @emph{do} match
|
---|
6439 | any of the @var{pattern} words, removing any words that @emph{do not}
|
---|
6440 | match. The patterns are written using @samp{%}, just like the patterns
|
---|
6441 | used in the @code{patsubst} function above.@refill
|
---|
6442 |
|
---|
6443 | The @code{filter} function can be used to separate out different types
|
---|
6444 | of strings (such as file names) in a variable. For example:
|
---|
6445 |
|
---|
6446 | @example
|
---|
6447 | sources := foo.c bar.c baz.s ugh.h
|
---|
6448 | foo: $(sources)
|
---|
6449 | cc $(filter %.c %.s,$(sources)) -o foo
|
---|
6450 | @end example
|
---|
6451 |
|
---|
6452 | @noindent
|
---|
6453 | says that @file{foo} depends of @file{foo.c}, @file{bar.c},
|
---|
6454 | @file{baz.s} and @file{ugh.h} but only @file{foo.c}, @file{bar.c} and
|
---|
6455 | @file{baz.s} should be specified in the command to the
|
---|
6456 | compiler.@refill
|
---|
6457 |
|
---|
6458 | @item $(filter-out @var{pattern}@dots{},@var{text})
|
---|
6459 | @findex filter-out
|
---|
6460 | @cindex filtering out words
|
---|
6461 | @cindex words, filtering out
|
---|
6462 | Returns all whitespace-separated words in @var{text} that @emph{do not}
|
---|
6463 | match any of the @var{pattern} words, removing the words that @emph{do}
|
---|
6464 | match one or more. This is the exact opposite of the @code{filter}
|
---|
6465 | function.@refill
|
---|
6466 |
|
---|
6467 | For example, given:
|
---|
6468 |
|
---|
6469 | @example
|
---|
6470 | @group
|
---|
6471 | objects=main1.o foo.o main2.o bar.o
|
---|
6472 | mains=main1.o main2.o
|
---|
6473 | @end group
|
---|
6474 | @end example
|
---|
6475 |
|
---|
6476 | @noindent
|
---|
6477 | the following generates a list which contains all the object files not
|
---|
6478 | in @samp{mains}:
|
---|
6479 |
|
---|
6480 | @example
|
---|
6481 | $(filter-out $(mains),$(objects))
|
---|
6482 | @end example
|
---|
6483 |
|
---|
6484 | @need 1500
|
---|
6485 | @findex sort
|
---|
6486 | @cindex sorting words
|
---|
6487 | @item $(sort @var{list})
|
---|
6488 | Sorts the words of @var{list} in lexical order, removing duplicate
|
---|
6489 | words. The output is a list of words separated by single spaces.
|
---|
6490 | Thus,
|
---|
6491 |
|
---|
6492 | @example
|
---|
6493 | $(sort foo bar lose)
|
---|
6494 | @end example
|
---|
6495 |
|
---|
6496 | @noindent
|
---|
6497 | returns the value @samp{bar foo lose}.
|
---|
6498 |
|
---|
6499 | @cindex removing duplicate words
|
---|
6500 | @cindex duplicate words, removing
|
---|
6501 | @cindex words, removing duplicates
|
---|
6502 | Incidentally, since @code{sort} removes duplicate words, you can use
|
---|
6503 | it for this purpose even if you don't care about the sort order.
|
---|
6504 |
|
---|
6505 | @item $(word @var{n},@var{text})
|
---|
6506 | @findex word
|
---|
6507 | @cindex word, selecting a
|
---|
6508 | @cindex selecting a word
|
---|
6509 | Returns the @var{n}th word of @var{text}. The legitimate values of
|
---|
6510 | @var{n} start from 1. If @var{n} is bigger than the number of words
|
---|
6511 | in @var{text}, the value is empty. For example,
|
---|
6512 |
|
---|
6513 | @example
|
---|
6514 | $(word 2, foo bar baz)
|
---|
6515 | @end example
|
---|
6516 |
|
---|
6517 | @noindent
|
---|
6518 | returns @samp{bar}.
|
---|
6519 |
|
---|
6520 | @item $(wordlist @var{s},@var{e},@var{text})
|
---|
6521 | @findex wordlist
|
---|
6522 | @cindex words, selecting lists of
|
---|
6523 | @cindex selecting word lists
|
---|
6524 | Returns the list of words in @var{text} starting with word @var{s} and
|
---|
6525 | ending with word @var{e} (inclusive). The legitimate values of @var{s}
|
---|
6526 | start from 1; @var{e} may start from 0. If @var{s} is bigger than the
|
---|
6527 | number of words in @var{text}, the value is empty. If @var{e} is
|
---|
6528 | bigger than the number of words in @var{text}, words up to the end of
|
---|
6529 | @var{text} are returned. If @var{s} is greater than @var{e}, nothing
|
---|
6530 | is returned. For example,
|
---|
6531 |
|
---|
6532 | @example
|
---|
6533 | $(wordlist 2, 3, foo bar baz)
|
---|
6534 | @end example
|
---|
6535 |
|
---|
6536 | @noindent
|
---|
6537 | returns @samp{bar baz}.
|
---|
6538 |
|
---|
6539 | @c Following item phrased to prevent overfull hbox. --RJC 17 Jul 92
|
---|
6540 | @item $(words @var{text})
|
---|
6541 | @findex words
|
---|
6542 | @cindex words, finding number
|
---|
6543 | Returns the number of words in @var{text}.
|
---|
6544 | Thus, the last word of @var{text} is
|
---|
6545 | @w{@code{$(word $(words @var{text}),@var{text})}}.@refill
|
---|
6546 |
|
---|
6547 | @item $(firstword @var{names}@dots{})
|
---|
6548 | @findex firstword
|
---|
6549 | @cindex words, extracting first
|
---|
6550 | The argument @var{names} is regarded as a series of names, separated
|
---|
6551 | by whitespace. The value is the first name in the series. The rest
|
---|
6552 | of the names are ignored.
|
---|
6553 |
|
---|
6554 | For example,
|
---|
6555 |
|
---|
6556 | @example
|
---|
6557 | $(firstword foo bar)
|
---|
6558 | @end example
|
---|
6559 |
|
---|
6560 | @noindent
|
---|
6561 | produces the result @samp{foo}. Although @code{$(firstword
|
---|
6562 | @var{text})} is the same as @code{$(word 1,@var{text})}, the
|
---|
6563 | @code{firstword} function is retained for its simplicity.@refill
|
---|
6564 |
|
---|
6565 |
|
---|
6566 | @item $(lastword @var{names}@dots{})
|
---|
6567 | @findex lastword
|
---|
6568 | @cindex words, extracting last
|
---|
6569 | The argument @var{names} is regarded as a series of names, separated
|
---|
6570 | by whitespace. The value is the last name in the series.
|
---|
6571 |
|
---|
6572 | For example,
|
---|
6573 |
|
---|
6574 | @example
|
---|
6575 | $(lastword foo bar)
|
---|
6576 | @end example
|
---|
6577 |
|
---|
6578 | @noindent
|
---|
6579 | produces the result @samp{bar}. Although @code{$(lastword
|
---|
6580 | @var{text})} is the same as @code{$(word $(words @var{text}),@var{text})},
|
---|
6581 | the @code{lastword} function was added for its simplicity and better
|
---|
6582 | performance.@refill
|
---|
6583 | @end table
|
---|
6584 |
|
---|
6585 |
|
---|
6586 | Here is a realistic example of the use of @code{subst} and
|
---|
6587 | @code{patsubst}. Suppose that a makefile uses the @code{VPATH} variable
|
---|
6588 | to specify a list of directories that @code{make} should search for
|
---|
6589 | prerequisite files
|
---|
6590 | (@pxref{General Search, , @code{VPATH} Search Path for All Prerequisites}).
|
---|
6591 | This example shows how to
|
---|
6592 | tell the C compiler to search for header files in the same list of
|
---|
6593 | directories.@refill
|
---|
6594 |
|
---|
6595 | The value of @code{VPATH} is a list of directories separated by colons,
|
---|
6596 | such as @samp{src:../headers}. First, the @code{subst} function is used to
|
---|
6597 | change the colons to spaces:
|
---|
6598 |
|
---|
6599 | @example
|
---|
6600 | $(subst :, ,$(VPATH))
|
---|
6601 | @end example
|
---|
6602 |
|
---|
6603 | @noindent
|
---|
6604 | This produces @samp{src ../headers}. Then @code{patsubst} is used to turn
|
---|
6605 | each directory name into a @samp{-I} flag. These can be added to the
|
---|
6606 | value of the variable @code{CFLAGS}, which is passed automatically to the C
|
---|
6607 | compiler, like this:
|
---|
6608 |
|
---|
6609 | @example
|
---|
6610 | override CFLAGS += $(patsubst %,-I%,$(subst :, ,$(VPATH)))
|
---|
6611 | @end example
|
---|
6612 |
|
---|
6613 | @noindent
|
---|
6614 | The effect is to append the text @samp{-Isrc -I../headers} to the
|
---|
6615 | previously given value of @code{CFLAGS}. The @code{override} directive is
|
---|
6616 | used so that the new value is assigned even if the previous value of
|
---|
6617 | @code{CFLAGS} was specified with a command argument (@pxref{Override
|
---|
6618 | Directive, , The @code{override} Directive}).
|
---|
6619 |
|
---|
6620 | @node File Name Functions, Conditional Functions, Text Functions, Functions
|
---|
6621 | @section Functions for File Names
|
---|
6622 | @cindex functions, for file names
|
---|
6623 | @cindex file name functions
|
---|
6624 |
|
---|
6625 | Several of the built-in expansion functions relate specifically to
|
---|
6626 | taking apart file names or lists of file names.
|
---|
6627 |
|
---|
6628 | Each of the following functions performs a specific transformation on a
|
---|
6629 | file name. The argument of the function is regarded as a series of file
|
---|
6630 | names, separated by whitespace. (Leading and trailing whitespace is
|
---|
6631 | ignored.) Each file name in the series is transformed in the same way and
|
---|
6632 | the results are concatenated with single spaces between them.
|
---|
6633 |
|
---|
6634 | @table @code
|
---|
6635 | @item $(dir @var{names}@dots{})
|
---|
6636 | @findex dir
|
---|
6637 | @cindex directory part
|
---|
6638 | @cindex file name, directory part
|
---|
6639 | Extracts the directory-part of each file name in @var{names}. The
|
---|
6640 | directory-part of the file name is everything up through (and
|
---|
6641 | including) the last slash in it. If the file name contains no slash,
|
---|
6642 | the directory part is the string @samp{./}. For example,
|
---|
6643 |
|
---|
6644 | @example
|
---|
6645 | $(dir src/foo.c hacks)
|
---|
6646 | @end example
|
---|
6647 |
|
---|
6648 | @noindent
|
---|
6649 | produces the result @samp{src/ ./}.
|
---|
6650 |
|
---|
6651 | @item $(notdir @var{names}@dots{})
|
---|
6652 | @findex notdir
|
---|
6653 | @cindex file name, nondirectory part
|
---|
6654 | @cindex nondirectory part
|
---|
6655 | Extracts all but the directory-part of each file name in @var{names}.
|
---|
6656 | If the file name contains no slash, it is left unchanged. Otherwise,
|
---|
6657 | everything through the last slash is removed from it.
|
---|
6658 |
|
---|
6659 | A file name that ends with a slash becomes an empty string. This is
|
---|
6660 | unfortunate, because it means that the result does not always have the
|
---|
6661 | same number of whitespace-separated file names as the argument had;
|
---|
6662 | but we do not see any other valid alternative.
|
---|
6663 |
|
---|
6664 | For example,
|
---|
6665 |
|
---|
6666 | @example
|
---|
6667 | $(notdir src/foo.c hacks)
|
---|
6668 | @end example
|
---|
6669 |
|
---|
6670 | @noindent
|
---|
6671 | produces the result @samp{foo.c hacks}.
|
---|
6672 |
|
---|
6673 | @item $(suffix @var{names}@dots{})
|
---|
6674 | @findex suffix
|
---|
6675 | @cindex suffix, function to find
|
---|
6676 | @cindex file name suffix
|
---|
6677 | Extracts the suffix of each file name in @var{names}. If the file name
|
---|
6678 | contains a period, the suffix is everything starting with the last
|
---|
6679 | period. Otherwise, the suffix is the empty string. This frequently
|
---|
6680 | means that the result will be empty when @var{names} is not, and if
|
---|
6681 | @var{names} contains multiple file names, the result may contain fewer
|
---|
6682 | file names.
|
---|
6683 |
|
---|
6684 | For example,
|
---|
6685 |
|
---|
6686 | @example
|
---|
6687 | $(suffix src/foo.c src-1.0/bar.c hacks)
|
---|
6688 | @end example
|
---|
6689 |
|
---|
6690 | @noindent
|
---|
6691 | produces the result @samp{.c .c}.
|
---|
6692 |
|
---|
6693 | @item $(basename @var{names}@dots{})
|
---|
6694 | @findex basename
|
---|
6695 | @cindex basename
|
---|
6696 | @cindex file name, basename of
|
---|
6697 | Extracts all but the suffix of each file name in @var{names}. If the
|
---|
6698 | file name contains a period, the basename is everything starting up to
|
---|
6699 | (and not including) the last period. Periods in the directory part are
|
---|
6700 | ignored. If there is no period, the basename is the entire file name.
|
---|
6701 | For example,
|
---|
6702 |
|
---|
6703 | @example
|
---|
6704 | $(basename src/foo.c src-1.0/bar hacks)
|
---|
6705 | @end example
|
---|
6706 |
|
---|
6707 | @noindent
|
---|
6708 | produces the result @samp{src/foo src-1.0/bar hacks}.
|
---|
6709 |
|
---|
6710 | @c plural convention with dots (be consistent)
|
---|
6711 | @item $(addsuffix @var{suffix},@var{names}@dots{})
|
---|
6712 | @findex addsuffix
|
---|
6713 | @cindex suffix, adding
|
---|
6714 | @cindex file name suffix, adding
|
---|
6715 | The argument @var{names} is regarded as a series of names, separated
|
---|
6716 | by whitespace; @var{suffix} is used as a unit. The value of
|
---|
6717 | @var{suffix} is appended to the end of each individual name and the
|
---|
6718 | resulting larger names are concatenated with single spaces between
|
---|
6719 | them. For example,
|
---|
6720 |
|
---|
6721 | @example
|
---|
6722 | $(addsuffix .c,foo bar)
|
---|
6723 | @end example
|
---|
6724 |
|
---|
6725 | @noindent
|
---|
6726 | produces the result @samp{foo.c bar.c}.
|
---|
6727 |
|
---|
6728 | @item $(addprefix @var{prefix},@var{names}@dots{})
|
---|
6729 | @findex addprefix
|
---|
6730 | @cindex prefix, adding
|
---|
6731 | @cindex file name prefix, adding
|
---|
6732 | The argument @var{names} is regarded as a series of names, separated
|
---|
6733 | by whitespace; @var{prefix} is used as a unit. The value of
|
---|
6734 | @var{prefix} is prepended to the front of each individual name and the
|
---|
6735 | resulting larger names are concatenated with single spaces between
|
---|
6736 | them. For example,
|
---|
6737 |
|
---|
6738 | @example
|
---|
6739 | $(addprefix src/,foo bar)
|
---|
6740 | @end example
|
---|
6741 |
|
---|
6742 | @noindent
|
---|
6743 | produces the result @samp{src/foo src/bar}.
|
---|
6744 |
|
---|
6745 | @item $(join @var{list1},@var{list2})
|
---|
6746 | @findex join
|
---|
6747 | @cindex joining lists of words
|
---|
6748 | @cindex words, joining lists
|
---|
6749 | Concatenates the two arguments word by word: the two first words (one
|
---|
6750 | from each argument) concatenated form the first word of the result, the
|
---|
6751 | two second words form the second word of the result, and so on. So the
|
---|
6752 | @var{n}th word of the result comes from the @var{n}th word of each
|
---|
6753 | argument. If one argument has more words that the other, the extra
|
---|
6754 | words are copied unchanged into the result.
|
---|
6755 |
|
---|
6756 | For example, @samp{$(join a b,.c .o)} produces @samp{a.c b.o}.
|
---|
6757 |
|
---|
6758 | Whitespace between the words in the lists is not preserved; it is
|
---|
6759 | replaced with a single space.
|
---|
6760 |
|
---|
6761 | This function can merge the results of the @code{dir} and
|
---|
6762 | @code{notdir} functions, to produce the original list of files which
|
---|
6763 | was given to those two functions.@refill
|
---|
6764 |
|
---|
6765 | @item $(wildcard @var{pattern})
|
---|
6766 | @findex wildcard
|
---|
6767 | @cindex wildcard, function
|
---|
6768 | The argument @var{pattern} is a file name pattern, typically containing
|
---|
6769 | wildcard characters (as in shell file name patterns). The result of
|
---|
6770 | @code{wildcard} is a space-separated list of the names of existing files
|
---|
6771 | that match the pattern.
|
---|
6772 | @xref{Wildcards, ,Using Wildcard Characters in File Names}.
|
---|
6773 |
|
---|
6774 | @item $(realpath @var{names}@dots{})
|
---|
6775 | @findex realpath
|
---|
6776 | @cindex realpath
|
---|
6777 | @cindex file name, realpath of
|
---|
6778 | For each file name in @var{names} return the canonical absolute name.
|
---|
6779 | A canonical name does not contain any @code{.} or @code{..} components,
|
---|
6780 | nor any repeated path separators (@code{/}) or symlinks. In case of a
|
---|
6781 | failure the empty string is returned. Consult the @code{realpath(3)}
|
---|
6782 | documentation for a list of possible failure causes.
|
---|
6783 |
|
---|
6784 | @item $(abspath @var{names}@dots{})
|
---|
6785 | @findex abspath
|
---|
6786 | @cindex abspath
|
---|
6787 | @cindex file name, abspath of
|
---|
6788 | For each file name in @var{names} return an absolute name that does
|
---|
6789 | not contain any @code{.} or @code{..} components, nor any repeated path
|
---|
6790 | separators (@code{/}). Note that, in contrast to @code{realpath}
|
---|
6791 | function, @code{abspath} does not resolve symlinks and does not require
|
---|
6792 | the file names to refer to an existing file or directory. Use the
|
---|
6793 | @code{wildcard} function to test for existence.
|
---|
6794 | @end table
|
---|
6795 |
|
---|
6796 | @node Conditional Functions, Foreach Function, File Name Functions, Functions
|
---|
6797 | @section Functions for Conditionals
|
---|
6798 | @findex if
|
---|
6799 | @cindex conditional expansion
|
---|
6800 | There are three functions that provide conditional expansion. A key
|
---|
6801 | aspect of these functions is that not all of the arguments are
|
---|
6802 | expanded initially. Only those arguments which need to be expanded,
|
---|
6803 | will be expanded.
|
---|
6804 |
|
---|
6805 | @table @code
|
---|
6806 | @item $(if @var{condition},@var{then-part}[,@var{else-part}])
|
---|
6807 | @findex if
|
---|
6808 | The @code{if} function provides support for conditional expansion in a
|
---|
6809 | functional context (as opposed to the GNU @code{make} makefile
|
---|
6810 | conditionals such as @code{ifeq} (@pxref{Conditional Syntax, ,Syntax of
|
---|
6811 | Conditionals}).
|
---|
6812 |
|
---|
6813 | The first argument, @var{condition}, first has all preceding and
|
---|
6814 | trailing whitespace stripped, then is expanded. If it expands to any
|
---|
6815 | non-empty string, then the condition is considered to be true. If it
|
---|
6816 | expands to an empty string, the condition is considered to be false.
|
---|
6817 |
|
---|
6818 | If the condition is true then the second argument, @var{then-part}, is
|
---|
6819 | evaluated and this is used as the result of the evaluation of the entire
|
---|
6820 | @code{if} function.
|
---|
6821 |
|
---|
6822 | If the condition is false then the third argument, @var{else-part}, is
|
---|
6823 | evaluated and this is the result of the @code{if} function. If there is
|
---|
6824 | no third argument, the @code{if} function evaluates to nothing (the
|
---|
6825 | empty string).
|
---|
6826 |
|
---|
6827 | Note that only one of the @var{then-part} or the @var{else-part} will be
|
---|
6828 | evaluated, never both. Thus, either can contain side-effects (such as
|
---|
6829 | @code{shell} function calls, etc.)
|
---|
6830 |
|
---|
6831 | @item $(or @var{condition1}[,@var{condition2}[,@var{condition3}@dots{}]])
|
---|
6832 | @findex or
|
---|
6833 | The @code{or} function provides a ``short-circuiting'' OR operation.
|
---|
6834 | Each argument is expanded, in order. If an argument expands to a
|
---|
6835 | non-empty string the processing stops and the result of the expansion
|
---|
6836 | is that string. If, after all arguments are expanded, all of them are
|
---|
6837 | false (empty), then the result of the expansion is the empty string.
|
---|
6838 |
|
---|
6839 | @item $(and @var{condition1}[,@var{condition2}[,@var{condition3}@dots{}]])
|
---|
6840 | @findex and
|
---|
6841 | The @code{and} function provides a ``short-circuiting'' AND operation.
|
---|
6842 | Each argument is expanded, in order. If an argument expands to an
|
---|
6843 | empty string the processing stops and the result of the expansion is
|
---|
6844 | the empty string. If all arguments expand to a non-empty string then
|
---|
6845 | the result of the expansion is the expansion of the last argument.
|
---|
6846 |
|
---|
6847 | @end table
|
---|
6848 |
|
---|
6849 | @node Foreach Function, Call Function, Conditional Functions, Functions
|
---|
6850 | @section The @code{foreach} Function
|
---|
6851 | @findex foreach
|
---|
6852 | @cindex words, iterating over
|
---|
6853 |
|
---|
6854 | The @code{foreach} function is very different from other functions. It
|
---|
6855 | causes one piece of text to be used repeatedly, each time with a different
|
---|
6856 | substitution performed on it. It resembles the @code{for} command in the
|
---|
6857 | shell @code{sh} and the @code{foreach} command in the C-shell @code{csh}.
|
---|
6858 |
|
---|
6859 | The syntax of the @code{foreach} function is:
|
---|
6860 |
|
---|
6861 | @example
|
---|
6862 | $(foreach @var{var},@var{list},@var{text})
|
---|
6863 | @end example
|
---|
6864 |
|
---|
6865 | @noindent
|
---|
6866 | The first two arguments, @var{var} and @var{list}, are expanded before
|
---|
6867 | anything else is done; note that the last argument, @var{text}, is
|
---|
6868 | @strong{not} expanded at the same time. Then for each word of the expanded
|
---|
6869 | value of @var{list}, the variable named by the expanded value of @var{var}
|
---|
6870 | is set to that word, and @var{text} is expanded. Presumably @var{text}
|
---|
6871 | contains references to that variable, so its expansion will be different
|
---|
6872 | each time.
|
---|
6873 |
|
---|
6874 | The result is that @var{text} is expanded as many times as there are
|
---|
6875 | whitespace-separated words in @var{list}. The multiple expansions of
|
---|
6876 | @var{text} are concatenated, with spaces between them, to make the result
|
---|
6877 | of @code{foreach}.
|
---|
6878 |
|
---|
6879 | This simple example sets the variable @samp{files} to the list of all files
|
---|
6880 | in the directories in the list @samp{dirs}:
|
---|
6881 |
|
---|
6882 | @example
|
---|
6883 | dirs := a b c d
|
---|
6884 | files := $(foreach dir,$(dirs),$(wildcard $(dir)/*))
|
---|
6885 | @end example
|
---|
6886 |
|
---|
6887 | Here @var{text} is @samp{$(wildcard $(dir)/*)}. The first repetition
|
---|
6888 | finds the value @samp{a} for @code{dir}, so it produces the same result
|
---|
6889 | as @samp{$(wildcard a/*)}; the second repetition produces the result
|
---|
6890 | of @samp{$(wildcard b/*)}; and the third, that of @samp{$(wildcard c/*)}.
|
---|
6891 |
|
---|
6892 | This example has the same result (except for setting @samp{dirs}) as
|
---|
6893 | the following example:
|
---|
6894 |
|
---|
6895 | @example
|
---|
6896 | files := $(wildcard a/* b/* c/* d/*)
|
---|
6897 | @end example
|
---|
6898 |
|
---|
6899 | When @var{text} is complicated, you can improve readability by giving it
|
---|
6900 | a name, with an additional variable:
|
---|
6901 |
|
---|
6902 | @example
|
---|
6903 | find_files = $(wildcard $(dir)/*)
|
---|
6904 | dirs := a b c d
|
---|
6905 | files := $(foreach dir,$(dirs),$(find_files))
|
---|
6906 | @end example
|
---|
6907 |
|
---|
6908 | @noindent
|
---|
6909 | Here we use the variable @code{find_files} this way. We use plain @samp{=}
|
---|
6910 | to define a recursively-expanding variable, so that its value contains an
|
---|
6911 | actual function call to be reexpanded under the control of @code{foreach};
|
---|
6912 | a simply-expanded variable would not do, since @code{wildcard} would be
|
---|
6913 | called only once at the time of defining @code{find_files}.
|
---|
6914 |
|
---|
6915 | The @code{foreach} function has no permanent effect on the variable
|
---|
6916 | @var{var}; its value and flavor after the @code{foreach} function call are
|
---|
6917 | the same as they were beforehand. The other values which are taken from
|
---|
6918 | @var{list} are in effect only temporarily, during the execution of
|
---|
6919 | @code{foreach}. The variable @var{var} is a simply-expanded variable
|
---|
6920 | during the execution of @code{foreach}. If @var{var} was undefined
|
---|
6921 | before the @code{foreach} function call, it is undefined after the call.
|
---|
6922 | @xref{Flavors, ,The Two Flavors of Variables}.@refill
|
---|
6923 |
|
---|
6924 | You must take care when using complex variable expressions that result in
|
---|
6925 | variable names because many strange things are valid variable names, but
|
---|
6926 | are probably not what you intended. For example,
|
---|
6927 |
|
---|
6928 | @smallexample
|
---|
6929 | files := $(foreach Esta escrito en espanol!,b c ch,$(find_files))
|
---|
6930 | @end smallexample
|
---|
6931 |
|
---|
6932 | @noindent
|
---|
6933 | might be useful if the value of @code{find_files} references the variable
|
---|
6934 | whose name is @samp{Esta escrito en espanol!} (es un nombre bastante largo,
|
---|
6935 | no?), but it is more likely to be a mistake.
|
---|
6936 |
|
---|
6937 | @node Call Function, Value Function, Foreach Function, Functions
|
---|
6938 | @section The @code{call} Function
|
---|
6939 | @findex call
|
---|
6940 | @cindex functions, user defined
|
---|
6941 | @cindex user defined functions
|
---|
6942 |
|
---|
6943 | The @code{call} function is unique in that it can be used to create new
|
---|
6944 | parameterized functions. You can write a complex expression as the
|
---|
6945 | value of a variable, then use @code{call} to expand it with different
|
---|
6946 | values.
|
---|
6947 |
|
---|
6948 | The syntax of the @code{call} function is:
|
---|
6949 |
|
---|
6950 | @example
|
---|
6951 | $(call @var{variable},@var{param},@var{param},@dots{})
|
---|
6952 | @end example
|
---|
6953 |
|
---|
6954 | When @code{make} expands this function, it assigns each @var{param} to
|
---|
6955 | temporary variables @code{$(1)}, @code{$(2)}, etc. The variable
|
---|
6956 | @code{$(0)} will contain @var{variable}. There is no maximum number of
|
---|
6957 | parameter arguments. There is no minimum, either, but it doesn't make
|
---|
6958 | sense to use @code{call} with no parameters.
|
---|
6959 |
|
---|
6960 | Then @var{variable} is expanded as a @code{make} variable in the context
|
---|
6961 | of these temporary assignments. Thus, any reference to @code{$(1)} in
|
---|
6962 | the value of @var{variable} will resolve to the first @var{param} in the
|
---|
6963 | invocation of @code{call}.
|
---|
6964 |
|
---|
6965 | Note that @var{variable} is the @emph{name} of a variable, not a
|
---|
6966 | @emph{reference} to that variable. Therefore you would not normally use
|
---|
6967 | a @samp{$} or parentheses when writing it. (You can, however, use a
|
---|
6968 | variable reference in the name if you want the name not to be a
|
---|
6969 | constant.)
|
---|
6970 |
|
---|
6971 | If @var{variable} is the name of a builtin function, the builtin function
|
---|
6972 | is always invoked (even if a @code{make} variable by that name also
|
---|
6973 | exists).
|
---|
6974 |
|
---|
6975 | The @code{call} function expands the @var{param} arguments before
|
---|
6976 | assigning them to temporary variables. This means that @var{variable}
|
---|
6977 | values containing references to builtin functions that have special
|
---|
6978 | expansion rules, like @code{foreach} or @code{if}, may not work as you
|
---|
6979 | expect.
|
---|
6980 |
|
---|
6981 | Some examples may make this clearer.
|
---|
6982 |
|
---|
6983 | This macro simply reverses its arguments:
|
---|
6984 |
|
---|
6985 | @smallexample
|
---|
6986 | reverse = $(2) $(1)
|
---|
6987 |
|
---|
6988 | foo = $(call reverse,a,b)
|
---|
6989 | @end smallexample
|
---|
6990 |
|
---|
6991 | @noindent
|
---|
6992 | Here @var{foo} will contain @samp{b a}.
|
---|
6993 |
|
---|
6994 | This one is slightly more interesting: it defines a macro to search for
|
---|
6995 | the first instance of a program in @code{PATH}:
|
---|
6996 |
|
---|
6997 | @smallexample
|
---|
6998 | pathsearch = $(firstword $(wildcard $(addsuffix /$(1),$(subst :, ,$(PATH)))))
|
---|
6999 |
|
---|
7000 | LS := $(call pathsearch,ls)
|
---|
7001 | @end smallexample
|
---|
7002 |
|
---|
7003 | @noindent
|
---|
7004 | Now the variable LS contains @code{/bin/ls} or similar.
|
---|
7005 |
|
---|
7006 | The @code{call} function can be nested. Each recursive invocation gets
|
---|
7007 | its own local values for @code{$(1)}, etc.@: that mask the values of
|
---|
7008 | higher-level @code{call}. For example, here is an implementation of a
|
---|
7009 | @dfn{map} function:
|
---|
7010 |
|
---|
7011 | @smallexample
|
---|
7012 | map = $(foreach a,$(2),$(call $(1),$(a)))
|
---|
7013 | @end smallexample
|
---|
7014 |
|
---|
7015 | Now you can @var{map} a function that normally takes only one argument,
|
---|
7016 | such as @code{origin}, to multiple values in one step:
|
---|
7017 |
|
---|
7018 | @smallexample
|
---|
7019 | o = $(call map,origin,o map MAKE)
|
---|
7020 | @end smallexample
|
---|
7021 |
|
---|
7022 | and end up with @var{o} containing something like @samp{file file default}.
|
---|
7023 |
|
---|
7024 | A final caution: be careful when adding whitespace to the arguments to
|
---|
7025 | @code{call}. As with other functions, any whitespace contained in the
|
---|
7026 | second and subsequent arguments is kept; this can cause strange
|
---|
7027 | effects. It's generally safest to remove all extraneous whitespace when
|
---|
7028 | providing parameters to @code{call}.
|
---|
7029 |
|
---|
7030 | @node Value Function, Eval Function, Call Function, Functions
|
---|
7031 | @comment node-name, next, previous, up
|
---|
7032 | @section The @code{value} Function
|
---|
7033 | @findex value
|
---|
7034 | @cindex variables, unexpanded value
|
---|
7035 |
|
---|
7036 | The @code{value} function provides a way for you to use the value of a
|
---|
7037 | variable @emph{without} having it expanded. Please note that this
|
---|
7038 | does not undo expansions which have already occurred; for example if
|
---|
7039 | you create a simply expanded variable its value is expanded during the
|
---|
7040 | definition; in that case the @code{value} function will return the
|
---|
7041 | same result as using the variable directly.
|
---|
7042 |
|
---|
7043 | The syntax of the @code{value} function is:
|
---|
7044 |
|
---|
7045 | @example
|
---|
7046 | $(value @var{variable})
|
---|
7047 | @end example
|
---|
7048 |
|
---|
7049 | Note that @var{variable} is the @emph{name} of a variable; not a
|
---|
7050 | @emph{reference} to that variable. Therefore you would not normally
|
---|
7051 | use a @samp{$} or parentheses when writing it. (You can, however, use
|
---|
7052 | a variable reference in the name if you want the name not to be a
|
---|
7053 | constant.)
|
---|
7054 |
|
---|
7055 | The result of this function is a string containing the value of
|
---|
7056 | @var{variable}, without any expansion occurring. For example, in this
|
---|
7057 | makefile:
|
---|
7058 |
|
---|
7059 | @example
|
---|
7060 | @group
|
---|
7061 | FOO = $PATH
|
---|
7062 |
|
---|
7063 | all:
|
---|
7064 | @@echo $(FOO)
|
---|
7065 | @@echo $(value FOO)
|
---|
7066 | @end group
|
---|
7067 | @end example
|
---|
7068 |
|
---|
7069 | @noindent
|
---|
7070 | The first output line would be @code{ATH}, since the ``$P'' would be
|
---|
7071 | expanded as a @code{make} variable, while the second output line would
|
---|
7072 | be the current value of your @code{$PATH} environment variable, since
|
---|
7073 | the @code{value} function avoided the expansion.
|
---|
7074 |
|
---|
7075 | The @code{value} function is most often used in conjunction with the
|
---|
7076 | @code{eval} function (@pxref{Eval Function}).
|
---|
7077 |
|
---|
7078 | @node Eval Function, Origin Function, Value Function, Functions
|
---|
7079 | @comment node-name, next, previous, up
|
---|
7080 | @section The @code{eval} Function
|
---|
7081 | @findex eval
|
---|
7082 | @cindex evaluating makefile syntax
|
---|
7083 | @cindex makefile syntax, evaluating
|
---|
7084 |
|
---|
7085 | The @code{eval} function is very special: it allows you to define new
|
---|
7086 | makefile constructs that are not constant; which are the result of
|
---|
7087 | evaluating other variables and functions. The argument to the
|
---|
7088 | @code{eval} function is expanded, then the results of that expansion
|
---|
7089 | are parsed as makefile syntax. The expanded results can define new
|
---|
7090 | @code{make} variables, targets, implicit or explicit rules, etc.
|
---|
7091 |
|
---|
7092 | The result of the @code{eval} function is always the empty string;
|
---|
7093 | thus, it can be placed virtually anywhere in a makefile without
|
---|
7094 | causing syntax errors.
|
---|
7095 |
|
---|
7096 | It's important to realize that the @code{eval} argument is expanded
|
---|
7097 | @emph{twice}; first by the @code{eval} function, then the results of
|
---|
7098 | that expansion are expanded again when they are parsed as makefile
|
---|
7099 | syntax. This means you may need to provide extra levels of escaping
|
---|
7100 | for ``$'' characters when using @code{eval}. The @code{value}
|
---|
7101 | function (@pxref{Value Function}) can sometimes be useful in these
|
---|
7102 | situations, to circumvent unwanted expansions.
|
---|
7103 |
|
---|
7104 | Here is an example of how @code{eval} can be used; this example
|
---|
7105 | combines a number of concepts and other functions. Although it might
|
---|
7106 | seem overly complex to use @code{eval} in this example, rather than
|
---|
7107 | just writing out the rules, consider two things: first, the template
|
---|
7108 | definition (in @code{PROGRAM_template}) could need to be much more
|
---|
7109 | complex than it is here; and second, you might put the complex,
|
---|
7110 | ``generic'' part of this example into another makefile, then include
|
---|
7111 | it in all the individual makefiles. Now your individual makefiles are
|
---|
7112 | quite straightforward.
|
---|
7113 |
|
---|
7114 | @example
|
---|
7115 | @group
|
---|
7116 | PROGRAMS = server client
|
---|
7117 |
|
---|
7118 | server_OBJS = server.o server_priv.o server_access.o
|
---|
7119 | server_LIBS = priv protocol
|
---|
7120 |
|
---|
7121 | client_OBJS = client.o client_api.o client_mem.o
|
---|
7122 | client_LIBS = protocol
|
---|
7123 |
|
---|
7124 | # Everything after this is generic
|
---|
7125 |
|
---|
7126 | .PHONY: all
|
---|
7127 | all: $(PROGRAMS)
|
---|
7128 |
|
---|
7129 | define PROGRAM_template
|
---|
7130 | $(1): $$($(1)_OBJS) $$($(1)_LIBS:%=-l%)
|
---|
7131 | ALL_OBJS += $$($(1)_OBJS)
|
---|
7132 | endef
|
---|
7133 |
|
---|
7134 | $(foreach prog,$(PROGRAMS),$(eval $(call PROGRAM_template,$(prog))))
|
---|
7135 |
|
---|
7136 | $(PROGRAMS):
|
---|
7137 | $(LINK.o) $^ $(LDLIBS) -o $@@
|
---|
7138 |
|
---|
7139 | clean:
|
---|
7140 | rm -f $(ALL_OBJS) $(PROGRAMS)
|
---|
7141 | @end group
|
---|
7142 | @end example
|
---|
7143 |
|
---|
7144 | @node Origin Function, Flavor Function, Eval Function, Functions
|
---|
7145 | @section The @code{origin} Function
|
---|
7146 | @findex origin
|
---|
7147 | @cindex variables, origin of
|
---|
7148 | @cindex origin of variable
|
---|
7149 |
|
---|
7150 | The @code{origin} function is unlike most other functions in that it does
|
---|
7151 | not operate on the values of variables; it tells you something @emph{about}
|
---|
7152 | a variable. Specifically, it tells you where it came from.
|
---|
7153 |
|
---|
7154 | The syntax of the @code{origin} function is:
|
---|
7155 |
|
---|
7156 | @example
|
---|
7157 | $(origin @var{variable})
|
---|
7158 | @end example
|
---|
7159 |
|
---|
7160 | Note that @var{variable} is the @emph{name} of a variable to inquire about;
|
---|
7161 | not a @emph{reference} to that variable. Therefore you would not normally
|
---|
7162 | use a @samp{$} or parentheses when writing it. (You can, however, use a
|
---|
7163 | variable reference in the name if you want the name not to be a constant.)
|
---|
7164 |
|
---|
7165 | The result of this function is a string telling you how the variable
|
---|
7166 | @var{variable} was defined:
|
---|
7167 |
|
---|
7168 | @table @samp
|
---|
7169 | @item undefined
|
---|
7170 |
|
---|
7171 | if @var{variable} was never defined.
|
---|
7172 |
|
---|
7173 | @item default
|
---|
7174 |
|
---|
7175 | if @var{variable} has a default definition, as is usual with @code{CC}
|
---|
7176 | and so on. @xref{Implicit Variables, ,Variables Used by Implicit Rules}.
|
---|
7177 | Note that if you have redefined a default variable, the @code{origin}
|
---|
7178 | function will return the origin of the later definition.
|
---|
7179 |
|
---|
7180 | @item environment
|
---|
7181 |
|
---|
7182 | if @var{variable} was defined as an environment variable and the
|
---|
7183 | @samp{-e} option is @emph{not} turned on (@pxref{Options Summary, ,Summary of Options}).
|
---|
7184 |
|
---|
7185 | @item environment override
|
---|
7186 |
|
---|
7187 | if @var{variable} was defined as an environment variable and the
|
---|
7188 | @w{@samp{-e}} option @emph{is} turned on (@pxref{Options Summary,
|
---|
7189 | ,Summary of Options}).@refill
|
---|
7190 |
|
---|
7191 | @item file
|
---|
7192 |
|
---|
7193 | if @var{variable} was defined in a makefile.
|
---|
7194 |
|
---|
7195 | @item command line
|
---|
7196 |
|
---|
7197 | if @var{variable} was defined on the command line.
|
---|
7198 |
|
---|
7199 | @item override
|
---|
7200 |
|
---|
7201 | if @var{variable} was defined with an @code{override} directive in a
|
---|
7202 | makefile (@pxref{Override Directive, ,The @code{override} Directive}).
|
---|
7203 |
|
---|
7204 | @item automatic
|
---|
7205 |
|
---|
7206 | if @var{variable} is an automatic variable defined for the
|
---|
7207 | execution of the commands for each rule
|
---|
7208 | (@pxref{Automatic Variables}).
|
---|
7209 | @end table
|
---|
7210 |
|
---|
7211 | This information is primarily useful (other than for your curiosity) to
|
---|
7212 | determine if you want to believe the value of a variable. For example,
|
---|
7213 | suppose you have a makefile @file{foo} that includes another makefile
|
---|
7214 | @file{bar}. You want a variable @code{bletch} to be defined in @file{bar}
|
---|
7215 | if you run the command @w{@samp{make -f bar}}, even if the environment contains
|
---|
7216 | a definition of @code{bletch}. However, if @file{foo} defined
|
---|
7217 | @code{bletch} before including @file{bar}, you do not want to override that
|
---|
7218 | definition. This could be done by using an @code{override} directive in
|
---|
7219 | @file{foo}, giving that definition precedence over the later definition in
|
---|
7220 | @file{bar}; unfortunately, the @code{override} directive would also
|
---|
7221 | override any command line definitions. So, @file{bar} could
|
---|
7222 | include:@refill
|
---|
7223 |
|
---|
7224 | @example
|
---|
7225 | @group
|
---|
7226 | ifdef bletch
|
---|
7227 | ifeq "$(origin bletch)" "environment"
|
---|
7228 | bletch = barf, gag, etc.
|
---|
7229 | endif
|
---|
7230 | endif
|
---|
7231 | @end group
|
---|
7232 | @end example
|
---|
7233 |
|
---|
7234 | @noindent
|
---|
7235 | If @code{bletch} has been defined from the environment, this will redefine
|
---|
7236 | it.
|
---|
7237 |
|
---|
7238 | If you want to override a previous definition of @code{bletch} if it came
|
---|
7239 | from the environment, even under @samp{-e}, you could instead write:
|
---|
7240 |
|
---|
7241 | @example
|
---|
7242 | @group
|
---|
7243 | ifneq "$(findstring environment,$(origin bletch))" ""
|
---|
7244 | bletch = barf, gag, etc.
|
---|
7245 | endif
|
---|
7246 | @end group
|
---|
7247 | @end example
|
---|
7248 |
|
---|
7249 | Here the redefinition takes place if @samp{$(origin bletch)} returns either
|
---|
7250 | @samp{environment} or @samp{environment override}.
|
---|
7251 | @xref{Text Functions, , Functions for String Substitution and Analysis}.
|
---|
7252 |
|
---|
7253 | @node Flavor Function, Shell Function, Origin Function, Functions
|
---|
7254 | @section The @code{flavor} Function
|
---|
7255 | @findex flavor
|
---|
7256 | @cindex variables, flavor of
|
---|
7257 | @cindex flavor of variable
|
---|
7258 |
|
---|
7259 | The @code{flavor} function is unlike most other functions (and like
|
---|
7260 | @code{origin} function) in that it does not operate on the values of
|
---|
7261 | variables; it tells you something @emph{about} a variable.
|
---|
7262 | Specifically, it tells you the flavor of a variable (@pxref{Flavors,
|
---|
7263 | ,The Two Flavors of Variables}).
|
---|
7264 |
|
---|
7265 | The syntax of the @code{flavor} function is:
|
---|
7266 |
|
---|
7267 | @example
|
---|
7268 | $(flavor @var{variable})
|
---|
7269 | @end example
|
---|
7270 |
|
---|
7271 | Note that @var{variable} is the @emph{name} of a variable to inquire about;
|
---|
7272 | not a @emph{reference} to that variable. Therefore you would not normally
|
---|
7273 | use a @samp{$} or parentheses when writing it. (You can, however, use a
|
---|
7274 | variable reference in the name if you want the name not to be a constant.)
|
---|
7275 |
|
---|
7276 | The result of this function is a string that identifies the flavor of the
|
---|
7277 | variable @var{variable}:
|
---|
7278 |
|
---|
7279 | @table @samp
|
---|
7280 | @item undefined
|
---|
7281 |
|
---|
7282 | if @var{variable} was never defined.
|
---|
7283 |
|
---|
7284 | @item recursive
|
---|
7285 |
|
---|
7286 | if @var{variable} is a recursively expanded variable.
|
---|
7287 |
|
---|
7288 | @item simple
|
---|
7289 |
|
---|
7290 | if @var{variable} is a simply expanded variable.
|
---|
7291 |
|
---|
7292 | @end table
|
---|
7293 |
|
---|
7294 |
|
---|
7295 | @node Shell Function, Make Control Functions, Flavor Function, Functions
|
---|
7296 | @section The @code{shell} Function
|
---|
7297 | @findex shell
|
---|
7298 | @cindex commands, expansion
|
---|
7299 | @cindex backquotes
|
---|
7300 | @cindex shell command, function for
|
---|
7301 |
|
---|
7302 | The @code{shell} function is unlike any other function other than the
|
---|
7303 | @code{wildcard} function
|
---|
7304 | (@pxref{Wildcard Function, ,The Function @code{wildcard}}) in that it
|
---|
7305 | communicates with the world outside of @code{make}.
|
---|
7306 |
|
---|
7307 | The @code{shell} function performs the same function that backquotes
|
---|
7308 | (@samp{`}) perform in most shells: it does @dfn{command expansion}.
|
---|
7309 | This means that it takes as an argument a shell command and evaluates
|
---|
7310 | to the output of the command. The only processing @code{make} does on
|
---|
7311 | the result is to convert each newline (or carriage-return / newline
|
---|
7312 | pair) to a single space. If there is a trailing (carriage-return
|
---|
7313 | and) newline it will simply be removed.@refill
|
---|
7314 |
|
---|
7315 | The commands run by calls to the @code{shell} function are run when the
|
---|
7316 | function calls are expanded (@pxref{Reading Makefiles, , How
|
---|
7317 | @code{make} Reads a Makefile}). Because this function involves
|
---|
7318 | spawning a new shell, you should carefully consider the performance
|
---|
7319 | implications of using the @code{shell} function within recursively
|
---|
7320 | expanded variables vs.@: simply expanded variables (@pxref{Flavors, ,The
|
---|
7321 | Two Flavors of Variables}).
|
---|
7322 |
|
---|
7323 | Here are some examples of the use of the @code{shell} function:
|
---|
7324 |
|
---|
7325 | @example
|
---|
7326 | contents := $(shell cat foo)
|
---|
7327 | @end example
|
---|
7328 |
|
---|
7329 | @noindent
|
---|
7330 | sets @code{contents} to the contents of the file @file{foo}, with a space
|
---|
7331 | (rather than a newline) separating each line.
|
---|
7332 |
|
---|
7333 | @example
|
---|
7334 | files := $(shell echo *.c)
|
---|
7335 | @end example
|
---|
7336 |
|
---|
7337 | @noindent
|
---|
7338 | sets @code{files} to the expansion of @samp{*.c}. Unless @code{make} is
|
---|
7339 | using a very strange shell, this has the same result as
|
---|
7340 | @w{@samp{$(wildcard *.c)}} (as long as at least one @samp{.c} file
|
---|
7341 | exists).@refill
|
---|
7342 |
|
---|
7343 | @node Make Control Functions, , Shell Function, Functions
|
---|
7344 | @section Functions That Control Make
|
---|
7345 | @cindex functions, for controlling make
|
---|
7346 | @cindex controlling make
|
---|
7347 |
|
---|
7348 | These functions control the way make runs. Generally, they are used to
|
---|
7349 | provide information to the user of the makefile or to cause make to stop
|
---|
7350 | if some sort of environmental error is detected.
|
---|
7351 |
|
---|
7352 | @table @code
|
---|
7353 | @item $(error @var{text}@dots{})
|
---|
7354 | @findex error
|
---|
7355 | @cindex error, stopping on
|
---|
7356 | @cindex stopping make
|
---|
7357 | Generates a fatal error where the message is @var{text}. Note that the
|
---|
7358 | error is generated whenever this function is evaluated. So, if you put
|
---|
7359 | it inside a command script or on the right side of a recursive variable
|
---|
7360 | assignment, it won't be evaluated until later. The @var{text} will be
|
---|
7361 | expanded before the error is generated.
|
---|
7362 |
|
---|
7363 | For example,
|
---|
7364 |
|
---|
7365 | @example
|
---|
7366 | ifdef ERROR1
|
---|
7367 | $(error error is $(ERROR1))
|
---|
7368 | endif
|
---|
7369 | @end example
|
---|
7370 |
|
---|
7371 | @noindent
|
---|
7372 | will generate a fatal error during the read of the makefile if the
|
---|
7373 | @code{make} variable @code{ERROR1} is defined. Or,
|
---|
7374 |
|
---|
7375 | @example
|
---|
7376 | ERR = $(error found an error!)
|
---|
7377 |
|
---|
7378 | .PHONY: err
|
---|
7379 | err: ; $(ERR)
|
---|
7380 | @end example
|
---|
7381 |
|
---|
7382 | @noindent
|
---|
7383 | will generate a fatal error while @code{make} is running, if the
|
---|
7384 | @code{err} target is invoked.
|
---|
7385 |
|
---|
7386 | @item $(warning @var{text}@dots{})
|
---|
7387 | @findex warning
|
---|
7388 | @cindex warnings, printing
|
---|
7389 | @cindex printing user warnings
|
---|
7390 | This function works similarly to the @code{error} function, above,
|
---|
7391 | except that @code{make} doesn't exit. Instead, @var{text} is expanded
|
---|
7392 | and the resulting message is displayed, but processing of the makefile
|
---|
7393 | continues.
|
---|
7394 |
|
---|
7395 | The result of the expansion of this function is the empty string.
|
---|
7396 |
|
---|
7397 | @item $(info @var{text}@dots{})
|
---|
7398 | @findex info
|
---|
7399 | @cindex printing messages
|
---|
7400 | This function does nothing more than print its (expanded) argument(s)
|
---|
7401 | to standard output. No makefile name or line number is added. The
|
---|
7402 | result of the expansion of this function is the empty string.
|
---|
7403 | @end table
|
---|
7404 |
|
---|
7405 | @node Running, Implicit Rules, Functions, Top
|
---|
7406 | @chapter How to Run @code{make}
|
---|
7407 |
|
---|
7408 | A makefile that says how to recompile a program can be used in more
|
---|
7409 | than one way. The simplest use is to recompile every file that is out
|
---|
7410 | of date. Usually, makefiles are written so that if you run
|
---|
7411 | @code{make} with no arguments, it does just that.
|
---|
7412 |
|
---|
7413 | But you might want to update only some of the files; you might want to use
|
---|
7414 | a different compiler or different compiler options; you might want just to
|
---|
7415 | find out which files are out of date without changing them.
|
---|
7416 |
|
---|
7417 | By giving arguments when you run @code{make}, you can do any of these
|
---|
7418 | things and many others.
|
---|
7419 |
|
---|
7420 | @cindex exit status of make
|
---|
7421 | The exit status of @code{make} is always one of three values:
|
---|
7422 | @table @code
|
---|
7423 | @item 0
|
---|
7424 | The exit status is zero if @code{make} is successful.
|
---|
7425 | @item 2
|
---|
7426 | The exit status is two if @code{make} encounters any errors.
|
---|
7427 | It will print messages describing the particular errors.
|
---|
7428 | @item 1
|
---|
7429 | The exit status is one if you use the @samp{-q} flag and @code{make}
|
---|
7430 | determines that some target is not already up to date.
|
---|
7431 | @xref{Instead of Execution, ,Instead of Executing the Commands}.
|
---|
7432 | @end table
|
---|
7433 |
|
---|
7434 | @menu
|
---|
7435 | * Makefile Arguments:: How to specify which makefile to use.
|
---|
7436 | * Goals:: How to use goal arguments to specify which
|
---|
7437 | parts of the makefile to use.
|
---|
7438 | * Instead of Execution:: How to use mode flags to specify what
|
---|
7439 | kind of thing to do with the commands
|
---|
7440 | in the makefile other than simply
|
---|
7441 | execute them.
|
---|
7442 | * Avoiding Compilation:: How to avoid recompiling certain files.
|
---|
7443 | * Overriding:: How to override a variable to specify
|
---|
7444 | an alternate compiler and other things.
|
---|
7445 | * Testing:: How to proceed past some errors, to
|
---|
7446 | test compilation.
|
---|
7447 | * Options Summary:: Summary of Options
|
---|
7448 | @end menu
|
---|
7449 |
|
---|
7450 | @node Makefile Arguments, Goals, Running, Running
|
---|
7451 | @section Arguments to Specify the Makefile
|
---|
7452 | @cindex @code{--file}
|
---|
7453 | @cindex @code{--makefile}
|
---|
7454 | @cindex @code{-f}
|
---|
7455 |
|
---|
7456 | The way to specify the name of the makefile is with the @samp{-f} or
|
---|
7457 | @samp{--file} option (@samp{--makefile} also works). For example,
|
---|
7458 | @samp{-f altmake} says to use the file @file{altmake} as the makefile.
|
---|
7459 |
|
---|
7460 | If you use the @samp{-f} flag several times and follow each @samp{-f}
|
---|
7461 | with an argument, all the specified files are used jointly as
|
---|
7462 | makefiles.
|
---|
7463 |
|
---|
7464 | If you do not use the @samp{-f} or @samp{--file} flag, the default is
|
---|
7465 | to try @file{GNUmakefile}, @file{makefile}, and @file{Makefile}, in
|
---|
7466 | that order, and use the first of these three which exists or can be made
|
---|
7467 | (@pxref{Makefiles, ,Writing Makefiles}).@refill
|
---|
7468 |
|
---|
7469 | @node Goals, Instead of Execution, Makefile Arguments, Running
|
---|
7470 | @section Arguments to Specify the Goals
|
---|
7471 | @cindex goal, how to specify
|
---|
7472 |
|
---|
7473 | The @dfn{goals} are the targets that @code{make} should strive ultimately
|
---|
7474 | to update. Other targets are updated as well if they appear as
|
---|
7475 | prerequisites of goals, or prerequisites of prerequisites of goals, etc.
|
---|
7476 |
|
---|
7477 | By default, the goal is the first target in the makefile (not counting
|
---|
7478 | targets that start with a period). Therefore, makefiles are usually
|
---|
7479 | written so that the first target is for compiling the entire program or
|
---|
7480 | programs they describe. If the first rule in the makefile has several
|
---|
7481 | targets, only the first target in the rule becomes the default goal, not
|
---|
7482 | the whole list. You can manage the selection of the default goal from
|
---|
7483 | within your makefile using the @code{.DEFAULT_GOAL} variable
|
---|
7484 | (@pxref{Special Variables, , Other Special Variables}).
|
---|
7485 |
|
---|
7486 | You can also specify a different goal or goals with command-line
|
---|
7487 | arguments to @code{make}. Use the name of the goal as an argument.
|
---|
7488 | If you specify several goals, @code{make} processes each of them in
|
---|
7489 | turn, in the order you name them.
|
---|
7490 |
|
---|
7491 | Any target in the makefile may be specified as a goal (unless it
|
---|
7492 | starts with @samp{-} or contains an @samp{=}, in which case it will be
|
---|
7493 | parsed as a switch or variable definition, respectively). Even
|
---|
7494 | targets not in the makefile may be specified, if @code{make} can find
|
---|
7495 | implicit rules that say how to make them.
|
---|
7496 |
|
---|
7497 | @vindex MAKECMDGOALS
|
---|
7498 | @code{Make} will set the special variable @code{MAKECMDGOALS} to the
|
---|
7499 | list of goals you specified on the command line. If no goals were given
|
---|
7500 | on the command line, this variable is empty. Note that this variable
|
---|
7501 | should be used only in special circumstances.
|
---|
7502 |
|
---|
7503 | An example of appropriate use is to avoid including @file{.d} files
|
---|
7504 | during @code{clean} rules (@pxref{Automatic Prerequisites}), so
|
---|
7505 | @code{make} won't create them only to immediately remove them
|
---|
7506 | again:@refill
|
---|
7507 |
|
---|
7508 | @example
|
---|
7509 | @group
|
---|
7510 | sources = foo.c bar.c
|
---|
7511 |
|
---|
7512 | ifneq ($(MAKECMDGOALS),clean)
|
---|
7513 | include $(sources:.c=.d)
|
---|
7514 | endif
|
---|
7515 | @end group
|
---|
7516 | @end example
|
---|
7517 |
|
---|
7518 | One use of specifying a goal is if you want to compile only a part of
|
---|
7519 | the program, or only one of several programs. Specify as a goal each
|
---|
7520 | file that you wish to remake. For example, consider a directory containing
|
---|
7521 | several programs, with a makefile that starts like this:
|
---|
7522 |
|
---|
7523 | @example
|
---|
7524 | .PHONY: all
|
---|
7525 | all: size nm ld ar as
|
---|
7526 | @end example
|
---|
7527 |
|
---|
7528 | If you are working on the program @code{size}, you might want to say
|
---|
7529 | @w{@samp{make size}} so that only the files of that program are recompiled.
|
---|
7530 |
|
---|
7531 | Another use of specifying a goal is to make files that are not normally
|
---|
7532 | made. For example, there may be a file of debugging output, or a
|
---|
7533 | version of the program that is compiled specially for testing, which has
|
---|
7534 | a rule in the makefile but is not a prerequisite of the default goal.
|
---|
7535 |
|
---|
7536 | Another use of specifying a goal is to run the commands associated with
|
---|
7537 | a phony target (@pxref{Phony Targets}) or empty target (@pxref{Empty
|
---|
7538 | Targets, ,Empty Target Files to Record Events}). Many makefiles contain
|
---|
7539 | a phony target named @file{clean} which deletes everything except source
|
---|
7540 | files. Naturally, this is done only if you request it explicitly with
|
---|
7541 | @w{@samp{make clean}}. Following is a list of typical phony and empty
|
---|
7542 | target names. @xref{Standard Targets}, for a detailed list of all the
|
---|
7543 | standard target names which GNU software packages use.
|
---|
7544 |
|
---|
7545 | @table @file
|
---|
7546 | @item all
|
---|
7547 | @cindex @code{all} @r{(standard target)}
|
---|
7548 | Make all the top-level targets the makefile knows about.
|
---|
7549 |
|
---|
7550 | @item clean
|
---|
7551 | @cindex @code{clean} @r{(standard target)}
|
---|
7552 | Delete all files that are normally created by running @code{make}.
|
---|
7553 |
|
---|
7554 | @item mostlyclean
|
---|
7555 | @cindex @code{mostlyclean} @r{(standard target)}
|
---|
7556 | Like @samp{clean}, but may refrain from deleting a few files that people
|
---|
7557 | normally don't want to recompile. For example, the @samp{mostlyclean}
|
---|
7558 | target for GCC does not delete @file{libgcc.a}, because recompiling it
|
---|
7559 | is rarely necessary and takes a lot of time.
|
---|
7560 |
|
---|
7561 | @item distclean
|
---|
7562 | @cindex @code{distclean} @r{(standard target)}
|
---|
7563 | @itemx realclean
|
---|
7564 | @cindex @code{realclean} @r{(standard target)}
|
---|
7565 | @itemx clobber
|
---|
7566 | @cindex @code{clobber} @r{(standard target)}
|
---|
7567 | Any of these targets might be defined to delete @emph{more} files than
|
---|
7568 | @samp{clean} does. For example, this would delete configuration files
|
---|
7569 | or links that you would normally create as preparation for compilation,
|
---|
7570 | even if the makefile itself cannot create these files.
|
---|
7571 |
|
---|
7572 | @item install
|
---|
7573 | @cindex @code{install} @r{(standard target)}
|
---|
7574 | Copy the executable file into a directory that users typically search
|
---|
7575 | for commands; copy any auxiliary files that the executable uses into
|
---|
7576 | the directories where it will look for them.
|
---|
7577 |
|
---|
7578 | @item print
|
---|
7579 | @cindex @code{print} @r{(standard target)}
|
---|
7580 | Print listings of the source files that have changed.
|
---|
7581 |
|
---|
7582 | @item tar
|
---|
7583 | @cindex @code{tar} @r{(standard target)}
|
---|
7584 | Create a tar file of the source files.
|
---|
7585 |
|
---|
7586 | @item shar
|
---|
7587 | @cindex @code{shar} @r{(standard target)}
|
---|
7588 | Create a shell archive (shar file) of the source files.
|
---|
7589 |
|
---|
7590 | @item dist
|
---|
7591 | @cindex @code{dist} @r{(standard target)}
|
---|
7592 | Create a distribution file of the source files. This might
|
---|
7593 | be a tar file, or a shar file, or a compressed version of one of the
|
---|
7594 | above, or even more than one of the above.
|
---|
7595 |
|
---|
7596 | @item TAGS
|
---|
7597 | @cindex @code{TAGS} @r{(standard target)}
|
---|
7598 | Update a tags table for this program.
|
---|
7599 |
|
---|
7600 | @item check
|
---|
7601 | @cindex @code{check} @r{(standard target)}
|
---|
7602 | @itemx test
|
---|
7603 | @cindex @code{test} @r{(standard target)}
|
---|
7604 | Perform self tests on the program this makefile builds.
|
---|
7605 | @end table
|
---|
7606 |
|
---|
7607 | @node Instead of Execution, Avoiding Compilation, Goals, Running
|
---|
7608 | @section Instead of Executing the Commands
|
---|
7609 | @cindex execution, instead of
|
---|
7610 | @cindex commands, instead of executing
|
---|
7611 |
|
---|
7612 | The makefile tells @code{make} how to tell whether a target is up to date,
|
---|
7613 | and how to update each target. But updating the targets is not always
|
---|
7614 | what you want. Certain options specify other activities for @code{make}.
|
---|
7615 |
|
---|
7616 | @comment Extra blank lines make it print better.
|
---|
7617 | @table @samp
|
---|
7618 | @item -n
|
---|
7619 | @itemx --just-print
|
---|
7620 | @itemx --dry-run
|
---|
7621 | @itemx --recon
|
---|
7622 | @cindex @code{--just-print}
|
---|
7623 | @cindex @code{--dry-run}
|
---|
7624 | @cindex @code{--recon}
|
---|
7625 | @cindex @code{-n}
|
---|
7626 |
|
---|
7627 | ``No-op''. The activity is to print what commands would be used to make
|
---|
7628 | the targets up to date, but not actually execute them.
|
---|
7629 |
|
---|
7630 | @item -t
|
---|
7631 | @itemx --touch
|
---|
7632 | @cindex @code{--touch}
|
---|
7633 | @cindex touching files
|
---|
7634 | @cindex target, touching
|
---|
7635 | @cindex @code{-t}
|
---|
7636 |
|
---|
7637 | ``Touch''. The activity is to mark the targets as up to date without
|
---|
7638 | actually changing them. In other words, @code{make} pretends to compile
|
---|
7639 | the targets but does not really change their contents.
|
---|
7640 |
|
---|
7641 | @item -q
|
---|
7642 | @itemx --question
|
---|
7643 | @cindex @code{--question}
|
---|
7644 | @cindex @code{-q}
|
---|
7645 | @cindex question mode
|
---|
7646 |
|
---|
7647 | ``Question''. The activity is to find out silently whether the targets
|
---|
7648 | are up to date already; but execute no commands in either case. In other
|
---|
7649 | words, neither compilation nor output will occur.
|
---|
7650 |
|
---|
7651 | @item -W @var{file}
|
---|
7652 | @itemx --what-if=@var{file}
|
---|
7653 | @itemx --assume-new=@var{file}
|
---|
7654 | @itemx --new-file=@var{file}
|
---|
7655 | @cindex @code{--what-if}
|
---|
7656 | @cindex @code{-W}
|
---|
7657 | @cindex @code{--assume-new}
|
---|
7658 | @cindex @code{--new-file}
|
---|
7659 | @cindex what if
|
---|
7660 | @cindex files, assuming new
|
---|
7661 |
|
---|
7662 | ``What if''. Each @samp{-W} flag is followed by a file name. The given
|
---|
7663 | files' modification times are recorded by @code{make} as being the present
|
---|
7664 | time, although the actual modification times remain the same.
|
---|
7665 | You can use the @samp{-W} flag in conjunction with the @samp{-n} flag
|
---|
7666 | to see what would happen if you were to modify specific files.@refill
|
---|
7667 | @end table
|
---|
7668 |
|
---|
7669 | With the @samp{-n} flag, @code{make} prints the commands that it would
|
---|
7670 | normally execute but does not execute them.
|
---|
7671 |
|
---|
7672 | With the @samp{-t} flag, @code{make} ignores the commands in the rules
|
---|
7673 | and uses (in effect) the command @code{touch} for each target that needs to
|
---|
7674 | be remade. The @code{touch} command is also printed, unless @samp{-s} or
|
---|
7675 | @code{.SILENT} is used. For speed, @code{make} does not actually invoke
|
---|
7676 | the program @code{touch}. It does the work directly.
|
---|
7677 |
|
---|
7678 | With the @samp{-q} flag, @code{make} prints nothing and executes no
|
---|
7679 | commands, but the exit status code it returns is zero if and only if the
|
---|
7680 | targets to be considered are already up to date. If the exit status is
|
---|
7681 | one, then some updating needs to be done. If @code{make} encounters an
|
---|
7682 | error, the exit status is two, so you can distinguish an error from a
|
---|
7683 | target that is not up to date.
|
---|
7684 |
|
---|
7685 | It is an error to use more than one of these three flags in the same
|
---|
7686 | invocation of @code{make}.
|
---|
7687 |
|
---|
7688 | @cindex +, and command execution
|
---|
7689 | The @samp{-n}, @samp{-t}, and @samp{-q} options do not affect command
|
---|
7690 | lines that begin with @samp{+} characters or contain the strings
|
---|
7691 | @samp{$(MAKE)} or @samp{$@{MAKE@}}. Note that only the line containing
|
---|
7692 | the @samp{+} character or the strings @samp{$(MAKE)} or @samp{$@{MAKE@}}
|
---|
7693 | is run regardless of these options. Other lines in the same rule are
|
---|
7694 | not run unless they too begin with @samp{+} or contain @samp{$(MAKE)} or
|
---|
7695 | @samp{$@{MAKE@}} (@xref{MAKE Variable, ,How the @code{MAKE} Variable Works}.)
|
---|
7696 |
|
---|
7697 | The @samp{-W} flag provides two features:
|
---|
7698 |
|
---|
7699 | @itemize @bullet
|
---|
7700 | @item
|
---|
7701 | If you also use the @samp{-n} or @samp{-q} flag, you can see what
|
---|
7702 | @code{make} would do if you were to modify some files.
|
---|
7703 |
|
---|
7704 | @item
|
---|
7705 | Without the @samp{-n} or @samp{-q} flag, when @code{make} is actually
|
---|
7706 | executing commands, the @samp{-W} flag can direct @code{make} to act
|
---|
7707 | as if some files had been modified, without actually modifying the
|
---|
7708 | files.@refill
|
---|
7709 | @end itemize
|
---|
7710 |
|
---|
7711 | Note that the options @samp{-p} and @samp{-v} allow you to obtain other
|
---|
7712 | information about @code{make} or about the makefiles in use
|
---|
7713 | (@pxref{Options Summary, ,Summary of Options}).@refill
|
---|
7714 |
|
---|
7715 | @node Avoiding Compilation, Overriding, Instead of Execution, Running
|
---|
7716 | @section Avoiding Recompilation of Some Files
|
---|
7717 | @cindex @code{-o}
|
---|
7718 | @cindex @code{--old-file}
|
---|
7719 | @cindex @code{--assume-old}
|
---|
7720 | @cindex files, assuming old
|
---|
7721 | @cindex files, avoiding recompilation of
|
---|
7722 | @cindex recompilation, avoiding
|
---|
7723 |
|
---|
7724 | Sometimes you may have changed a source file but you do not want to
|
---|
7725 | recompile all the files that depend on it. For example, suppose you add
|
---|
7726 | a macro or a declaration to a header file that many other files depend
|
---|
7727 | on. Being conservative, @code{make} assumes that any change in the
|
---|
7728 | header file requires recompilation of all dependent files, but you know
|
---|
7729 | that they do not need to be recompiled and you would rather not waste
|
---|
7730 | the time waiting for them to compile.
|
---|
7731 |
|
---|
7732 | If you anticipate the problem before changing the header file, you can
|
---|
7733 | use the @samp{-t} flag. This flag tells @code{make} not to run the
|
---|
7734 | commands in the rules, but rather to mark the target up to date by
|
---|
7735 | changing its last-modification date. You would follow this procedure:
|
---|
7736 |
|
---|
7737 | @enumerate
|
---|
7738 | @item
|
---|
7739 | Use the command @samp{make} to recompile the source files that really
|
---|
7740 | need recompilation, ensuring that the object files are up-to-date
|
---|
7741 | before you begin.
|
---|
7742 |
|
---|
7743 | @item
|
---|
7744 | Make the changes in the header files.
|
---|
7745 |
|
---|
7746 | @item
|
---|
7747 | Use the command @samp{make -t} to mark all the object files as
|
---|
7748 | up to date. The next time you run @code{make}, the changes in the
|
---|
7749 | header files will not cause any recompilation.
|
---|
7750 | @end enumerate
|
---|
7751 |
|
---|
7752 | If you have already changed the header file at a time when some files
|
---|
7753 | do need recompilation, it is too late to do this. Instead, you can
|
---|
7754 | use the @w{@samp{-o @var{file}}} flag, which marks a specified file as
|
---|
7755 | ``old'' (@pxref{Options Summary, ,Summary of Options}). This means
|
---|
7756 | that the file itself will not be remade, and nothing else will be
|
---|
7757 | remade on its account. Follow this procedure:
|
---|
7758 |
|
---|
7759 | @enumerate
|
---|
7760 | @item
|
---|
7761 | Recompile the source files that need compilation for reasons independent
|
---|
7762 | of the particular header file, with @samp{make -o @var{headerfile}}.
|
---|
7763 | If several header files are involved, use a separate @samp{-o} option
|
---|
7764 | for each header file.
|
---|
7765 |
|
---|
7766 | @item
|
---|
7767 | Touch all the object files with @samp{make -t}.
|
---|
7768 | @end enumerate
|
---|
7769 |
|
---|
7770 | @node Overriding, Testing, Avoiding Compilation, Running
|
---|
7771 | @section Overriding Variables
|
---|
7772 | @cindex overriding variables with arguments
|
---|
7773 | @cindex variables, overriding with arguments
|
---|
7774 | @cindex command line variables
|
---|
7775 | @cindex variables, command line
|
---|
7776 |
|
---|
7777 | An argument that contains @samp{=} specifies the value of a variable:
|
---|
7778 | @samp{@var{v}=@var{x}} sets the value of the variable @var{v} to @var{x}.
|
---|
7779 | If you specify a value in this way, all ordinary assignments of the same
|
---|
7780 | variable in the makefile are ignored; we say they have been
|
---|
7781 | @dfn{overridden} by the command line argument.
|
---|
7782 |
|
---|
7783 | The most common way to use this facility is to pass extra flags to
|
---|
7784 | compilers. For example, in a properly written makefile, the variable
|
---|
7785 | @code{CFLAGS} is included in each command that runs the C compiler, so a
|
---|
7786 | file @file{foo.c} would be compiled something like this:
|
---|
7787 |
|
---|
7788 | @example
|
---|
7789 | cc -c $(CFLAGS) foo.c
|
---|
7790 | @end example
|
---|
7791 |
|
---|
7792 | Thus, whatever value you set for @code{CFLAGS} affects each compilation
|
---|
7793 | that occurs. The makefile probably specifies the usual value for
|
---|
7794 | @code{CFLAGS}, like this:
|
---|
7795 |
|
---|
7796 | @example
|
---|
7797 | CFLAGS=-g
|
---|
7798 | @end example
|
---|
7799 |
|
---|
7800 | Each time you run @code{make}, you can override this value if you
|
---|
7801 | wish. For example, if you say @samp{make CFLAGS='-g -O'}, each C
|
---|
7802 | compilation will be done with @samp{cc -c -g -O}. (This also
|
---|
7803 | illustrates how you can use quoting in the shell to enclose spaces and
|
---|
7804 | other special characters in the value of a variable when you override
|
---|
7805 | it.)
|
---|
7806 |
|
---|
7807 | The variable @code{CFLAGS} is only one of many standard variables that
|
---|
7808 | exist just so that you can change them this way. @xref{Implicit
|
---|
7809 | Variables, , Variables Used by Implicit Rules}, for a complete list.
|
---|
7810 |
|
---|
7811 | You can also program the makefile to look at additional variables of your
|
---|
7812 | own, giving the user the ability to control other aspects of how the
|
---|
7813 | makefile works by changing the variables.
|
---|
7814 |
|
---|
7815 | When you override a variable with a command argument, you can define either
|
---|
7816 | a recursively-expanded variable or a simply-expanded variable. The
|
---|
7817 | examples shown above make a recursively-expanded variable; to make a
|
---|
7818 | simply-expanded variable, write @samp{:=} instead of @samp{=}. But, unless
|
---|
7819 | you want to include a variable reference or function call in the
|
---|
7820 | @emph{value} that you specify, it makes no difference which kind of
|
---|
7821 | variable you create.
|
---|
7822 |
|
---|
7823 | There is one way that the makefile can change a variable that you have
|
---|
7824 | overridden. This is to use the @code{override} directive, which is a line
|
---|
7825 | that looks like this: @samp{override @var{variable} = @var{value}}
|
---|
7826 | (@pxref{Override Directive, ,The @code{override} Directive}).
|
---|
7827 |
|
---|
7828 | @node Testing, Options Summary, Overriding, Running
|
---|
7829 | @section Testing the Compilation of a Program
|
---|
7830 | @cindex testing compilation
|
---|
7831 | @cindex compilation, testing
|
---|
7832 |
|
---|
7833 | Normally, when an error happens in executing a shell command, @code{make}
|
---|
7834 | gives up immediately, returning a nonzero status. No further commands are
|
---|
7835 | executed for any target. The error implies that the goal cannot be
|
---|
7836 | correctly remade, and @code{make} reports this as soon as it knows.
|
---|
7837 |
|
---|
7838 | When you are compiling a program that you have just changed, this is not
|
---|
7839 | what you want. Instead, you would rather that @code{make} try compiling
|
---|
7840 | every file that can be tried, to show you as many compilation errors
|
---|
7841 | as possible.
|
---|
7842 |
|
---|
7843 | @cindex @code{-k}
|
---|
7844 | @cindex @code{--keep-going}
|
---|
7845 | On these occasions, you should use the @samp{-k} or
|
---|
7846 | @samp{--keep-going} flag. This tells @code{make} to continue to
|
---|
7847 | consider the other prerequisites of the pending targets, remaking them
|
---|
7848 | if necessary, before it gives up and returns nonzero status. For
|
---|
7849 | example, after an error in compiling one object file, @samp{make -k}
|
---|
7850 | will continue compiling other object files even though it already
|
---|
7851 | knows that linking them will be impossible. In addition to continuing
|
---|
7852 | after failed shell commands, @samp{make -k} will continue as much as
|
---|
7853 | possible after discovering that it does not know how to make a target
|
---|
7854 | or prerequisite file. This will always cause an error message, but
|
---|
7855 | without @samp{-k}, it is a fatal error (@pxref{Options Summary,
|
---|
7856 | ,Summary of Options}).@refill
|
---|
7857 |
|
---|
7858 | The usual behavior of @code{make} assumes that your purpose is to get the
|
---|
7859 | goals up to date; once @code{make} learns that this is impossible, it might
|
---|
7860 | as well report the failure immediately. The @samp{-k} flag says that the
|
---|
7861 | real purpose is to test as much as possible of the changes made in the
|
---|
7862 | program, perhaps to find several independent problems so that you can
|
---|
7863 | correct them all before the next attempt to compile. This is why Emacs'
|
---|
7864 | @kbd{M-x compile} command passes the @samp{-k} flag by default.
|
---|
7865 |
|
---|
7866 | @node Options Summary, , Testing, Running
|
---|
7867 | @section Summary of Options
|
---|
7868 | @cindex options
|
---|
7869 | @cindex flags
|
---|
7870 | @cindex switches
|
---|
7871 |
|
---|
7872 | Here is a table of all the options @code{make} understands:
|
---|
7873 |
|
---|
7874 | @table @samp
|
---|
7875 | @item -b
|
---|
7876 | @cindex @code{-b}
|
---|
7877 | @itemx -m
|
---|
7878 | @cindex @code{-m}
|
---|
7879 | These options are ignored for compatibility with other versions of @code{make}.
|
---|
7880 |
|
---|
7881 | @item -B
|
---|
7882 | @cindex @code{-B}
|
---|
7883 | @itemx --always-make
|
---|
7884 | @cindex @code{--always-make}
|
---|
7885 | Consider all targets out-of-date. GNU @code{make} proceeds to
|
---|
7886 | consider targets and their prerequisites using the normal algorithms;
|
---|
7887 | however, all targets so considered are always remade regardless of the
|
---|
7888 | status of their prerequisites. To avoid infinite recursion, if
|
---|
7889 | @code{MAKE_RESTARTS} (@pxref{Special Variables, , Other Special
|
---|
7890 | Variables}) is set to a number greater than 0 this option is disabled
|
---|
7891 | when considering whether to remake makefiles (@pxref{Remaking
|
---|
7892 | Makefiles, , How Makefiles Are Remade}).
|
---|
7893 |
|
---|
7894 | @item -C @var{dir}
|
---|
7895 | @cindex @code{-C}
|
---|
7896 | @itemx --directory=@var{dir}
|
---|
7897 | @cindex @code{--directory}
|
---|
7898 | Change to directory @var{dir} before reading the makefiles. If multiple
|
---|
7899 | @samp{-C} options are specified, each is interpreted relative to the
|
---|
7900 | previous one: @samp{-C / -C etc} is equivalent to @samp{-C /etc}.
|
---|
7901 | This is typically used with recursive invocations of @code{make}
|
---|
7902 | (@pxref{Recursion, ,Recursive Use of @code{make}}).
|
---|
7903 |
|
---|
7904 | @item -d
|
---|
7905 | @cindex @code{-d}
|
---|
7906 | @c Extra blank line here makes the table look better.
|
---|
7907 |
|
---|
7908 | Print debugging information in addition to normal processing. The
|
---|
7909 | debugging information says which files are being considered for
|
---|
7910 | remaking, which file-times are being compared and with what results,
|
---|
7911 | which files actually need to be remade, which implicit rules are
|
---|
7912 | considered and which are applied---everything interesting about how
|
---|
7913 | @code{make} decides what to do. The @code{-d} option is equivalent to
|
---|
7914 | @samp{--debug=a} (see below).
|
---|
7915 |
|
---|
7916 | @item --debug[=@var{options}]
|
---|
7917 | @cindex @code{--debug}
|
---|
7918 | @c Extra blank line here makes the table look better.
|
---|
7919 |
|
---|
7920 | Print debugging information in addition to normal processing. Various
|
---|
7921 | levels and types of output can be chosen. With no arguments, print the
|
---|
7922 | ``basic'' level of debugging. Possible arguments are below; only the
|
---|
7923 | first character is considered, and values must be comma- or
|
---|
7924 | space-separated.
|
---|
7925 |
|
---|
7926 | @table @code
|
---|
7927 | @item a (@i{all})
|
---|
7928 | All types of debugging output are enabled. This is equivalent to using
|
---|
7929 | @samp{-d}.
|
---|
7930 |
|
---|
7931 | @item b (@i{basic})
|
---|
7932 | Basic debugging prints each target that was found to be out-of-date, and
|
---|
7933 | whether the build was successful or not.
|
---|
7934 |
|
---|
7935 | @item v (@i{verbose})
|
---|
7936 | A level above @samp{basic}; includes messages about which makefiles were
|
---|
7937 | parsed, prerequisites that did not need to be rebuilt, etc. This option
|
---|
7938 | also enables @samp{basic} messages.
|
---|
7939 |
|
---|
7940 | @item i (@i{implicit})
|
---|
7941 | Prints messages describing the implicit rule searches for each target.
|
---|
7942 | This option also enables @samp{basic} messages.
|
---|
7943 |
|
---|
7944 | @item j (@i{jobs})
|
---|
7945 | Prints messages giving details on the invocation of specific subcommands.
|
---|
7946 |
|
---|
7947 | @item m (@i{makefile})
|
---|
7948 | By default, the above messages are not enabled while trying to remake
|
---|
7949 | the makefiles. This option enables messages while rebuilding makefiles,
|
---|
7950 | too. Note that the @samp{all} option does enable this option. This
|
---|
7951 | option also enables @samp{basic} messages.
|
---|
7952 | @end table
|
---|
7953 |
|
---|
7954 | @item -e
|
---|
7955 | @cindex @code{-e}
|
---|
7956 | @itemx --environment-overrides
|
---|
7957 | @cindex @code{--environment-overrides}
|
---|
7958 | Give variables taken from the environment precedence
|
---|
7959 | over variables from makefiles.
|
---|
7960 | @xref{Environment, ,Variables from the Environment}.
|
---|
7961 |
|
---|
7962 | @item -f @var{file}
|
---|
7963 | @cindex @code{-f}
|
---|
7964 | @itemx --file=@var{file}
|
---|
7965 | @cindex @code{--file}
|
---|
7966 | @itemx --makefile=@var{file}
|
---|
7967 | @cindex @code{--makefile}
|
---|
7968 | Read the file named @var{file} as a makefile.
|
---|
7969 | @xref{Makefiles, ,Writing Makefiles}.
|
---|
7970 |
|
---|
7971 | @item -h
|
---|
7972 | @cindex @code{-h}
|
---|
7973 | @itemx --help
|
---|
7974 | @cindex @code{--help}
|
---|
7975 | @c Extra blank line here makes the table look better.
|
---|
7976 |
|
---|
7977 | Remind you of the options that @code{make} understands and then exit.
|
---|
7978 |
|
---|
7979 | @item -i
|
---|
7980 | @cindex @code{-i}
|
---|
7981 | @itemx --ignore-errors
|
---|
7982 | @cindex @code{--ignore-errors}
|
---|
7983 | Ignore all errors in commands executed to remake files.
|
---|
7984 | @xref{Errors, ,Errors in Commands}.
|
---|
7985 |
|
---|
7986 | @item -I @var{dir}
|
---|
7987 | @cindex @code{-I}
|
---|
7988 | @itemx --include-dir=@var{dir}
|
---|
7989 | @cindex @code{--include-dir}
|
---|
7990 | Specifies a directory @var{dir} to search for included makefiles.
|
---|
7991 | @xref{Include, ,Including Other Makefiles}. If several @samp{-I}
|
---|
7992 | options are used to specify several directories, the directories are
|
---|
7993 | searched in the order specified.
|
---|
7994 |
|
---|
7995 | @item -j [@var{jobs}]
|
---|
7996 | @cindex @code{-j}
|
---|
7997 | @itemx --jobs[=@var{jobs}]
|
---|
7998 | @cindex @code{--jobs}
|
---|
7999 | Specifies the number of jobs (commands) to run simultaneously. With no
|
---|
8000 | argument, @code{make} runs as many jobs simultaneously as possible. If
|
---|
8001 | there is more than one @samp{-j} option, the last one is effective.
|
---|
8002 | @xref{Parallel, ,Parallel Execution},
|
---|
8003 | for more information on how commands are run.
|
---|
8004 | Note that this option is ignored on MS-DOS.
|
---|
8005 |
|
---|
8006 | @item -k
|
---|
8007 | @cindex @code{-k}
|
---|
8008 | @itemx --keep-going
|
---|
8009 | @cindex @code{--keep-going}
|
---|
8010 | Continue as much as possible after an error. While the target that
|
---|
8011 | failed, and those that depend on it, cannot be remade, the other
|
---|
8012 | prerequisites of these targets can be processed all the same.
|
---|
8013 | @xref{Testing, ,Testing the Compilation of a Program}.
|
---|
8014 |
|
---|
8015 | @item -l [@var{load}]
|
---|
8016 | @cindex @code{-l}
|
---|
8017 | @itemx --load-average[=@var{load}]
|
---|
8018 | @cindex @code{--load-average}
|
---|
8019 | @itemx --max-load[=@var{load}]
|
---|
8020 | @cindex @code{--max-load}
|
---|
8021 | Specifies that no new jobs (commands) should be started if there are
|
---|
8022 | other jobs running and the load average is at least @var{load} (a
|
---|
8023 | floating-point number). With no argument, removes a previous load
|
---|
8024 | limit. @xref{Parallel, ,Parallel Execution}.
|
---|
8025 |
|
---|
8026 | @item -L
|
---|
8027 | @cindex @code{-L}
|
---|
8028 | @itemx --check-symlink-times
|
---|
8029 | @cindex @code{--check-symlink-times}
|
---|
8030 | On systems that support symbolic links, this option causes @code{make}
|
---|
8031 | to consider the timestamps on any symbolic links in addition to the
|
---|
8032 | timestamp on the file referenced by those links. When this option is
|
---|
8033 | provided, the most recent timestamp among the file and the symbolic
|
---|
8034 | links is taken as the modification time for this target file.
|
---|
8035 |
|
---|
8036 | @item -n
|
---|
8037 | @cindex @code{-n}
|
---|
8038 | @itemx --just-print
|
---|
8039 | @cindex @code{--just-print}
|
---|
8040 | @itemx --dry-run
|
---|
8041 | @cindex @code{--dry-run}
|
---|
8042 | @itemx --recon
|
---|
8043 | @cindex @code{--recon}
|
---|
8044 | @c Extra blank line here makes the table look better.
|
---|
8045 |
|
---|
8046 | Print the commands that would be executed, but do not execute them.
|
---|
8047 | @xref{Instead of Execution, ,Instead of Executing the Commands}.
|
---|
8048 |
|
---|
8049 | @item -o @var{file}
|
---|
8050 | @cindex @code{-o}
|
---|
8051 | @itemx --old-file=@var{file}
|
---|
8052 | @cindex @code{--old-file}
|
---|
8053 | @itemx --assume-old=@var{file}
|
---|
8054 | @cindex @code{--assume-old}
|
---|
8055 | Do not remake the file @var{file} even if it is older than its
|
---|
8056 | prerequisites, and do not remake anything on account of changes in
|
---|
8057 | @var{file}. Essentially the file is treated as very old and its rules
|
---|
8058 | are ignored. @xref{Avoiding Compilation, ,Avoiding Recompilation of
|
---|
8059 | Some Files}.@refill
|
---|
8060 |
|
---|
8061 | @item -p
|
---|
8062 | @cindex @code{-p}
|
---|
8063 | @itemx --print-data-base
|
---|
8064 | @cindex @code{--print-data-base}
|
---|
8065 | @cindex data base of @code{make} rules
|
---|
8066 | @cindex predefined rules and variables, printing
|
---|
8067 | Print the data base (rules and variable values) that results from
|
---|
8068 | reading the makefiles; then execute as usual or as otherwise specified.
|
---|
8069 | This also prints the version information given by the @samp{-v} switch
|
---|
8070 | (see below). To print the data base without trying to remake any files,
|
---|
8071 | use @w{@samp{make -qp}}. To print the data base of predefined rules and
|
---|
8072 | variables, use @w{@samp{make -p -f /dev/null}}. The data base output
|
---|
8073 | contains filename and linenumber information for command and variable
|
---|
8074 | definitions, so it can be a useful debugging tool in complex environments.
|
---|
8075 |
|
---|
8076 | @item -q
|
---|
8077 | @cindex @code{-q}
|
---|
8078 | @itemx --question
|
---|
8079 | @cindex @code{--question}
|
---|
8080 | ``Question mode''. Do not run any commands, or print anything; just
|
---|
8081 | return an exit status that is zero if the specified targets are already
|
---|
8082 | up to date, one if any remaking is required, or two if an error is
|
---|
8083 | encountered. @xref{Instead of Execution, ,Instead of Executing the
|
---|
8084 | Commands}.@refill
|
---|
8085 |
|
---|
8086 | @item -r
|
---|
8087 | @cindex @code{-r}
|
---|
8088 | @itemx --no-builtin-rules
|
---|
8089 | @cindex @code{--no-builtin-rules}
|
---|
8090 | Eliminate use of the built-in implicit rules (@pxref{Implicit Rules,
|
---|
8091 | ,Using Implicit Rules}). You can still define your own by writing
|
---|
8092 | pattern rules (@pxref{Pattern Rules, ,Defining and Redefining Pattern
|
---|
8093 | Rules}). The @samp{-r} option also clears out the default list of
|
---|
8094 | suffixes for suffix rules (@pxref{Suffix Rules, ,Old-Fashioned Suffix
|
---|
8095 | Rules}). But you can still define your own suffixes with a rule for
|
---|
8096 | @code{.SUFFIXES}, and then define your own suffix rules. Note that only
|
---|
8097 | @emph{rules} are affected by the @code{-r} option; default variables
|
---|
8098 | remain in effect (@pxref{Implicit Variables, ,Variables Used by Implicit
|
---|
8099 | Rules}); see the @samp{-R} option below.
|
---|
8100 |
|
---|
8101 | @item -R
|
---|
8102 | @cindex @code{-R}
|
---|
8103 | @itemx --no-builtin-variables
|
---|
8104 | @cindex @code{--no-builtin-variables}
|
---|
8105 | Eliminate use of the built-in rule-specific variables (@pxref{Implicit
|
---|
8106 | Variables, ,Variables Used by Implicit Rules}). You can still define
|
---|
8107 | your own, of course. The @samp{-R} option also automatically enables
|
---|
8108 | the @samp{-r} option (see above), since it doesn't make sense to have
|
---|
8109 | implicit rules without any definitions for the variables that they use.
|
---|
8110 |
|
---|
8111 | @item -s
|
---|
8112 | @cindex @code{-s}
|
---|
8113 | @itemx --silent
|
---|
8114 | @cindex @code{--silent}
|
---|
8115 | @itemx --quiet
|
---|
8116 | @cindex @code{--quiet}
|
---|
8117 | @c Extra blank line here makes the table look better.
|
---|
8118 |
|
---|
8119 | Silent operation; do not print the commands as they are executed.
|
---|
8120 | @xref{Echoing, ,Command Echoing}.
|
---|
8121 |
|
---|
8122 | @item -S
|
---|
8123 | @cindex @code{-S}
|
---|
8124 | @itemx --no-keep-going
|
---|
8125 | @cindex @code{--no-keep-going}
|
---|
8126 | @itemx --stop
|
---|
8127 | @cindex @code{--stop}
|
---|
8128 | @c Extra blank line here makes the table look better.
|
---|
8129 |
|
---|
8130 | Cancel the effect of the @samp{-k} option. This is never necessary
|
---|
8131 | except in a recursive @code{make} where @samp{-k} might be inherited
|
---|
8132 | from the top-level @code{make} via @code{MAKEFLAGS}
|
---|
8133 | (@pxref{Recursion, ,Recursive Use of @code{make}})
|
---|
8134 | or if you set @samp{-k} in @code{MAKEFLAGS} in your environment.@refill
|
---|
8135 |
|
---|
8136 | @item -t
|
---|
8137 | @cindex @code{-t}
|
---|
8138 | @itemx --touch
|
---|
8139 | @cindex @code{--touch}
|
---|
8140 | @c Extra blank line here makes the table look better.
|
---|
8141 |
|
---|
8142 | Touch files (mark them up to date without really changing them)
|
---|
8143 | instead of running their commands. This is used to pretend that the
|
---|
8144 | commands were done, in order to fool future invocations of
|
---|
8145 | @code{make}. @xref{Instead of Execution, ,Instead of Executing the Commands}.
|
---|
8146 |
|
---|
8147 | @item -v
|
---|
8148 | @cindex @code{-v}
|
---|
8149 | @itemx --version
|
---|
8150 | @cindex @code{--version}
|
---|
8151 | Print the version of the @code{make} program plus a copyright, a list
|
---|
8152 | of authors, and a notice that there is no warranty; then exit.
|
---|
8153 |
|
---|
8154 | @item -w
|
---|
8155 | @cindex @code{-w}
|
---|
8156 | @itemx --print-directory
|
---|
8157 | @cindex @code{--print-directory}
|
---|
8158 | Print a message containing the working directory both before and after
|
---|
8159 | executing the makefile. This may be useful for tracking down errors
|
---|
8160 | from complicated nests of recursive @code{make} commands.
|
---|
8161 | @xref{Recursion, ,Recursive Use of @code{make}}. (In practice, you
|
---|
8162 | rarely need to specify this option since @samp{make} does it for you;
|
---|
8163 | see @ref{-w Option, ,The @samp{--print-directory} Option}.)
|
---|
8164 |
|
---|
8165 | @itemx --no-print-directory
|
---|
8166 | @cindex @code{--no-print-directory}
|
---|
8167 | Disable printing of the working directory under @code{-w}.
|
---|
8168 | This option is useful when @code{-w} is turned on automatically,
|
---|
8169 | but you do not want to see the extra messages.
|
---|
8170 | @xref{-w Option, ,The @samp{--print-directory} Option}.
|
---|
8171 |
|
---|
8172 | @item -W @var{file}
|
---|
8173 | @cindex @code{-W}
|
---|
8174 | @itemx --what-if=@var{file}
|
---|
8175 | @cindex @code{--what-if}
|
---|
8176 | @itemx --new-file=@var{file}
|
---|
8177 | @cindex @code{--new-file}
|
---|
8178 | @itemx --assume-new=@var{file}
|
---|
8179 | @cindex @code{--assume-new}
|
---|
8180 | Pretend that the target @var{file} has just been modified. When used
|
---|
8181 | with the @samp{-n} flag, this shows you what would happen if you were
|
---|
8182 | to modify that file. Without @samp{-n}, it is almost the same as
|
---|
8183 | running a @code{touch} command on the given file before running
|
---|
8184 | @code{make}, except that the modification time is changed only in the
|
---|
8185 | imagination of @code{make}.
|
---|
8186 | @xref{Instead of Execution, ,Instead of Executing the Commands}.
|
---|
8187 |
|
---|
8188 | @item --warn-undefined-variables
|
---|
8189 | @cindex @code{--warn-undefined-variables}
|
---|
8190 | @cindex variables, warning for undefined
|
---|
8191 | @cindex undefined variables, warning message
|
---|
8192 | Issue a warning message whenever @code{make} sees a reference to an
|
---|
8193 | undefined variable. This can be helpful when you are trying to debug
|
---|
8194 | makefiles which use variables in complex ways.
|
---|
8195 | @end table
|
---|
8196 |
|
---|
8197 | @node Implicit Rules, Archives, Running, Top
|
---|
8198 | @chapter Using Implicit Rules
|
---|
8199 | @cindex implicit rule
|
---|
8200 | @cindex rule, implicit
|
---|
8201 |
|
---|
8202 | Certain standard ways of remaking target files are used very often. For
|
---|
8203 | example, one customary way to make an object file is from a C source file
|
---|
8204 | using the C compiler, @code{cc}.
|
---|
8205 |
|
---|
8206 | @dfn{Implicit rules} tell @code{make} how to use customary techniques so
|
---|
8207 | that you do not have to specify them in detail when you want to use
|
---|
8208 | them. For example, there is an implicit rule for C compilation. File
|
---|
8209 | names determine which implicit rules are run. For example, C
|
---|
8210 | compilation typically takes a @file{.c} file and makes a @file{.o} file.
|
---|
8211 | So @code{make} applies the implicit rule for C compilation when it sees
|
---|
8212 | this combination of file name endings.@refill
|
---|
8213 |
|
---|
8214 | A chain of implicit rules can apply in sequence; for example, @code{make}
|
---|
8215 | will remake a @file{.o} file from a @file{.y} file by way of a @file{.c} file.
|
---|
8216 | @iftex
|
---|
8217 | @xref{Chained Rules, ,Chains of Implicit Rules}.
|
---|
8218 | @end iftex
|
---|
8219 |
|
---|
8220 | The built-in implicit rules use several variables in their commands so
|
---|
8221 | that, by changing the values of the variables, you can change the way the
|
---|
8222 | implicit rule works. For example, the variable @code{CFLAGS} controls the
|
---|
8223 | flags given to the C compiler by the implicit rule for C compilation.
|
---|
8224 | @iftex
|
---|
8225 | @xref{Implicit Variables, ,Variables Used by Implicit Rules}.
|
---|
8226 | @end iftex
|
---|
8227 |
|
---|
8228 | You can define your own implicit rules by writing @dfn{pattern rules}.
|
---|
8229 | @iftex
|
---|
8230 | @xref{Pattern Rules, ,Defining and Redefining Pattern Rules}.
|
---|
8231 | @end iftex
|
---|
8232 |
|
---|
8233 | @dfn{Suffix rules} are a more limited way to define implicit rules.
|
---|
8234 | Pattern rules are more general and clearer, but suffix rules are
|
---|
8235 | retained for compatibility.
|
---|
8236 | @iftex
|
---|
8237 | @xref{Suffix Rules, ,Old-Fashioned Suffix Rules}.
|
---|
8238 | @end iftex
|
---|
8239 |
|
---|
8240 | @menu
|
---|
8241 | * Using Implicit:: How to use an existing implicit rule
|
---|
8242 | to get the commands for updating a file.
|
---|
8243 | * Catalogue of Rules:: A list of built-in implicit rules.
|
---|
8244 | * Implicit Variables:: How to change what predefined rules do.
|
---|
8245 | * Chained Rules:: How to use a chain of implicit rules.
|
---|
8246 | * Pattern Rules:: How to define new implicit rules.
|
---|
8247 | * Last Resort:: How to define commands for rules which
|
---|
8248 | cannot find any.
|
---|
8249 | * Suffix Rules:: The old-fashioned style of implicit rule.
|
---|
8250 | * Implicit Rule Search:: The precise algorithm for applying
|
---|
8251 | implicit rules.
|
---|
8252 | @end menu
|
---|
8253 |
|
---|
8254 | @node Using Implicit, Catalogue of Rules, Implicit Rules, Implicit Rules
|
---|
8255 | @section Using Implicit Rules
|
---|
8256 | @cindex implicit rule, how to use
|
---|
8257 | @cindex rule, implicit, how to use
|
---|
8258 |
|
---|
8259 | To allow @code{make} to find a customary method for updating a target file,
|
---|
8260 | all you have to do is refrain from specifying commands yourself. Either
|
---|
8261 | write a rule with no command lines, or don't write a rule at all. Then
|
---|
8262 | @code{make} will figure out which implicit rule to use based on which
|
---|
8263 | kind of source file exists or can be made.
|
---|
8264 |
|
---|
8265 | For example, suppose the makefile looks like this:
|
---|
8266 |
|
---|
8267 | @example
|
---|
8268 | foo : foo.o bar.o
|
---|
8269 | cc -o foo foo.o bar.o $(CFLAGS) $(LDFLAGS)
|
---|
8270 | @end example
|
---|
8271 |
|
---|
8272 | @noindent
|
---|
8273 | Because you mention @file{foo.o} but do not give a rule for it, @code{make}
|
---|
8274 | will automatically look for an implicit rule that tells how to update it.
|
---|
8275 | This happens whether or not the file @file{foo.o} currently exists.
|
---|
8276 |
|
---|
8277 | If an implicit rule is found, it can supply both commands and one or
|
---|
8278 | more prerequisites (the source files). You would want to write a rule
|
---|
8279 | for @file{foo.o} with no command lines if you need to specify additional
|
---|
8280 | prerequisites, such as header files, that the implicit rule cannot
|
---|
8281 | supply.
|
---|
8282 |
|
---|
8283 | Each implicit rule has a target pattern and prerequisite patterns. There may
|
---|
8284 | be many implicit rules with the same target pattern. For example, numerous
|
---|
8285 | rules make @samp{.o} files: one, from a @samp{.c} file with the C compiler;
|
---|
8286 | another, from a @samp{.p} file with the Pascal compiler; and so on. The rule
|
---|
8287 | that actually applies is the one whose prerequisites exist or can be made.
|
---|
8288 | So, if you have a file @file{foo.c}, @code{make} will run the C compiler;
|
---|
8289 | otherwise, if you have a file @file{foo.p}, @code{make} will run the Pascal
|
---|
8290 | compiler; and so on.
|
---|
8291 |
|
---|
8292 | Of course, when you write the makefile, you know which implicit rule you
|
---|
8293 | want @code{make} to use, and you know it will choose that one because you
|
---|
8294 | know which possible prerequisite files are supposed to exist.
|
---|
8295 | @xref{Catalogue of Rules, ,Catalogue of Implicit Rules},
|
---|
8296 | for a catalogue of all the predefined implicit rules.
|
---|
8297 |
|
---|
8298 | Above, we said an implicit rule applies if the required prerequisites ``exist
|
---|
8299 | or can be made''. A file ``can be made'' if it is mentioned explicitly in
|
---|
8300 | the makefile as a target or a prerequisite, or if an implicit rule can be
|
---|
8301 | recursively found for how to make it. When an implicit prerequisite is the
|
---|
8302 | result of another implicit rule, we say that @dfn{chaining} is occurring.
|
---|
8303 | @xref{Chained Rules, ,Chains of Implicit Rules}.
|
---|
8304 |
|
---|
8305 | In general, @code{make} searches for an implicit rule for each target, and
|
---|
8306 | for each double-colon rule, that has no commands. A file that is mentioned
|
---|
8307 | only as a prerequisite is considered a target whose rule specifies nothing,
|
---|
8308 | so implicit rule search happens for it. @xref{Implicit Rule Search, ,Implicit Rule Search Algorithm}, for the
|
---|
8309 | details of how the search is done.
|
---|
8310 |
|
---|
8311 | Note that explicit prerequisites do not influence implicit rule search.
|
---|
8312 | For example, consider this explicit rule:
|
---|
8313 |
|
---|
8314 | @example
|
---|
8315 | foo.o: foo.p
|
---|
8316 | @end example
|
---|
8317 |
|
---|
8318 | @noindent
|
---|
8319 | The prerequisite on @file{foo.p} does not necessarily mean that
|
---|
8320 | @code{make} will remake @file{foo.o} according to the implicit rule to
|
---|
8321 | make an object file, a @file{.o} file, from a Pascal source file, a
|
---|
8322 | @file{.p} file. For example, if @file{foo.c} also exists, the implicit
|
---|
8323 | rule to make an object file from a C source file is used instead,
|
---|
8324 | because it appears before the Pascal rule in the list of predefined
|
---|
8325 | implicit rules (@pxref{Catalogue of Rules, , Catalogue of Implicit
|
---|
8326 | Rules}).
|
---|
8327 |
|
---|
8328 | If you do not want an implicit rule to be used for a target that has no
|
---|
8329 | commands, you can give that target empty commands by writing a semicolon
|
---|
8330 | (@pxref{Empty Commands, ,Defining Empty Commands}).
|
---|
8331 |
|
---|
8332 | @node Catalogue of Rules, Implicit Variables, Using Implicit, Implicit Rules
|
---|
8333 | @section Catalogue of Implicit Rules
|
---|
8334 | @cindex implicit rule, predefined
|
---|
8335 | @cindex rule, implicit, predefined
|
---|
8336 |
|
---|
8337 | Here is a catalogue of predefined implicit rules which are always
|
---|
8338 | available unless the makefile explicitly overrides or cancels them.
|
---|
8339 | @xref{Canceling Rules, ,Canceling Implicit Rules}, for information on
|
---|
8340 | canceling or overriding an implicit rule. The @samp{-r} or
|
---|
8341 | @samp{--no-builtin-rules} option cancels all predefined rules.
|
---|
8342 |
|
---|
8343 | This manual only documents the default rules available on POSIX-based
|
---|
8344 | operating systems. Other operating systems, such as VMS, Windows,
|
---|
8345 | OS/2, etc. may have different sets of default rules. To see the full
|
---|
8346 | list of default rules and variables available in your version of GNU
|
---|
8347 | @code{make}, run @samp{make -p} in a directory with no makefile.
|
---|
8348 |
|
---|
8349 | Not all of these rules will always be defined, even when the @samp{-r}
|
---|
8350 | option is not given. Many of the predefined implicit rules are
|
---|
8351 | implemented in @code{make} as suffix rules, so which ones will be
|
---|
8352 | defined depends on the @dfn{suffix list} (the list of prerequisites of
|
---|
8353 | the special target @code{.SUFFIXES}). The default suffix list is:
|
---|
8354 | @code{.out}, @code{.a}, @code{.ln}, @code{.o}, @code{.c}, @code{.cc},
|
---|
8355 | @code{.C}, @code{.cpp}, @code{.p}, @code{.f}, @code{.F}, @code{.r}, @code{.y},
|
---|
8356 | @code{.l}, @code{.s}, @code{.S}, @code{.mod}, @code{.sym}, @code{.def},
|
---|
8357 | @code{.h}, @code{.info}, @code{.dvi}, @code{.tex}, @code{.texinfo},
|
---|
8358 | @code{.texi}, @code{.txinfo}, @code{.w}, @code{.ch} @code{.web},
|
---|
8359 | @code{.sh}, @code{.elc}, @code{.el}. All of the implicit rules
|
---|
8360 | described below whose prerequisites have one of these suffixes are
|
---|
8361 | actually suffix rules. If you modify the suffix list, the only
|
---|
8362 | predefined suffix rules in effect will be those named by one or two of
|
---|
8363 | the suffixes that are on the list you specify; rules whose suffixes fail
|
---|
8364 | to be on the list are disabled. @xref{Suffix Rules, ,Old-Fashioned
|
---|
8365 | Suffix Rules}, for full details on suffix rules.
|
---|
8366 |
|
---|
8367 | @table @asis
|
---|
8368 | @item Compiling C programs
|
---|
8369 | @cindex C, rule to compile
|
---|
8370 | @pindex cc
|
---|
8371 | @pindex gcc
|
---|
8372 | @pindex .o
|
---|
8373 | @pindex .c
|
---|
8374 | @file{@var{n}.o} is made automatically from @file{@var{n}.c} with
|
---|
8375 | a command of the form @samp{$(CC) -c $(CPPFLAGS) $(CFLAGS)}.@refill
|
---|
8376 |
|
---|
8377 | @item Compiling C++ programs
|
---|
8378 | @cindex C++, rule to compile
|
---|
8379 | @pindex g++
|
---|
8380 | @pindex .cc
|
---|
8381 | @pindex .cpp
|
---|
8382 | @pindex .C
|
---|
8383 | @file{@var{n}.o} is made automatically from @file{@var{n}.cc},
|
---|
8384 | @file{@var{n}.cpp}, or @file{@var{n}.C} with a command of the form
|
---|
8385 | @samp{$(CXX) -c $(CPPFLAGS) $(CXXFLAGS)}. We encourage you to use the
|
---|
8386 | suffix @samp{.cc} for C++ source files instead of @samp{.C}.@refill
|
---|
8387 |
|
---|
8388 | @item Compiling Pascal programs
|
---|
8389 | @cindex Pascal, rule to compile
|
---|
8390 | @pindex pc
|
---|
8391 | @pindex .p
|
---|
8392 | @file{@var{n}.o} is made automatically from @file{@var{n}.p}
|
---|
8393 | with the command @samp{$(PC) -c $(PFLAGS)}.@refill
|
---|
8394 |
|
---|
8395 | @item Compiling Fortran and Ratfor programs
|
---|
8396 | @cindex Fortran, rule to compile
|
---|
8397 | @cindex Ratfor, rule to compile
|
---|
8398 | @pindex f77
|
---|
8399 | @pindex .f
|
---|
8400 | @pindex .r
|
---|
8401 | @pindex .F
|
---|
8402 | @file{@var{n}.o} is made automatically from @file{@var{n}.r},
|
---|
8403 | @file{@var{n}.F} or @file{@var{n}.f} by running the
|
---|
8404 | Fortran compiler. The precise command used is as follows:@refill
|
---|
8405 |
|
---|
8406 | @table @samp
|
---|
8407 | @item .f
|
---|
8408 | @samp{$(FC) -c $(FFLAGS)}.
|
---|
8409 | @item .F
|
---|
8410 | @samp{$(FC) -c $(FFLAGS) $(CPPFLAGS)}.
|
---|
8411 | @item .r
|
---|
8412 | @samp{$(FC) -c $(FFLAGS) $(RFLAGS)}.
|
---|
8413 | @end table
|
---|
8414 |
|
---|
8415 | @item Preprocessing Fortran and Ratfor programs
|
---|
8416 | @file{@var{n}.f} is made automatically from @file{@var{n}.r} or
|
---|
8417 | @file{@var{n}.F}. This rule runs just the preprocessor to convert a
|
---|
8418 | Ratfor or preprocessable Fortran program into a strict Fortran
|
---|
8419 | program. The precise command used is as follows:@refill
|
---|
8420 |
|
---|
8421 | @table @samp
|
---|
8422 | @item .F
|
---|
8423 | @samp{$(FC) -F $(CPPFLAGS) $(FFLAGS)}.
|
---|
8424 | @item .r
|
---|
8425 | @samp{$(FC) -F $(FFLAGS) $(RFLAGS)}.
|
---|
8426 | @end table
|
---|
8427 |
|
---|
8428 | @item Compiling Modula-2 programs
|
---|
8429 | @cindex Modula-2, rule to compile
|
---|
8430 | @pindex m2c
|
---|
8431 | @pindex .sym
|
---|
8432 | @pindex .def
|
---|
8433 | @pindex .mod
|
---|
8434 | @file{@var{n}.sym} is made from @file{@var{n}.def} with a command
|
---|
8435 | of the form @samp{$(M2C) $(M2FLAGS) $(DEFFLAGS)}. @file{@var{n}.o}
|
---|
8436 | is made from @file{@var{n}.mod}; the form is:
|
---|
8437 | @w{@samp{$(M2C) $(M2FLAGS) $(MODFLAGS)}}.@refill
|
---|
8438 |
|
---|
8439 | @need 1200
|
---|
8440 | @item Assembling and preprocessing assembler programs
|
---|
8441 | @cindex assembly, rule to compile
|
---|
8442 | @pindex as
|
---|
8443 | @pindex .s
|
---|
8444 | @file{@var{n}.o} is made automatically from @file{@var{n}.s} by
|
---|
8445 | running the assembler, @code{as}. The precise command is
|
---|
8446 | @samp{$(AS) $(ASFLAGS)}.@refill
|
---|
8447 |
|
---|
8448 | @pindex .S
|
---|
8449 | @file{@var{n}.s} is made automatically from @file{@var{n}.S} by
|
---|
8450 | running the C preprocessor, @code{cpp}. The precise command is
|
---|
8451 | @w{@samp{$(CPP) $(CPPFLAGS)}}.
|
---|
8452 |
|
---|
8453 | @item Linking a single object file
|
---|
8454 | @cindex linking, predefined rule for
|
---|
8455 | @pindex ld
|
---|
8456 | @pindex .o
|
---|
8457 | @file{@var{n}} is made automatically from @file{@var{n}.o} by running
|
---|
8458 | the linker (usually called @code{ld}) via the C compiler. The precise
|
---|
8459 | command used is @w{@samp{$(CC) $(LDFLAGS) @var{n}.o $(LOADLIBES) $(LDLIBS)}}.
|
---|
8460 |
|
---|
8461 | This rule does the right thing for a simple program with only one
|
---|
8462 | source file. It will also do the right thing if there are multiple
|
---|
8463 | object files (presumably coming from various other source files), one
|
---|
8464 | of which has a name matching that of the executable file. Thus,
|
---|
8465 |
|
---|
8466 | @example
|
---|
8467 | x: y.o z.o
|
---|
8468 | @end example
|
---|
8469 |
|
---|
8470 | @noindent
|
---|
8471 | when @file{x.c}, @file{y.c} and @file{z.c} all exist will execute:
|
---|
8472 |
|
---|
8473 | @example
|
---|
8474 | @group
|
---|
8475 | cc -c x.c -o x.o
|
---|
8476 | cc -c y.c -o y.o
|
---|
8477 | cc -c z.c -o z.o
|
---|
8478 | cc x.o y.o z.o -o x
|
---|
8479 | rm -f x.o
|
---|
8480 | rm -f y.o
|
---|
8481 | rm -f z.o
|
---|
8482 | @end group
|
---|
8483 | @end example
|
---|
8484 |
|
---|
8485 | @noindent
|
---|
8486 | In more complicated cases, such as when there is no object file whose
|
---|
8487 | name derives from the executable file name, you must write an explicit
|
---|
8488 | command for linking.
|
---|
8489 |
|
---|
8490 | Each kind of file automatically made into @samp{.o} object files will
|
---|
8491 | be automatically linked by using the compiler (@samp{$(CC)},
|
---|
8492 | @samp{$(FC)} or @samp{$(PC)}; the C compiler @samp{$(CC)} is used to
|
---|
8493 | assemble @samp{.s} files) without the @samp{-c} option. This could be
|
---|
8494 | done by using the @samp{.o} object files as intermediates, but it is
|
---|
8495 | faster to do the compiling and linking in one step, so that's how it's
|
---|
8496 | done.@refill
|
---|
8497 |
|
---|
8498 | @item Yacc for C programs
|
---|
8499 | @pindex yacc
|
---|
8500 | @cindex Yacc, rule to run
|
---|
8501 | @pindex .y
|
---|
8502 | @file{@var{n}.c} is made automatically from @file{@var{n}.y} by
|
---|
8503 | running Yacc with the command @samp{$(YACC) $(YFLAGS)}.
|
---|
8504 |
|
---|
8505 | @item Lex for C programs
|
---|
8506 | @pindex lex
|
---|
8507 | @cindex Lex, rule to run
|
---|
8508 | @pindex .l
|
---|
8509 | @file{@var{n}.c} is made automatically from @file{@var{n}.l} by
|
---|
8510 | running Lex. The actual command is @samp{$(LEX) $(LFLAGS)}.
|
---|
8511 |
|
---|
8512 | @item Lex for Ratfor programs
|
---|
8513 | @file{@var{n}.r} is made automatically from @file{@var{n}.l} by
|
---|
8514 | running Lex. The actual command is @samp{$(LEX) $(LFLAGS)}.
|
---|
8515 |
|
---|
8516 | The convention of using the same suffix @samp{.l} for all Lex files
|
---|
8517 | regardless of whether they produce C code or Ratfor code makes it
|
---|
8518 | impossible for @code{make} to determine automatically which of the two
|
---|
8519 | languages you are using in any particular case. If @code{make} is
|
---|
8520 | called upon to remake an object file from a @samp{.l} file, it must
|
---|
8521 | guess which compiler to use. It will guess the C compiler, because
|
---|
8522 | that is more common. If you are using Ratfor, make sure @code{make}
|
---|
8523 | knows this by mentioning @file{@var{n}.r} in the makefile. Or, if you
|
---|
8524 | are using Ratfor exclusively, with no C files, remove @samp{.c} from
|
---|
8525 | the list of implicit rule suffixes with:@refill
|
---|
8526 |
|
---|
8527 | @example
|
---|
8528 | @group
|
---|
8529 | .SUFFIXES:
|
---|
8530 | .SUFFIXES: .o .r .f .l @dots{}
|
---|
8531 | @end group
|
---|
8532 | @end example
|
---|
8533 |
|
---|
8534 | @item Making Lint Libraries from C, Yacc, or Lex programs
|
---|
8535 | @pindex lint
|
---|
8536 | @cindex @code{lint}, rule to run
|
---|
8537 | @pindex .ln
|
---|
8538 | @file{@var{n}.ln} is made from @file{@var{n}.c} by running @code{lint}.
|
---|
8539 | The precise command is @w{@samp{$(LINT) $(LINTFLAGS) $(CPPFLAGS) -i}}.
|
---|
8540 | The same command is used on the C code produced from
|
---|
8541 | @file{@var{n}.y} or @file{@var{n}.l}.@refill
|
---|
8542 |
|
---|
8543 | @item @TeX{} and Web
|
---|
8544 | @cindex @TeX{}, rule to run
|
---|
8545 | @cindex Web, rule to run
|
---|
8546 | @pindex tex
|
---|
8547 | @pindex cweave
|
---|
8548 | @pindex weave
|
---|
8549 | @pindex tangle
|
---|
8550 | @pindex ctangle
|
---|
8551 | @pindex .dvi
|
---|
8552 | @pindex .tex
|
---|
8553 | @pindex .web
|
---|
8554 | @pindex .w
|
---|
8555 | @pindex .ch
|
---|
8556 | @file{@var{n}.dvi} is made from @file{@var{n}.tex} with the command
|
---|
8557 | @samp{$(TEX)}. @file{@var{n}.tex} is made from @file{@var{n}.web} with
|
---|
8558 | @samp{$(WEAVE)}, or from @file{@var{n}.w} (and from @file{@var{n}.ch} if
|
---|
8559 | it exists or can be made) with @samp{$(CWEAVE)}. @file{@var{n}.p} is
|
---|
8560 | made from @file{@var{n}.web} with @samp{$(TANGLE)} and @file{@var{n}.c}
|
---|
8561 | is made from @file{@var{n}.w} (and from @file{@var{n}.ch} if it exists
|
---|
8562 | or can be made) with @samp{$(CTANGLE)}.@refill
|
---|
8563 |
|
---|
8564 | @item Texinfo and Info
|
---|
8565 | @cindex Texinfo, rule to format
|
---|
8566 | @cindex Info, rule to format
|
---|
8567 | @pindex texi2dvi
|
---|
8568 | @pindex makeinfo
|
---|
8569 | @pindex .texinfo
|
---|
8570 | @pindex .info
|
---|
8571 | @pindex .texi
|
---|
8572 | @pindex .txinfo
|
---|
8573 | @file{@var{n}.dvi} is made from @file{@var{n}.texinfo},
|
---|
8574 | @file{@var{n}.texi}, or @file{@var{n}.txinfo}, with the command
|
---|
8575 | @w{@samp{$(TEXI2DVI) $(TEXI2DVI_FLAGS)}}. @file{@var{n}.info} is made from
|
---|
8576 | @file{@var{n}.texinfo}, @file{@var{n}.texi}, or @file{@var{n}.txinfo}, with
|
---|
8577 | the command @w{@samp{$(MAKEINFO) $(MAKEINFO_FLAGS)}}.
|
---|
8578 |
|
---|
8579 | @item RCS
|
---|
8580 | @cindex RCS, rule to extract from
|
---|
8581 | @pindex co
|
---|
8582 | @pindex ,v @r{(RCS file extension)}
|
---|
8583 | Any file @file{@var{n}} is extracted if necessary from an RCS file
|
---|
8584 | named either @file{@var{n},v} or @file{RCS/@var{n},v}. The precise
|
---|
8585 | command used is @w{@samp{$(CO) $(COFLAGS)}}. @file{@var{n}} will not be
|
---|
8586 | extracted from RCS if it already exists, even if the RCS file is
|
---|
8587 | newer. The rules for RCS are terminal
|
---|
8588 | (@pxref{Match-Anything Rules, ,Match-Anything Pattern Rules}),
|
---|
8589 | so RCS files cannot be generated from another source; they must
|
---|
8590 | actually exist.@refill
|
---|
8591 |
|
---|
8592 | @item SCCS
|
---|
8593 | @cindex SCCS, rule to extract from
|
---|
8594 | @pindex get
|
---|
8595 | @pindex s. @r{(SCCS file prefix)}
|
---|
8596 | Any file @file{@var{n}} is extracted if necessary from an SCCS file
|
---|
8597 | named either @file{s.@var{n}} or @file{SCCS/s.@var{n}}. The precise
|
---|
8598 | command used is @w{@samp{$(GET) $(GFLAGS)}}. The rules for SCCS are
|
---|
8599 | terminal (@pxref{Match-Anything Rules, ,Match-Anything Pattern Rules}),
|
---|
8600 | so SCCS files cannot be generated from another source; they must
|
---|
8601 | actually exist.@refill
|
---|
8602 |
|
---|
8603 | @pindex .sh
|
---|
8604 | For the benefit of SCCS, a file @file{@var{n}} is copied from
|
---|
8605 | @file{@var{n}.sh} and made executable (by everyone). This is for
|
---|
8606 | shell scripts that are checked into SCCS. Since RCS preserves the
|
---|
8607 | execution permission of a file, you do not need to use this feature
|
---|
8608 | with RCS.@refill
|
---|
8609 |
|
---|
8610 | We recommend that you avoid using of SCCS. RCS is widely held to be
|
---|
8611 | superior, and is also free. By choosing free software in place of
|
---|
8612 | comparable (or inferior) proprietary software, you support the free
|
---|
8613 | software movement.
|
---|
8614 | @end table
|
---|
8615 |
|
---|
8616 | Usually, you want to change only the variables listed in the table
|
---|
8617 | above, which are documented in the following section.
|
---|
8618 |
|
---|
8619 | However, the commands in built-in implicit rules actually use
|
---|
8620 | variables such as @code{COMPILE.c}, @code{LINK.p}, and
|
---|
8621 | @code{PREPROCESS.S}, whose values contain the commands listed above.
|
---|
8622 |
|
---|
8623 | @code{make} follows the convention that the rule to compile a
|
---|
8624 | @file{.@var{x}} source file uses the variable @code{COMPILE.@var{x}}.
|
---|
8625 | Similarly, the rule to produce an executable from a @file{.@var{x}}
|
---|
8626 | file uses @code{LINK.@var{x}}; and the rule to preprocess a
|
---|
8627 | @file{.@var{x}} file uses @code{PREPROCESS.@var{x}}.
|
---|
8628 |
|
---|
8629 | @vindex OUTPUT_OPTION
|
---|
8630 | Every rule that produces an object file uses the variable
|
---|
8631 | @code{OUTPUT_OPTION}. @code{make} defines this variable either to
|
---|
8632 | contain @samp{-o $@@}, or to be empty, depending on a compile-time
|
---|
8633 | option. You need the @samp{-o} option to ensure that the output goes
|
---|
8634 | into the right file when the source file is in a different directory,
|
---|
8635 | as when using @code{VPATH} (@pxref{Directory Search}). However,
|
---|
8636 | compilers on some systems do not accept a @samp{-o} switch for object
|
---|
8637 | files. If you use such a system, and use @code{VPATH}, some
|
---|
8638 | compilations will put their output in the wrong place.
|
---|
8639 | A possible workaround for this problem is to give @code{OUTPUT_OPTION}
|
---|
8640 | the value @w{@samp{; mv $*.o $@@}}.
|
---|
8641 |
|
---|
8642 | @node Implicit Variables, Chained Rules, Catalogue of Rules, Implicit Rules
|
---|
8643 | @section Variables Used by Implicit Rules
|
---|
8644 | @cindex flags for compilers
|
---|
8645 |
|
---|
8646 | The commands in built-in implicit rules make liberal use of certain
|
---|
8647 | predefined variables. You can alter the values of these variables in
|
---|
8648 | the makefile, with arguments to @code{make}, or in the environment to
|
---|
8649 | alter how the implicit rules work without redefining the rules
|
---|
8650 | themselves. You can cancel all variables used by implicit rules with
|
---|
8651 | the @samp{-R} or @samp{--no-builtin-variables} option.
|
---|
8652 |
|
---|
8653 | For example, the command used to compile a C source file actually says
|
---|
8654 | @samp{$(CC) -c $(CFLAGS) $(CPPFLAGS)}. The default values of the variables
|
---|
8655 | used are @samp{cc} and nothing, resulting in the command @samp{cc -c}. By
|
---|
8656 | redefining @samp{CC} to @samp{ncc}, you could cause @samp{ncc} to be
|
---|
8657 | used for all C compilations performed by the implicit rule. By redefining
|
---|
8658 | @samp{CFLAGS} to be @samp{-g}, you could pass the @samp{-g} option to
|
---|
8659 | each compilation. @emph{All} implicit rules that do C compilation use
|
---|
8660 | @samp{$(CC)} to get the program name for the compiler and @emph{all}
|
---|
8661 | include @samp{$(CFLAGS)} among the arguments given to the compiler.@refill
|
---|
8662 |
|
---|
8663 | The variables used in implicit rules fall into two classes: those that are
|
---|
8664 | names of programs (like @code{CC}) and those that contain arguments for the
|
---|
8665 | programs (like @code{CFLAGS}). (The ``name of a program'' may also contain
|
---|
8666 | some command arguments, but it must start with an actual executable program
|
---|
8667 | name.) If a variable value contains more than one argument, separate them
|
---|
8668 | with spaces.
|
---|
8669 |
|
---|
8670 | The following tables describe of some of the more commonly-used predefined
|
---|
8671 | variables. This list is not exhaustive, and the default values shown here may
|
---|
8672 | not be what are selected by @code{make} for your environment. To see the
|
---|
8673 | complete list of predefined variables for your instance of GNU @code{make} you
|
---|
8674 | can run @samp{make -p} in a directory with no makefiles.
|
---|
8675 |
|
---|
8676 | Here is a table of some of the more common variables used as names of
|
---|
8677 | programs in built-in rules:
|
---|
8678 | makefiles.
|
---|
8679 |
|
---|
8680 | @table @code
|
---|
8681 | @item AR
|
---|
8682 | @vindex AR
|
---|
8683 | Archive-maintaining program; default @samp{ar}.
|
---|
8684 | @pindex ar
|
---|
8685 |
|
---|
8686 | @item AS
|
---|
8687 | @vindex AS
|
---|
8688 | Program for compiling assembly files; default @samp{as}.
|
---|
8689 | @pindex as
|
---|
8690 |
|
---|
8691 | @item CC
|
---|
8692 | @vindex CC
|
---|
8693 | Program for compiling C programs; default @samp{cc}.
|
---|
8694 | @pindex cc
|
---|
8695 |
|
---|
8696 | @item CO
|
---|
8697 | @vindex CO
|
---|
8698 | Program for checking out files from RCS; default @samp{co}.
|
---|
8699 | @pindex cc
|
---|
8700 |
|
---|
8701 | @item CXX
|
---|
8702 | @vindex CXX
|
---|
8703 | Program for compiling C++ programs; default @samp{g++}.
|
---|
8704 | @pindex g++
|
---|
8705 |
|
---|
8706 | @item CO
|
---|
8707 | @vindex CO
|
---|
8708 | Program for extracting a file from RCS; default @samp{co}.
|
---|
8709 | @pindex co
|
---|
8710 |
|
---|
8711 | @item CPP
|
---|
8712 | @vindex CPP
|
---|
8713 | Program for running the C preprocessor, with results to standard output;
|
---|
8714 | default @samp{$(CC) -E}.
|
---|
8715 |
|
---|
8716 | @item FC
|
---|
8717 | @vindex FC
|
---|
8718 | Program for compiling or preprocessing Fortran and Ratfor programs;
|
---|
8719 | default @samp{f77}.
|
---|
8720 | @pindex f77
|
---|
8721 |
|
---|
8722 | @item GET
|
---|
8723 | @vindex GET
|
---|
8724 | Program for extracting a file from SCCS; default @samp{get}.
|
---|
8725 | @pindex get
|
---|
8726 |
|
---|
8727 | @item LEX
|
---|
8728 | @vindex LEX
|
---|
8729 | Program to use to turn Lex grammars into source code; default @samp{lex}.
|
---|
8730 | @pindex lex
|
---|
8731 |
|
---|
8732 | @item YACC
|
---|
8733 | @vindex YACC
|
---|
8734 | Program to use to turn Yacc grammars into source code; default @samp{yacc}.
|
---|
8735 | @pindex yacc
|
---|
8736 |
|
---|
8737 | @item LINT
|
---|
8738 | @vindex LINT
|
---|
8739 | Program to use to run lint on source code; default @samp{lint}.
|
---|
8740 | @pindex lint
|
---|
8741 |
|
---|
8742 | @item M2C
|
---|
8743 | @vindex M2C
|
---|
8744 | Program to use to compile Modula-2 source code; default @samp{m2c}.
|
---|
8745 | @pindex m2c
|
---|
8746 |
|
---|
8747 | @item PC
|
---|
8748 | @vindex PC
|
---|
8749 | Program for compiling Pascal programs; default @samp{pc}.
|
---|
8750 | @pindex pc
|
---|
8751 |
|
---|
8752 | @item MAKEINFO
|
---|
8753 | @vindex MAKEINFO
|
---|
8754 | Program to convert a Texinfo source file into an Info file; default
|
---|
8755 | @samp{makeinfo}.
|
---|
8756 | @pindex makeinfo
|
---|
8757 |
|
---|
8758 | @item TEX
|
---|
8759 | @vindex TEX
|
---|
8760 | Program to make @TeX{} @sc{dvi} files from @TeX{} source;
|
---|
8761 | default @samp{tex}.
|
---|
8762 | @pindex tex
|
---|
8763 |
|
---|
8764 | @item TEXI2DVI
|
---|
8765 | @vindex TEXI2DVI
|
---|
8766 | Program to make @TeX{} @sc{dvi} files from Texinfo source;
|
---|
8767 | default @samp{texi2dvi}.
|
---|
8768 | @pindex texi2dvi
|
---|
8769 |
|
---|
8770 | @item WEAVE
|
---|
8771 | @vindex WEAVE
|
---|
8772 | Program to translate Web into @TeX{}; default @samp{weave}.
|
---|
8773 | @pindex weave
|
---|
8774 |
|
---|
8775 | @item CWEAVE
|
---|
8776 | @vindex CWEAVE
|
---|
8777 | Program to translate C Web into @TeX{}; default @samp{cweave}.
|
---|
8778 | @pindex cweave
|
---|
8779 |
|
---|
8780 | @item TANGLE
|
---|
8781 | @vindex TANGLE
|
---|
8782 | Program to translate Web into Pascal; default @samp{tangle}.
|
---|
8783 | @pindex tangle
|
---|
8784 |
|
---|
8785 | @item CTANGLE
|
---|
8786 | @vindex CTANGLE
|
---|
8787 | Program to translate C Web into C; default @samp{ctangle}.
|
---|
8788 | @pindex ctangle
|
---|
8789 |
|
---|
8790 | @item RM
|
---|
8791 | @vindex RM
|
---|
8792 | Command to remove a file; default @samp{rm -f}.
|
---|
8793 | @pindex rm
|
---|
8794 | @end table
|
---|
8795 |
|
---|
8796 | Here is a table of variables whose values are additional arguments for the
|
---|
8797 | programs above. The default values for all of these is the empty
|
---|
8798 | string, unless otherwise noted.
|
---|
8799 |
|
---|
8800 | @table @code
|
---|
8801 | @item ARFLAGS
|
---|
8802 | @vindex ARFLAGS
|
---|
8803 | Flags to give the archive-maintaining program; default @samp{rv}.
|
---|
8804 |
|
---|
8805 | @item ASFLAGS
|
---|
8806 | @vindex ASFLAGS
|
---|
8807 | Extra flags to give to the assembler (when explicitly
|
---|
8808 | invoked on a @samp{.s} or @samp{.S} file).
|
---|
8809 |
|
---|
8810 | @item CFLAGS
|
---|
8811 | @vindex CFLAGS
|
---|
8812 | Extra flags to give to the C compiler.
|
---|
8813 |
|
---|
8814 | @item CXXFLAGS
|
---|
8815 | @vindex CXXFLAGS
|
---|
8816 | Extra flags to give to the C++ compiler.
|
---|
8817 |
|
---|
8818 | @item COFLAGS
|
---|
8819 | @vindex COFLAGS
|
---|
8820 | Extra flags to give to the RCS @code{co} program.
|
---|
8821 |
|
---|
8822 | @item CPPFLAGS
|
---|
8823 | @vindex CPPFLAGS
|
---|
8824 | Extra flags to give to the C preprocessor and programs
|
---|
8825 | that use it (the C and Fortran compilers).
|
---|
8826 |
|
---|
8827 | @item FFLAGS
|
---|
8828 | @vindex FFLAGS
|
---|
8829 | Extra flags to give to the Fortran compiler.
|
---|
8830 |
|
---|
8831 | @item GFLAGS
|
---|
8832 | @vindex GFLAGS
|
---|
8833 | Extra flags to give to the SCCS @code{get} program.
|
---|
8834 |
|
---|
8835 | @item LDFLAGS
|
---|
8836 | @vindex LDFLAGS
|
---|
8837 | Extra flags to give to compilers when they are supposed to invoke the linker,
|
---|
8838 | @samp{ld}.
|
---|
8839 |
|
---|
8840 | @item LFLAGS
|
---|
8841 | @vindex LFLAGS
|
---|
8842 | Extra flags to give to Lex.
|
---|
8843 |
|
---|
8844 | @item YFLAGS
|
---|
8845 | @vindex YFLAGS
|
---|
8846 | Extra flags to give to Yacc.
|
---|
8847 |
|
---|
8848 | @item PFLAGS
|
---|
8849 | @vindex PFLAGS
|
---|
8850 | Extra flags to give to the Pascal compiler.
|
---|
8851 |
|
---|
8852 | @item RFLAGS
|
---|
8853 | @vindex RFLAGS
|
---|
8854 | Extra flags to give to the Fortran compiler for Ratfor programs.
|
---|
8855 |
|
---|
8856 | @item LINTFLAGS
|
---|
8857 | @vindex LINTFLAGS
|
---|
8858 | Extra flags to give to lint.
|
---|
8859 | @end table
|
---|
8860 |
|
---|
8861 | @node Chained Rules, Pattern Rules, Implicit Variables, Implicit Rules
|
---|
8862 | @section Chains of Implicit Rules
|
---|
8863 |
|
---|
8864 | @cindex chains of rules
|
---|
8865 | @cindex rule, implicit, chains of
|
---|
8866 | Sometimes a file can be made by a sequence of implicit rules. For example,
|
---|
8867 | a file @file{@var{n}.o} could be made from @file{@var{n}.y} by running
|
---|
8868 | first Yacc and then @code{cc}. Such a sequence is called a @dfn{chain}.
|
---|
8869 |
|
---|
8870 | If the file @file{@var{n}.c} exists, or is mentioned in the makefile, no
|
---|
8871 | special searching is required: @code{make} finds that the object file can
|
---|
8872 | be made by C compilation from @file{@var{n}.c}; later on, when considering
|
---|
8873 | how to make @file{@var{n}.c}, the rule for running Yacc is
|
---|
8874 | used. Ultimately both @file{@var{n}.c} and @file{@var{n}.o} are
|
---|
8875 | updated.@refill
|
---|
8876 |
|
---|
8877 | @cindex intermediate files
|
---|
8878 | @cindex files, intermediate
|
---|
8879 | However, even if @file{@var{n}.c} does not exist and is not mentioned,
|
---|
8880 | @code{make} knows how to envision it as the missing link between
|
---|
8881 | @file{@var{n}.o} and @file{@var{n}.y}! In this case, @file{@var{n}.c} is
|
---|
8882 | called an @dfn{intermediate file}. Once @code{make} has decided to use the
|
---|
8883 | intermediate file, it is entered in the data base as if it had been
|
---|
8884 | mentioned in the makefile, along with the implicit rule that says how to
|
---|
8885 | create it.@refill
|
---|
8886 |
|
---|
8887 | Intermediate files are remade using their rules just like all other
|
---|
8888 | files. But intermediate files are treated differently in two ways.
|
---|
8889 |
|
---|
8890 | The first difference is what happens if the intermediate file does not
|
---|
8891 | exist. If an ordinary file @var{b} does not exist, and @code{make}
|
---|
8892 | considers a target that depends on @var{b}, it invariably creates
|
---|
8893 | @var{b} and then updates the target from @var{b}. But if @var{b} is an
|
---|
8894 | intermediate file, then @code{make} can leave well enough alone. It
|
---|
8895 | won't bother updating @var{b}, or the ultimate target, unless some
|
---|
8896 | prerequisite of @var{b} is newer than that target or there is some other
|
---|
8897 | reason to update that target.
|
---|
8898 |
|
---|
8899 | The second difference is that if @code{make} @emph{does} create @var{b}
|
---|
8900 | in order to update something else, it deletes @var{b} later on after it
|
---|
8901 | is no longer needed. Therefore, an intermediate file which did not
|
---|
8902 | exist before @code{make} also does not exist after @code{make}.
|
---|
8903 | @code{make} reports the deletion to you by printing a @samp{rm -f}
|
---|
8904 | command showing which file it is deleting.
|
---|
8905 |
|
---|
8906 | Ordinarily, a file cannot be intermediate if it is mentioned in the
|
---|
8907 | makefile as a target or prerequisite. However, you can explicitly mark a
|
---|
8908 | file as intermediate by listing it as a prerequisite of the special target
|
---|
8909 | @code{.INTERMEDIATE}. This takes effect even if the file is mentioned
|
---|
8910 | explicitly in some other way.
|
---|
8911 |
|
---|
8912 | @cindex intermediate files, preserving
|
---|
8913 | @cindex preserving intermediate files
|
---|
8914 | @cindex secondary files
|
---|
8915 | You can prevent automatic deletion of an intermediate file by marking it
|
---|
8916 | as a @dfn{secondary} file. To do this, list it as a prerequisite of the
|
---|
8917 | special target @code{.SECONDARY}. When a file is secondary, @code{make}
|
---|
8918 | will not create the file merely because it does not already exist, but
|
---|
8919 | @code{make} does not automatically delete the file. Marking a file as
|
---|
8920 | secondary also marks it as intermediate.
|
---|
8921 |
|
---|
8922 | You can list the target pattern of an implicit rule (such as @samp{%.o})
|
---|
8923 | as a prerequisite of the special target @code{.PRECIOUS} to preserve
|
---|
8924 | intermediate files made by implicit rules whose target patterns match
|
---|
8925 | that file's name; see @ref{Interrupts}.@refill
|
---|
8926 | @cindex preserving with @code{.PRECIOUS}
|
---|
8927 | @cindex @code{.PRECIOUS} intermediate files
|
---|
8928 |
|
---|
8929 | A chain can involve more than two implicit rules. For example, it is
|
---|
8930 | possible to make a file @file{foo} from @file{RCS/foo.y,v} by running RCS,
|
---|
8931 | Yacc and @code{cc}. Then both @file{foo.y} and @file{foo.c} are
|
---|
8932 | intermediate files that are deleted at the end.@refill
|
---|
8933 |
|
---|
8934 | No single implicit rule can appear more than once in a chain. This means
|
---|
8935 | that @code{make} will not even consider such a ridiculous thing as making
|
---|
8936 | @file{foo} from @file{foo.o.o} by running the linker twice. This
|
---|
8937 | constraint has the added benefit of preventing any infinite loop in the
|
---|
8938 | search for an implicit rule chain.
|
---|
8939 |
|
---|
8940 | There are some special implicit rules to optimize certain cases that would
|
---|
8941 | otherwise be handled by rule chains. For example, making @file{foo} from
|
---|
8942 | @file{foo.c} could be handled by compiling and linking with separate
|
---|
8943 | chained rules, using @file{foo.o} as an intermediate file. But what
|
---|
8944 | actually happens is that a special rule for this case does the compilation
|
---|
8945 | and linking with a single @code{cc} command. The optimized rule is used in
|
---|
8946 | preference to the step-by-step chain because it comes earlier in the
|
---|
8947 | ordering of rules.
|
---|
8948 |
|
---|
8949 | @node Pattern Rules, Last Resort, Chained Rules, Implicit Rules
|
---|
8950 | @section Defining and Redefining Pattern Rules
|
---|
8951 |
|
---|
8952 | You define an implicit rule by writing a @dfn{pattern rule}. A pattern
|
---|
8953 | rule looks like an ordinary rule, except that its target contains the
|
---|
8954 | character @samp{%} (exactly one of them). The target is considered a
|
---|
8955 | pattern for matching file names; the @samp{%} can match any nonempty
|
---|
8956 | substring, while other characters match only themselves. The prerequisites
|
---|
8957 | likewise use @samp{%} to show how their names relate to the target name.
|
---|
8958 |
|
---|
8959 | Thus, a pattern rule @samp{%.o : %.c} says how to make any file
|
---|
8960 | @file{@var{stem}.o} from another file @file{@var{stem}.c}.@refill
|
---|
8961 |
|
---|
8962 | Note that expansion using @samp{%} in pattern rules occurs
|
---|
8963 | @strong{after} any variable or function expansions, which take place
|
---|
8964 | when the makefile is read. @xref{Using Variables, , How to Use
|
---|
8965 | Variables}, and @ref{Functions, ,Functions for Transforming Text}.
|
---|
8966 |
|
---|
8967 | @menu
|
---|
8968 | * Pattern Intro:: An introduction to pattern rules.
|
---|
8969 | * Pattern Examples:: Examples of pattern rules.
|
---|
8970 | * Automatic Variables:: How to use automatic variables in the
|
---|
8971 | commands of implicit rules.
|
---|
8972 | * Pattern Match:: How patterns match.
|
---|
8973 | * Match-Anything Rules:: Precautions you should take prior to
|
---|
8974 | defining rules that can match any
|
---|
8975 | target file whatever.
|
---|
8976 | * Canceling Rules:: How to override or cancel built-in rules.
|
---|
8977 | @end menu
|
---|
8978 |
|
---|
8979 | @node Pattern Intro, Pattern Examples, Pattern Rules, Pattern Rules
|
---|
8980 | @subsection Introduction to Pattern Rules
|
---|
8981 | @cindex pattern rule
|
---|
8982 | @cindex rule, pattern
|
---|
8983 |
|
---|
8984 | A pattern rule contains the character @samp{%} (exactly one of them)
|
---|
8985 | in the target; otherwise, it looks exactly like an ordinary rule. The
|
---|
8986 | target is a pattern for matching file names; the @samp{%} matches any
|
---|
8987 | nonempty substring, while other characters match only themselves.
|
---|
8988 | @cindex target pattern, implicit
|
---|
8989 | @cindex @code{%}, in pattern rules
|
---|
8990 |
|
---|
8991 | For example, @samp{%.c} as a pattern matches any file name that ends in
|
---|
8992 | @samp{.c}. @samp{s.%.c} as a pattern matches any file name that starts
|
---|
8993 | with @samp{s.}, ends in @samp{.c} and is at least five characters long.
|
---|
8994 | (There must be at least one character to match the @samp{%}.) The substring
|
---|
8995 | that the @samp{%} matches is called the @dfn{stem}.@refill
|
---|
8996 |
|
---|
8997 | @samp{%} in a prerequisite of a pattern rule stands for the same stem
|
---|
8998 | that was matched by the @samp{%} in the target. In order for the
|
---|
8999 | pattern rule to apply, its target pattern must match the file name
|
---|
9000 | under consideration and all of its prerequisites (after pattern
|
---|
9001 | substitution) must name files that exist or can be made. These files
|
---|
9002 | become prerequisites of the target.
|
---|
9003 | @cindex prerequisite pattern, implicit
|
---|
9004 |
|
---|
9005 | Thus, a rule of the form
|
---|
9006 |
|
---|
9007 | @example
|
---|
9008 | %.o : %.c ; @var{command}@dots{}
|
---|
9009 | @end example
|
---|
9010 |
|
---|
9011 | @noindent
|
---|
9012 | specifies how to make a file @file{@var{n}.o}, with another file
|
---|
9013 | @file{@var{n}.c} as its prerequisite, provided that @file{@var{n}.c}
|
---|
9014 | exists or can be made.
|
---|
9015 |
|
---|
9016 | There may also be prerequisites that do not use @samp{%}; such a prerequisite
|
---|
9017 | attaches to every file made by this pattern rule. These unvarying
|
---|
9018 | prerequisites are useful occasionally.
|
---|
9019 |
|
---|
9020 | A pattern rule need not have any prerequisites that contain @samp{%}, or
|
---|
9021 | in fact any prerequisites at all. Such a rule is effectively a general
|
---|
9022 | wildcard. It provides a way to make any file that matches the target
|
---|
9023 | pattern. @xref{Last Resort}.
|
---|
9024 |
|
---|
9025 | @c !!! The end of of this paragraph should be rewritten. --bob
|
---|
9026 | Pattern rules may have more than one target. Unlike normal rules, this
|
---|
9027 | does not act as many different rules with the same prerequisites and
|
---|
9028 | commands. If a pattern rule has multiple targets, @code{make} knows that
|
---|
9029 | the rule's commands are responsible for making all of the targets. The
|
---|
9030 | commands are executed only once to make all the targets. When searching
|
---|
9031 | for a pattern rule to match a target, the target patterns of a rule other
|
---|
9032 | than the one that matches the target in need of a rule are incidental:
|
---|
9033 | @code{make} worries only about giving commands and prerequisites to the file
|
---|
9034 | presently in question. However, when this file's commands are run, the
|
---|
9035 | other targets are marked as having been updated themselves.
|
---|
9036 | @cindex multiple targets, in pattern rule
|
---|
9037 | @cindex target, multiple in pattern rule
|
---|
9038 |
|
---|
9039 | The order in which pattern rules appear in the makefile is important
|
---|
9040 | since this is the order in which they are considered.
|
---|
9041 | Of equally applicable
|
---|
9042 | rules, only the first one found is used. The rules you write take precedence
|
---|
9043 | over those that are built in. Note however, that a rule whose
|
---|
9044 | prerequisites actually exist or are mentioned always takes priority over a
|
---|
9045 | rule with prerequisites that must be made by chaining other implicit rules.
|
---|
9046 | @cindex pattern rules, order of
|
---|
9047 | @cindex order of pattern rules
|
---|
9048 |
|
---|
9049 | @node Pattern Examples, Automatic Variables, Pattern Intro, Pattern Rules
|
---|
9050 | @subsection Pattern Rule Examples
|
---|
9051 |
|
---|
9052 | Here are some examples of pattern rules actually predefined in
|
---|
9053 | @code{make}. First, the rule that compiles @samp{.c} files into @samp{.o}
|
---|
9054 | files:@refill
|
---|
9055 |
|
---|
9056 | @example
|
---|
9057 | %.o : %.c
|
---|
9058 | $(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o $@@
|
---|
9059 | @end example
|
---|
9060 |
|
---|
9061 | @noindent
|
---|
9062 | defines a rule that can make any file @file{@var{x}.o} from
|
---|
9063 | @file{@var{x}.c}. The command uses the automatic variables @samp{$@@} and
|
---|
9064 | @samp{$<} to substitute the names of the target file and the source file
|
---|
9065 | in each case where the rule applies (@pxref{Automatic Variables}).@refill
|
---|
9066 |
|
---|
9067 | Here is a second built-in rule:
|
---|
9068 |
|
---|
9069 | @example
|
---|
9070 | % :: RCS/%,v
|
---|
9071 | $(CO) $(COFLAGS) $<
|
---|
9072 | @end example
|
---|
9073 |
|
---|
9074 | @noindent
|
---|
9075 | defines a rule that can make any file @file{@var{x}} whatsoever from a
|
---|
9076 | corresponding file @file{@var{x},v} in the subdirectory @file{RCS}. Since
|
---|
9077 | the target is @samp{%}, this rule will apply to any file whatever, provided
|
---|
9078 | the appropriate prerequisite file exists. The double colon makes the rule
|
---|
9079 | @dfn{terminal}, which means that its prerequisite may not be an intermediate
|
---|
9080 | file (@pxref{Match-Anything Rules, ,Match-Anything Pattern Rules}).@refill
|
---|
9081 |
|
---|
9082 | @need 500
|
---|
9083 | This pattern rule has two targets:
|
---|
9084 |
|
---|
9085 | @example
|
---|
9086 | @group
|
---|
9087 | %.tab.c %.tab.h: %.y
|
---|
9088 | bison -d $<
|
---|
9089 | @end group
|
---|
9090 | @end example
|
---|
9091 |
|
---|
9092 | @noindent
|
---|
9093 | @c The following paragraph is rewritten to avoid overfull hboxes
|
---|
9094 | This tells @code{make} that the command @samp{bison -d @var{x}.y} will
|
---|
9095 | make both @file{@var{x}.tab.c} and @file{@var{x}.tab.h}. If the file
|
---|
9096 | @file{foo} depends on the files @file{parse.tab.o} and @file{scan.o}
|
---|
9097 | and the file @file{scan.o} depends on the file @file{parse.tab.h},
|
---|
9098 | when @file{parse.y} is changed, the command @samp{bison -d parse.y}
|
---|
9099 | will be executed only once, and the prerequisites of both
|
---|
9100 | @file{parse.tab.o} and @file{scan.o} will be satisfied. (Presumably
|
---|
9101 | the file @file{parse.tab.o} will be recompiled from @file{parse.tab.c}
|
---|
9102 | and the file @file{scan.o} from @file{scan.c}, while @file{foo} is
|
---|
9103 | linked from @file{parse.tab.o}, @file{scan.o}, and its other
|
---|
9104 | prerequisites, and it will execute happily ever after.)@refill
|
---|
9105 |
|
---|
9106 | @node Automatic Variables, Pattern Match, Pattern Examples, Pattern Rules
|
---|
9107 | @subsection Automatic Variables
|
---|
9108 | @cindex automatic variables
|
---|
9109 | @cindex variables, automatic
|
---|
9110 | @cindex variables, and implicit rule
|
---|
9111 |
|
---|
9112 | Suppose you are writing a pattern rule to compile a @samp{.c} file into a
|
---|
9113 | @samp{.o} file: how do you write the @samp{cc} command so that it operates
|
---|
9114 | on the right source file name? You cannot write the name in the command,
|
---|
9115 | because the name is different each time the implicit rule is applied.
|
---|
9116 |
|
---|
9117 | What you do is use a special feature of @code{make}, the @dfn{automatic
|
---|
9118 | variables}. These variables have values computed afresh for each rule that
|
---|
9119 | is executed, based on the target and prerequisites of the rule. In this
|
---|
9120 | example, you would use @samp{$@@} for the object file name and @samp{$<}
|
---|
9121 | for the source file name.
|
---|
9122 |
|
---|
9123 | @cindex automatic variables in prerequisites
|
---|
9124 | @cindex prerequisites, and automatic variables
|
---|
9125 | It's very important that you recognize the limited scope in which
|
---|
9126 | automatic variable values are available: they only have values within
|
---|
9127 | the command script. In particular, you cannot use them anywhere
|
---|
9128 | within the target list of a rule; they have no value there and will
|
---|
9129 | expand to the empty string. Also, they cannot be accessed directly
|
---|
9130 | within the prerequisite list of a rule. A common mistake is
|
---|
9131 | attempting to use @code{$@@} within the prerequisites list; this will
|
---|
9132 | not work. However, there is a special feature of GNU @code{make},
|
---|
9133 | secondary expansion (@pxref{Secondary Expansion}), which will allow
|
---|
9134 | automatic variable values to be used in prerequisite lists.
|
---|
9135 |
|
---|
9136 | Here is a table of automatic variables:
|
---|
9137 |
|
---|
9138 | @table @code
|
---|
9139 | @vindex $@@
|
---|
9140 | @vindex @@ @r{(automatic variable)}
|
---|
9141 | @item $@@
|
---|
9142 | The file name of the target of the rule. If the target is an archive
|
---|
9143 | member, then @samp{$@@} is the name of the archive file. In a pattern
|
---|
9144 | rule that has multiple targets (@pxref{Pattern Intro, ,Introduction to
|
---|
9145 | Pattern Rules}), @samp{$@@} is the name of whichever target caused the
|
---|
9146 | rule's commands to be run.
|
---|
9147 |
|
---|
9148 | @vindex $%
|
---|
9149 | @vindex % @r{(automatic variable)}
|
---|
9150 | @item $%
|
---|
9151 | The target member name, when the target is an archive member.
|
---|
9152 | @xref{Archives}. For example, if the target is @file{foo.a(bar.o)} then
|
---|
9153 | @samp{$%} is @file{bar.o} and @samp{$@@} is @file{foo.a}. @samp{$%} is
|
---|
9154 | empty when the target is not an archive member.
|
---|
9155 |
|
---|
9156 | @vindex $<
|
---|
9157 | @vindex < @r{(automatic variable)}
|
---|
9158 | @item $<
|
---|
9159 | The name of the first prerequisite. If the target got its commands from
|
---|
9160 | an implicit rule, this will be the first prerequisite added by the
|
---|
9161 | implicit rule (@pxref{Implicit Rules}).
|
---|
9162 |
|
---|
9163 | @vindex $?
|
---|
9164 | @vindex ? @r{(automatic variable)}
|
---|
9165 | @item $?
|
---|
9166 | The names of all the prerequisites that are newer than the target, with
|
---|
9167 | spaces between them. For prerequisites which are archive members, only
|
---|
9168 | the member named is used (@pxref{Archives}).
|
---|
9169 | @cindex prerequisites, list of changed
|
---|
9170 | @cindex list of changed prerequisites
|
---|
9171 |
|
---|
9172 | @vindex $^
|
---|
9173 | @vindex ^ @r{(automatic variable)}
|
---|
9174 | @item $^
|
---|
9175 | The names of all the prerequisites, with spaces between them. For
|
---|
9176 | prerequisites which are archive members, only the member named is used
|
---|
9177 | (@pxref{Archives}). A target has only one prerequisite on each other file
|
---|
9178 | it depends on, no matter how many times each file is listed as a
|
---|
9179 | prerequisite. So if you list a prerequisite more than once for a target,
|
---|
9180 | the value of @code{$^} contains just one copy of the name. This list
|
---|
9181 | does @strong{not} contain any of the order-only prerequisites; for those
|
---|
9182 | see the @samp{$|} variable, below.
|
---|
9183 | @cindex prerequisites, list of all
|
---|
9184 | @cindex list of all prerequisites
|
---|
9185 |
|
---|
9186 | @vindex $+
|
---|
9187 | @vindex + @r{(automatic variable)}
|
---|
9188 | @item $+
|
---|
9189 | This is like @samp{$^}, but prerequisites listed more than once are
|
---|
9190 | duplicated in the order they were listed in the makefile. This is
|
---|
9191 | primarily useful for use in linking commands where it is meaningful to
|
---|
9192 | repeat library file names in a particular order.
|
---|
9193 |
|
---|
9194 | @vindex $|
|
---|
9195 | @vindex | @r{(automatic variable)}
|
---|
9196 | @item $|
|
---|
9197 | The names of all the order-only prerequisites, with spaces between
|
---|
9198 | them.
|
---|
9199 |
|
---|
9200 | @vindex $*
|
---|
9201 | @vindex * @r{(automatic variable)}
|
---|
9202 | @item $*
|
---|
9203 | The stem with which an implicit rule matches (@pxref{Pattern Match, ,How
|
---|
9204 | Patterns Match}). If the target is @file{dir/a.foo.b} and the target
|
---|
9205 | pattern is @file{a.%.b} then the stem is @file{dir/foo}. The stem is
|
---|
9206 | useful for constructing names of related files.@refill
|
---|
9207 | @cindex stem, variable for
|
---|
9208 |
|
---|
9209 | In a static pattern rule, the stem is part of the file name that matched
|
---|
9210 | the @samp{%} in the target pattern.
|
---|
9211 |
|
---|
9212 | In an explicit rule, there is no stem; so @samp{$*} cannot be determined
|
---|
9213 | in that way. Instead, if the target name ends with a recognized suffix
|
---|
9214 | (@pxref{Suffix Rules, ,Old-Fashioned Suffix Rules}), @samp{$*} is set to
|
---|
9215 | the target name minus the suffix. For example, if the target name is
|
---|
9216 | @samp{foo.c}, then @samp{$*} is set to @samp{foo}, since @samp{.c} is a
|
---|
9217 | suffix. GNU @code{make} does this bizarre thing only for compatibility
|
---|
9218 | with other implementations of @code{make}. You should generally avoid
|
---|
9219 | using @samp{$*} except in implicit rules or static pattern rules.@refill
|
---|
9220 |
|
---|
9221 | If the target name in an explicit rule does not end with a recognized
|
---|
9222 | suffix, @samp{$*} is set to the empty string for that rule.
|
---|
9223 | @end table
|
---|
9224 |
|
---|
9225 | @samp{$?} is useful even in explicit rules when you wish to operate on only
|
---|
9226 | the prerequisites that have changed. For example, suppose that an archive
|
---|
9227 | named @file{lib} is supposed to contain copies of several object files.
|
---|
9228 | This rule copies just the changed object files into the archive:
|
---|
9229 |
|
---|
9230 | @example
|
---|
9231 | @group
|
---|
9232 | lib: foo.o bar.o lose.o win.o
|
---|
9233 | ar r lib $?
|
---|
9234 | @end group
|
---|
9235 | @end example
|
---|
9236 |
|
---|
9237 | Of the variables listed above, four have values that are single file
|
---|
9238 | names, and three have values that are lists of file names. These seven
|
---|
9239 | have variants that get just the file's directory name or just the file
|
---|
9240 | name within the directory. The variant variables' names are formed by
|
---|
9241 | appending @samp{D} or @samp{F}, respectively. These variants are
|
---|
9242 | semi-obsolete in GNU @code{make} since the functions @code{dir} and
|
---|
9243 | @code{notdir} can be used to get a similar effect (@pxref{File Name
|
---|
9244 | Functions, , Functions for File Names}). Note, however, that the
|
---|
9245 | @samp{D} variants all omit the trailing slash which always appears in
|
---|
9246 | the output of the @code{dir} function. Here is a table of the variants:
|
---|
9247 |
|
---|
9248 | @table @samp
|
---|
9249 | @vindex $(@@D)
|
---|
9250 | @vindex @@D @r{(automatic variable)}
|
---|
9251 | @item $(@@D)
|
---|
9252 | The directory part of the file name of the target, with the trailing
|
---|
9253 | slash removed. If the value of @samp{$@@} is @file{dir/foo.o} then
|
---|
9254 | @samp{$(@@D)} is @file{dir}. This value is @file{.} if @samp{$@@} does
|
---|
9255 | not contain a slash.
|
---|
9256 |
|
---|
9257 | @vindex $(@@F)
|
---|
9258 | @vindex @@F @r{(automatic variable)}
|
---|
9259 | @item $(@@F)
|
---|
9260 | The file-within-directory part of the file name of the target. If the
|
---|
9261 | value of @samp{$@@} is @file{dir/foo.o} then @samp{$(@@F)} is
|
---|
9262 | @file{foo.o}. @samp{$(@@F)} is equivalent to @samp{$(notdir $@@)}.
|
---|
9263 |
|
---|
9264 | @vindex $(*D)
|
---|
9265 | @vindex *D @r{(automatic variable)}
|
---|
9266 | @item $(*D)
|
---|
9267 | @vindex $(*F)
|
---|
9268 | @vindex *F @r{(automatic variable)}
|
---|
9269 | @itemx $(*F)
|
---|
9270 | The directory part and the file-within-directory
|
---|
9271 | part of the stem; @file{dir} and @file{foo} in this example.
|
---|
9272 |
|
---|
9273 | @vindex $(%D)
|
---|
9274 | @vindex %D @r{(automatic variable)}
|
---|
9275 | @item $(%D)
|
---|
9276 | @vindex $(%F)
|
---|
9277 | @vindex %F @r{(automatic variable)}
|
---|
9278 | @itemx $(%F)
|
---|
9279 | The directory part and the file-within-directory part of the target
|
---|
9280 | archive member name. This makes sense only for archive member targets
|
---|
9281 | of the form @file{@var{archive}(@var{member})} and is useful only when
|
---|
9282 | @var{member} may contain a directory name. (@xref{Archive Members,
|
---|
9283 | ,Archive Members as Targets}.)
|
---|
9284 |
|
---|
9285 | @vindex $(<D)
|
---|
9286 | @vindex <D @r{(automatic variable)}
|
---|
9287 | @item $(<D)
|
---|
9288 | @vindex $(<F)
|
---|
9289 | @vindex <F @r{(automatic variable)}
|
---|
9290 | @itemx $(<F)
|
---|
9291 | The directory part and the file-within-directory
|
---|
9292 | part of the first prerequisite.
|
---|
9293 |
|
---|
9294 | @vindex $(^D)
|
---|
9295 | @vindex ^D @r{(automatic variable)}
|
---|
9296 | @item $(^D)
|
---|
9297 | @vindex $(^F)
|
---|
9298 | @vindex ^F @r{(automatic variable)}
|
---|
9299 | @itemx $(^F)
|
---|
9300 | Lists of the directory parts and the file-within-directory
|
---|
9301 | parts of all prerequisites.
|
---|
9302 |
|
---|
9303 | @vindex $(+D)
|
---|
9304 | @vindex +D @r{(automatic variable)}
|
---|
9305 | @item $(+D)
|
---|
9306 | @vindex $(+F)
|
---|
9307 | @vindex +F @r{(automatic variable)}
|
---|
9308 | @itemx $(+F)
|
---|
9309 | Lists of the directory parts and the file-within-directory
|
---|
9310 | parts of all prerequisites, including multiple instances of duplicated
|
---|
9311 | prerequisites.
|
---|
9312 |
|
---|
9313 | @vindex $(?D)
|
---|
9314 | @vindex ?D @r{(automatic variable)}
|
---|
9315 | @item $(?D)
|
---|
9316 | @vindex $(?F)
|
---|
9317 | @vindex ?F @r{(automatic variable)}
|
---|
9318 | @itemx $(?F)
|
---|
9319 | Lists of the directory parts and the file-within-directory parts of
|
---|
9320 | all prerequisites that are newer than the target.
|
---|
9321 | @end table
|
---|
9322 |
|
---|
9323 | Note that we use a special stylistic convention when we talk about these
|
---|
9324 | automatic variables; we write ``the value of @samp{$<}'', rather than
|
---|
9325 | @w{``the variable @code{<}''} as we would write for ordinary variables
|
---|
9326 | such as @code{objects} and @code{CFLAGS}. We think this convention
|
---|
9327 | looks more natural in this special case. Please do not assume it has a
|
---|
9328 | deep significance; @samp{$<} refers to the variable named @code{<} just
|
---|
9329 | as @samp{$(CFLAGS)} refers to the variable named @code{CFLAGS}.
|
---|
9330 | You could just as well use @samp{$(<)} in place of @samp{$<}.
|
---|
9331 |
|
---|
9332 | @node Pattern Match, Match-Anything Rules, Automatic Variables, Pattern Rules
|
---|
9333 | @subsection How Patterns Match
|
---|
9334 |
|
---|
9335 | @cindex stem
|
---|
9336 | A target pattern is composed of a @samp{%} between a prefix and a suffix,
|
---|
9337 | either or both of which may be empty. The pattern matches a file name only
|
---|
9338 | if the file name starts with the prefix and ends with the suffix, without
|
---|
9339 | overlap. The text between the prefix and the suffix is called the
|
---|
9340 | @dfn{stem}. Thus, when the pattern @samp{%.o} matches the file name
|
---|
9341 | @file{test.o}, the stem is @samp{test}. The pattern rule prerequisites are
|
---|
9342 | turned into actual file names by substituting the stem for the character
|
---|
9343 | @samp{%}. Thus, if in the same example one of the prerequisites is written
|
---|
9344 | as @samp{%.c}, it expands to @samp{test.c}.@refill
|
---|
9345 |
|
---|
9346 | When the target pattern does not contain a slash (and it usually does
|
---|
9347 | not), directory names in the file names are removed from the file name
|
---|
9348 | before it is compared with the target prefix and suffix. After the
|
---|
9349 | comparison of the file name to the target pattern, the directory
|
---|
9350 | names, along with the slash that ends them, are added on to the
|
---|
9351 | prerequisite file names generated from the pattern rule's prerequisite
|
---|
9352 | patterns and the file name. The directories are ignored only for the
|
---|
9353 | purpose of finding an implicit rule to use, not in the application of
|
---|
9354 | that rule. Thus, @samp{e%t} matches the file name @file{src/eat},
|
---|
9355 | with @samp{src/a} as the stem. When prerequisites are turned into file
|
---|
9356 | names, the directories from the stem are added at the front, while the
|
---|
9357 | rest of the stem is substituted for the @samp{%}. The stem
|
---|
9358 | @samp{src/a} with a prerequisite pattern @samp{c%r} gives the file name
|
---|
9359 | @file{src/car}.@refill
|
---|
9360 |
|
---|
9361 | @node Match-Anything Rules, Canceling Rules, Pattern Match, Pattern Rules
|
---|
9362 | @subsection Match-Anything Pattern Rules
|
---|
9363 |
|
---|
9364 | @cindex match-anything rule
|
---|
9365 | @cindex terminal rule
|
---|
9366 | When a pattern rule's target is just @samp{%}, it matches any file name
|
---|
9367 | whatever. We call these rules @dfn{match-anything} rules. They are very
|
---|
9368 | useful, but it can take a lot of time for @code{make} to think about them,
|
---|
9369 | because it must consider every such rule for each file name listed either
|
---|
9370 | as a target or as a prerequisite.
|
---|
9371 |
|
---|
9372 | Suppose the makefile mentions @file{foo.c}. For this target, @code{make}
|
---|
9373 | would have to consider making it by linking an object file @file{foo.c.o},
|
---|
9374 | or by C compilation-and-linking in one step from @file{foo.c.c}, or by
|
---|
9375 | Pascal compilation-and-linking from @file{foo.c.p}, and many other
|
---|
9376 | possibilities.
|
---|
9377 |
|
---|
9378 | We know these possibilities are ridiculous since @file{foo.c} is a C source
|
---|
9379 | file, not an executable. If @code{make} did consider these possibilities,
|
---|
9380 | it would ultimately reject them, because files such as @file{foo.c.o} and
|
---|
9381 | @file{foo.c.p} would not exist. But these possibilities are so
|
---|
9382 | numerous that @code{make} would run very slowly if it had to consider
|
---|
9383 | them.@refill
|
---|
9384 |
|
---|
9385 | To gain speed, we have put various constraints on the way @code{make}
|
---|
9386 | considers match-anything rules. There are two different constraints that
|
---|
9387 | can be applied, and each time you define a match-anything rule you must
|
---|
9388 | choose one or the other for that rule.
|
---|
9389 |
|
---|
9390 | One choice is to mark the match-anything rule as @dfn{terminal} by defining
|
---|
9391 | it with a double colon. When a rule is terminal, it does not apply unless
|
---|
9392 | its prerequisites actually exist. Prerequisites that could be made with
|
---|
9393 | other implicit rules are not good enough. In other words, no further
|
---|
9394 | chaining is allowed beyond a terminal rule.
|
---|
9395 |
|
---|
9396 | For example, the built-in implicit rules for extracting sources from RCS
|
---|
9397 | and SCCS files are terminal; as a result, if the file @file{foo.c,v} does
|
---|
9398 | not exist, @code{make} will not even consider trying to make it as an
|
---|
9399 | intermediate file from @file{foo.c,v.o} or from @file{RCS/SCCS/s.foo.c,v}.
|
---|
9400 | RCS and SCCS files are generally ultimate source files, which should not be
|
---|
9401 | remade from any other files; therefore, @code{make} can save time by not
|
---|
9402 | looking for ways to remake them.@refill
|
---|
9403 |
|
---|
9404 | If you do not mark the match-anything rule as terminal, then it is
|
---|
9405 | nonterminal. A nonterminal match-anything rule cannot apply to a file name
|
---|
9406 | that indicates a specific type of data. A file name indicates a specific
|
---|
9407 | type of data if some non-match-anything implicit rule target matches it.
|
---|
9408 |
|
---|
9409 | For example, the file name @file{foo.c} matches the target for the pattern
|
---|
9410 | rule @samp{%.c : %.y} (the rule to run Yacc). Regardless of whether this
|
---|
9411 | rule is actually applicable (which happens only if there is a file
|
---|
9412 | @file{foo.y}), the fact that its target matches is enough to prevent
|
---|
9413 | consideration of any nonterminal match-anything rules for the file
|
---|
9414 | @file{foo.c}. Thus, @code{make} will not even consider trying to make
|
---|
9415 | @file{foo.c} as an executable file from @file{foo.c.o}, @file{foo.c.c},
|
---|
9416 | @file{foo.c.p}, etc.@refill
|
---|
9417 |
|
---|
9418 | The motivation for this constraint is that nonterminal match-anything
|
---|
9419 | rules are used for making files containing specific types of data (such as
|
---|
9420 | executable files) and a file name with a recognized suffix indicates some
|
---|
9421 | other specific type of data (such as a C source file).
|
---|
9422 |
|
---|
9423 | Special built-in dummy pattern rules are provided solely to recognize
|
---|
9424 | certain file names so that nonterminal match-anything rules will not be
|
---|
9425 | considered. These dummy rules have no prerequisites and no commands, and
|
---|
9426 | they are ignored for all other purposes. For example, the built-in
|
---|
9427 | implicit rule
|
---|
9428 |
|
---|
9429 | @example
|
---|
9430 | %.p :
|
---|
9431 | @end example
|
---|
9432 |
|
---|
9433 | @noindent
|
---|
9434 | exists to make sure that Pascal source files such as @file{foo.p} match a
|
---|
9435 | specific target pattern and thereby prevent time from being wasted looking
|
---|
9436 | for @file{foo.p.o} or @file{foo.p.c}.
|
---|
9437 |
|
---|
9438 | Dummy pattern rules such as the one for @samp{%.p} are made for every
|
---|
9439 | suffix listed as valid for use in suffix rules (@pxref{Suffix Rules, ,Old-Fashioned Suffix Rules}).
|
---|
9440 |
|
---|
9441 | @node Canceling Rules, , Match-Anything Rules, Pattern Rules
|
---|
9442 | @subsection Canceling Implicit Rules
|
---|
9443 |
|
---|
9444 | You can override a built-in implicit rule (or one you have defined
|
---|
9445 | yourself) by defining a new pattern rule with the same target and
|
---|
9446 | prerequisites, but different commands. When the new rule is defined, the
|
---|
9447 | built-in one is replaced. The new rule's position in the sequence of
|
---|
9448 | implicit rules is determined by where you write the new rule.
|
---|
9449 |
|
---|
9450 | You can cancel a built-in implicit rule by defining a pattern rule with the
|
---|
9451 | same target and prerequisites, but no commands. For example, the following
|
---|
9452 | would cancel the rule that runs the assembler:
|
---|
9453 |
|
---|
9454 | @example
|
---|
9455 | %.o : %.s
|
---|
9456 | @end example
|
---|
9457 |
|
---|
9458 | @node Last Resort, Suffix Rules, Pattern Rules, Implicit Rules
|
---|
9459 | @section Defining Last-Resort Default Rules
|
---|
9460 | @cindex last-resort default rules
|
---|
9461 | @cindex default rules, last-resort
|
---|
9462 |
|
---|
9463 | You can define a last-resort implicit rule by writing a terminal
|
---|
9464 | match-anything pattern rule with no prerequisites (@pxref{Match-Anything
|
---|
9465 | Rules}). This is just like any other pattern rule; the only thing
|
---|
9466 | special about it is that it will match any target. So such a rule's
|
---|
9467 | commands are used for all targets and prerequisites that have no commands
|
---|
9468 | of their own and for which no other implicit rule applies.
|
---|
9469 |
|
---|
9470 | For example, when testing a makefile, you might not care if the source
|
---|
9471 | files contain real data, only that they exist. Then you might do this:
|
---|
9472 |
|
---|
9473 | @example
|
---|
9474 | %::
|
---|
9475 | touch $@@
|
---|
9476 | @end example
|
---|
9477 |
|
---|
9478 | @noindent
|
---|
9479 | to cause all the source files needed (as prerequisites) to be created
|
---|
9480 | automatically.
|
---|
9481 |
|
---|
9482 | @findex .DEFAULT
|
---|
9483 | You can instead define commands to be used for targets for which there
|
---|
9484 | are no rules at all, even ones which don't specify commands. You do
|
---|
9485 | this by writing a rule for the target @code{.DEFAULT}. Such a rule's
|
---|
9486 | commands are used for all prerequisites which do not appear as targets in
|
---|
9487 | any explicit rule, and for which no implicit rule applies. Naturally,
|
---|
9488 | there is no @code{.DEFAULT} rule unless you write one.
|
---|
9489 |
|
---|
9490 | If you use @code{.DEFAULT} with no commands or prerequisites:
|
---|
9491 |
|
---|
9492 | @example
|
---|
9493 | .DEFAULT:
|
---|
9494 | @end example
|
---|
9495 |
|
---|
9496 | @noindent
|
---|
9497 | the commands previously stored for @code{.DEFAULT} are cleared.
|
---|
9498 | Then @code{make} acts as if you had never defined @code{.DEFAULT} at all.
|
---|
9499 |
|
---|
9500 | If you do not want a target to get the commands from a match-anything
|
---|
9501 | pattern rule or @code{.DEFAULT}, but you also do not want any commands
|
---|
9502 | to be run for the target, you can give it empty commands (@pxref{Empty
|
---|
9503 | Commands, ,Defining Empty Commands}).@refill
|
---|
9504 |
|
---|
9505 | You can use a last-resort rule to override part of another makefile.
|
---|
9506 | @xref{Overriding Makefiles, , Overriding Part of Another Makefile}.
|
---|
9507 |
|
---|
9508 | @node Suffix Rules, Implicit Rule Search, Last Resort, Implicit Rules
|
---|
9509 | @section Old-Fashioned Suffix Rules
|
---|
9510 | @cindex old-fashioned suffix rules
|
---|
9511 | @cindex suffix rule
|
---|
9512 |
|
---|
9513 | @dfn{Suffix rules} are the old-fashioned way of defining implicit rules for
|
---|
9514 | @code{make}. Suffix rules are obsolete because pattern rules are more
|
---|
9515 | general and clearer. They are supported in GNU @code{make} for
|
---|
9516 | compatibility with old makefiles. They come in two kinds:
|
---|
9517 | @dfn{double-suffix} and @dfn{single-suffix}.@refill
|
---|
9518 |
|
---|
9519 | A double-suffix rule is defined by a pair of suffixes: the target suffix
|
---|
9520 | and the source suffix. It matches any file whose name ends with the
|
---|
9521 | target suffix. The corresponding implicit prerequisite is made by
|
---|
9522 | replacing the target suffix with the source suffix in the file name. A
|
---|
9523 | two-suffix rule whose target and source suffixes are @samp{.o} and
|
---|
9524 | @samp{.c} is equivalent to the pattern rule @samp{%.o : %.c}.
|
---|
9525 |
|
---|
9526 | A single-suffix rule is defined by a single suffix, which is the source
|
---|
9527 | suffix. It matches any file name, and the corresponding implicit
|
---|
9528 | prerequisite name is made by appending the source suffix. A single-suffix
|
---|
9529 | rule whose source suffix is @samp{.c} is equivalent to the pattern rule
|
---|
9530 | @samp{% : %.c}.
|
---|
9531 |
|
---|
9532 | Suffix rule definitions are recognized by comparing each rule's target
|
---|
9533 | against a defined list of known suffixes. When @code{make} sees a rule
|
---|
9534 | whose target is a known suffix, this rule is considered a single-suffix
|
---|
9535 | rule. When @code{make} sees a rule whose target is two known suffixes
|
---|
9536 | concatenated, this rule is taken as a double-suffix rule.
|
---|
9537 |
|
---|
9538 | For example, @samp{.c} and @samp{.o} are both on the default list of
|
---|
9539 | known suffixes. Therefore, if you define a rule whose target is
|
---|
9540 | @samp{.c.o}, @code{make} takes it to be a double-suffix rule with source
|
---|
9541 | suffix @samp{.c} and target suffix @samp{.o}. Here is the old-fashioned
|
---|
9542 | way to define the rule for compiling a C source file:@refill
|
---|
9543 |
|
---|
9544 | @example
|
---|
9545 | .c.o:
|
---|
9546 | $(CC) -c $(CFLAGS) $(CPPFLAGS) -o $@@ $<
|
---|
9547 | @end example
|
---|
9548 |
|
---|
9549 | Suffix rules cannot have any prerequisites of their own. If they have any,
|
---|
9550 | they are treated as normal files with funny names, not as suffix rules.
|
---|
9551 | Thus, the rule:
|
---|
9552 |
|
---|
9553 | @example
|
---|
9554 | .c.o: foo.h
|
---|
9555 | $(CC) -c $(CFLAGS) $(CPPFLAGS) -o $@@ $<
|
---|
9556 | @end example
|
---|
9557 |
|
---|
9558 | @noindent
|
---|
9559 | tells how to make the file @file{.c.o} from the prerequisite file
|
---|
9560 | @file{foo.h}, and is not at all like the pattern rule:
|
---|
9561 |
|
---|
9562 | @example
|
---|
9563 | %.o: %.c foo.h
|
---|
9564 | $(CC) -c $(CFLAGS) $(CPPFLAGS) -o $@@ $<
|
---|
9565 | @end example
|
---|
9566 |
|
---|
9567 | @noindent
|
---|
9568 | which tells how to make @samp{.o} files from @samp{.c} files, and makes all
|
---|
9569 | @samp{.o} files using this pattern rule also depend on @file{foo.h}.
|
---|
9570 |
|
---|
9571 | Suffix rules with no commands are also meaningless. They do not remove
|
---|
9572 | previous rules as do pattern rules with no commands (@pxref{Canceling
|
---|
9573 | Rules, , Canceling Implicit Rules}). They simply enter the suffix or pair of suffixes concatenated as
|
---|
9574 | a target in the data base.@refill
|
---|
9575 |
|
---|
9576 | @findex .SUFFIXES
|
---|
9577 | The known suffixes are simply the names of the prerequisites of the special
|
---|
9578 | target @code{.SUFFIXES}. You can add your own suffixes by writing a rule
|
---|
9579 | for @code{.SUFFIXES} that adds more prerequisites, as in:
|
---|
9580 |
|
---|
9581 | @example
|
---|
9582 | .SUFFIXES: .hack .win
|
---|
9583 | @end example
|
---|
9584 |
|
---|
9585 | @noindent
|
---|
9586 | which adds @samp{.hack} and @samp{.win} to the end of the list of suffixes.
|
---|
9587 |
|
---|
9588 | If you wish to eliminate the default known suffixes instead of just adding
|
---|
9589 | to them, write a rule for @code{.SUFFIXES} with no prerequisites. By
|
---|
9590 | special dispensation, this eliminates all existing prerequisites of
|
---|
9591 | @code{.SUFFIXES}. You can then write another rule to add the suffixes you
|
---|
9592 | want. For example,
|
---|
9593 |
|
---|
9594 | @example
|
---|
9595 | @group
|
---|
9596 | .SUFFIXES: # @r{Delete the default suffixes}
|
---|
9597 | .SUFFIXES: .c .o .h # @r{Define our suffix list}
|
---|
9598 | @end group
|
---|
9599 | @end example
|
---|
9600 |
|
---|
9601 | The @samp{-r} or @samp{--no-builtin-rules} flag causes the default
|
---|
9602 | list of suffixes to be empty.
|
---|
9603 |
|
---|
9604 | @vindex SUFFIXES
|
---|
9605 | The variable @code{SUFFIXES} is defined to the default list of suffixes
|
---|
9606 | before @code{make} reads any makefiles. You can change the list of suffixes
|
---|
9607 | with a rule for the special target @code{.SUFFIXES}, but that does not alter
|
---|
9608 | this variable.
|
---|
9609 |
|
---|
9610 | @node Implicit Rule Search, , Suffix Rules, Implicit Rules
|
---|
9611 | @section Implicit Rule Search Algorithm
|
---|
9612 | @cindex implicit rule, search algorithm
|
---|
9613 | @cindex search algorithm, implicit rule
|
---|
9614 |
|
---|
9615 | Here is the procedure @code{make} uses for searching for an implicit rule
|
---|
9616 | for a target @var{t}. This procedure is followed for each double-colon
|
---|
9617 | rule with no commands, for each target of ordinary rules none of which have
|
---|
9618 | commands, and for each prerequisite that is not the target of any rule. It
|
---|
9619 | is also followed recursively for prerequisites that come from implicit
|
---|
9620 | rules, in the search for a chain of rules.
|
---|
9621 |
|
---|
9622 | Suffix rules are not mentioned in this algorithm because suffix rules are
|
---|
9623 | converted to equivalent pattern rules once the makefiles have been read in.
|
---|
9624 |
|
---|
9625 | For an archive member target of the form
|
---|
9626 | @samp{@var{archive}(@var{member})}, the following algorithm is run
|
---|
9627 | twice, first using the entire target name @var{t}, and second using
|
---|
9628 | @samp{(@var{member})} as the target @var{t} if the first run found no
|
---|
9629 | rule.@refill
|
---|
9630 |
|
---|
9631 | @enumerate
|
---|
9632 | @item
|
---|
9633 | Split @var{t} into a directory part, called @var{d}, and the rest,
|
---|
9634 | called @var{n}. For example, if @var{t} is @samp{src/foo.o}, then
|
---|
9635 | @var{d} is @samp{src/} and @var{n} is @samp{foo.o}.@refill
|
---|
9636 |
|
---|
9637 | @item
|
---|
9638 | Make a list of all the pattern rules one of whose targets matches
|
---|
9639 | @var{t} or @var{n}. If the target pattern contains a slash, it is
|
---|
9640 | matched against @var{t}; otherwise, against @var{n}.
|
---|
9641 |
|
---|
9642 | @item
|
---|
9643 | If any rule in that list is @emph{not} a match-anything rule, then
|
---|
9644 | remove all nonterminal match-anything rules from the list.
|
---|
9645 |
|
---|
9646 | @item
|
---|
9647 | Remove from the list all rules with no commands.
|
---|
9648 |
|
---|
9649 | @item
|
---|
9650 | For each pattern rule in the list:
|
---|
9651 |
|
---|
9652 | @enumerate a
|
---|
9653 | @item
|
---|
9654 | Find the stem @var{s}, which is the nonempty part of @var{t} or @var{n}
|
---|
9655 | matched by the @samp{%} in the target pattern.@refill
|
---|
9656 |
|
---|
9657 | @item
|
---|
9658 | Compute the prerequisite names by substituting @var{s} for @samp{%}; if
|
---|
9659 | the target pattern does not contain a slash, append @var{d} to
|
---|
9660 | the front of each prerequisite name.@refill
|
---|
9661 |
|
---|
9662 | @item
|
---|
9663 | Test whether all the prerequisites exist or ought to exist. (If a
|
---|
9664 | file name is mentioned in the makefile as a target or as an explicit
|
---|
9665 | prerequisite, then we say it ought to exist.)
|
---|
9666 |
|
---|
9667 | If all prerequisites exist or ought to exist, or there are no prerequisites,
|
---|
9668 | then this rule applies.
|
---|
9669 | @end enumerate
|
---|
9670 |
|
---|
9671 | @item
|
---|
9672 | If no pattern rule has been found so far, try harder.
|
---|
9673 | For each pattern rule in the list:
|
---|
9674 |
|
---|
9675 | @enumerate a
|
---|
9676 | @item
|
---|
9677 | If the rule is terminal, ignore it and go on to the next rule.
|
---|
9678 |
|
---|
9679 | @item
|
---|
9680 | Compute the prerequisite names as before.
|
---|
9681 |
|
---|
9682 | @item
|
---|
9683 | Test whether all the prerequisites exist or ought to exist.
|
---|
9684 |
|
---|
9685 | @item
|
---|
9686 | For each prerequisite that does not exist, follow this algorithm
|
---|
9687 | recursively to see if the prerequisite can be made by an implicit
|
---|
9688 | rule.
|
---|
9689 |
|
---|
9690 | @item
|
---|
9691 | If all prerequisites exist, ought to exist, or can be
|
---|
9692 | made by implicit rules, then this rule applies.
|
---|
9693 | @end enumerate
|
---|
9694 |
|
---|
9695 | @item
|
---|
9696 | If no implicit rule applies, the rule for @code{.DEFAULT}, if any,
|
---|
9697 | applies. In that case, give @var{t} the same commands that
|
---|
9698 | @code{.DEFAULT} has. Otherwise, there are no commands for @var{t}.
|
---|
9699 | @end enumerate
|
---|
9700 |
|
---|
9701 | Once a rule that applies has been found, for each target pattern of the
|
---|
9702 | rule other than the one that matched @var{t} or @var{n}, the @samp{%} in
|
---|
9703 | the pattern is replaced with @var{s} and the resultant file name is stored
|
---|
9704 | until the commands to remake the target file @var{t} are executed. After
|
---|
9705 | these commands are executed, each of these stored file names are entered
|
---|
9706 | into the data base and marked as having been updated and having the same
|
---|
9707 | update status as the file @var{t}.
|
---|
9708 |
|
---|
9709 | When the commands of a pattern rule are executed for @var{t}, the automatic
|
---|
9710 | variables are set corresponding to the target and prerequisites.
|
---|
9711 | @xref{Automatic Variables}.
|
---|
9712 |
|
---|
9713 | @node Archives, Features, Implicit Rules, Top
|
---|
9714 | @chapter Using @code{make} to Update Archive Files
|
---|
9715 | @cindex archive
|
---|
9716 |
|
---|
9717 | @dfn{Archive files} are files containing named subfiles called
|
---|
9718 | @dfn{members}; they are maintained with the program @code{ar} and their
|
---|
9719 | main use is as subroutine libraries for linking.
|
---|
9720 |
|
---|
9721 | @menu
|
---|
9722 | * Archive Members:: Archive members as targets.
|
---|
9723 | * Archive Update:: The implicit rule for archive member targets.
|
---|
9724 | * Archive Pitfalls:: Dangers to watch out for when using archives.
|
---|
9725 | * Archive Suffix Rules:: You can write a special kind of suffix rule
|
---|
9726 | for updating archives.
|
---|
9727 | @end menu
|
---|
9728 |
|
---|
9729 | @node Archive Members, Archive Update, Archives, Archives
|
---|
9730 | @section Archive Members as Targets
|
---|
9731 | @cindex archive member targets
|
---|
9732 |
|
---|
9733 | An individual member of an archive file can be used as a target or
|
---|
9734 | prerequisite in @code{make}. You specify the member named @var{member} in
|
---|
9735 | archive file @var{archive} as follows:
|
---|
9736 |
|
---|
9737 | @example
|
---|
9738 | @var{archive}(@var{member})
|
---|
9739 | @end example
|
---|
9740 |
|
---|
9741 | @noindent
|
---|
9742 | This construct is available only in targets and prerequisites, not in
|
---|
9743 | commands! Most programs that you might use in commands do not support this
|
---|
9744 | syntax and cannot act directly on archive members. Only @code{ar} and
|
---|
9745 | other programs specifically designed to operate on archives can do so.
|
---|
9746 | Therefore, valid commands to update an archive member target probably must
|
---|
9747 | use @code{ar}. For example, this rule says to create a member
|
---|
9748 | @file{hack.o} in archive @file{foolib} by copying the file @file{hack.o}:
|
---|
9749 |
|
---|
9750 | @example
|
---|
9751 | foolib(hack.o) : hack.o
|
---|
9752 | ar cr foolib hack.o
|
---|
9753 | @end example
|
---|
9754 |
|
---|
9755 | In fact, nearly all archive member targets are updated in just this way
|
---|
9756 | and there is an implicit rule to do it for you. @strong{Please note:} The
|
---|
9757 | @samp{c} flag to @code{ar} is required if the archive file does not
|
---|
9758 | already exist.
|
---|
9759 |
|
---|
9760 | To specify several members in the same archive, you can write all the
|
---|
9761 | member names together between the parentheses. For example:
|
---|
9762 |
|
---|
9763 | @example
|
---|
9764 | foolib(hack.o kludge.o)
|
---|
9765 | @end example
|
---|
9766 |
|
---|
9767 | @noindent
|
---|
9768 | is equivalent to:
|
---|
9769 |
|
---|
9770 | @example
|
---|
9771 | foolib(hack.o) foolib(kludge.o)
|
---|
9772 | @end example
|
---|
9773 |
|
---|
9774 | @cindex wildcard, in archive member
|
---|
9775 | You can also use shell-style wildcards in an archive member reference.
|
---|
9776 | @xref{Wildcards, ,Using Wildcard Characters in File Names}. For
|
---|
9777 | example, @w{@samp{foolib(*.o)}} expands to all existing members of the
|
---|
9778 | @file{foolib} archive whose names end in @samp{.o}; perhaps
|
---|
9779 | @samp{@w{foolib(hack.o)} @w{foolib(kludge.o)}}.
|
---|
9780 |
|
---|
9781 | @node Archive Update, Archive Pitfalls, Archive Members, Archives
|
---|
9782 | @section Implicit Rule for Archive Member Targets
|
---|
9783 |
|
---|
9784 | Recall that a target that looks like @file{@var{a}(@var{m})} stands for the
|
---|
9785 | member named @var{m} in the archive file @var{a}.
|
---|
9786 |
|
---|
9787 | When @code{make} looks for an implicit rule for such a target, as a special
|
---|
9788 | feature it considers implicit rules that match @file{(@var{m})}, as well as
|
---|
9789 | those that match the actual target @file{@var{a}(@var{m})}.
|
---|
9790 |
|
---|
9791 | This causes one special rule whose target is @file{(%)} to match. This
|
---|
9792 | rule updates the target @file{@var{a}(@var{m})} by copying the file @var{m}
|
---|
9793 | into the archive. For example, it will update the archive member target
|
---|
9794 | @file{foo.a(bar.o)} by copying the @emph{file} @file{bar.o} into the
|
---|
9795 | archive @file{foo.a} as a @emph{member} named @file{bar.o}.
|
---|
9796 |
|
---|
9797 | When this rule is chained with others, the result is very powerful.
|
---|
9798 | Thus, @samp{make "foo.a(bar.o)"} (the quotes are needed to protect the
|
---|
9799 | @samp{(} and @samp{)} from being interpreted specially by the shell) in
|
---|
9800 | the presence of a file @file{bar.c} is enough to cause the following
|
---|
9801 | commands to be run, even without a makefile:
|
---|
9802 |
|
---|
9803 | @example
|
---|
9804 | cc -c bar.c -o bar.o
|
---|
9805 | ar r foo.a bar.o
|
---|
9806 | rm -f bar.o
|
---|
9807 | @end example
|
---|
9808 |
|
---|
9809 | @noindent
|
---|
9810 | Here @code{make} has envisioned the file @file{bar.o} as an intermediate
|
---|
9811 | file. @xref{Chained Rules, ,Chains of Implicit Rules}.
|
---|
9812 |
|
---|
9813 | Implicit rules such as this one are written using the automatic variable
|
---|
9814 | @samp{$%}. @xref{Automatic Variables}.
|
---|
9815 |
|
---|
9816 | An archive member name in an archive cannot contain a directory name, but
|
---|
9817 | it may be useful in a makefile to pretend that it does. If you write an
|
---|
9818 | archive member target @file{foo.a(dir/file.o)}, @code{make} will perform
|
---|
9819 | automatic updating with this command:
|
---|
9820 |
|
---|
9821 | @example
|
---|
9822 | ar r foo.a dir/file.o
|
---|
9823 | @end example
|
---|
9824 |
|
---|
9825 | @noindent
|
---|
9826 | which has the effect of copying the file @file{dir/file.o} into a member
|
---|
9827 | named @file{file.o}. In connection with such usage, the automatic variables
|
---|
9828 | @code{%D} and @code{%F} may be useful.
|
---|
9829 |
|
---|
9830 | @menu
|
---|
9831 | * Archive Symbols:: How to update archive symbol directories.
|
---|
9832 | @end menu
|
---|
9833 |
|
---|
9834 | @node Archive Symbols, , Archive Update, Archive Update
|
---|
9835 | @subsection Updating Archive Symbol Directories
|
---|
9836 | @cindex @code{__.SYMDEF}
|
---|
9837 | @cindex updating archive symbol directories
|
---|
9838 | @cindex archive symbol directory updating
|
---|
9839 | @cindex symbol directories, updating archive
|
---|
9840 | @cindex directories, updating archive symbol
|
---|
9841 |
|
---|
9842 | An archive file that is used as a library usually contains a special member
|
---|
9843 | named @file{__.SYMDEF} that contains a directory of the external symbol
|
---|
9844 | names defined by all the other members. After you update any other
|
---|
9845 | members, you need to update @file{__.SYMDEF} so that it will summarize the
|
---|
9846 | other members properly. This is done by running the @code{ranlib} program:
|
---|
9847 |
|
---|
9848 | @example
|
---|
9849 | ranlib @var{archivefile}
|
---|
9850 | @end example
|
---|
9851 |
|
---|
9852 | Normally you would put this command in the rule for the archive file,
|
---|
9853 | and make all the members of the archive file prerequisites of that rule.
|
---|
9854 | For example,
|
---|
9855 |
|
---|
9856 | @example
|
---|
9857 | libfoo.a: libfoo.a(x.o) libfoo.a(y.o) @dots{}
|
---|
9858 | ranlib libfoo.a
|
---|
9859 | @end example
|
---|
9860 |
|
---|
9861 | @noindent
|
---|
9862 | The effect of this is to update archive members @file{x.o}, @file{y.o},
|
---|
9863 | etc., and then update the symbol directory member @file{__.SYMDEF} by
|
---|
9864 | running @code{ranlib}. The rules for updating the members are not shown
|
---|
9865 | here; most likely you can omit them and use the implicit rule which copies
|
---|
9866 | files into the archive, as described in the preceding section.
|
---|
9867 |
|
---|
9868 | This is not necessary when using the GNU @code{ar} program, which
|
---|
9869 | updates the @file{__.SYMDEF} member automatically.
|
---|
9870 |
|
---|
9871 | @node Archive Pitfalls, Archive Suffix Rules, Archive Update, Archives
|
---|
9872 | @section Dangers When Using Archives
|
---|
9873 | @cindex archive, and parallel execution
|
---|
9874 | @cindex parallel execution, and archive update
|
---|
9875 | @cindex archive, and @code{-j}
|
---|
9876 | @cindex @code{-j}, and archive update
|
---|
9877 |
|
---|
9878 | It is important to be careful when using parallel execution (the
|
---|
9879 | @code{-j} switch; @pxref{Parallel, ,Parallel Execution}) and archives.
|
---|
9880 | If multiple @code{ar} commands run at the same time on the same archive
|
---|
9881 | file, they will not know about each other and can corrupt the file.
|
---|
9882 |
|
---|
9883 | Possibly a future version of @code{make} will provide a mechanism to
|
---|
9884 | circumvent this problem by serializing all commands that operate on the
|
---|
9885 | same archive file. But for the time being, you must either write your
|
---|
9886 | makefiles to avoid this problem in some other way, or not use @code{-j}.
|
---|
9887 |
|
---|
9888 | @node Archive Suffix Rules, , Archive Pitfalls, Archives
|
---|
9889 | @section Suffix Rules for Archive Files
|
---|
9890 | @cindex suffix rule, for archive
|
---|
9891 | @cindex archive, suffix rule for
|
---|
9892 | @cindex library archive, suffix rule for
|
---|
9893 | @cindex @code{.a} (archives)
|
---|
9894 |
|
---|
9895 | You can write a special kind of suffix rule for dealing with archive
|
---|
9896 | files. @xref{Suffix Rules}, for a full explanation of suffix rules.
|
---|
9897 | Archive suffix rules are obsolete in GNU @code{make}, because pattern
|
---|
9898 | rules for archives are a more general mechanism (@pxref{Archive
|
---|
9899 | Update}). But they are retained for compatibility with other
|
---|
9900 | @code{make}s.
|
---|
9901 |
|
---|
9902 | To write a suffix rule for archives, you simply write a suffix rule
|
---|
9903 | using the target suffix @samp{.a} (the usual suffix for archive files).
|
---|
9904 | For example, here is the old-fashioned suffix rule to update a library
|
---|
9905 | archive from C source files:
|
---|
9906 |
|
---|
9907 | @example
|
---|
9908 | @group
|
---|
9909 | .c.a:
|
---|
9910 | $(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $*.o
|
---|
9911 | $(AR) r $@@ $*.o
|
---|
9912 | $(RM) $*.o
|
---|
9913 | @end group
|
---|
9914 | @end example
|
---|
9915 |
|
---|
9916 | @noindent
|
---|
9917 | This works just as if you had written the pattern rule:
|
---|
9918 |
|
---|
9919 | @example
|
---|
9920 | @group
|
---|
9921 | (%.o): %.c
|
---|
9922 | $(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $*.o
|
---|
9923 | $(AR) r $@@ $*.o
|
---|
9924 | $(RM) $*.o
|
---|
9925 | @end group
|
---|
9926 | @end example
|
---|
9927 |
|
---|
9928 | In fact, this is just what @code{make} does when it sees a suffix rule
|
---|
9929 | with @samp{.a} as the target suffix. Any double-suffix rule
|
---|
9930 | @w{@samp{.@var{x}.a}} is converted to a pattern rule with the target
|
---|
9931 | pattern @samp{(%.o)} and a prerequisite pattern of @samp{%.@var{x}}.
|
---|
9932 |
|
---|
9933 | Since you might want to use @samp{.a} as the suffix for some other kind
|
---|
9934 | of file, @code{make} also converts archive suffix rules to pattern rules
|
---|
9935 | in the normal way (@pxref{Suffix Rules}). Thus a double-suffix rule
|
---|
9936 | @w{@samp{.@var{x}.a}} produces two pattern rules: @samp{@w{(%.o):}
|
---|
9937 | @w{%.@var{x}}} and @samp{@w{%.a}: @w{%.@var{x}}}.@refill
|
---|
9938 |
|
---|
9939 | @node Features, Missing, Archives, Top
|
---|
9940 | @chapter Features of GNU @code{make}
|
---|
9941 | @cindex features of GNU @code{make}
|
---|
9942 | @cindex portability
|
---|
9943 | @cindex compatibility
|
---|
9944 |
|
---|
9945 | Here is a summary of the features of GNU @code{make}, for comparison
|
---|
9946 | with and credit to other versions of @code{make}. We consider the
|
---|
9947 | features of @code{make} in 4.2 BSD systems as a baseline. If you are
|
---|
9948 | concerned with writing portable makefiles, you should not use the
|
---|
9949 | features of @code{make} listed here, nor the ones in @ref{Missing}.
|
---|
9950 |
|
---|
9951 | Many features come from the version of @code{make} in System V.
|
---|
9952 |
|
---|
9953 | @itemize @bullet
|
---|
9954 | @item
|
---|
9955 | The @code{VPATH} variable and its special meaning.
|
---|
9956 | @xref{Directory Search, , Searching Directories for Prerequisites}.
|
---|
9957 | This feature exists in System V @code{make}, but is undocumented.
|
---|
9958 | It is documented in 4.3 BSD @code{make} (which says it mimics System V's
|
---|
9959 | @code{VPATH} feature).@refill
|
---|
9960 |
|
---|
9961 | @item
|
---|
9962 | Included makefiles. @xref{Include, ,Including Other Makefiles}.
|
---|
9963 | Allowing multiple files to be included with a single directive is a GNU
|
---|
9964 | extension.
|
---|
9965 |
|
---|
9966 | @item
|
---|
9967 | Variables are read from and communicated via the environment.
|
---|
9968 | @xref{Environment, ,Variables from the Environment}.
|
---|
9969 |
|
---|
9970 | @item
|
---|
9971 | Options passed through the variable @code{MAKEFLAGS} to recursive
|
---|
9972 | invocations of @code{make}.
|
---|
9973 | @xref{Options/Recursion, ,Communicating Options to a Sub-@code{make}}.
|
---|
9974 |
|
---|
9975 | @item
|
---|
9976 | The automatic variable @code{$%} is set to the member name
|
---|
9977 | in an archive reference. @xref{Automatic Variables}.
|
---|
9978 |
|
---|
9979 | @item
|
---|
9980 | The automatic variables @code{$@@}, @code{$*}, @code{$<}, @code{$%},
|
---|
9981 | and @code{$?} have corresponding forms like @code{$(@@F)} and
|
---|
9982 | @code{$(@@D)}. We have generalized this to @code{$^} as an obvious
|
---|
9983 | extension. @xref{Automatic Variables}.@refill
|
---|
9984 |
|
---|
9985 | @item
|
---|
9986 | Substitution variable references.
|
---|
9987 | @xref{Reference, ,Basics of Variable References}.
|
---|
9988 |
|
---|
9989 | @item
|
---|
9990 | The command-line options @samp{-b} and @samp{-m}, accepted and
|
---|
9991 | ignored. In System V @code{make}, these options actually do something.
|
---|
9992 |
|
---|
9993 | @item
|
---|
9994 | Execution of recursive commands to run @code{make} via the variable
|
---|
9995 | @code{MAKE} even if @samp{-n}, @samp{-q} or @samp{-t} is specified.
|
---|
9996 | @xref{Recursion, ,Recursive Use of @code{make}}.
|
---|
9997 |
|
---|
9998 | @item
|
---|
9999 | Support for suffix @samp{.a} in suffix rules. @xref{Archive Suffix
|
---|
10000 | Rules}. This feature is obsolete in GNU @code{make}, because the
|
---|
10001 | general feature of rule chaining (@pxref{Chained Rules, ,Chains of
|
---|
10002 | Implicit Rules}) allows one pattern rule for installing members in an
|
---|
10003 | archive (@pxref{Archive Update}) to be sufficient.
|
---|
10004 |
|
---|
10005 | @item
|
---|
10006 | The arrangement of lines and backslash-newline combinations in
|
---|
10007 | commands is retained when the commands are printed, so they appear as
|
---|
10008 | they do in the makefile, except for the stripping of initial
|
---|
10009 | whitespace.
|
---|
10010 | @end itemize
|
---|
10011 |
|
---|
10012 | The following features were inspired by various other versions of
|
---|
10013 | @code{make}. In some cases it is unclear exactly which versions inspired
|
---|
10014 | which others.
|
---|
10015 |
|
---|
10016 | @itemize @bullet
|
---|
10017 | @item
|
---|
10018 | Pattern rules using @samp{%}.
|
---|
10019 | This has been implemented in several versions of @code{make}.
|
---|
10020 | We're not sure who invented it first, but it's been spread around a bit.
|
---|
10021 | @xref{Pattern Rules, ,Defining and Redefining Pattern Rules}.@refill
|
---|
10022 |
|
---|
10023 | @item
|
---|
10024 | Rule chaining and implicit intermediate files.
|
---|
10025 | This was implemented by Stu Feldman in his version of @code{make}
|
---|
10026 | for AT&T Eighth Edition Research Unix, and later by Andrew Hume of
|
---|
10027 | AT&T Bell Labs in his @code{mk} program (where he terms it
|
---|
10028 | ``transitive closure''). We do not really know if
|
---|
10029 | we got this from either of them or thought it up ourselves at the
|
---|
10030 | same time. @xref{Chained Rules, ,Chains of Implicit Rules}.
|
---|
10031 |
|
---|
10032 | @item
|
---|
10033 | The automatic variable @code{$^} containing a list of all prerequisites
|
---|
10034 | of the current target. We did not invent this, but we have no idea who
|
---|
10035 | did. @xref{Automatic Variables}. The automatic variable
|
---|
10036 | @code{$+} is a simple extension of @code{$^}.
|
---|
10037 |
|
---|
10038 | @item
|
---|
10039 | The ``what if'' flag (@samp{-W} in GNU @code{make}) was (as far as we know)
|
---|
10040 | invented by Andrew Hume in @code{mk}.
|
---|
10041 | @xref{Instead of Execution, ,Instead of Executing the Commands}.
|
---|
10042 |
|
---|
10043 | @item
|
---|
10044 | The concept of doing several things at once (parallelism) exists in
|
---|
10045 | many incarnations of @code{make} and similar programs, though not in the
|
---|
10046 | System V or BSD implementations. @xref{Execution, ,Command Execution}.
|
---|
10047 |
|
---|
10048 | @item
|
---|
10049 | Modified variable references using pattern substitution come from
|
---|
10050 | SunOS 4. @xref{Reference, ,Basics of Variable References}.
|
---|
10051 | This functionality was provided in GNU @code{make} by the
|
---|
10052 | @code{patsubst} function before the alternate syntax was implemented
|
---|
10053 | for compatibility with SunOS 4. It is not altogether clear who
|
---|
10054 | inspired whom, since GNU @code{make} had @code{patsubst} before SunOS
|
---|
10055 | 4 was released.@refill
|
---|
10056 |
|
---|
10057 | @item
|
---|
10058 | The special significance of @samp{+} characters preceding command lines
|
---|
10059 | (@pxref{Instead of Execution, ,Instead of Executing the Commands}) is
|
---|
10060 | mandated by
|
---|
10061 | @cite{IEEE Standard 1003.2-1992} (POSIX.2).
|
---|
10062 |
|
---|
10063 | @item
|
---|
10064 | The @samp{+=} syntax to append to the value of a variable comes from SunOS
|
---|
10065 | 4 @code{make}. @xref{Appending, , Appending More Text to Variables}.
|
---|
10066 |
|
---|
10067 | @item
|
---|
10068 | The syntax @w{@samp{@var{archive}(@var{mem1} @var{mem2}@dots{})}} to list
|
---|
10069 | multiple members in a single archive file comes from SunOS 4 @code{make}.
|
---|
10070 | @xref{Archive Members}.
|
---|
10071 |
|
---|
10072 | @item
|
---|
10073 | The @code{-include} directive to include makefiles with no error for a
|
---|
10074 | nonexistent file comes from SunOS 4 @code{make}. (But note that SunOS 4
|
---|
10075 | @code{make} does not allow multiple makefiles to be specified in one
|
---|
10076 | @code{-include} directive.) The same feature appears with the name
|
---|
10077 | @code{sinclude} in SGI @code{make} and perhaps others.
|
---|
10078 | @end itemize
|
---|
10079 |
|
---|
10080 | The remaining features are inventions new in GNU @code{make}:
|
---|
10081 |
|
---|
10082 | @itemize @bullet
|
---|
10083 | @item
|
---|
10084 | Use the @samp{-v} or @samp{--version} option to print version and
|
---|
10085 | copyright information.
|
---|
10086 |
|
---|
10087 | @item
|
---|
10088 | Use the @samp{-h} or @samp{--help} option to summarize the options to
|
---|
10089 | @code{make}.
|
---|
10090 |
|
---|
10091 | @item
|
---|
10092 | Simply-expanded variables. @xref{Flavors, ,The Two Flavors of Variables}.
|
---|
10093 |
|
---|
10094 | @item
|
---|
10095 | Pass command-line variable assignments automatically through the
|
---|
10096 | variable @code{MAKE} to recursive @code{make} invocations.
|
---|
10097 | @xref{Recursion, ,Recursive Use of @code{make}}.
|
---|
10098 |
|
---|
10099 | @item
|
---|
10100 | Use the @samp{-C} or @samp{--directory} command option to change
|
---|
10101 | directory. @xref{Options Summary, ,Summary of Options}.
|
---|
10102 |
|
---|
10103 | @item
|
---|
10104 | Make verbatim variable definitions with @code{define}.
|
---|
10105 | @xref{Defining, ,Defining Variables Verbatim}.
|
---|
10106 |
|
---|
10107 | @item
|
---|
10108 | Declare phony targets with the special target @code{.PHONY}.
|
---|
10109 |
|
---|
10110 | Andrew Hume of AT&T Bell Labs implemented a similar feature with a
|
---|
10111 | different syntax in his @code{mk} program. This seems to be a case of
|
---|
10112 | parallel discovery. @xref{Phony Targets, ,Phony Targets}.
|
---|
10113 |
|
---|
10114 | @item
|
---|
10115 | Manipulate text by calling functions.
|
---|
10116 | @xref{Functions, ,Functions for Transforming Text}.
|
---|
10117 |
|
---|
10118 | @item
|
---|
10119 | Use the @samp{-o} or @samp{--old-file}
|
---|
10120 | option to pretend a file's modification-time is old.
|
---|
10121 | @xref{Avoiding Compilation, ,Avoiding Recompilation of Some Files}.
|
---|
10122 |
|
---|
10123 | @item
|
---|
10124 | Conditional execution.
|
---|
10125 |
|
---|
10126 | This feature has been implemented numerous times in various versions
|
---|
10127 | of @code{make}; it seems a natural extension derived from the features
|
---|
10128 | of the C preprocessor and similar macro languages and is not a
|
---|
10129 | revolutionary concept. @xref{Conditionals, ,Conditional Parts of Makefiles}.
|
---|
10130 |
|
---|
10131 | @item
|
---|
10132 | Specify a search path for included makefiles.
|
---|
10133 | @xref{Include, ,Including Other Makefiles}.
|
---|
10134 |
|
---|
10135 | @item
|
---|
10136 | Specify extra makefiles to read with an environment variable.
|
---|
10137 | @xref{MAKEFILES Variable, ,The Variable @code{MAKEFILES}}.
|
---|
10138 |
|
---|
10139 | @item
|
---|
10140 | Strip leading sequences of @samp{./} from file names, so that
|
---|
10141 | @file{./@var{file}} and @file{@var{file}} are considered to be the
|
---|
10142 | same file.@refill
|
---|
10143 |
|
---|
10144 | @item
|
---|
10145 | Use a special search method for library prerequisites written in the
|
---|
10146 | form @samp{-l@var{name}}.
|
---|
10147 | @xref{Libraries/Search, ,Directory Search for Link Libraries}.
|
---|
10148 |
|
---|
10149 | @item
|
---|
10150 | Allow suffixes for suffix rules
|
---|
10151 | (@pxref{Suffix Rules, ,Old-Fashioned Suffix Rules}) to contain any
|
---|
10152 | characters. In other versions of @code{make}, they must begin with
|
---|
10153 | @samp{.} and not contain any @samp{/} characters.
|
---|
10154 |
|
---|
10155 | @item
|
---|
10156 | Keep track of the current level of @code{make} recursion using the
|
---|
10157 | variable @code{MAKELEVEL}. @xref{Recursion, ,Recursive Use of @code{make}}.
|
---|
10158 |
|
---|
10159 | @item
|
---|
10160 | Provide any goals given on the command line in the variable
|
---|
10161 | @code{MAKECMDGOALS}. @xref{Goals, ,Arguments to Specify the Goals}.
|
---|
10162 |
|
---|
10163 | @item
|
---|
10164 | Specify static pattern rules. @xref{Static Pattern, ,Static Pattern Rules}.
|
---|
10165 |
|
---|
10166 | @item
|
---|
10167 | Provide selective @code{vpath} search.
|
---|
10168 | @xref{Directory Search, ,Searching Directories for Prerequisites}.
|
---|
10169 |
|
---|
10170 | @item
|
---|
10171 | Provide computed variable references.
|
---|
10172 | @xref{Reference, ,Basics of Variable References}.
|
---|
10173 |
|
---|
10174 | @item
|
---|
10175 | Update makefiles. @xref{Remaking Makefiles, ,How Makefiles Are Remade}.
|
---|
10176 | System V @code{make} has a very, very limited form of this
|
---|
10177 | functionality in that it will check out SCCS files for makefiles.
|
---|
10178 |
|
---|
10179 | @item
|
---|
10180 | Various new built-in implicit rules.
|
---|
10181 | @xref{Catalogue of Rules, ,Catalogue of Implicit Rules}.
|
---|
10182 |
|
---|
10183 | @item
|
---|
10184 | The built-in variable @samp{MAKE_VERSION} gives the version number of
|
---|
10185 | @code{make}.
|
---|
10186 | @vindex MAKE_VERSION
|
---|
10187 | @end itemize
|
---|
10188 |
|
---|
10189 | @node Missing, Makefile Conventions, Features, Top
|
---|
10190 | @chapter Incompatibilities and Missing Features
|
---|
10191 | @cindex incompatibilities
|
---|
10192 | @cindex missing features
|
---|
10193 | @cindex features, missing
|
---|
10194 |
|
---|
10195 | The @code{make} programs in various other systems support a few features
|
---|
10196 | that are not implemented in GNU @code{make}. The POSIX.2 standard
|
---|
10197 | (@cite{IEEE Standard 1003.2-1992}) which specifies @code{make} does not
|
---|
10198 | require any of these features.@refill
|
---|
10199 |
|
---|
10200 | @itemize @bullet
|
---|
10201 | @item
|
---|
10202 | A target of the form @samp{@var{file}((@var{entry}))} stands for a member
|
---|
10203 | of archive file @var{file}. The member is chosen, not by name, but by
|
---|
10204 | being an object file which defines the linker symbol @var{entry}.@refill
|
---|
10205 |
|
---|
10206 | This feature was not put into GNU @code{make} because of the
|
---|
10207 | nonmodularity of putting knowledge into @code{make} of the internal
|
---|
10208 | format of archive file symbol tables.
|
---|
10209 | @xref{Archive Symbols, ,Updating Archive Symbol Directories}.
|
---|
10210 |
|
---|
10211 | @item
|
---|
10212 | Suffixes (used in suffix rules) that end with the character @samp{~}
|
---|
10213 | have a special meaning to System V @code{make};
|
---|
10214 | they refer to the SCCS file that corresponds
|
---|
10215 | to the file one would get without the @samp{~}. For example, the
|
---|
10216 | suffix rule @samp{.c~.o} would make the file @file{@var{n}.o} from
|
---|
10217 | the SCCS file @file{s.@var{n}.c}. For complete coverage, a whole
|
---|
10218 | series of such suffix rules is required.
|
---|
10219 | @xref{Suffix Rules, ,Old-Fashioned Suffix Rules}.
|
---|
10220 |
|
---|
10221 | In GNU @code{make}, this entire series of cases is handled by two
|
---|
10222 | pattern rules for extraction from SCCS, in combination with the
|
---|
10223 | general feature of rule chaining.
|
---|
10224 | @xref{Chained Rules, ,Chains of Implicit Rules}.
|
---|
10225 |
|
---|
10226 | @item
|
---|
10227 | In System V and 4.3 BSD @code{make}, files found by @code{VPATH} search
|
---|
10228 | (@pxref{Directory Search, ,Searching Directories for Prerequisites}) have their names changed inside command
|
---|
10229 | strings. We feel it is much cleaner to always use automatic variables
|
---|
10230 | and thus make this feature obsolete.@refill
|
---|
10231 |
|
---|
10232 | @item
|
---|
10233 | In some Unix @code{make}s, the automatic variable @code{$*} appearing in
|
---|
10234 | the prerequisites of a rule has the amazingly strange ``feature'' of
|
---|
10235 | expanding to the full name of the @emph{target of that rule}. We cannot
|
---|
10236 | imagine what went on in the minds of Unix @code{make} developers to do
|
---|
10237 | this; it is utterly inconsistent with the normal definition of @code{$*}.
|
---|
10238 | @vindex * @r{(automatic variable), unsupported bizarre usage}
|
---|
10239 |
|
---|
10240 | @item
|
---|
10241 | In some Unix @code{make}s, implicit rule search
|
---|
10242 | (@pxref{Implicit Rules, ,Using Implicit Rules}) is apparently done for
|
---|
10243 | @emph{all} targets, not just those without commands. This means you can
|
---|
10244 | do:@refill
|
---|
10245 |
|
---|
10246 | @example
|
---|
10247 | @group
|
---|
10248 | foo.o:
|
---|
10249 | cc -c foo.c
|
---|
10250 | @end group
|
---|
10251 | @end example
|
---|
10252 |
|
---|
10253 | @noindent
|
---|
10254 | and Unix @code{make} will intuit that @file{foo.o} depends on
|
---|
10255 | @file{foo.c}.@refill
|
---|
10256 |
|
---|
10257 | We feel that such usage is broken. The prerequisite properties of
|
---|
10258 | @code{make} are well-defined (for GNU @code{make}, at least),
|
---|
10259 | and doing such a thing simply does not fit the model.@refill
|
---|
10260 |
|
---|
10261 | @item
|
---|
10262 | GNU @code{make} does not include any built-in implicit rules for
|
---|
10263 | compiling or preprocessing EFL programs. If we hear of anyone who is
|
---|
10264 | using EFL, we will gladly add them.
|
---|
10265 |
|
---|
10266 | @item
|
---|
10267 | It appears that in SVR4 @code{make}, a suffix rule can be specified with
|
---|
10268 | no commands, and it is treated as if it had empty commands
|
---|
10269 | (@pxref{Empty Commands}). For example:
|
---|
10270 |
|
---|
10271 | @example
|
---|
10272 | .c.a:
|
---|
10273 | @end example
|
---|
10274 |
|
---|
10275 | @noindent
|
---|
10276 | will override the built-in @file{.c.a} suffix rule.
|
---|
10277 |
|
---|
10278 | We feel that it is cleaner for a rule without commands to always simply
|
---|
10279 | add to the prerequisite list for the target. The above example can be
|
---|
10280 | easily rewritten to get the desired behavior in GNU @code{make}:
|
---|
10281 |
|
---|
10282 | @example
|
---|
10283 | .c.a: ;
|
---|
10284 | @end example
|
---|
10285 |
|
---|
10286 | @item
|
---|
10287 | Some versions of @code{make} invoke the shell with the @samp{-e} flag,
|
---|
10288 | except under @samp{-k} (@pxref{Testing, ,Testing the Compilation of a
|
---|
10289 | Program}). The @samp{-e} flag tells the shell to exit as soon as any
|
---|
10290 | program it runs returns a nonzero status. We feel it is cleaner to
|
---|
10291 | write each shell command line to stand on its own and not require this
|
---|
10292 | special treatment.
|
---|
10293 | @end itemize
|
---|
10294 |
|
---|
10295 | @comment The makefile standards are in a separate file that is also
|
---|
10296 | @comment included by standards.texi.
|
---|
10297 | @include make-stds.texi
|
---|
10298 |
|
---|
10299 | @node Quick Reference, Error Messages, Makefile Conventions, Top
|
---|
10300 | @appendix Quick Reference
|
---|
10301 |
|
---|
10302 | This appendix summarizes the directives, text manipulation functions,
|
---|
10303 | and special variables which GNU @code{make} understands.
|
---|
10304 | @xref{Special Targets}, @ref{Catalogue of Rules, ,Catalogue of Implicit Rules},
|
---|
10305 | and @ref{Options Summary, ,Summary of Options},
|
---|
10306 | for other summaries.
|
---|
10307 |
|
---|
10308 | Here is a summary of the directives GNU @code{make} recognizes:
|
---|
10309 |
|
---|
10310 | @table @code
|
---|
10311 | @item define @var{variable}
|
---|
10312 | @itemx endef
|
---|
10313 |
|
---|
10314 | Define a multi-line, recursively-expanded variable.@*
|
---|
10315 | @xref{Sequences}.
|
---|
10316 |
|
---|
10317 | @item ifdef @var{variable}
|
---|
10318 | @itemx ifndef @var{variable}
|
---|
10319 | @itemx ifeq (@var{a},@var{b})
|
---|
10320 | @itemx ifeq "@var{a}" "@var{b}"
|
---|
10321 | @itemx ifeq '@var{a}' '@var{b}'
|
---|
10322 | @itemx ifneq (@var{a},@var{b})
|
---|
10323 | @itemx ifneq "@var{a}" "@var{b}"
|
---|
10324 | @itemx ifneq '@var{a}' '@var{b}'
|
---|
10325 | @itemx else
|
---|
10326 | @itemx endif
|
---|
10327 |
|
---|
10328 | Conditionally evaluate part of the makefile.@*
|
---|
10329 | @xref{Conditionals}.
|
---|
10330 |
|
---|
10331 | @item include @var{file}
|
---|
10332 | @itemx -include @var{file}
|
---|
10333 | @itemx sinclude @var{file}
|
---|
10334 |
|
---|
10335 | Include another makefile.@*
|
---|
10336 | @xref{Include, ,Including Other Makefiles}.
|
---|
10337 |
|
---|
10338 | @item override @var{variable} = @var{value}
|
---|
10339 | @itemx override @var{variable} := @var{value}
|
---|
10340 | @itemx override @var{variable} += @var{value}
|
---|
10341 | @itemx override @var{variable} ?= @var{value}
|
---|
10342 | @itemx override define @var{variable}
|
---|
10343 | @itemx endef
|
---|
10344 |
|
---|
10345 | Define a variable, overriding any previous definition, even one from
|
---|
10346 | the command line.@*
|
---|
10347 | @xref{Override Directive, ,The @code{override} Directive}.
|
---|
10348 |
|
---|
10349 | @item export
|
---|
10350 |
|
---|
10351 | Tell @code{make} to export all variables to child processes by default.@*
|
---|
10352 | @xref{Variables/Recursion, , Communicating Variables to a Sub-@code{make}}.
|
---|
10353 |
|
---|
10354 | @item export @var{variable}
|
---|
10355 | @itemx export @var{variable} = @var{value}
|
---|
10356 | @itemx export @var{variable} := @var{value}
|
---|
10357 | @itemx export @var{variable} += @var{value}
|
---|
10358 | @itemx export @var{variable} ?= @var{value}
|
---|
10359 | @itemx unexport @var{variable}
|
---|
10360 | Tell @code{make} whether or not to export a particular variable to child
|
---|
10361 | processes.@*
|
---|
10362 | @xref{Variables/Recursion, , Communicating Variables to a Sub-@code{make}}.
|
---|
10363 |
|
---|
10364 | @item vpath @var{pattern} @var{path}
|
---|
10365 | Specify a search path for files matching a @samp{%} pattern.@*
|
---|
10366 | @xref{Selective Search, , The @code{vpath} Directive}.
|
---|
10367 |
|
---|
10368 | @item vpath @var{pattern}
|
---|
10369 | Remove all search paths previously specified for @var{pattern}.
|
---|
10370 |
|
---|
10371 | @item vpath
|
---|
10372 | Remove all search paths previously specified in any @code{vpath}
|
---|
10373 | directive.
|
---|
10374 | @end table
|
---|
10375 |
|
---|
10376 | Here is a summary of the built-in functions (@pxref{Functions}):
|
---|
10377 |
|
---|
10378 | @table @code
|
---|
10379 | @item $(subst @var{from},@var{to},@var{text})
|
---|
10380 | Replace @var{from} with @var{to} in @var{text}.@*
|
---|
10381 | @xref{Text Functions, , Functions for String Substitution and Analysis}.
|
---|
10382 |
|
---|
10383 | @item $(patsubst @var{pattern},@var{replacement},@var{text})
|
---|
10384 | Replace words matching @var{pattern} with @var{replacement} in @var{text}.@*
|
---|
10385 | @xref{Text Functions, , Functions for String Substitution and Analysis}.
|
---|
10386 |
|
---|
10387 | @item $(strip @var{string})
|
---|
10388 | Remove excess whitespace characters from @var{string}.@*
|
---|
10389 | @xref{Text Functions, , Functions for String Substitution and Analysis}.
|
---|
10390 |
|
---|
10391 | @item $(findstring @var{find},@var{text})
|
---|
10392 | Locate @var{find} in @var{text}.@*
|
---|
10393 | @xref{Text Functions, , Functions for String Substitution and Analysis}.
|
---|
10394 |
|
---|
10395 | @item $(filter @var{pattern}@dots{},@var{text})
|
---|
10396 | Select words in @var{text} that match one of the @var{pattern} words.@*
|
---|
10397 | @xref{Text Functions, , Functions for String Substitution and Analysis}.
|
---|
10398 |
|
---|
10399 | @item $(filter-out @var{pattern}@dots{},@var{text})
|
---|
10400 | Select words in @var{text} that @emph{do not} match any of the @var{pattern} words.@*
|
---|
10401 | @xref{Text Functions, , Functions for String Substitution and Analysis}.
|
---|
10402 |
|
---|
10403 | @item $(sort @var{list})
|
---|
10404 | Sort the words in @var{list} lexicographically, removing duplicates.@*
|
---|
10405 | @xref{Text Functions, , Functions for String Substitution and Analysis}.
|
---|
10406 |
|
---|
10407 | @item $(word @var{n},@var{text})
|
---|
10408 | Extract the @var{n}th word (one-origin) of @var{text}.@*
|
---|
10409 | @xref{Text Functions, , Functions for String Substitution and Analysis}.
|
---|
10410 |
|
---|
10411 | @item $(words @var{text})
|
---|
10412 | Count the number of words in @var{text}.@*
|
---|
10413 | @xref{Text Functions, , Functions for String Substitution and Analysis}.
|
---|
10414 |
|
---|
10415 | @item $(wordlist @var{s},@var{e},@var{text})
|
---|
10416 | Returns the list of words in @var{text} from @var{s} to @var{e}.@*
|
---|
10417 | @xref{Text Functions, , Functions for String Substitution and Analysis}.
|
---|
10418 |
|
---|
10419 | @item $(firstword @var{names}@dots{})
|
---|
10420 | Extract the first word of @var{names}.@*
|
---|
10421 | @xref{Text Functions, , Functions for String Substitution and Analysis}.
|
---|
10422 |
|
---|
10423 | @item $(lastword @var{names}@dots{})
|
---|
10424 | Extract the last word of @var{names}.@*
|
---|
10425 | @xref{Text Functions, , Functions for String Substitution and Analysis}.
|
---|
10426 |
|
---|
10427 | @item $(dir @var{names}@dots{})
|
---|
10428 | Extract the directory part of each file name.@*
|
---|
10429 | @xref{File Name Functions, ,Functions for File Names}.
|
---|
10430 |
|
---|
10431 | @item $(notdir @var{names}@dots{})
|
---|
10432 | Extract the non-directory part of each file name.@*
|
---|
10433 | @xref{File Name Functions, ,Functions for File Names}.
|
---|
10434 |
|
---|
10435 | @item $(suffix @var{names}@dots{})
|
---|
10436 | Extract the suffix (the last @samp{.} and following characters) of each file name.@*
|
---|
10437 | @xref{File Name Functions, ,Functions for File Names}.
|
---|
10438 |
|
---|
10439 | @item $(basename @var{names}@dots{})
|
---|
10440 | Extract the base name (name without suffix) of each file name.@*
|
---|
10441 | @xref{File Name Functions, ,Functions for File Names}.
|
---|
10442 |
|
---|
10443 | @item $(addsuffix @var{suffix},@var{names}@dots{})
|
---|
10444 | Append @var{suffix} to each word in @var{names}.@*
|
---|
10445 | @xref{File Name Functions, ,Functions for File Names}.
|
---|
10446 |
|
---|
10447 | @item $(addprefix @var{prefix},@var{names}@dots{})
|
---|
10448 | Prepend @var{prefix} to each word in @var{names}.@*
|
---|
10449 | @xref{File Name Functions, ,Functions for File Names}.
|
---|
10450 |
|
---|
10451 | @item $(join @var{list1},@var{list2})
|
---|
10452 | Join two parallel lists of words.@*
|
---|
10453 | @xref{File Name Functions, ,Functions for File Names}.
|
---|
10454 |
|
---|
10455 | @item $(wildcard @var{pattern}@dots{})
|
---|
10456 | Find file names matching a shell file name pattern (@emph{not} a
|
---|
10457 | @samp{%} pattern).@*
|
---|
10458 | @xref{Wildcard Function, ,The Function @code{wildcard}}.
|
---|
10459 |
|
---|
10460 | @item $(realpath @var{names}@dots{})
|
---|
10461 | For each file name in @var{names}, expand to an absolute name that
|
---|
10462 | does not contain any @code{.}, @code{..}, nor symlinks.@*
|
---|
10463 | @xref{File Name Functions, ,Functions for File Names}.
|
---|
10464 |
|
---|
10465 | @item $(abspath @var{names}@dots{})
|
---|
10466 | For each file name in @var{names}, expand to an absolute name that
|
---|
10467 | does not contain any @code{.} or @code{..} components, but preserves
|
---|
10468 | symlinks.@*
|
---|
10469 | @xref{File Name Functions, ,Functions for File Names}.
|
---|
10470 |
|
---|
10471 | @item $(error @var{text}@dots{})
|
---|
10472 |
|
---|
10473 | When this function is evaluated, @code{make} generates a fatal error
|
---|
10474 | with the message @var{text}.@*
|
---|
10475 | @xref{Make Control Functions, ,Functions That Control Make}.
|
---|
10476 |
|
---|
10477 | @item $(warning @var{text}@dots{})
|
---|
10478 |
|
---|
10479 | When this function is evaluated, @code{make} generates a warning with
|
---|
10480 | the message @var{text}.@*
|
---|
10481 | @xref{Make Control Functions, ,Functions That Control Make}.
|
---|
10482 |
|
---|
10483 | @item $(shell @var{command})
|
---|
10484 |
|
---|
10485 | Execute a shell command and return its output.@*
|
---|
10486 | @xref{Shell Function, , The @code{shell} Function}.
|
---|
10487 |
|
---|
10488 | @item $(origin @var{variable})
|
---|
10489 |
|
---|
10490 | Return a string describing how the @code{make} variable @var{variable} was
|
---|
10491 | defined.@*
|
---|
10492 | @xref{Origin Function, , The @code{origin} Function}.
|
---|
10493 |
|
---|
10494 | @item $(flavor @var{variable})
|
---|
10495 |
|
---|
10496 | Return a string describing the flavor of the @code{make} variable
|
---|
10497 | @var{variable}.@*
|
---|
10498 | @xref{Flavor Function, , The @code{flavor} Function}.
|
---|
10499 |
|
---|
10500 | @item $(foreach @var{var},@var{words},@var{text})
|
---|
10501 |
|
---|
10502 | Evaluate @var{text} with @var{var} bound to each word in @var{words},
|
---|
10503 | and concatenate the results.@*
|
---|
10504 | @xref{Foreach Function, ,The @code{foreach} Function}.
|
---|
10505 |
|
---|
10506 | @item $(call @var{var},@var{param},@dots{})
|
---|
10507 |
|
---|
10508 | Evaluate the variable @var{var} replacing any references to @code{$(1)},
|
---|
10509 | @code{$(2)} with the first, second, etc.@: @var{param} values.@*
|
---|
10510 | @xref{Call Function, ,The @code{call} Function}.
|
---|
10511 |
|
---|
10512 | @item $(eval @var{text})
|
---|
10513 |
|
---|
10514 | Evaluate @var{text} then read the results as makefile commands.
|
---|
10515 | Expands to the empty string.@*
|
---|
10516 | @xref{Eval Function, ,The @code{eval} Function}.
|
---|
10517 |
|
---|
10518 | @item $(value @var{var})
|
---|
10519 |
|
---|
10520 | Evaluates to the contents of the variable @var{var}, with no expansion
|
---|
10521 | performed on it.@*
|
---|
10522 | @xref{Value Function, ,The @code{value} Function}.
|
---|
10523 | @end table
|
---|
10524 |
|
---|
10525 | Here is a summary of the automatic variables.
|
---|
10526 | @xref{Automatic Variables},
|
---|
10527 | for full information.
|
---|
10528 |
|
---|
10529 | @table @code
|
---|
10530 | @item $@@
|
---|
10531 | The file name of the target.
|
---|
10532 |
|
---|
10533 | @item $%
|
---|
10534 | The target member name, when the target is an archive member.
|
---|
10535 |
|
---|
10536 | @item $<
|
---|
10537 | The name of the first prerequisite.
|
---|
10538 |
|
---|
10539 | @item $?
|
---|
10540 | The names of all the prerequisites that are
|
---|
10541 | newer than the target, with spaces between them.
|
---|
10542 | For prerequisites which are archive members, only
|
---|
10543 | the member named is used (@pxref{Archives}).
|
---|
10544 |
|
---|
10545 | @item $^
|
---|
10546 | @itemx $+
|
---|
10547 | The names of all the prerequisites, with spaces between them. For
|
---|
10548 | prerequisites which are archive members, only the member named is used
|
---|
10549 | (@pxref{Archives}). The value of @code{$^} omits duplicate
|
---|
10550 | prerequisites, while @code{$+} retains them and preserves their order.
|
---|
10551 |
|
---|
10552 | @item $*
|
---|
10553 | The stem with which an implicit rule matches
|
---|
10554 | (@pxref{Pattern Match, ,How Patterns Match}).
|
---|
10555 |
|
---|
10556 | @item $(@@D)
|
---|
10557 | @itemx $(@@F)
|
---|
10558 | The directory part and the file-within-directory part of @code{$@@}.
|
---|
10559 |
|
---|
10560 | @item $(*D)
|
---|
10561 | @itemx $(*F)
|
---|
10562 | The directory part and the file-within-directory part of @code{$*}.
|
---|
10563 |
|
---|
10564 | @item $(%D)
|
---|
10565 | @itemx $(%F)
|
---|
10566 | The directory part and the file-within-directory part of @code{$%}.
|
---|
10567 |
|
---|
10568 | @item $(<D)
|
---|
10569 | @itemx $(<F)
|
---|
10570 | The directory part and the file-within-directory part of @code{$<}.
|
---|
10571 |
|
---|
10572 | @item $(^D)
|
---|
10573 | @itemx $(^F)
|
---|
10574 | The directory part and the file-within-directory part of @code{$^}.
|
---|
10575 |
|
---|
10576 | @item $(+D)
|
---|
10577 | @itemx $(+F)
|
---|
10578 | The directory part and the file-within-directory part of @code{$+}.
|
---|
10579 |
|
---|
10580 | @item $(?D)
|
---|
10581 | @itemx $(?F)
|
---|
10582 | The directory part and the file-within-directory part of @code{$?}.
|
---|
10583 | @end table
|
---|
10584 |
|
---|
10585 | These variables are used specially by GNU @code{make}:
|
---|
10586 |
|
---|
10587 | @table @code
|
---|
10588 | @item MAKEFILES
|
---|
10589 |
|
---|
10590 | Makefiles to be read on every invocation of @code{make}.@*
|
---|
10591 | @xref{MAKEFILES Variable, ,The Variable @code{MAKEFILES}}.
|
---|
10592 |
|
---|
10593 | @item VPATH
|
---|
10594 |
|
---|
10595 | Directory search path for files not found in the current directory.@*
|
---|
10596 | @xref{General Search, , @code{VPATH} Search Path for All Prerequisites}.
|
---|
10597 |
|
---|
10598 | @item SHELL
|
---|
10599 |
|
---|
10600 | The name of the system default command interpreter, usually @file{/bin/sh}.
|
---|
10601 | You can set @code{SHELL} in the makefile to change the shell used to run
|
---|
10602 | commands. @xref{Execution, ,Command Execution}. The @code{SHELL}
|
---|
10603 | variable is handled specially when importing from and exporting to the
|
---|
10604 | environment. @xref{Choosing the Shell}.
|
---|
10605 |
|
---|
10606 | @item MAKESHELL
|
---|
10607 |
|
---|
10608 | On MS-DOS only, the name of the command interpreter that is to be used
|
---|
10609 | by @code{make}. This value takes precedence over the value of
|
---|
10610 | @code{SHELL}. @xref{Execution, ,MAKESHELL variable}.
|
---|
10611 |
|
---|
10612 | @item MAKE
|
---|
10613 |
|
---|
10614 | The name with which @code{make} was invoked.
|
---|
10615 | Using this variable in commands has special meaning.
|
---|
10616 | @xref{MAKE Variable, ,How the @code{MAKE} Variable Works}.
|
---|
10617 |
|
---|
10618 | @item MAKELEVEL
|
---|
10619 |
|
---|
10620 | The number of levels of recursion (sub-@code{make}s).@*
|
---|
10621 | @xref{Variables/Recursion}.
|
---|
10622 |
|
---|
10623 | @item MAKEFLAGS
|
---|
10624 |
|
---|
10625 | The flags given to @code{make}. You can set this in the environment or
|
---|
10626 | a makefile to set flags.@*
|
---|
10627 | @xref{Options/Recursion, ,Communicating Options to a Sub-@code{make}}.
|
---|
10628 |
|
---|
10629 | It is @emph{never} appropriate to use @code{MAKEFLAGS} directly on a
|
---|
10630 | command line: its contents may not be quoted correctly for use in the
|
---|
10631 | shell. Always allow recursive @code{make}'s to obtain these values
|
---|
10632 | through the environment from its parent.
|
---|
10633 |
|
---|
10634 | @item MAKECMDGOALS
|
---|
10635 |
|
---|
10636 | The targets given to @code{make} on the command line. Setting this
|
---|
10637 | variable has no effect on the operation of @code{make}.@*
|
---|
10638 | @xref{Goals, ,Arguments to Specify the Goals}.
|
---|
10639 |
|
---|
10640 | @item CURDIR
|
---|
10641 |
|
---|
10642 | Set to the pathname of the current working directory (after all
|
---|
10643 | @code{-C} options are processed, if any). Setting this variable has no
|
---|
10644 | effect on the operation of @code{make}.@*
|
---|
10645 | @xref{Recursion, ,Recursive Use of @code{make}}.
|
---|
10646 |
|
---|
10647 | @item SUFFIXES
|
---|
10648 |
|
---|
10649 | The default list of suffixes before @code{make} reads any makefiles.
|
---|
10650 |
|
---|
10651 | @item .LIBPATTERNS
|
---|
10652 | Defines the naming of the libraries @code{make} searches for, and their
|
---|
10653 | order.@*
|
---|
10654 | @xref{Libraries/Search, ,Directory Search for Link Libraries}.
|
---|
10655 | @end table
|
---|
10656 |
|
---|
10657 | @node Error Messages, Complex Makefile, Quick Reference, Top
|
---|
10658 | @comment node-name, next, previous, up
|
---|
10659 | @appendix Errors Generated by Make
|
---|
10660 |
|
---|
10661 | Here is a list of the more common errors you might see generated by
|
---|
10662 | @code{make}, and some information about what they mean and how to fix
|
---|
10663 | them.
|
---|
10664 |
|
---|
10665 | Sometimes @code{make} errors are not fatal, especially in the presence
|
---|
10666 | of a @code{-} prefix on a command script line, or the @code{-k} command
|
---|
10667 | line option. Errors that are fatal are prefixed with the string
|
---|
10668 | @code{***}.
|
---|
10669 |
|
---|
10670 | Error messages are all either prefixed with the name of the program
|
---|
10671 | (usually @samp{make}), or, if the error is found in a makefile, the name
|
---|
10672 | of the file and linenumber containing the problem.
|
---|
10673 |
|
---|
10674 | In the table below, these common prefixes are left off.
|
---|
10675 |
|
---|
10676 | @table @samp
|
---|
10677 |
|
---|
10678 | @item [@var{foo}] Error @var{NN}
|
---|
10679 | @itemx [@var{foo}] @var{signal description}
|
---|
10680 | These errors are not really @code{make} errors at all. They mean that a
|
---|
10681 | program that @code{make} invoked as part of a command script returned a
|
---|
10682 | non-0 error code (@samp{Error @var{NN}}), which @code{make} interprets
|
---|
10683 | as failure, or it exited in some other abnormal fashion (with a
|
---|
10684 | signal of some type). @xref{Errors, ,Errors in Commands}.
|
---|
10685 |
|
---|
10686 | If no @code{***} is attached to the message, then the subprocess failed
|
---|
10687 | but the rule in the makefile was prefixed with the @code{-} special
|
---|
10688 | character, so @code{make} ignored the error.
|
---|
10689 |
|
---|
10690 | @item missing separator. Stop.
|
---|
10691 | @itemx missing separator (did you mean TAB instead of 8 spaces?). Stop.
|
---|
10692 | This means that @code{make} could not understand much of anything about
|
---|
10693 | the command line it just read. GNU @code{make} looks for various kinds
|
---|
10694 | of separators (@code{:}, @code{=}, TAB characters, etc.) to help it
|
---|
10695 | decide what kind of commandline it's seeing. This means it couldn't
|
---|
10696 | find a valid one.
|
---|
10697 |
|
---|
10698 | One of the most common reasons for this message is that you (or perhaps
|
---|
10699 | your oh-so-helpful editor, as is the case with many MS-Windows editors)
|
---|
10700 | have attempted to indent your command scripts with spaces instead of a
|
---|
10701 | TAB character. In this case, @code{make} will use the second form of
|
---|
10702 | the error above. Remember that every line in the command script must
|
---|
10703 | begin with a TAB character. Eight spaces do not count. @xref{Rule
|
---|
10704 | Syntax}.
|
---|
10705 |
|
---|
10706 | @item commands commence before first target. Stop.
|
---|
10707 | @itemx missing rule before commands. Stop.
|
---|
10708 | This means the first thing in the makefile seems to be part of a command
|
---|
10709 | script: it begins with a TAB character and doesn't appear to be a legal
|
---|
10710 | @code{make} command (such as a variable assignment). Command scripts
|
---|
10711 | must always be associated with a target.
|
---|
10712 |
|
---|
10713 | The second form is generated if the line has a semicolon as the first
|
---|
10714 | non-whitespace character; @code{make} interprets this to mean you left
|
---|
10715 | out the "target: prerequisite" section of a rule. @xref{Rule Syntax}.
|
---|
10716 |
|
---|
10717 | @item No rule to make target `@var{xxx}'.
|
---|
10718 | @itemx No rule to make target `@var{xxx}', needed by `@var{yyy}'.
|
---|
10719 | This means that @code{make} decided it needed to build a target, but
|
---|
10720 | then couldn't find any instructions in the makefile on how to do that,
|
---|
10721 | either explicit or implicit (including in the default rules database).
|
---|
10722 |
|
---|
10723 | If you want that file to be built, you will need to add a rule to your
|
---|
10724 | makefile describing how that target can be built. Other possible
|
---|
10725 | sources of this problem are typos in the makefile (if that filename is
|
---|
10726 | wrong) or a corrupted source tree (if that file is not supposed to be
|
---|
10727 | built, but rather only a prerequisite).
|
---|
10728 |
|
---|
10729 | @item No targets specified and no makefile found. Stop.
|
---|
10730 | @itemx No targets. Stop.
|
---|
10731 | The former means that you didn't provide any targets to be built on the
|
---|
10732 | command line, and @code{make} couldn't find any makefiles to read in.
|
---|
10733 | The latter means that some makefile was found, but it didn't contain any
|
---|
10734 | default goal and none was given on the command line. GNU @code{make}
|
---|
10735 | has nothing to do in these situations.
|
---|
10736 | @xref{Makefile Arguments, ,Arguments to Specify the Makefile}.@refill
|
---|
10737 |
|
---|
10738 | @item Makefile `@var{xxx}' was not found.
|
---|
10739 | @itemx Included makefile `@var{xxx}' was not found.
|
---|
10740 | A makefile specified on the command line (first form) or included
|
---|
10741 | (second form) was not found.
|
---|
10742 |
|
---|
10743 | @item warning: overriding commands for target `@var{xxx}'
|
---|
10744 | @itemx warning: ignoring old commands for target `@var{xxx}'
|
---|
10745 | GNU @code{make} allows commands to be specified only once per target
|
---|
10746 | (except for double-colon rules). If you give commands for a target
|
---|
10747 | which already has been defined to have commands, this warning is issued
|
---|
10748 | and the second set of commands will overwrite the first set.
|
---|
10749 | @xref{Multiple Rules, ,Multiple Rules for One Target}.
|
---|
10750 |
|
---|
10751 | @item Circular @var{xxx} <- @var{yyy} dependency dropped.
|
---|
10752 | This means that @code{make} detected a loop in the dependency graph:
|
---|
10753 | after tracing the prerequisite @var{yyy} of target @var{xxx}, and its
|
---|
10754 | prerequisites, etc., one of them depended on @var{xxx} again.
|
---|
10755 |
|
---|
10756 | @item Recursive variable `@var{xxx}' references itself (eventually). Stop.
|
---|
10757 | This means you've defined a normal (recursive) @code{make} variable
|
---|
10758 | @var{xxx} that, when it's expanded, will refer to itself (@var{xxx}).
|
---|
10759 | This is not allowed; either use simply-expanded variables (@code{:=}) or
|
---|
10760 | use the append operator (@code{+=}). @xref{Using Variables, ,How to Use
|
---|
10761 | Variables}.
|
---|
10762 |
|
---|
10763 | @item Unterminated variable reference. Stop.
|
---|
10764 | This means you forgot to provide the proper closing parenthesis
|
---|
10765 | or brace in your variable or function reference.
|
---|
10766 |
|
---|
10767 | @item insufficient arguments to function `@var{xxx}'. Stop.
|
---|
10768 | This means you haven't provided the requisite number of arguments for
|
---|
10769 | this function. See the documentation of the function for a description
|
---|
10770 | of its arguments. @xref{Functions, ,Functions for Transforming Text}.
|
---|
10771 |
|
---|
10772 | @item missing target pattern. Stop.
|
---|
10773 | @itemx multiple target patterns. Stop.
|
---|
10774 | @itemx target pattern contains no `%'. Stop.
|
---|
10775 | @itemx mixed implicit and static pattern rules. Stop.
|
---|
10776 | These are generated for malformed static pattern rules. The first means
|
---|
10777 | there's no pattern in the target section of the rule; the second means
|
---|
10778 | there are multiple patterns in the target section; the third means
|
---|
10779 | the target doesn't contain a pattern character (@code{%}); and the
|
---|
10780 | fourth means that all three parts of the static pattern rule contain
|
---|
10781 | pattern characters (@code{%})--only the first two parts should.
|
---|
10782 | @xref{Static Usage, ,Syntax of Static Pattern Rules}.
|
---|
10783 |
|
---|
10784 | @item warning: -jN forced in submake: disabling jobserver mode.
|
---|
10785 | This warning and the next are generated if @code{make} detects error
|
---|
10786 | conditions related to parallel processing on systems where
|
---|
10787 | sub-@code{make}s can communicate (@pxref{Options/Recursion,
|
---|
10788 | ,Communicating Options to a Sub-@code{make}}). This warning is
|
---|
10789 | generated if a recursive invocation of a @code{make} process is forced
|
---|
10790 | to have @samp{-j@var{N}} in its argument list (where @var{N} is greater
|
---|
10791 | than one). This could happen, for example, if you set the @code{MAKE}
|
---|
10792 | environment variable to @samp{make -j2}. In this case, the
|
---|
10793 | sub-@code{make} doesn't communicate with other @code{make} processes and
|
---|
10794 | will simply pretend it has two jobs of its own.
|
---|
10795 |
|
---|
10796 | @item warning: jobserver unavailable: using -j1. Add `+' to parent make rule.
|
---|
10797 | In order for @code{make} processes to communicate, the parent will pass
|
---|
10798 | information to the child. Since this could result in problems if the
|
---|
10799 | child process isn't actually a @code{make}, the parent will only do this
|
---|
10800 | if it thinks the child is a @code{make}. The parent uses the normal
|
---|
10801 | algorithms to determine this (@pxref{MAKE Variable, ,How the @code{MAKE}
|
---|
10802 | Variable Works}). If the makefile is constructed such that the parent
|
---|
10803 | doesn't know the child is a @code{make} process, then the child will
|
---|
10804 | receive only part of the information necessary. In this case, the child
|
---|
10805 | will generate this warning message and proceed with its build in a
|
---|
10806 | sequential manner.
|
---|
10807 |
|
---|
10808 | @end table
|
---|
10809 |
|
---|
10810 | @node Complex Makefile, GNU Free Documentation License, Error Messages, Top
|
---|
10811 | @appendix Complex Makefile Example
|
---|
10812 |
|
---|
10813 | Here is the makefile for the GNU @code{tar} program. This is a
|
---|
10814 | moderately complex makefile.
|
---|
10815 |
|
---|
10816 | Because it is the first target, the default goal is @samp{all}. An
|
---|
10817 | interesting feature of this makefile is that @file{testpad.h} is a
|
---|
10818 | source file automatically created by the @code{testpad} program,
|
---|
10819 | itself compiled from @file{testpad.c}.
|
---|
10820 |
|
---|
10821 | If you type @samp{make} or @samp{make all}, then @code{make} creates
|
---|
10822 | the @file{tar} executable, the @file{rmt} daemon that provides
|
---|
10823 | remote tape access, and the @file{tar.info} Info file.
|
---|
10824 |
|
---|
10825 | If you type @samp{make install}, then @code{make} not only creates
|
---|
10826 | @file{tar}, @file{rmt}, and @file{tar.info}, but also installs
|
---|
10827 | them.
|
---|
10828 |
|
---|
10829 | If you type @samp{make clean}, then @code{make} removes the @samp{.o}
|
---|
10830 | files, and the @file{tar}, @file{rmt}, @file{testpad},
|
---|
10831 | @file{testpad.h}, and @file{core} files.
|
---|
10832 |
|
---|
10833 | If you type @samp{make distclean}, then @code{make} not only removes
|
---|
10834 | the same files as does @samp{make clean} but also the
|
---|
10835 | @file{TAGS}, @file{Makefile}, and @file{config.status} files.
|
---|
10836 | (Although it is not evident, this makefile (and
|
---|
10837 | @file{config.status}) is generated by the user with the
|
---|
10838 | @code{configure} program, which is provided in the @code{tar}
|
---|
10839 | distribution, but is not shown here.)
|
---|
10840 |
|
---|
10841 | If you type @samp{make realclean}, then @code{make} removes the same
|
---|
10842 | files as does @samp{make distclean} and also removes the Info files
|
---|
10843 | generated from @file{tar.texinfo}.
|
---|
10844 |
|
---|
10845 | In addition, there are targets @code{shar} and @code{dist} that create
|
---|
10846 | distribution kits.
|
---|
10847 |
|
---|
10848 | @example
|
---|
10849 | @group
|
---|
10850 | # Generated automatically from Makefile.in by configure.
|
---|
10851 | # Un*x Makefile for GNU tar program.
|
---|
10852 | # Copyright (C) 1991 Free Software Foundation, Inc.
|
---|
10853 | @end group
|
---|
10854 |
|
---|
10855 | @group
|
---|
10856 | # This program is free software; you can redistribute
|
---|
10857 | # it and/or modify it under the terms of the GNU
|
---|
10858 | # General Public License @dots{}
|
---|
10859 | @dots{}
|
---|
10860 | @dots{}
|
---|
10861 | @end group
|
---|
10862 |
|
---|
10863 | SHELL = /bin/sh
|
---|
10864 |
|
---|
10865 | #### Start of system configuration section. ####
|
---|
10866 |
|
---|
10867 | srcdir = .
|
---|
10868 |
|
---|
10869 | @group
|
---|
10870 | # If you use gcc, you should either run the
|
---|
10871 | # fixincludes script that comes with it or else use
|
---|
10872 | # gcc with the -traditional option. Otherwise ioctl
|
---|
10873 | # calls will be compiled incorrectly on some systems.
|
---|
10874 | CC = gcc -O
|
---|
10875 | YACC = bison -y
|
---|
10876 | INSTALL = /usr/local/bin/install -c
|
---|
10877 | INSTALLDATA = /usr/local/bin/install -c -m 644
|
---|
10878 | @end group
|
---|
10879 |
|
---|
10880 | # Things you might add to DEFS:
|
---|
10881 | # -DSTDC_HEADERS If you have ANSI C headers and
|
---|
10882 | # libraries.
|
---|
10883 | # -DPOSIX If you have POSIX.1 headers and
|
---|
10884 | # libraries.
|
---|
10885 | # -DBSD42 If you have sys/dir.h (unless
|
---|
10886 | # you use -DPOSIX), sys/file.h,
|
---|
10887 | # and st_blocks in `struct stat'.
|
---|
10888 | # -DUSG If you have System V/ANSI C
|
---|
10889 | # string and memory functions
|
---|
10890 | # and headers, sys/sysmacros.h,
|
---|
10891 | # fcntl.h, getcwd, no valloc,
|
---|
10892 | # and ndir.h (unless
|
---|
10893 | # you use -DDIRENT).
|
---|
10894 | # -DNO_MEMORY_H If USG or STDC_HEADERS but do not
|
---|
10895 | # include memory.h.
|
---|
10896 | # -DDIRENT If USG and you have dirent.h
|
---|
10897 | # instead of ndir.h.
|
---|
10898 | # -DSIGTYPE=int If your signal handlers
|
---|
10899 | # return int, not void.
|
---|
10900 | # -DNO_MTIO If you lack sys/mtio.h
|
---|
10901 | # (magtape ioctls).
|
---|
10902 | # -DNO_REMOTE If you do not have a remote shell
|
---|
10903 | # or rexec.
|
---|
10904 | # -DUSE_REXEC To use rexec for remote tape
|
---|
10905 | # operations instead of
|
---|
10906 | # forking rsh or remsh.
|
---|
10907 | # -DVPRINTF_MISSING If you lack vprintf function
|
---|
10908 | # (but have _doprnt).
|
---|
10909 | # -DDOPRNT_MISSING If you lack _doprnt function.
|
---|
10910 | # Also need to define
|
---|
10911 | # -DVPRINTF_MISSING.
|
---|
10912 | # -DFTIME_MISSING If you lack ftime system call.
|
---|
10913 | # -DSTRSTR_MISSING If you lack strstr function.
|
---|
10914 | # -DVALLOC_MISSING If you lack valloc function.
|
---|
10915 | # -DMKDIR_MISSING If you lack mkdir and
|
---|
10916 | # rmdir system calls.
|
---|
10917 | # -DRENAME_MISSING If you lack rename system call.
|
---|
10918 | # -DFTRUNCATE_MISSING If you lack ftruncate
|
---|
10919 | # system call.
|
---|
10920 | # -DV7 On Version 7 Unix (not
|
---|
10921 | # tested in a long time).
|
---|
10922 | # -DEMUL_OPEN3 If you lack a 3-argument version
|
---|
10923 | # of open, and want to emulate it
|
---|
10924 | # with system calls you do have.
|
---|
10925 | # -DNO_OPEN3 If you lack the 3-argument open
|
---|
10926 | # and want to disable the tar -k
|
---|
10927 | # option instead of emulating open.
|
---|
10928 | # -DXENIX If you have sys/inode.h
|
---|
10929 | # and need it 94 to be included.
|
---|
10930 |
|
---|
10931 | DEFS = -DSIGTYPE=int -DDIRENT -DSTRSTR_MISSING \
|
---|
10932 | -DVPRINTF_MISSING -DBSD42
|
---|
10933 | # Set this to rtapelib.o unless you defined NO_REMOTE,
|
---|
10934 | # in which case make it empty.
|
---|
10935 | RTAPELIB = rtapelib.o
|
---|
10936 | LIBS =
|
---|
10937 | DEF_AR_FILE = /dev/rmt8
|
---|
10938 | DEFBLOCKING = 20
|
---|
10939 |
|
---|
10940 | @group
|
---|
10941 | CDEBUG = -g
|
---|
10942 | CFLAGS = $(CDEBUG) -I. -I$(srcdir) $(DEFS) \
|
---|
10943 | -DDEF_AR_FILE=\"$(DEF_AR_FILE)\" \
|
---|
10944 | -DDEFBLOCKING=$(DEFBLOCKING)
|
---|
10945 | LDFLAGS = -g
|
---|
10946 | @end group
|
---|
10947 |
|
---|
10948 | @group
|
---|
10949 | prefix = /usr/local
|
---|
10950 | # Prefix for each installed program,
|
---|
10951 | # normally empty or `g'.
|
---|
10952 | binprefix =
|
---|
10953 |
|
---|
10954 | # The directory to install tar in.
|
---|
10955 | bindir = $(prefix)/bin
|
---|
10956 |
|
---|
10957 | # The directory to install the info files in.
|
---|
10958 | infodir = $(prefix)/info
|
---|
10959 | @end group
|
---|
10960 |
|
---|
10961 | #### End of system configuration section. ####
|
---|
10962 |
|
---|
10963 | SRC1 = tar.c create.c extract.c buffer.c \
|
---|
10964 | getoldopt.c update.c gnu.c mangle.c
|
---|
10965 | SRC2 = version.c list.c names.c diffarch.c \
|
---|
10966 | port.c wildmat.c getopt.c
|
---|
10967 | SRC3 = getopt1.c regex.c getdate.y
|
---|
10968 | SRCS = $(SRC1) $(SRC2) $(SRC3)
|
---|
10969 | OBJ1 = tar.o create.o extract.o buffer.o \
|
---|
10970 | getoldopt.o update.o gnu.o mangle.o
|
---|
10971 | OBJ2 = version.o list.o names.o diffarch.o \
|
---|
10972 | port.o wildmat.o getopt.o
|
---|
10973 | OBJ3 = getopt1.o regex.o getdate.o $(RTAPELIB)
|
---|
10974 | OBJS = $(OBJ1) $(OBJ2) $(OBJ3)
|
---|
10975 | @group
|
---|
10976 | AUX = README COPYING ChangeLog Makefile.in \
|
---|
10977 | makefile.pc configure configure.in \
|
---|
10978 | tar.texinfo tar.info* texinfo.tex \
|
---|
10979 | tar.h port.h open3.h getopt.h regex.h \
|
---|
10980 | rmt.h rmt.c rtapelib.c alloca.c \
|
---|
10981 | msd_dir.h msd_dir.c tcexparg.c \
|
---|
10982 | level-0 level-1 backup-specs testpad.c
|
---|
10983 | @end group
|
---|
10984 |
|
---|
10985 | .PHONY: all
|
---|
10986 | all: tar rmt tar.info
|
---|
10987 |
|
---|
10988 | @group
|
---|
10989 | .PHONY: tar
|
---|
10990 | tar: $(OBJS)
|
---|
10991 | $(CC) $(LDFLAGS) -o $@@ $(OBJS) $(LIBS)
|
---|
10992 | @end group
|
---|
10993 |
|
---|
10994 | @group
|
---|
10995 | rmt: rmt.c
|
---|
10996 | $(CC) $(CFLAGS) $(LDFLAGS) -o $@@ rmt.c
|
---|
10997 | @end group
|
---|
10998 |
|
---|
10999 | @group
|
---|
11000 | tar.info: tar.texinfo
|
---|
11001 | makeinfo tar.texinfo
|
---|
11002 | @end group
|
---|
11003 |
|
---|
11004 | @group
|
---|
11005 | .PHONY: install
|
---|
11006 | install: all
|
---|
11007 | $(INSTALL) tar $(bindir)/$(binprefix)tar
|
---|
11008 | -test ! -f rmt || $(INSTALL) rmt /etc/rmt
|
---|
11009 | $(INSTALLDATA) $(srcdir)/tar.info* $(infodir)
|
---|
11010 | @end group
|
---|
11011 |
|
---|
11012 | @group
|
---|
11013 | $(OBJS): tar.h port.h testpad.h
|
---|
11014 | regex.o buffer.o tar.o: regex.h
|
---|
11015 | # getdate.y has 8 shift/reduce conflicts.
|
---|
11016 | @end group
|
---|
11017 |
|
---|
11018 | @group
|
---|
11019 | testpad.h: testpad
|
---|
11020 | ./testpad
|
---|
11021 | @end group
|
---|
11022 |
|
---|
11023 | @group
|
---|
11024 | testpad: testpad.o
|
---|
11025 | $(CC) -o $@@ testpad.o
|
---|
11026 | @end group
|
---|
11027 |
|
---|
11028 | @group
|
---|
11029 | TAGS: $(SRCS)
|
---|
11030 | etags $(SRCS)
|
---|
11031 | @end group
|
---|
11032 |
|
---|
11033 | @group
|
---|
11034 | .PHONY: clean
|
---|
11035 | clean:
|
---|
11036 | rm -f *.o tar rmt testpad testpad.h core
|
---|
11037 | @end group
|
---|
11038 |
|
---|
11039 | @group
|
---|
11040 | .PHONY: distclean
|
---|
11041 | distclean: clean
|
---|
11042 | rm -f TAGS Makefile config.status
|
---|
11043 | @end group
|
---|
11044 |
|
---|
11045 | @group
|
---|
11046 | .PHONY: realclean
|
---|
11047 | realclean: distclean
|
---|
11048 | rm -f tar.info*
|
---|
11049 | @end group
|
---|
11050 |
|
---|
11051 | @group
|
---|
11052 | .PHONY: shar
|
---|
11053 | shar: $(SRCS) $(AUX)
|
---|
11054 | shar $(SRCS) $(AUX) | compress \
|
---|
11055 | > tar-`sed -e '/version_string/!d' \
|
---|
11056 | -e 's/[^0-9.]*\([0-9.]*\).*/\1/' \
|
---|
11057 | -e q
|
---|
11058 | version.c`.shar.Z
|
---|
11059 | @end group
|
---|
11060 |
|
---|
11061 | @group
|
---|
11062 | .PHONY: dist
|
---|
11063 | dist: $(SRCS) $(AUX)
|
---|
11064 | echo tar-`sed \
|
---|
11065 | -e '/version_string/!d' \
|
---|
11066 | -e 's/[^0-9.]*\([0-9.]*\).*/\1/' \
|
---|
11067 | -e q
|
---|
11068 | version.c` > .fname
|
---|
11069 | -rm -rf `cat .fname`
|
---|
11070 | mkdir `cat .fname`
|
---|
11071 | ln $(SRCS) $(AUX) `cat .fname`
|
---|
11072 | tar chZf `cat .fname`.tar.Z `cat .fname`
|
---|
11073 | -rm -rf `cat .fname` .fname
|
---|
11074 | @end group
|
---|
11075 |
|
---|
11076 | @group
|
---|
11077 | tar.zoo: $(SRCS) $(AUX)
|
---|
11078 | -rm -rf tmp.dir
|
---|
11079 | -mkdir tmp.dir
|
---|
11080 | -rm tar.zoo
|
---|
11081 | for X in $(SRCS) $(AUX) ; do \
|
---|
11082 | echo $$X ; \
|
---|
11083 | sed 's/$$/^M/' $$X \
|
---|
11084 | > tmp.dir/$$X ; done
|
---|
11085 | cd tmp.dir ; zoo aM ../tar.zoo *
|
---|
11086 | -rm -rf tmp.dir
|
---|
11087 | @end group
|
---|
11088 | @end example
|
---|
11089 |
|
---|
11090 | @raisesections
|
---|
11091 | @include fdl.texi
|
---|
11092 | @lowersections
|
---|
11093 |
|
---|
11094 | @node Concept Index, Name Index, GNU Free Documentation License, Top
|
---|
11095 | @unnumbered Index of Concepts
|
---|
11096 |
|
---|
11097 | @printindex cp
|
---|
11098 |
|
---|
11099 | @node Name Index, , Concept Index, Top
|
---|
11100 | @unnumbered Index of Functions, Variables, & Directives
|
---|
11101 |
|
---|
11102 | @printindex fn
|
---|
11103 |
|
---|
11104 | @bye
|
---|