1
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html401/loose.dtd">
3
<!-- This file documents the GNU make utility, which determines
4
automatically which pieces of a large program need to be recompiled,
5
and issues the commands to recompile them.
7
This is Edition 0.70, last updated 1 April 2006,
8
of The GNU Make Manual, for GNU make version 3.81.
10
Copyright C 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
11
1996, 1997, 1998, 1999, 2000, 2002, 2003, 2004, 2005, 2006
12
Free Software Foundation, Inc.
14
Permission is granted to copy, distribute and/or modify this document
15
under the terms of the GNU Free Documentation License, Version 1.2 or
16
any later version published by the Free Software Foundation; with no
17
Invariant Sections, with the Front-Cover Texts being "A GNU Manual,"
18
and with the Back-Cover Texts as in (a) below. A copy of the
19
license is included in the section entitled "GNU Free Documentation
22
(a) The FSF's Back-Cover Text is: "You have freedom to copy and modify
23
this GNU Manual, like GNU software. Copies published by the Free
24
Software Foundation raise funds for GNU development."
27
<!-- Created on August, 17 2009 by texi2html 1.78 -->
29
Written by: Lionel Cons <Lionel.Cons@cern.ch> (original author)
30
Karl Berry <karl@freefriends.org>
31
Olaf Bachmann <obachman@mathematik.uni-kl.de>
33
Maintained by: Many creative people.
34
Send bugs and suggestions to <texi2html-bug@nongnu.org>
38
<title>GNU make: 5. Writing the Commands in Rules</title>
40
<meta name="description" content="GNU make: 5. Writing the Commands in Rules">
41
<meta name="keywords" content="GNU make: 5. Writing the Commands in Rules">
42
<meta name="resource-type" content="document">
43
<meta name="distribution" content="global">
44
<meta name="Generator" content="texi2html 1.78">
45
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
46
<style type="text/css">
48
a.summary-letter {text-decoration: none}
49
pre.display {font-family: serif}
50
pre.format {font-family: serif}
51
pre.menu-comment {font-family: serif}
52
pre.menu-preformatted {font-family: serif}
53
pre.smalldisplay {font-family: serif; font-size: smaller}
54
pre.smallexample {font-size: smaller}
55
pre.smallformat {font-family: serif; font-size: smaller}
56
pre.smalllisp {font-size: smaller}
57
span.roman {font-family:serif; font-weight:normal;}
58
span.sansserif {font-family:sans-serif; font-weight:normal;}
59
ul.toc {list-style: none}
66
<body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
68
<a name="Commands"></a>
70
<table cellpadding="1" cellspacing="1" border="0">
71
<tr><td valign="middle" align="left">[<a href="make_4.html#SEC55" title="Previous section in reading order"> < </a>]</td>
72
<td valign="middle" align="left">[<a href="#SEC57" title="Next section in reading order"> > </a>]</td>
73
<td valign="middle" align="left"> </td>
74
<td valign="middle" align="left">[<a href="make_4.html#SEC30" title="Beginning of this chapter or previous chapter"> << </a>]</td>
75
<td valign="middle" align="left">[<a href="make.html#SEC_Top" title="Up section"> Up </a>]</td>
76
<td valign="middle" align="left">[<a href="make_6.html#SEC74" title="Next chapter"> >> </a>]</td>
77
<td valign="middle" align="left"> </td>
78
<td valign="middle" align="left"> </td>
79
<td valign="middle" align="left"> </td>
80
<td valign="middle" align="left"> </td>
81
<td valign="middle" align="left">[<a href="make.html#SEC_Top" title="Cover (top) of document">Top</a>]</td>
82
<td valign="middle" align="left">[<a href="make_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
83
<td valign="middle" align="left">[<a href="make_19.html#SEC148" title="Index">Index</a>]</td>
84
<td valign="middle" align="left">[<a href="make_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
86
<h1 class="chapter"> 5. Writing the Commands in Rules </h1>
88
<p>The commands of a rule consist of one or more shell command lines to
89
be executed, one at a time, in the order they appear. Typically, the
90
result of executing these commands is that the target of the rule is
93
<p>Users use many different shell programs, but commands in makefiles are
94
always interpreted by ‘<tt>/bin/sh</tt>’ unless the makefile specifies
95
otherwise. See section <a href="#SEC61">Command Execution</a>.
97
<table class="menu" border="0" cellspacing="0">
98
<tr><td align="left" valign="top"><a href="#SEC57">5.1 Command Syntax</a></td><td> </td><td align="left" valign="top"> Command syntax features and pitfalls.
100
<tr><td align="left" valign="top"><a href="#SEC60">5.2 Command Echoing</a></td><td> </td><td align="left" valign="top"> How to control when commands are echoed.
102
<tr><td align="left" valign="top"><a href="#SEC61">5.3 Command Execution</a></td><td> </td><td align="left" valign="top"> How commands are executed.
104
<tr><td align="left" valign="top"><a href="#SEC64">5.4 Parallel Execution</a></td><td> </td><td align="left" valign="top"> How commands can be executed in parallel.
106
<tr><td align="left" valign="top"><a href="#SEC65">5.5 Errors in Commands</a></td><td> </td><td align="left" valign="top"> What happens after a command execution error.
108
<tr><td align="left" valign="top"><a href="#SEC66">5.6 Interrupting or Killing <code>make</code></a></td><td> </td><td align="left" valign="top"> What happens when a command is interrupted.
110
<tr><td align="left" valign="top"><a href="#SEC67">5.7 Recursive Use of <code>make</code></a></td><td> </td><td align="left" valign="top"> Invoking <code>make</code> from makefiles.
112
<tr><td align="left" valign="top"><a href="#SEC72">5.8 Defining Canned Command Sequences</a></td><td> </td><td align="left" valign="top"> Defining canned sequences of commands.
114
<tr><td align="left" valign="top"><a href="#SEC73">5.9 Using Empty Commands</a></td><td> </td><td align="left" valign="top"> Defining useful, do-nothing commands.
119
<a name="Command-Syntax"></a>
121
<table cellpadding="1" cellspacing="1" border="0">
122
<tr><td valign="middle" align="left">[<a href="#SEC56" title="Previous section in reading order"> < </a>]</td>
123
<td valign="middle" align="left">[<a href="#SEC58" title="Next section in reading order"> > </a>]</td>
124
<td valign="middle" align="left"> </td>
125
<td valign="middle" align="left">[<a href="#SEC56" title="Beginning of this chapter or previous chapter"> << </a>]</td>
126
<td valign="middle" align="left">[<a href="#SEC56" title="Up section"> Up </a>]</td>
127
<td valign="middle" align="left">[<a href="make_6.html#SEC74" title="Next chapter"> >> </a>]</td>
128
<td valign="middle" align="left"> </td>
129
<td valign="middle" align="left"> </td>
130
<td valign="middle" align="left"> </td>
131
<td valign="middle" align="left"> </td>
132
<td valign="middle" align="left">[<a href="make.html#SEC_Top" title="Cover (top) of document">Top</a>]</td>
133
<td valign="middle" align="left">[<a href="make_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
134
<td valign="middle" align="left">[<a href="make_19.html#SEC148" title="Index">Index</a>]</td>
135
<td valign="middle" align="left">[<a href="make_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
137
<h2 class="section"> 5.1 Command Syntax </h2>
139
<p>Makefiles have the unusual property that there are really two distinct
140
syntaxes in one file. Most of the makefile uses <code>make</code> syntax
141
(see section <a href="make_3.html#SEC13">Writing Makefiles</a>). However, commands are meant to be
142
interpreted by the shell and so they are written using shell syntax.
143
The <code>make</code> program does not try to understand shell syntax: it
144
performs only a very few specific translations on the content of the
145
command before handing it to the shell.
147
<p>Each command line must start with a tab, except that the first command
148
line may be attached to the target-and-prerequisites line with a
149
semicolon in between. <em>Any</em> line in the makefile that begins
150
with a tab and appears in a “rule context” (that is, after a rule
151
has been started until another rule or variable definition) will be
152
considered a command line for that rule. Blank lines and lines of
153
just comments may appear among the command lines; they are ignored.
155
<p>Some consequences of these rules include:
159
A blank line that begins with a tab is not blank: it's an empty
160
command (see section <a href="#SEC73">Using Empty Commands</a>).
162
<a name="IDX139"></a>
163
<a name="IDX140"></a>
164
<a name="IDX141"></a>
166
A comment in a command line is not a <code>make</code> comment; it will be
167
passed to the shell as-is. Whether the shell treats it as a comment
168
or not depends on your shell.
171
A variable definition in a “rule context” which is indented by a tab
172
as the first character on the line, will be considered a command line,
173
not a <code>make</code> variable definition, and passed to the shell.
176
A conditional expression (<code>ifdef</code>, <code>ifeq</code>,
177
etc. see section <a href="make_7.html#SEC90">Syntax of Conditionals</a>) in a “rule
178
context” which is indented by a tab as the first character on the
179
line, will be considered a command line and be passed to the shell.
183
<table class="menu" border="0" cellspacing="0">
184
<tr><td align="left" valign="top"><a href="#SEC58">5.1.1 Splitting Command Lines</a></td><td> </td><td align="left" valign="top"> Breaking long command lines for readability.
186
<tr><td align="left" valign="top"><a href="#SEC59">5.1.2 Using Variables in Commands</a></td><td> </td><td align="left" valign="top"> Using <code>make</code> variables in commands.
191
<a name="Splitting-Lines"></a>
193
<table cellpadding="1" cellspacing="1" border="0">
194
<tr><td valign="middle" align="left">[<a href="#SEC57" title="Previous section in reading order"> < </a>]</td>
195
<td valign="middle" align="left">[<a href="#SEC59" title="Next section in reading order"> > </a>]</td>
196
<td valign="middle" align="left"> </td>
197
<td valign="middle" align="left">[<a href="#SEC56" title="Beginning of this chapter or previous chapter"> << </a>]</td>
198
<td valign="middle" align="left">[<a href="#SEC57" title="Up section"> Up </a>]</td>
199
<td valign="middle" align="left">[<a href="make_6.html#SEC74" title="Next chapter"> >> </a>]</td>
200
<td valign="middle" align="left"> </td>
201
<td valign="middle" align="left"> </td>
202
<td valign="middle" align="left"> </td>
203
<td valign="middle" align="left"> </td>
204
<td valign="middle" align="left">[<a href="make.html#SEC_Top" title="Cover (top) of document">Top</a>]</td>
205
<td valign="middle" align="left">[<a href="make_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
206
<td valign="middle" align="left">[<a href="make_19.html#SEC148" title="Index">Index</a>]</td>
207
<td valign="middle" align="left">[<a href="make_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
209
<h3 class="subsection"> 5.1.1 Splitting Command Lines </h3>
211
<p>One of the few ways in which <code>make</code> does interpret command lines
212
is checking for a backslash just before the newline. As in normal
213
makefile syntax, a single command can be split into multiple lines in
214
the makefile by placing a backslash before each newline. A sequence
215
of lines like this is considered a single command, and one instance of
216
the shell will be invoked to run it.
218
<p>However, in contrast to how they are treated in other places in a
219
makefile, backslash-newline pairs are <em>not</em> removed from the
220
command. Both the backslash and the newline characters are preserved
221
and passed to the shell. How the backslash-newline is interpreted
222
depends on your shell. If the first character of the next line
223
after the backslash-newline is a tab, then that tab (and only that
224
tab) is removed. Whitespace is never added to the command.
226
<p>For example, this makefile:
228
<table><tr><td> </td><td><pre class="example">all :
237
</pre></td></tr></table>
239
<p>consists of four separate shell commands where the output is:
241
<table><tr><td> </td><td><pre class="example">nospace
245
</pre></td></tr></table>
247
<p>As a more complex example, this makefile:
249
<table><tr><td> </td><td><pre class="example">all : ; @echo 'hello \
250
world' ; echo "hello \
252
</pre></td></tr></table>
254
<p>will run one shell with a command script of:
256
<table><tr><td> </td><td><pre class="example">echo 'hello \
257
world' ; echo "hello \
259
</pre></td></tr></table>
261
<p>which, according to shell quoting rules, will yield the following output:
263
<table><tr><td> </td><td><pre class="example">hello \
266
</pre></td></tr></table>
268
<p>Notice how the backslash/newline pair was removed inside the string quoted
269
with double quotes (<code>"..."</code>), but not from the string quoted with single
270
quotes (<code>'...'</code>). This is the way the default shell (‘<tt>/bin/sh</tt>’)
271
handles backslash/newline pairs. If you specify a different shell in your
272
makefiles it may treat them differently.
274
<p>Sometimes you want to split a long line inside of single quotes, but
275
you don't want the backslash-newline to appear in the quoted content.
276
This is often the case when passing scripts to languages such as Perl,
277
where extraneous backslashes inside the script can change its meaning
278
or even be a syntax error. One simple way of handling this is to
279
place the quoted string, or even the entire command, into a
280
<code>make</code> variable then use the variable in the command. In this
281
situation the newline quoting rules for makefiles will be used, and
282
the backslash-newline will be removed. If we rewrite our example
283
above using this method:
285
<table><tr><td> </td><td><pre class="example">HELLO = 'hello \
288
all : ; @echo $(HELLO)
289
</pre></td></tr></table>
291
<p>we will get output like this:
293
<table><tr><td> </td><td><pre class="example">hello world
294
</pre></td></tr></table>
296
<p>If you like, you can also use target-specific variables
297
(see section <a href="make_6.html#SEC86">Target-specific Variable Values</a>) to obtain
298
a tighter correspondence between the variable and the command that
302
<a name="Variables-in-Commands"></a>
304
<table cellpadding="1" cellspacing="1" border="0">
305
<tr><td valign="middle" align="left">[<a href="#SEC58" title="Previous section in reading order"> < </a>]</td>
306
<td valign="middle" align="left">[<a href="#SEC60" title="Next section in reading order"> > </a>]</td>
307
<td valign="middle" align="left"> </td>
308
<td valign="middle" align="left">[<a href="#SEC56" title="Beginning of this chapter or previous chapter"> << </a>]</td>
309
<td valign="middle" align="left">[<a href="#SEC57" title="Up section"> Up </a>]</td>
310
<td valign="middle" align="left">[<a href="make_6.html#SEC74" title="Next chapter"> >> </a>]</td>
311
<td valign="middle" align="left"> </td>
312
<td valign="middle" align="left"> </td>
313
<td valign="middle" align="left"> </td>
314
<td valign="middle" align="left"> </td>
315
<td valign="middle" align="left">[<a href="make.html#SEC_Top" title="Cover (top) of document">Top</a>]</td>
316
<td valign="middle" align="left">[<a href="make_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
317
<td valign="middle" align="left">[<a href="make_19.html#SEC148" title="Index">Index</a>]</td>
318
<td valign="middle" align="left">[<a href="make_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
320
<h3 class="subsection"> 5.1.2 Using Variables in Commands </h3>
322
<p>The other way in which <code>make</code> processes commands is by expanding
323
any variable references in them (see section <a href="make_6.html#SEC75">Basics of Variable References</a>). This occurs after make has finished reading all the
324
makefiles and the target is determined to be out of date; so, the
325
commands for targets which are not rebuilt are never expanded.
327
<p>Variable and function references in commands have identical syntax and
328
semantics to references elsewhere in the makefile. They also have the
329
same quoting rules: if you want a dollar sign to appear in your
330
command, you must double it (‘<samp>$$</samp>’). For shells like the default
331
shell, that use dollar signs to introduce variables, it's important to
332
keep clear in your mind whether the variable you want to reference is
333
a <code>make</code> variable (use a single dollar sign) or a shell variable
334
(use two dollar signs). For example:
336
<table><tr><td> </td><td><pre class="example">LIST = one two three
338
for i in $(LIST); do \
341
</pre></td></tr></table>
343
<p>results in the following command being passed to the shell:
345
<table><tr><td> </td><td><pre class="example">for i in one two three; do \
348
</pre></td></tr></table>
350
<p>which generates the expected result:
352
<table><tr><td> </td><td><pre class="example">one
355
</pre></td></tr></table>
358
<a name="Echoing"></a>
360
<table cellpadding="1" cellspacing="1" border="0">
361
<tr><td valign="middle" align="left">[<a href="#SEC59" title="Previous section in reading order"> < </a>]</td>
362
<td valign="middle" align="left">[<a href="#SEC61" title="Next section in reading order"> > </a>]</td>
363
<td valign="middle" align="left"> </td>
364
<td valign="middle" align="left">[<a href="#SEC56" title="Beginning of this chapter or previous chapter"> << </a>]</td>
365
<td valign="middle" align="left">[<a href="#SEC56" title="Up section"> Up </a>]</td>
366
<td valign="middle" align="left">[<a href="make_6.html#SEC74" title="Next chapter"> >> </a>]</td>
367
<td valign="middle" align="left"> </td>
368
<td valign="middle" align="left"> </td>
369
<td valign="middle" align="left"> </td>
370
<td valign="middle" align="left"> </td>
371
<td valign="middle" align="left">[<a href="make.html#SEC_Top" title="Cover (top) of document">Top</a>]</td>
372
<td valign="middle" align="left">[<a href="make_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
373
<td valign="middle" align="left">[<a href="make_19.html#SEC148" title="Index">Index</a>]</td>
374
<td valign="middle" align="left">[<a href="make_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
376
<h2 class="section"> 5.2 Command Echoing </h2>
378
<p>Normally <code>make</code> prints each command line before it is executed.
379
We call this <em>echoing</em> because it gives the appearance that you
380
are typing the commands yourself.
382
<p>When a line starts with ‘<samp>@</samp>’, the echoing of that line is suppressed.
383
The ‘<samp>@</samp>’ is discarded before the command is passed to the shell.
384
Typically you would use this for a command whose only effect is to print
385
something, such as an <code>echo</code> command to indicate progress through
388
<table><tr><td> </td><td><pre class="example">@echo About to make distribution files
389
</pre></td></tr></table>
391
<a name="IDX142"></a>
392
<a name="IDX143"></a>
393
<a name="IDX144"></a>
394
<a name="IDX145"></a>
395
<p>When <code>make</code> is given the flag ‘<samp>-n</samp>’ or ‘<samp>--just-print</samp>’
396
it only echoes commands, it won't execute them. See section <a href="make_9.html#SEC112">Summary of Options</a>. In this case and only this case, even the
397
commands starting with ‘<samp>@</samp>’ are printed. This flag is useful for
398
finding out which commands <code>make</code> thinks are necessary without
401
<a name="IDX146"></a>
402
<a name="IDX147"></a>
403
<a name="IDX148"></a>
404
<a name="IDX149"></a>
405
<p>The ‘<samp>-s</samp>’ or ‘<samp>--silent</samp>’
406
flag to <code>make</code> prevents all echoing, as if all commands
407
started with ‘<samp>@</samp>’. A rule in the makefile for the special target
408
<code>.SILENT</code> without prerequisites has the same effect
409
(see section <a href="make_4.html#SEC48">Special Built-in Target Names</a>).
410
<code>.SILENT</code> is essentially obsolete since ‘<samp>@</samp>’ is more flexible.
413
<a name="Execution"></a>
415
<table cellpadding="1" cellspacing="1" border="0">
416
<tr><td valign="middle" align="left">[<a href="#SEC60" title="Previous section in reading order"> < </a>]</td>
417
<td valign="middle" align="left">[<a href="#SEC62" title="Next section in reading order"> > </a>]</td>
418
<td valign="middle" align="left"> </td>
419
<td valign="middle" align="left">[<a href="#SEC56" title="Beginning of this chapter or previous chapter"> << </a>]</td>
420
<td valign="middle" align="left">[<a href="#SEC56" title="Up section"> Up </a>]</td>
421
<td valign="middle" align="left">[<a href="make_6.html#SEC74" title="Next chapter"> >> </a>]</td>
422
<td valign="middle" align="left"> </td>
423
<td valign="middle" align="left"> </td>
424
<td valign="middle" align="left"> </td>
425
<td valign="middle" align="left"> </td>
426
<td valign="middle" align="left">[<a href="make.html#SEC_Top" title="Cover (top) of document">Top</a>]</td>
427
<td valign="middle" align="left">[<a href="make_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
428
<td valign="middle" align="left">[<a href="make_19.html#SEC148" title="Index">Index</a>]</td>
429
<td valign="middle" align="left">[<a href="make_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
431
<h2 class="section"> 5.3 Command Execution </h2>
433
<p>When it is time to execute commands to update a target, they are
434
executed by invoking a new subshell for each command line. (In
435
practice, <code>make</code> may take shortcuts that do not affect the
438
<a name="IDX150"></a>
439
<a name="IDX151"></a>
440
<a name="IDX152"></a>
441
<p><strong>Please note:</strong> this implies that setting shell variables and
442
invoking shell commands such as <code>cd</code> that set a context local to
443
each process will not affect the following command lines.<a name="DOCF2" href="make_fot.html#FOOT2">(2)</a> If you want to use <code>cd</code> to affect the next statement,
444
put both statements in a single command line. Then <code>make</code> will
445
invoke one shell to run the entire line, and the shell will execute
446
the statements in sequence. For example:
448
<table><tr><td> </td><td><pre class="example">foo : bar/lose
449
cd $(@D) && gobble $(@F) > ../$@
450
</pre></td></tr></table>
452
<p>Here we use the shell AND operator (<code>&&</code>) so that if the
453
<code>cd</code> command fails, the script will fail without trying to invoke
454
the <code>gobble</code> command in the wrong directory, which could cause
455
problems (in this case it would certainly cause ‘<tt>../foo</tt>’ to be
456
truncated, at least).
458
<table class="menu" border="0" cellspacing="0">
459
<tr><td align="left" valign="top"><a href="#SEC62">5.3.1 Choosing the Shell</a></td><td> </td><td align="left" valign="top"> How <code>make</code> chooses the shell used
465
<a name="Choosing-the-Shell"></a>
467
<table cellpadding="1" cellspacing="1" border="0">
468
<tr><td valign="middle" align="left">[<a href="#SEC61" title="Previous section in reading order"> < </a>]</td>
469
<td valign="middle" align="left">[<a href="#SEC64" title="Next section in reading order"> > </a>]</td>
470
<td valign="middle" align="left"> </td>
471
<td valign="middle" align="left">[<a href="#SEC56" title="Beginning of this chapter or previous chapter"> << </a>]</td>
472
<td valign="middle" align="left">[<a href="#SEC61" title="Up section"> Up </a>]</td>
473
<td valign="middle" align="left">[<a href="make_6.html#SEC74" title="Next chapter"> >> </a>]</td>
474
<td valign="middle" align="left"> </td>
475
<td valign="middle" align="left"> </td>
476
<td valign="middle" align="left"> </td>
477
<td valign="middle" align="left"> </td>
478
<td valign="middle" align="left">[<a href="make.html#SEC_Top" title="Cover (top) of document">Top</a>]</td>
479
<td valign="middle" align="left">[<a href="make_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
480
<td valign="middle" align="left">[<a href="make_19.html#SEC148" title="Index">Index</a>]</td>
481
<td valign="middle" align="left">[<a href="make_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
483
<h3 class="subsection"> 5.3.1 Choosing the Shell </h3>
485
<p>The program used as the shell is taken from the variable <code>SHELL</code>.
486
If this variable is not set in your makefile, the program
487
‘<tt>/bin/sh</tt>’ is used as the shell.
489
<a name="IDX153"></a>
490
<p>Unlike most variables, the variable <code>SHELL</code> is never set from the
491
environment. This is because the <code>SHELL</code> environment variable is
492
used to specify your personal choice of shell program for interactive
493
use. It would be very bad for personal choices like this to affect the
494
functioning of makefiles. See section <a href="make_6.html#SEC85">Variables from the Environment</a>.
496
<p>Furthermore, when you do set <code>SHELL</code> in your makefile that value
497
is <em>not</em> exported in the environment to commands that <code>make</code>
498
invokes. Instead, the value inherited from the user's environment, if
499
any, is exported. You can override this behavior by explicitly
500
exporting <code>SHELL</code> (see section <a href="#SEC69">Communicating Variables to a Sub-<code>make</code></a>), forcing it to be passed in the
501
environment to commands.
503
<a name="IDX154"></a>
504
<p>However, on MS-DOS and MS-Windows the value of <code>SHELL</code> in the
505
environment <strong>is</strong> used, since on those systems most users do not
506
set this variable, and therefore it is most likely set specifically to
507
be used by <code>make</code>. On MS-DOS, if the setting of <code>SHELL</code> is
508
not suitable for <code>make</code>, you can set the variable
509
<code>MAKESHELL</code> to the shell that <code>make</code> should use; if set it
510
will be used as the shell instead of the value of <code>SHELL</code>.
513
<h4 class="subsubheading"> Choosing a Shell in DOS and Windows </h4>
515
<p>Choosing a shell in MS-DOS and MS-Windows is much more complex than on
518
<a name="IDX155"></a>
519
<p>On MS-DOS, if <code>SHELL</code> is not set, the value of the variable
520
<code>COMSPEC</code> (which is always set) is used instead.
522
<a name="IDX156"></a>
523
<p>The processing of lines that set the variable <code>SHELL</code> in Makefiles
524
is different on MS-DOS. The stock shell, ‘<tt>command.com</tt>’, is
525
ridiculously limited in its functionality and many users of <code>make</code>
526
tend to install a replacement shell. Therefore, on MS-DOS, <code>make</code>
527
examines the value of <code>SHELL</code>, and changes its behavior based on
528
whether it points to a Unix-style or DOS-style shell. This allows
529
reasonable functionality even if <code>SHELL</code> points to
530
‘<tt>command.com</tt>’.
532
<p>If <code>SHELL</code> points to a Unix-style shell, <code>make</code> on MS-DOS
533
additionally checks whether that shell can indeed be found; if not, it
534
ignores the line that sets <code>SHELL</code>. In MS-DOS, GNU <code>make</code>
535
searches for the shell in the following places:
539
In the precise place pointed to by the value of <code>SHELL</code>. For
540
example, if the makefile specifies ‘<samp>SHELL = /bin/sh</samp>’, <code>make</code>
541
will look in the directory ‘<tt>/bin</tt>’ on the current drive.
544
In the current directory.
547
In each of the directories in the <code>PATH</code> variable, in order.
551
<p>In every directory it examines, <code>make</code> will first look for the
552
specific file (‘<tt>sh</tt>’ in the example above). If this is not found,
553
it will also look in that directory for that file with one of the known
554
extensions which identify executable files. For example ‘<tt>.exe</tt>’,
555
‘<tt>.com</tt>’, ‘<tt>.bat</tt>’, ‘<tt>.btm</tt>’, ‘<tt>.sh</tt>’, and some others.
557
<p>If any of these attempts is successful, the value of <code>SHELL</code> will
558
be set to the full pathname of the shell as found. However, if none of
559
these is found, the value of <code>SHELL</code> will not be changed, and thus
560
the line that sets it will be effectively ignored. This is so
561
<code>make</code> will only support features specific to a Unix-style shell if
562
such a shell is actually installed on the system where <code>make</code> runs.
564
<p>Note that this extended search for the shell is limited to the cases
565
where <code>SHELL</code> is set from the Makefile; if it is set in the
566
environment or command line, you are expected to set it to the full
567
pathname of the shell, exactly as things are on Unix.
569
<p>The effect of the above DOS-specific processing is that a Makefile that
570
contains ‘<samp>SHELL = /bin/sh</samp>’ (as many Unix makefiles do), will work
571
on MS-DOS unaltered if you have e.g. ‘<tt>sh.exe</tt>’ installed in some
572
directory along your <code>PATH</code>.
575
<a name="Parallel"></a>
577
<table cellpadding="1" cellspacing="1" border="0">
578
<tr><td valign="middle" align="left">[<a href="#SEC62" title="Previous section in reading order"> < </a>]</td>
579
<td valign="middle" align="left">[<a href="#SEC65" title="Next section in reading order"> > </a>]</td>
580
<td valign="middle" align="left"> </td>
581
<td valign="middle" align="left">[<a href="#SEC56" title="Beginning of this chapter or previous chapter"> << </a>]</td>
582
<td valign="middle" align="left">[<a href="#SEC56" title="Up section"> Up </a>]</td>
583
<td valign="middle" align="left">[<a href="make_6.html#SEC74" title="Next chapter"> >> </a>]</td>
584
<td valign="middle" align="left"> </td>
585
<td valign="middle" align="left"> </td>
586
<td valign="middle" align="left"> </td>
587
<td valign="middle" align="left"> </td>
588
<td valign="middle" align="left">[<a href="make.html#SEC_Top" title="Cover (top) of document">Top</a>]</td>
589
<td valign="middle" align="left">[<a href="make_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
590
<td valign="middle" align="left">[<a href="make_19.html#SEC148" title="Index">Index</a>]</td>
591
<td valign="middle" align="left">[<a href="make_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
593
<h2 class="section"> 5.4 Parallel Execution </h2>
595
<p>GNU <code>make</code> knows how to execute several commands at once.
596
Normally, <code>make</code> will execute only one command at a time, waiting
597
for it to finish before executing the next. However, the ‘<samp>-j</samp>’ or
598
‘<samp>--jobs</samp>’ option tells <code>make</code> to execute many commands
601
<p>On MS-DOS, the ‘<samp>-j</samp>’ option has no effect, since that system doesn't
602
support multi-processing.
604
<p>If the ‘<samp>-j</samp>’ option is followed by an integer, this is the number of
605
commands to execute at once; this is called the number of <em>job slots</em>.
606
If there is nothing looking like an integer after the ‘<samp>-j</samp>’ option,
607
there is no limit on the number of job slots. The default number of job
608
slots is one, which means serial execution (one thing at a time).
610
<p>One unpleasant consequence of running several commands simultaneously is
611
that output generated by the commands appears whenever each command
612
sends it, so messages from different commands may be interspersed.
614
<p>Another problem is that two processes cannot both take input from the
615
same device; so to make sure that only one command tries to take input
616
from the terminal at once, <code>make</code> will invalidate the standard
617
input streams of all but one running command. This means that
618
attempting to read from standard input will usually be a fatal error (a
619
‘<samp>Broken pipe</samp>’ signal) for most child processes if there are
621
<a name="IDX157"></a>
622
<a name="IDX158"></a>
624
<p>It is unpredictable which command will have a valid standard input stream
625
(which will come from the terminal, or wherever you redirect the standard
626
input of <code>make</code>). The first command run will always get it first, and
627
the first command started after that one finishes will get it next, and so
630
<p>We will change how this aspect of <code>make</code> works if we find a better
631
alternative. In the mean time, you should not rely on any command using
632
standard input at all if you are using the parallel execution feature; but
633
if you are not using this feature, then standard input works normally in
636
<p>Finally, handling recursive <code>make</code> invocations raises issues. For
637
more information on this, see
638
<a href="#SEC70">Communicating Options to a Sub-<code>make</code></a>.
640
<p>If a command fails (is killed by a signal or exits with a nonzero
641
status), and errors are not ignored for that command
642
(see section <a href="#SEC65">Errors in Commands</a>),
643
the remaining command lines to remake the same target will not be run.
644
If a command fails and the ‘<samp>-k</samp>’ or ‘<samp>--keep-going</samp>’
646
(see section <a href="make_9.html#SEC112">Summary of Options</a>),
647
<code>make</code> aborts execution. If make
648
terminates for any reason (including a signal) with child processes
649
running, it waits for them to finish before actually exiting.
651
<a name="IDX159"></a>
652
<a name="IDX160"></a>
653
<a name="IDX161"></a>
654
<a name="IDX162"></a>
655
<a name="IDX163"></a>
656
<a name="IDX164"></a>
657
<p>When the system is heavily loaded, you will probably want to run fewer jobs
658
than when it is lightly loaded. You can use the ‘<samp>-l</samp>’ option to tell
659
<code>make</code> to limit the number of jobs to run at once, based on the load
660
average. The ‘<samp>-l</samp>’ or ‘<samp>--max-load</samp>’
661
option is followed by a floating-point number. For
664
<table><tr><td> </td><td><pre class="example">-l 2.5
665
</pre></td></tr></table>
667
<p>will not let <code>make</code> start more than one job if the load average is
668
above 2.5. The ‘<samp>-l</samp>’ option with no following number removes the
669
load limit, if one was given with a previous ‘<samp>-l</samp>’ option.
671
<p>More precisely, when <code>make</code> goes to start up a job, and it already has
672
at least one job running, it checks the current load average; if it is not
673
lower than the limit given with ‘<samp>-l</samp>’, <code>make</code> waits until the load
674
average goes below that limit, or until all the other jobs finish.
676
<p>By default, there is no load limit.
679
<a name="Errors"></a>
681
<table cellpadding="1" cellspacing="1" border="0">
682
<tr><td valign="middle" align="left">[<a href="#SEC64" title="Previous section in reading order"> < </a>]</td>
683
<td valign="middle" align="left">[<a href="#SEC66" title="Next section in reading order"> > </a>]</td>
684
<td valign="middle" align="left"> </td>
685
<td valign="middle" align="left">[<a href="#SEC56" title="Beginning of this chapter or previous chapter"> << </a>]</td>
686
<td valign="middle" align="left">[<a href="#SEC56" title="Up section"> Up </a>]</td>
687
<td valign="middle" align="left">[<a href="make_6.html#SEC74" title="Next chapter"> >> </a>]</td>
688
<td valign="middle" align="left"> </td>
689
<td valign="middle" align="left"> </td>
690
<td valign="middle" align="left"> </td>
691
<td valign="middle" align="left"> </td>
692
<td valign="middle" align="left">[<a href="make.html#SEC_Top" title="Cover (top) of document">Top</a>]</td>
693
<td valign="middle" align="left">[<a href="make_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
694
<td valign="middle" align="left">[<a href="make_19.html#SEC148" title="Index">Index</a>]</td>
695
<td valign="middle" align="left">[<a href="make_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
697
<h2 class="section"> 5.5 Errors in Commands </h2>
699
<p>After each shell command returns, <code>make</code> looks at its exit status.
700
If the command completed successfully, the next command line is executed
701
in a new shell; after the last command line is finished, the rule is
704
<p>If there is an error (the exit status is nonzero), <code>make</code> gives up on
705
the current rule, and perhaps on all rules.
707
<p>Sometimes the failure of a certain command does not indicate a problem.
708
For example, you may use the <code>mkdir</code> command to ensure that a
709
directory exists. If the directory already exists, <code>mkdir</code> will
710
report an error, but you probably want <code>make</code> to continue regardless.
712
<a name="IDX165"></a>
713
<p>To ignore errors in a command line, write a ‘<samp>-</samp>’ at the beginning of
714
the line's text (after the initial tab). The ‘<samp>-</samp>’ is discarded before
715
the command is passed to the shell for execution.
719
<table><tr><td> </td><td><pre class="example">clean:
721
</pre></td></tr></table>
722
<a name="IDX166"></a>
724
<p>This causes <code>rm</code> to continue even if it is unable to remove a file.
726
<a name="IDX167"></a>
727
<a name="IDX168"></a>
728
<a name="IDX169"></a>
729
<p>When you run <code>make</code> with the ‘<samp>-i</samp>’ or ‘<samp>--ignore-errors</samp>’
730
flag, errors are ignored in all commands of all rules. A rule in the
731
makefile for the special target <code>.IGNORE</code> has the same effect, if
732
there are no prerequisites. These ways of ignoring errors are obsolete
733
because ‘<samp>-</samp>’ is more flexible.
735
<p>When errors are to be ignored, because of either a ‘<samp>-</samp>’ or the
736
‘<samp>-i</samp>’ flag, <code>make</code> treats an error return just like success,
737
except that it prints out a message that tells you the status code
738
the command exited with, and says that the error has been ignored.
740
<p>When an error happens that <code>make</code> has not been told to ignore,
741
it implies that the current target cannot be correctly remade, and neither
742
can any other that depends on it either directly or indirectly. No further
743
commands will be executed for these targets, since their preconditions
744
have not been achieved.
747
<a name="IDX170"></a>
748
<a name="IDX171"></a>
749
<p>Normally <code>make</code> gives up immediately in this circumstance, returning a
750
nonzero status. However, if the ‘<samp>-k</samp>’ or ‘<samp>--keep-going</samp>’
751
flag is specified, <code>make</code>
752
continues to consider the other prerequisites of the pending targets,
753
remaking them if necessary, before it gives up and returns nonzero status.
754
For example, after an error in compiling one object file, ‘<samp>make -k</samp>’
755
will continue compiling other object files even though it already knows
756
that linking them will be impossible. See section <a href="make_9.html#SEC112">Summary of Options</a>.
758
<p>The usual behavior assumes that your purpose is to get the specified
759
targets up to date; once <code>make</code> learns that this is impossible, it
760
might as well report the failure immediately. The ‘<samp>-k</samp>’ option says
761
that the real purpose is to test as many of the changes made in the
762
program as possible, perhaps to find several independent problems so
763
that you can correct them all before the next attempt to compile. This
764
is why Emacs' <code>compile</code> command passes the ‘<samp>-k</samp>’ flag by
766
<a name="IDX172"></a>
768
<a name="IDX173"></a>
769
<a name="IDX174"></a>
770
<a name="IDX175"></a>
771
<a name="IDX176"></a>
772
<p>Usually when a command fails, if it has changed the target file at all,
773
the file is corrupted and cannot be used—or at least it is not
774
completely updated. Yet the file's time stamp says that it is now up to
775
date, so the next time <code>make</code> runs, it will not try to update that
776
file. The situation is just the same as when the command is killed by a
777
signal; see section <a href="#SEC66">Interrupting or Killing <code>make</code></a>. So generally the right thing to do is to
778
delete the target file if the command fails after beginning to change
779
the file. <code>make</code> will do this if <code>.DELETE_ON_ERROR</code> appears
780
as a target. This is almost always what you want <code>make</code> to do, but
781
it is not historical practice; so for compatibility, you must explicitly
785
<a name="Interrupts"></a>
787
<table cellpadding="1" cellspacing="1" border="0">
788
<tr><td valign="middle" align="left">[<a href="#SEC65" title="Previous section in reading order"> < </a>]</td>
789
<td valign="middle" align="left">[<a href="#SEC67" title="Next section in reading order"> > </a>]</td>
790
<td valign="middle" align="left"> </td>
791
<td valign="middle" align="left">[<a href="#SEC56" title="Beginning of this chapter or previous chapter"> << </a>]</td>
792
<td valign="middle" align="left">[<a href="#SEC56" title="Up section"> Up </a>]</td>
793
<td valign="middle" align="left">[<a href="make_6.html#SEC74" title="Next chapter"> >> </a>]</td>
794
<td valign="middle" align="left"> </td>
795
<td valign="middle" align="left"> </td>
796
<td valign="middle" align="left"> </td>
797
<td valign="middle" align="left"> </td>
798
<td valign="middle" align="left">[<a href="make.html#SEC_Top" title="Cover (top) of document">Top</a>]</td>
799
<td valign="middle" align="left">[<a href="make_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
800
<td valign="middle" align="left">[<a href="make_19.html#SEC148" title="Index">Index</a>]</td>
801
<td valign="middle" align="left">[<a href="make_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
803
<h2 class="section"> 5.6 Interrupting or Killing <code>make</code> </h2>
805
<p>If <code>make</code> gets a fatal signal while a command is executing, it may
806
delete the target file that the command was supposed to update. This is
807
done if the target file's last-modification time has changed since
808
<code>make</code> first checked it.
810
<p>The purpose of deleting the target is to make sure that it is remade from
811
scratch when <code>make</code> is next run. Why is this? Suppose you type
812
<kbd>Ctrl-c</kbd> while a compiler is running, and it has begun to write an
813
object file ‘<tt>foo.o</tt>’. The <kbd>Ctrl-c</kbd> kills the compiler, resulting
814
in an incomplete file whose last-modification time is newer than the source
815
file ‘<tt>foo.c</tt>’. But <code>make</code> also receives the <kbd>Ctrl-c</kbd> signal
816
and deletes this incomplete file. If <code>make</code> did not do this, the next
817
invocation of <code>make</code> would think that ‘<tt>foo.o</tt>’ did not require
818
updating—resulting in a strange error message from the linker when it
819
tries to link an object file half of which is missing.
821
<a name="IDX177"></a>
822
<p>You can prevent the deletion of a target file in this way by making the
823
special target <code>.PRECIOUS</code> depend on it. Before remaking a target,
824
<code>make</code> checks to see whether it appears on the prerequisites of
825
<code>.PRECIOUS</code>, and thereby decides whether the target should be deleted
826
if a signal happens. Some reasons why you might do this are that the
827
target is updated in some atomic fashion, or exists only to record a
828
modification-time (its contents do not matter), or must exist at all
829
times to prevent other sorts of trouble.
832
<a name="Recursion"></a>
834
<table cellpadding="1" cellspacing="1" border="0">
835
<tr><td valign="middle" align="left">[<a href="#SEC66" title="Previous section in reading order"> < </a>]</td>
836
<td valign="middle" align="left">[<a href="#SEC68" title="Next section in reading order"> > </a>]</td>
837
<td valign="middle" align="left"> </td>
838
<td valign="middle" align="left">[<a href="#SEC56" title="Beginning of this chapter or previous chapter"> << </a>]</td>
839
<td valign="middle" align="left">[<a href="#SEC56" title="Up section"> Up </a>]</td>
840
<td valign="middle" align="left">[<a href="make_6.html#SEC74" title="Next chapter"> >> </a>]</td>
841
<td valign="middle" align="left"> </td>
842
<td valign="middle" align="left"> </td>
843
<td valign="middle" align="left"> </td>
844
<td valign="middle" align="left"> </td>
845
<td valign="middle" align="left">[<a href="make.html#SEC_Top" title="Cover (top) of document">Top</a>]</td>
846
<td valign="middle" align="left">[<a href="make_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
847
<td valign="middle" align="left">[<a href="make_19.html#SEC148" title="Index">Index</a>]</td>
848
<td valign="middle" align="left">[<a href="make_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
850
<h2 class="section"> 5.7 Recursive Use of <code>make</code> </h2>
852
<p>Recursive use of <code>make</code> means using <code>make</code> as a command in a
853
makefile. This technique is useful when you want separate makefiles for
854
various subsystems that compose a larger system. For example, suppose you
855
have a subdirectory ‘<tt>subdir</tt>’ which has its own makefile, and you would
856
like the containing directory's makefile to run <code>make</code> on the
857
subdirectory. You can do it by writing this:
859
<table><tr><td> </td><td><pre class="example">subsystem:
860
cd subdir && $(MAKE)
861
</pre></td></tr></table>
863
<p>or, equivalently, this (see section <a href="make_9.html#SEC112">Summary of Options</a>):
865
<table><tr><td> </td><td><pre class="example">subsystem:
867
</pre></td></tr></table>
868
<a name="IDX178"></a>
869
<a name="IDX179"></a>
871
<p>You can write recursive <code>make</code> commands just by copying this example,
872
but there are many things to know about how they work and why, and about
873
how the sub-<code>make</code> relates to the top-level <code>make</code>. You may
874
also find it useful to declare targets that invoke recursive
875
<code>make</code> commands as ‘<samp>.PHONY</samp>’ (for more discussion on when
876
this is useful, see <a href="make_4.html#SEC45">Phony Targets</a>).
878
<a name="IDX180"></a>
879
<p>For your convenience, when GNU <code>make</code> starts (after it has
880
processed any <code>-C</code> options) it sets the variable <code>CURDIR</code> to
881
the pathname of the current working directory. This value is never
882
touched by <code>make</code> again: in particular note that if you include
883
files from other directories the value of <code>CURDIR</code> does not
884
change. The value has the same precedence it would have if it were
885
set in the makefile (by default, an environment variable <code>CURDIR</code>
886
will not override this value). Note that setting this variable has no
887
impact on the operation of <code>make</code> (it does not cause <code>make</code>
888
to change its working directory, for example).
890
<table class="menu" border="0" cellspacing="0">
891
<tr><td align="left" valign="top"><a href="#SEC68">5.7.1 How the <code>MAKE</code> Variable Works</a></td><td> </td><td align="left" valign="top"> The special effects of using ‘<samp>$(MAKE)</samp>’.
893
<tr><td align="left" valign="top"><a href="#SEC69">5.7.2 Communicating Variables to a Sub-<code>make</code></a></td><td> </td><td align="left" valign="top"> How to communicate variables to a sub-<code>make</code>.
895
<tr><td align="left" valign="top"><a href="#SEC70">5.7.3 Communicating Options to a Sub-<code>make</code></a></td><td> </td><td align="left" valign="top"> How to communicate options to a sub-<code>make</code>.
897
<tr><td align="left" valign="top"><a href="#SEC71">5.7.4 The ‘<samp>--print-directory</samp>’ Option</a></td><td> </td><td align="left" valign="top"> How the ‘<samp>-w</samp>’ or ‘<samp>--print-directory</samp>’ option
898
helps debug use of recursive <code>make</code> commands.
903
<a name="MAKE-Variable"></a>
905
<table cellpadding="1" cellspacing="1" border="0">
906
<tr><td valign="middle" align="left">[<a href="#SEC67" title="Previous section in reading order"> < </a>]</td>
907
<td valign="middle" align="left">[<a href="#SEC69" title="Next section in reading order"> > </a>]</td>
908
<td valign="middle" align="left"> </td>
909
<td valign="middle" align="left">[<a href="#SEC56" title="Beginning of this chapter or previous chapter"> << </a>]</td>
910
<td valign="middle" align="left">[<a href="#SEC67" title="Up section"> Up </a>]</td>
911
<td valign="middle" align="left">[<a href="make_6.html#SEC74" title="Next chapter"> >> </a>]</td>
912
<td valign="middle" align="left"> </td>
913
<td valign="middle" align="left"> </td>
914
<td valign="middle" align="left"> </td>
915
<td valign="middle" align="left"> </td>
916
<td valign="middle" align="left">[<a href="make.html#SEC_Top" title="Cover (top) of document">Top</a>]</td>
917
<td valign="middle" align="left">[<a href="make_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
918
<td valign="middle" align="left">[<a href="make_19.html#SEC148" title="Index">Index</a>]</td>
919
<td valign="middle" align="left">[<a href="make_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
921
<h3 class="subsection"> 5.7.1 How the <code>MAKE</code> Variable Works </h3>
923
<p>Recursive <code>make</code> commands should always use the variable <code>MAKE</code>,
924
not the explicit command name ‘<samp>make</samp>’, as shown here:
926
<table><tr><td> </td><td><pre class="example">subsystem:
927
cd subdir && $(MAKE)
928
</pre></td></tr></table>
930
<p>The value of this variable is the file name with which <code>make</code> was
931
invoked. If this file name was ‘<tt>/bin/make</tt>’, then the command executed
932
is ‘<samp>cd subdir && /bin/make</samp>’. If you use a special version of
933
<code>make</code> to run the top-level makefile, the same special version will be
934
executed for recursive invocations.
935
<a name="IDX181"></a>
937
<a name="IDX182"></a>
938
<p>As a special feature, using the variable <code>MAKE</code> in the commands of
939
a rule alters the effects of the ‘<samp>-t</samp>’ (‘<samp>--touch</samp>’), ‘<samp>-n</samp>’
940
(‘<samp>--just-print</samp>’), or ‘<samp>-q</samp>’ (‘<samp>--question</samp>’) option.
941
Using the <code>MAKE</code> variable has the same effect as using a ‘<samp>+</samp>’
942
character at the beginning of the command line. See section <a href="make_9.html#SEC108">Instead of Executing the Commands</a>. This special feature
943
is only enabled if the <code>MAKE</code> variable appears directly in the
944
command script: it does not apply if the <code>MAKE</code> variable is
945
referenced through expansion of another variable. In the latter case
946
you must use the ‘<samp>+</samp>’ token to get these special effects.
948
<p>Consider the command ‘<samp>make -t</samp>’ in the above example. (The
949
‘<samp>-t</samp>’ option marks targets as up to date without actually running
950
any commands; see <a href="make_9.html#SEC108">Instead of Executing the Commands</a>.) Following the usual
951
definition of ‘<samp>-t</samp>’, a ‘<samp>make -t</samp>’ command in the example would
952
create a file named ‘<tt>subsystem</tt>’ and do nothing else. What you
953
really want it to do is run ‘<samp>cd subdir && make -t</samp>’; but that would
954
require executing the command, and ‘<samp>-t</samp>’ says not to execute
956
<a name="IDX183"></a>
957
<a name="IDX184"></a>
958
<a name="IDX185"></a>
960
<p>The special feature makes this do what you want: whenever a command
961
line of a rule contains the variable <code>MAKE</code>, the flags ‘<samp>-t</samp>’,
962
‘<samp>-n</samp>’ and ‘<samp>-q</samp>’ do not apply to that line. Command lines
963
containing <code>MAKE</code> are executed normally despite the presence of a
964
flag that causes most commands not to be run. The usual
965
<code>MAKEFLAGS</code> mechanism passes the flags to the sub-<code>make</code>
966
(see section <a href="#SEC70">Communicating Options to a Sub-<code>make</code></a>), so your request to touch the files, or print the
967
commands, is propagated to the subsystem.
970
<a name="Variables_002fRecursion"></a>
972
<table cellpadding="1" cellspacing="1" border="0">
973
<tr><td valign="middle" align="left">[<a href="#SEC68" title="Previous section in reading order"> < </a>]</td>
974
<td valign="middle" align="left">[<a href="#SEC70" title="Next section in reading order"> > </a>]</td>
975
<td valign="middle" align="left"> </td>
976
<td valign="middle" align="left">[<a href="#SEC56" title="Beginning of this chapter or previous chapter"> << </a>]</td>
977
<td valign="middle" align="left">[<a href="#SEC67" title="Up section"> Up </a>]</td>
978
<td valign="middle" align="left">[<a href="make_6.html#SEC74" title="Next chapter"> >> </a>]</td>
979
<td valign="middle" align="left"> </td>
980
<td valign="middle" align="left"> </td>
981
<td valign="middle" align="left"> </td>
982
<td valign="middle" align="left"> </td>
983
<td valign="middle" align="left">[<a href="make.html#SEC_Top" title="Cover (top) of document">Top</a>]</td>
984
<td valign="middle" align="left">[<a href="make_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
985
<td valign="middle" align="left">[<a href="make_19.html#SEC148" title="Index">Index</a>]</td>
986
<td valign="middle" align="left">[<a href="make_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
988
<h3 class="subsection"> 5.7.2 Communicating Variables to a Sub-<code>make</code> </h3>
990
<p>Variable values of the top-level <code>make</code> can be passed to the
991
sub-<code>make</code> through the environment by explicit request. These
992
variables are defined in the sub-<code>make</code> as defaults, but do not
993
override what is specified in the makefile used by the sub-<code>make</code>
994
makefile unless you use the ‘<samp>-e</samp>’ switch (see section <a href="make_9.html#SEC112">Summary of Options</a>).
996
<p>To pass down, or <em>export</em>, a variable, <code>make</code> adds the variable
997
and its value to the environment for running each command. The
998
sub-<code>make</code>, in turn, uses the environment to initialize its table
999
of variable values. See section <a href="make_6.html#SEC85">Variables from the Environment</a>.
1001
<p>Except by explicit request, <code>make</code> exports a variable only if it
1002
is either defined in the environment initially or set on the command
1003
line, and if its name consists only of letters, numbers, and underscores.
1004
Some shells cannot cope with environment variable names consisting of
1005
characters other than letters, numbers, and underscores.
1007
<a name="IDX186"></a>
1008
<p>The value of the <code>make</code> variable <code>SHELL</code> is not exported.
1009
Instead, the value of the <code>SHELL</code> variable from the invoking
1010
environment is passed to the sub-<code>make</code>. You can force
1011
<code>make</code> to export its value for <code>SHELL</code> by using the
1012
<code>export</code> directive, described below. See section <a href="#SEC62">Choosing the Shell</a>.
1014
<p>The special variable <code>MAKEFLAGS</code> is always exported (unless you
1015
unexport it). <code>MAKEFILES</code> is exported if you set it to anything.
1017
<p><code>make</code> automatically passes down variable values that were defined
1018
on the command line, by putting them in the <code>MAKEFLAGS</code> variable.
1019
See section <a href="#SEC70">Communicating Options to a Sub-<code>make</code></a>.
1021
<p>Variables are <em>not</em> normally passed down if they were created by
1022
default by <code>make</code> (see section <a href="make_10.html#SEC116">Variables Used by Implicit Rules</a>). The sub-<code>make</code> will define these for
1025
<a name="IDX187"></a>
1026
<p>If you want to export specific variables to a sub-<code>make</code>, use the
1027
<code>export</code> directive, like this:
1029
<table><tr><td> </td><td><pre class="example">export <var>variable</var> …
1030
</pre></td></tr></table>
1032
<a name="IDX188"></a>
1033
<p>If you want to <em>prevent</em> a variable from being exported, use the
1034
<code>unexport</code> directive, like this:
1036
<table><tr><td> </td><td><pre class="example">unexport <var>variable</var> …
1037
</pre></td></tr></table>
1039
<p>In both of these forms, the arguments to <code>export</code> and
1040
<code>unexport</code> are expanded, and so could be variables or functions
1041
which expand to a (list of) variable names to be (un)exported.
1043
<p>As a convenience, you can define a variable and export it at the same
1046
<table><tr><td> </td><td><pre class="example">export <var>variable</var> = value
1047
</pre></td></tr></table>
1049
<p>has the same result as:
1051
<table><tr><td> </td><td><pre class="example"><var>variable</var> = value
1052
export <var>variable</var>
1053
</pre></td></tr></table>
1057
<table><tr><td> </td><td><pre class="example">export <var>variable</var> := value
1058
</pre></td></tr></table>
1060
<p>has the same result as:
1062
<table><tr><td> </td><td><pre class="example"><var>variable</var> := value
1063
export <var>variable</var>
1064
</pre></td></tr></table>
1068
<table><tr><td> </td><td><pre class="example">export <var>variable</var> += value
1069
</pre></td></tr></table>
1073
<table><tr><td> </td><td><pre class="example"><var>variable</var> += value
1074
export <var>variable</var>
1075
</pre></td></tr></table>
1077
<p>See section <a href="make_6.html#SEC82">Appending More Text to Variables</a>.
1079
<p>You may notice that the <code>export</code> and <code>unexport</code> directives
1080
work in <code>make</code> in the same way they work in the shell, <code>sh</code>.
1082
<p>If you want all variables to be exported by default, you can use
1083
<code>export</code> by itself:
1085
<table><tr><td> </td><td><pre class="example">export
1086
</pre></td></tr></table>
1088
<p>This tells <code>make</code> that variables which are not explicitly mentioned
1089
in an <code>export</code> or <code>unexport</code> directive should be exported.
1090
Any variable given in an <code>unexport</code> directive will still <em>not</em>
1091
be exported. If you use <code>export</code> by itself to export variables by
1092
default, variables whose names contain characters other than
1093
alphanumerics and underscores will not be exported unless specifically
1094
mentioned in an <code>export</code> directive.
1096
<a name="IDX189"></a>
1097
<p>The behavior elicited by an <code>export</code> directive by itself was the
1098
default in older versions of GNU <code>make</code>. If your makefiles depend
1099
on this behavior and you want to be compatible with old versions of
1100
<code>make</code>, you can write a rule for the special target
1101
<code>.EXPORT_ALL_VARIABLES</code> instead of using the <code>export</code> directive.
1102
This will be ignored by old <code>make</code>s, while the <code>export</code>
1103
directive will cause a syntax error.
1104
<a name="IDX190"></a>
1106
<p>Likewise, you can use <code>unexport</code> by itself to tell <code>make</code>
1107
<em>not</em> to export variables by default. Since this is the default
1108
behavior, you would only need to do this if <code>export</code> had been used
1109
by itself earlier (in an included makefile, perhaps). You
1110
<strong>cannot</strong> use <code>export</code> and <code>unexport</code> by themselves to
1111
have variables exported for some commands and not for others. The last
1112
<code>export</code> or <code>unexport</code> directive that appears by itself
1113
determines the behavior for the entire run of <code>make</code>.
1115
<a name="IDX191"></a>
1116
<a name="IDX192"></a>
1117
<p>As a special feature, the variable <code>MAKELEVEL</code> is changed when it
1118
is passed down from level to level. This variable's value is a string
1119
which is the depth of the level as a decimal number. The value is
1120
‘<samp>0</samp>’ for the top-level <code>make</code>; ‘<samp>1</samp>’ for a sub-<code>make</code>,
1121
‘<samp>2</samp>’ for a sub-sub-<code>make</code>, and so on. The incrementation
1122
happens when <code>make</code> sets up the environment for a command.
1124
<p>The main use of <code>MAKELEVEL</code> is to test it in a conditional
1125
directive (see section <a href="make_7.html#SEC88">Conditional Parts of Makefiles</a>); this
1126
way you can write a makefile that behaves one way if run recursively and
1127
another way if run directly by you.
1129
<a name="IDX193"></a>
1130
<p>You can use the variable <code>MAKEFILES</code> to cause all sub-<code>make</code>
1131
commands to use additional makefiles. The value of <code>MAKEFILES</code> is
1132
a whitespace-separated list of file names. This variable, if defined in
1133
the outer-level makefile, is passed down through the environment; then
1134
it serves as a list of extra makefiles for the sub-<code>make</code> to read
1135
before the usual or specified ones. See section <a href="make_3.html#SEC17">The Variable <code>MAKEFILES</code></a>.
1138
<a name="Options_002fRecursion"></a>
1139
<a name="SEC70"></a>
1140
<table cellpadding="1" cellspacing="1" border="0">
1141
<tr><td valign="middle" align="left">[<a href="#SEC69" title="Previous section in reading order"> < </a>]</td>
1142
<td valign="middle" align="left">[<a href="#SEC71" title="Next section in reading order"> > </a>]</td>
1143
<td valign="middle" align="left"> </td>
1144
<td valign="middle" align="left">[<a href="#SEC56" title="Beginning of this chapter or previous chapter"> << </a>]</td>
1145
<td valign="middle" align="left">[<a href="#SEC67" title="Up section"> Up </a>]</td>
1146
<td valign="middle" align="left">[<a href="make_6.html#SEC74" title="Next chapter"> >> </a>]</td>
1147
<td valign="middle" align="left"> </td>
1148
<td valign="middle" align="left"> </td>
1149
<td valign="middle" align="left"> </td>
1150
<td valign="middle" align="left"> </td>
1151
<td valign="middle" align="left">[<a href="make.html#SEC_Top" title="Cover (top) of document">Top</a>]</td>
1152
<td valign="middle" align="left">[<a href="make_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
1153
<td valign="middle" align="left">[<a href="make_19.html#SEC148" title="Index">Index</a>]</td>
1154
<td valign="middle" align="left">[<a href="make_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
1156
<h3 class="subsection"> 5.7.3 Communicating Options to a Sub-<code>make</code> </h3>
1158
<p>Flags such as ‘<samp>-s</samp>’ and ‘<samp>-k</samp>’ are passed automatically to the
1159
sub-<code>make</code> through the variable <code>MAKEFLAGS</code>. This variable is
1160
set up automatically by <code>make</code> to contain the flag letters that
1161
<code>make</code> received. Thus, if you do ‘<samp>make -ks</samp>’ then
1162
<code>MAKEFLAGS</code> gets the value ‘<samp>ks</samp>’.
1164
<p>As a consequence, every sub-<code>make</code> gets a value for <code>MAKEFLAGS</code>
1165
in its environment. In response, it takes the flags from that value and
1166
processes them as if they had been given as arguments.
1167
See section <a href="make_9.html#SEC112">Summary of Options</a>.
1169
<a name="IDX194"></a>
1170
<a name="IDX195"></a>
1171
<a name="IDX196"></a>
1172
<p>Likewise variables defined on the command line are passed to the
1173
sub-<code>make</code> through <code>MAKEFLAGS</code>. Words in the value of
1174
<code>MAKEFLAGS</code> that contain ‘<samp>=</samp>’, <code>make</code> treats as variable
1175
definitions just as if they appeared on the command line.
1176
See section <a href="make_9.html#SEC110">Overriding Variables</a>.
1178
<a name="IDX197"></a>
1179
<a name="IDX198"></a>
1180
<a name="IDX199"></a>
1181
<a name="IDX200"></a>
1182
<a name="IDX201"></a>
1183
<a name="IDX202"></a>
1184
<a name="IDX203"></a>
1185
<a name="IDX204"></a>
1186
<a name="IDX205"></a>
1187
<a name="IDX206"></a>
1188
<a name="IDX207"></a>
1189
<a name="IDX208"></a>
1190
<a name="IDX209"></a>
1191
<a name="IDX210"></a>
1192
<p>The options ‘<samp>-C</samp>’, ‘<samp>-f</samp>’, ‘<samp>-o</samp>’, and ‘<samp>-W</samp>’ are not put
1193
into <code>MAKEFLAGS</code>; these options are not passed down.
1195
<a name="IDX211"></a>
1196
<a name="IDX212"></a>
1197
<a name="IDX213"></a>
1198
<a name="IDX214"></a>
1199
<p>The ‘<samp>-j</samp>’ option is a special case (see section <a href="#SEC64">Parallel Execution</a>).
1200
If you set it to some numeric value ‘<samp>N</samp>’ and your operating system
1201
supports it (most any UNIX system will; others typically won't), the
1202
parent <code>make</code> and all the sub-<code>make</code>s will communicate to
1203
ensure that there are only ‘<samp>N</samp>’ jobs running at the same time
1204
between them all. Note that any job that is marked recursive
1205
(see section <a href="make_9.html#SEC108">Instead of Executing the Commands</a>)
1206
doesn't count against the total jobs (otherwise we could get ‘<samp>N</samp>’
1207
sub-<code>make</code>s running and have no slots left over for any real work!)
1209
<p>If your operating system doesn't support the above communication, then
1210
‘<samp>-j 1</samp>’ is always put into <code>MAKEFLAGS</code> instead of the value you
1211
specified. This is because if the ‘<samp>-j</samp>’ option were passed down
1212
to sub-<code>make</code>s, you would get many more jobs running in parallel
1213
than you asked for. If you give ‘<samp>-j</samp>’ with no numeric argument,
1214
meaning to run as many jobs as possible in parallel, this is passed
1215
down, since multiple infinities are no more than one.
1217
<p>If you do not want to pass the other flags down, you must change the
1218
value of <code>MAKEFLAGS</code>, like this:
1220
<table><tr><td> </td><td><pre class="example">subsystem:
1221
cd subdir && $(MAKE) MAKEFLAGS=
1222
</pre></td></tr></table>
1224
<a name="IDX215"></a>
1225
<p>The command line variable definitions really appear in the variable
1226
<code>MAKEOVERRIDES</code>, and <code>MAKEFLAGS</code> contains a reference to this
1227
variable. If you do want to pass flags down normally, but don't want to
1228
pass down the command line variable definitions, you can reset
1229
<code>MAKEOVERRIDES</code> to empty, like this:
1231
<table><tr><td> </td><td><pre class="example">MAKEOVERRIDES =
1232
</pre></td></tr></table>
1234
<a name="IDX216"></a>
1235
<a name="IDX217"></a>
1236
<p>This is not usually useful to do. However, some systems have a small
1237
fixed limit on the size of the environment, and putting so much
1238
information into the value of <code>MAKEFLAGS</code> can exceed it. If you
1239
see the error message ‘<samp>Arg list too long</samp>’, this may be the problem.
1240
<a name="IDX218"></a>
1241
<a name="IDX219"></a>
1242
(For strict compliance with POSIX.2, changing <code>MAKEOVERRIDES</code> does
1243
not affect <code>MAKEFLAGS</code> if the special target ‘<samp>.POSIX</samp>’ appears
1244
in the makefile. You probably do not care about this.)
1246
<a name="IDX220"></a>
1247
<p>A similar variable <code>MFLAGS</code> exists also, for historical
1248
compatibility. It has the same value as <code>MAKEFLAGS</code> except that it
1249
does not contain the command line variable definitions, and it always
1250
begins with a hyphen unless it is empty (<code>MAKEFLAGS</code> begins with a
1251
hyphen only when it begins with an option that has no single-letter
1252
version, such as ‘<samp>--warn-undefined-variables</samp>’). <code>MFLAGS</code> was
1253
traditionally used explicitly in the recursive <code>make</code> command, like
1256
<table><tr><td> </td><td><pre class="example">subsystem:
1257
cd subdir && $(MAKE) $(MFLAGS)
1258
</pre></td></tr></table>
1260
<p>but now <code>MAKEFLAGS</code> makes this usage redundant. If you want your
1261
makefiles to be compatible with old <code>make</code> programs, use this
1262
technique; it will work fine with more modern <code>make</code> versions too.
1264
<a name="IDX221"></a>
1265
<a name="IDX222"></a>
1266
<a name="IDX223"></a>
1267
<a name="IDX224"></a>
1268
<p>The <code>MAKEFLAGS</code> variable can also be useful if you want to have
1269
certain options, such as ‘<samp>-k</samp>’ (see section <a href="make_9.html#SEC112">Summary of Options</a>), set each time you run <code>make</code>. You simply put a value for
1270
<code>MAKEFLAGS</code> in your environment. You can also set <code>MAKEFLAGS</code> in
1271
a makefile, to specify additional flags that should also be in effect for
1272
that makefile. (Note that you cannot use <code>MFLAGS</code> this way. That
1273
variable is set only for compatibility; <code>make</code> does not interpret a
1274
value you set for it in any way.)
1276
<p>When <code>make</code> interprets the value of <code>MAKEFLAGS</code> (either from the
1277
environment or from a makefile), it first prepends a hyphen if the value
1278
does not already begin with one. Then it chops the value into words
1279
separated by blanks, and parses these words as if they were options given
1280
on the command line (except that ‘<samp>-C</samp>’, ‘<samp>-f</samp>’, ‘<samp>-h</samp>’,
1281
‘<samp>-o</samp>’, ‘<samp>-W</samp>’, and their long-named versions are ignored; and there
1282
is no error for an invalid option).
1284
<p>If you do put <code>MAKEFLAGS</code> in your environment, you should be sure not
1285
to include any options that will drastically affect the actions of
1286
<code>make</code> and undermine the purpose of makefiles and of <code>make</code>
1287
itself. For instance, the ‘<samp>-t</samp>’, ‘<samp>-n</samp>’, and ‘<samp>-q</samp>’ options, if
1288
put in one of these variables, could have disastrous consequences and would
1289
certainly have at least surprising and probably annoying effects.
1292
<a name="g_t_002dw-Option"></a>
1293
<a name="SEC71"></a>
1294
<table cellpadding="1" cellspacing="1" border="0">
1295
<tr><td valign="middle" align="left">[<a href="#SEC70" title="Previous section in reading order"> < </a>]</td>
1296
<td valign="middle" align="left">[<a href="#SEC72" title="Next section in reading order"> > </a>]</td>
1297
<td valign="middle" align="left"> </td>
1298
<td valign="middle" align="left">[<a href="#SEC56" title="Beginning of this chapter or previous chapter"> << </a>]</td>
1299
<td valign="middle" align="left">[<a href="#SEC67" title="Up section"> Up </a>]</td>
1300
<td valign="middle" align="left">[<a href="make_6.html#SEC74" title="Next chapter"> >> </a>]</td>
1301
<td valign="middle" align="left"> </td>
1302
<td valign="middle" align="left"> </td>
1303
<td valign="middle" align="left"> </td>
1304
<td valign="middle" align="left"> </td>
1305
<td valign="middle" align="left">[<a href="make.html#SEC_Top" title="Cover (top) of document">Top</a>]</td>
1306
<td valign="middle" align="left">[<a href="make_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
1307
<td valign="middle" align="left">[<a href="make_19.html#SEC148" title="Index">Index</a>]</td>
1308
<td valign="middle" align="left">[<a href="make_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
1310
<h3 class="subsection"> 5.7.4 The ‘<samp>--print-directory</samp>’ Option </h3>
1312
<p>If you use several levels of recursive <code>make</code> invocations, the
1313
‘<samp>-w</samp>’ or ‘<samp>--print-directory</samp>’ option can make the output a
1314
lot easier to understand by showing each directory as <code>make</code>
1315
starts processing it and as <code>make</code> finishes processing it. For
1316
example, if ‘<samp>make -w</samp>’ is run in the directory ‘<tt>/u/gnu/make</tt>’,
1317
<code>make</code> will print a line of the form:
1319
<table><tr><td> </td><td><pre class="example">make: Entering directory `/u/gnu/make'.
1320
</pre></td></tr></table>
1322
<p>before doing anything else, and a line of the form:
1324
<table><tr><td> </td><td><pre class="example">make: Leaving directory `/u/gnu/make'.
1325
</pre></td></tr></table>
1327
<p>when processing is completed.
1329
<a name="IDX225"></a>
1330
<a name="IDX226"></a>
1331
<a name="IDX227"></a>
1332
<a name="IDX228"></a>
1333
<a name="IDX229"></a>
1334
<a name="IDX230"></a>
1335
<a name="IDX231"></a>
1336
<a name="IDX232"></a>
1337
<a name="IDX233"></a>
1338
<a name="IDX234"></a>
1339
<p>Normally, you do not need to specify this option because ‘<samp>make</samp>’
1340
does it for you: ‘<samp>-w</samp>’ is turned on automatically when you use the
1341
‘<samp>-C</samp>’ option, and in sub-<code>make</code>s. <code>make</code> will not
1342
automatically turn on ‘<samp>-w</samp>’ if you also use ‘<samp>-s</samp>’, which says to
1343
be silent, or if you use ‘<samp>--no-print-directory</samp>’ to explicitly
1347
<a name="Sequences"></a>
1348
<a name="SEC72"></a>
1349
<table cellpadding="1" cellspacing="1" border="0">
1350
<tr><td valign="middle" align="left">[<a href="#SEC71" title="Previous section in reading order"> < </a>]</td>
1351
<td valign="middle" align="left">[<a href="#SEC73" title="Next section in reading order"> > </a>]</td>
1352
<td valign="middle" align="left"> </td>
1353
<td valign="middle" align="left">[<a href="#SEC56" title="Beginning of this chapter or previous chapter"> << </a>]</td>
1354
<td valign="middle" align="left">[<a href="#SEC56" title="Up section"> Up </a>]</td>
1355
<td valign="middle" align="left">[<a href="make_6.html#SEC74" title="Next chapter"> >> </a>]</td>
1356
<td valign="middle" align="left"> </td>
1357
<td valign="middle" align="left"> </td>
1358
<td valign="middle" align="left"> </td>
1359
<td valign="middle" align="left"> </td>
1360
<td valign="middle" align="left">[<a href="make.html#SEC_Top" title="Cover (top) of document">Top</a>]</td>
1361
<td valign="middle" align="left">[<a href="make_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
1362
<td valign="middle" align="left">[<a href="make_19.html#SEC148" title="Index">Index</a>]</td>
1363
<td valign="middle" align="left">[<a href="make_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
1365
<h2 class="section"> 5.8 Defining Canned Command Sequences </h2>
1367
<p>When the same sequence of commands is useful in making various targets, you
1368
can define it as a canned sequence with the <code>define</code> directive, and
1369
refer to the canned sequence from the rules for those targets. The canned
1370
sequence is actually a variable, so the name must not conflict with other
1373
<p>Here is an example of defining a canned sequence of commands:
1375
<table><tr><td> </td><td><pre class="example">define run-yacc
1376
yacc $(firstword $^)
1379
</pre></td></tr></table>
1380
<a name="IDX235"></a>
1382
<p>Here <code>run-yacc</code> is the name of the variable being defined;
1383
<code>endef</code> marks the end of the definition; the lines in between are the
1384
commands. The <code>define</code> directive does not expand variable references
1385
and function calls in the canned sequence; the ‘<samp>$</samp>’ characters,
1386
parentheses, variable names, and so on, all become part of the value of the
1387
variable you are defining.
1388
See section <a href="make_6.html#SEC84">Defining Variables Verbatim</a>,
1389
for a complete explanation of <code>define</code>.
1391
<p>The first command in this example runs Yacc on the first prerequisite of
1392
whichever rule uses the canned sequence. The output file from Yacc is
1393
always named ‘<tt>y.tab.c</tt>’. The second command moves the output to the
1394
rule's target file name.
1396
<p>To use the canned sequence, substitute the variable into the commands of a
1397
rule. You can substitute it like any other variable
1398
(see section <a href="make_6.html#SEC75">Basics of Variable References</a>).
1399
Because variables defined by <code>define</code> are recursively expanded
1400
variables, all the variable references you wrote inside the <code>define</code>
1401
are expanded now. For example:
1403
<table><tr><td> </td><td><pre class="example">foo.c : foo.y
1405
</pre></td></tr></table>
1407
<p>‘<samp>foo.y</samp>’ will be substituted for the variable ‘<samp>$^</samp>’ when it occurs in
1408
<code>run-yacc</code>'s value, and ‘<samp>foo.c</samp>’ for ‘<samp>$@</samp>’.
1410
<p>This is a realistic example, but this particular one is not needed in
1411
practice because <code>make</code> has an implicit rule to figure out these
1412
commands based on the file names involved
1413
(see section <a href="make_10.html#SEC113">Using Implicit Rules</a>).
1415
<a name="IDX236"></a>
1416
<a name="IDX237"></a>
1417
<a name="IDX238"></a>
1418
<p>In command execution, each line of a canned sequence is treated just as
1419
if the line appeared on its own in the rule, preceded by a tab. In
1420
particular, <code>make</code> invokes a separate subshell for each line. You
1421
can use the special prefix characters that affect command lines
1422
(‘<samp>@</samp>’, ‘<samp>-</samp>’, and ‘<samp>+</samp>’) on each line of a canned sequence.
1423
See section <a href="#SEC56">Writing the Commands in Rules</a>.
1424
For example, using this canned sequence:
1426
<table><tr><td> </td><td><pre class="example">define frobnicate
1427
@echo "frobnicating target $@"
1428
frob-step-1 $< -o $@-step-1
1429
frob-step-2 $@-step-1 -o $@
1431
</pre></td></tr></table>
1433
<p><code>make</code> will not echo the first line, the <code>echo</code> command.
1434
But it <em>will</em> echo the following two command lines.
1436
<p>On the other hand, prefix characters on the command line that refers to
1437
a canned sequence apply to every line in the sequence. So the rule:
1439
<table><tr><td> </td><td><pre class="example">frob.out: frob.in
1441
</pre></td></tr></table>
1443
<p>does not echo <em>any</em> commands.
1444
(See section <a href="#SEC60">Command Echoing</a>, for a full explanation of ‘<samp>@</samp>’.)
1447
<a name="Empty-Commands"></a>
1448
<a name="SEC73"></a>
1449
<table cellpadding="1" cellspacing="1" border="0">
1450
<tr><td valign="middle" align="left">[<a href="#SEC72" title="Previous section in reading order"> < </a>]</td>
1451
<td valign="middle" align="left">[<a href="make_6.html#SEC74" title="Next section in reading order"> > </a>]</td>
1452
<td valign="middle" align="left"> </td>
1453
<td valign="middle" align="left">[<a href="#SEC56" title="Beginning of this chapter or previous chapter"> << </a>]</td>
1454
<td valign="middle" align="left">[<a href="#SEC56" title="Up section"> Up </a>]</td>
1455
<td valign="middle" align="left">[<a href="make_6.html#SEC74" title="Next chapter"> >> </a>]</td>
1456
<td valign="middle" align="left"> </td>
1457
<td valign="middle" align="left"> </td>
1458
<td valign="middle" align="left"> </td>
1459
<td valign="middle" align="left"> </td>
1460
<td valign="middle" align="left">[<a href="make.html#SEC_Top" title="Cover (top) of document">Top</a>]</td>
1461
<td valign="middle" align="left">[<a href="make_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
1462
<td valign="middle" align="left">[<a href="make_19.html#SEC148" title="Index">Index</a>]</td>
1463
<td valign="middle" align="left">[<a href="make_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
1465
<h2 class="section"> 5.9 Using Empty Commands </h2>
1467
<p>It is sometimes useful to define commands which do nothing. This is done
1468
simply by giving a command that consists of nothing but whitespace. For
1471
<table><tr><td> </td><td><pre class="example">target: ;
1472
</pre></td></tr></table>
1474
<p>defines an empty command string for ‘<tt>target</tt>’. You could also use a
1475
line beginning with a tab character to define an empty command string,
1476
but this would be confusing because such a line looks empty.
1478
<a name="IDX239"></a>
1479
<p>You may be wondering why you would want to define a command string that
1480
does nothing. The only reason this is useful is to prevent a target
1481
from getting implicit commands (from implicit rules or the
1482
<code>.DEFAULT</code> special target; see section <a href="make_10.html#SEC113">Using Implicit Rules</a> and
1483
see section <a href="make_10.html#SEC125">Defining Last-Resort Default Rules</a>).
1486
<p>You may be inclined to define empty command strings for targets that are
1487
not actual files, but only exist so that their prerequisites can be
1488
remade. However, this is not the best way to do that, because the
1489
prerequisites may not be remade properly if the target file actually does exist.
1490
See section <a href="make_4.html#SEC45">Phony Targets</a>, for a better way to do this.
1493
<table cellpadding="1" cellspacing="1" border="0">
1494
<tr><td valign="middle" align="left">[<a href="#SEC56" title="Beginning of this chapter or previous chapter"> << </a>]</td>
1495
<td valign="middle" align="left">[<a href="make_6.html#SEC74" title="Next chapter"> >> </a>]</td>
1496
<td valign="middle" align="left"> </td>
1497
<td valign="middle" align="left"> </td>
1498
<td valign="middle" align="left"> </td>
1499
<td valign="middle" align="left"> </td>
1500
<td valign="middle" align="left"> </td>
1501
<td valign="middle" align="left">[<a href="make.html#SEC_Top" title="Cover (top) of document">Top</a>]</td>
1502
<td valign="middle" align="left">[<a href="make_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
1503
<td valign="middle" align="left">[<a href="make_19.html#SEC148" title="Index">Index</a>]</td>
1504
<td valign="middle" align="left">[<a href="make_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
1508
This document was generated by <em>Manoj Srivastava</em> on <em>August, 17 2009</em> using <a href="http://www.nongnu.org/texi2html/"><em>texi2html 1.78</em></a>.