home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / make-3.67 / make.info-3 < prev    next >
Encoding:
GNU Info File  |  1993-05-16  |  50.1 KB  |  1,214 lines

  1. This is Info file make.info, produced by Makeinfo-1.54 from the input
  2. file make.texinfo.
  3.  
  4.    This file documents the GNU Make utility, which determines
  5. automatically which pieces of a large program need to be recompiled,
  6. and issues the commands to recompile them.
  7.  
  8.    This is Edition 0.42, last updated 14 May 1993, of `The GNU Make
  9. Manual', for `make', Version 3.66 Beta.
  10.  
  11.    Copyright (C) 1988, '89, '90, '91, '92, '93 Free Software
  12. Foundation, Inc.
  13.  
  14.    Permission is granted to make and distribute verbatim copies of this
  15. manual provided the copyright notice and this permission notice are
  16. preserved on all copies.
  17.  
  18.    Permission is granted to copy and distribute modified versions of
  19. this manual under the conditions for verbatim copying, provided also
  20. that the section entitled "GNU General Public License" is included
  21. exactly as in the original, and provided that the entire resulting
  22. derived work is distributed under the terms of a permission notice
  23. identical to this one.
  24.  
  25.    Permission is granted to copy and distribute translations of this
  26. manual into another language, under the above conditions for modified
  27. versions, except that the text of the translations of the section
  28. entitled "GNU General Public License" must be approved for accuracy by
  29. the Foundation.
  30.  
  31. 
  32. File: make.info,  Node: Static Usage,  Next: Static versus Implicit,  Up: Static Pattern
  33.  
  34. Syntax of Static Pattern Rules
  35. ------------------------------
  36.  
  37.    Here is the syntax of a static pattern rule:
  38.  
  39.      TARGETS ...: TARGET-PATTERN: DEP-PATTERNS ...
  40.              COMMANDS
  41.              ...
  42.  
  43. The TARGETS list specifies the targets that the rule applies to.  The
  44. targets can contain wildcard characters, just like the targets of
  45. ordinary rules (*note Using Wildcard Characters in File Names:
  46. Wildcards.).
  47.  
  48.    The TARGET-PATTERN and DEP-PATTERNS say how to compute the
  49. dependencies of each target.  Each target is matched against the
  50. TARGET-PATTERN to extract a part of the target name, called the "stem".
  51. This stem is substituted into each of the DEP-PATTERNS to make the
  52. dependency names (one from each DEP-PATTERN).
  53.  
  54.    Each pattern normally contains the character `%' just once.  When the
  55. TARGET-PATTERN matches a target, the `%' can match any part of the
  56. target name; this part is called the "stem".  The rest of the pattern
  57. must match exactly.  For example, the target `foo.o' matches the
  58. pattern `%.o', with `foo' as the stem.  The targets `foo.c' and
  59. `foo.out' do not match that pattern.
  60.  
  61.    The dependency names for each target are made by substituting the
  62. stem for the `%' in each dependency pattern.  For example, if one
  63. dependency pattern is `%.c', then substitution of the stem `foo' gives
  64. the dependency name `foo.c'.  It is legitimate to write a dependency
  65. pattern that does not contain `%'; then this dependency is the same for
  66. all targets.
  67.  
  68.    `%' characters in pattern rules can be quoted with preceding
  69. backslashes (`\').  Backslashes that would otherwise quote `%'
  70. characters can be quoted with more backslashes.  Backslashes that quote
  71. `%' characters or other backslashes are removed from the pattern before
  72. it is compared to file names or has a stem substituted into it.
  73. Backslashes that are not in danger of quoting `%' characters go
  74. unmolested.  For example, the pattern `the\%weird\\%pattern\\' has
  75. `the%weird\' preceding the operative `%' character, and `pattern\\'
  76. following it.  The final two backslashes are left alone because they
  77. cannot affect any `%' character.
  78.  
  79.    Here is an example, which compiles each of `foo.o' and `bar.o' from
  80. the corresponding `.c' file:
  81.  
  82.      objects = foo.o bar.o
  83.      
  84.      $(objects): %.o: %.c
  85.              $(CC) -c $(CFLAGS) $< -o $@
  86.  
  87. Here `$<' is the automatic variable that holds the name of the
  88. dependency and `$@' is the automatic variable that holds the name of
  89. the target; see *Note Automatic Variables: Automatic.
  90.  
  91.    Each target specified must match the target pattern; a warning is
  92. issued for each target that does not.  If you have a list of files,
  93. only some of which will match the pattern, you can use the `filter'
  94. function to remove nonmatching file names (*note Functions for String
  95. Substitution and Analysis: Text Functions.):
  96.  
  97.      files = foo.elc bar.o lose.o
  98.      
  99.      $(filter %.o,$(files)): %.o: %.c
  100.              $(CC) -c $(CFLAGS) $< -o $@
  101.      $(filter %.elc,$(files)): %.elc: %.el
  102.              emacs -f batch-byte-compile $<
  103.  
  104. Here the result of `$(filter %.o,$(files))' is `bar.o lose.o', and the
  105. first static pattern rule causes each of these object files to be
  106. updated by compiling the corresponding C source file.  The result of
  107. `$(filter %.elc,$(files))' is `foo.elc', so that file is made from
  108. `foo.el'.
  109.  
  110.    Another example shows how to use `$*' in static pattern rules:
  111.  
  112.      bigoutput littleoutput : %output : text.g
  113.              generate text.g -$* > $@
  114.  
  115. When the `generate' command is run, `$*' will expand to the stem,
  116. either `big' or `little'.
  117.  
  118. 
  119. File: make.info,  Node: Static versus Implicit,  Prev: Static Usage,  Up: Static Pattern
  120.  
  121. Static Pattern Rules versus Implicit Rules
  122. ------------------------------------------
  123.  
  124.    A static pattern rule has much in common with an implicit rule
  125. defined as a pattern rule (*note Defining and Redefining Pattern Rules:
  126. Pattern Rules.).  Both have a pattern for the target and patterns for
  127. constructing the names of dependencies.  The difference is in how
  128. `make' decides *when* the rule applies.
  129.  
  130.    An implicit rule *can* apply to any target that matches its pattern,
  131. but it *does* apply only when the target has no commands otherwise
  132. specified, and only when the dependencies can be found.  If more than
  133. one implicit rule appears applicable, only one applies; the choice
  134. depends on the order of rules.
  135.  
  136.    By contrast, a static pattern rule applies to the precise list of
  137. targets that you specify in the rule.  It cannot apply to any other
  138. target and it invariably does apply to each of the targets specified.
  139. If two conflicting rules apply, and both have commands, that's an error.
  140.  
  141.    The static pattern rule can be better than an implicit rule for these
  142. reasons:
  143.  
  144.    * You may wish to override the usual implicit rule for a few files
  145.      whose names cannot be categorized syntactically but can be given
  146.      in an explicit list.
  147.  
  148.    * If you cannot be sure of the precise contents of the directories
  149.      you are using, you may not be sure which other irrelevant files
  150.      might lead `make' to use the wrong implicit rule.  The choice
  151.      might depend on the order in which the implicit rule search is
  152.      done.  With static pattern rules, there is no uncertainty: each
  153.      rule applies to precisely the targets specified.
  154.  
  155. 
  156. File: make.info,  Node: Double-Colon,  Next: Automatic Dependencies,  Prev: Static Pattern,  Up: Rules
  157.  
  158. Double-Colon Rules
  159. ==================
  160.  
  161.    "Double-colon" rules are rules written with `::' instead of `:'
  162. after the target names.  They are handled differently from ordinary
  163. rules when the same target appears in more than one rule.
  164.  
  165.    When a target appears in multiple rules, all the rules must be the
  166. same type: all ordinary, or all double-colon.  If they are
  167. double-colon, each of them is independent of the others.  Each
  168. double-colon rule's commands are executed if the target is older than
  169. any dependencies of that rule.  This can result in executing none, any,
  170. or all of the double-colon rules.
  171.  
  172.    Double-colon rules with the same target are in fact completely
  173. separate from one another.  Each double-colon rule is processed
  174. individually, just as rules with different targets are processed.
  175.  
  176.    The double-colon rules for a target are executed in the order they
  177. appear in the makefile.  However, the cases where double-colon rules
  178. really make sense are those where the order of executing the commands
  179. would not matter.
  180.  
  181.    Double-colon rules are somewhat obscure and not often very useful;
  182. they provide a mechanism for cases in which the method used to update a
  183. target differs depending on which dependency files caused the update,
  184. and such cases are rare.
  185.  
  186.    Each double-colon rule should specify commands; if it does not, an
  187. implicit rule will be used if one applies.  *Note Using Implicit Rules:
  188. Implicit Rules.
  189.  
  190. 
  191. File: make.info,  Node: Automatic Dependencies,  Prev: Double-Colon,  Up: Rules
  192.  
  193. Generating Dependencies Automatically
  194. =====================================
  195.  
  196.    In the makefile for a program, many of the rules you need to write
  197. often say only that some object file depends on some header file.  For
  198. example, if `main.c' uses `defs.h' via an `#include', you would write:
  199.  
  200.      main.o: defs.h
  201.  
  202. You need this rule so that `make' knows that it must remake `main.o'
  203. whenever `defs.h' changes.  You can see that for a large program you
  204. would have to write dozens of such rules in your makefile.  And, you
  205. must always be very careful to update the makefile every time you add
  206. or remove an `#include'.
  207.  
  208.    To avoid this hassle, most modern C compilers can write these rules
  209. for you, by looking at the `#include' lines in the source files.
  210. Usually this is done with the `-M' option to the compiler.  For
  211. example, the command:
  212.  
  213.      cc -M main.c
  214.  
  215. generates the output:
  216.  
  217.      main.o : main.c defs.h
  218.  
  219. Thus you no longer have to write all those rules yourself.  The
  220. compiler will do it for you.
  221.  
  222.    With old `make' programs, it was traditional practice to use this
  223. compiler feature to generate dependencies on demand with a command like
  224. `make depend'.  That command would create a file `depend' containing
  225. all the automatically-generated dependencies; then the makefile could
  226. use `include' to read them in (*note Include::.).
  227.  
  228.    In GNU `make', the feature of remaking makefiles makes this practice
  229. obsolete--you need never tell `make' explicitly to regenerate the
  230. dependencies, because it always regenerates any makefile that is out of
  231. date.  *Note Remaking Makefiles::.
  232.  
  233.    The practice we recommend for automatic dependency generation is to
  234. have one makefile corresponding to each source file.  For each source
  235. file `NAME.c' there is a makefile `NAME.d' which lists what files the
  236. object file `NAME.o' depends on.  That way only the source files that
  237. have changed need to be rescanned to produce the new dependencies.
  238.  
  239.    Here is the pattern rule to generate a file of dependencies (i.e., a
  240. makefile) called `NAME.d' from a C source file called `NAME.c':
  241.  
  242.      %.d: %.c
  243.          $(CC) -M $(CPPFLAGS) $< | sed 's/$*.o/& $@/g' > $@
  244.  
  245. *Note Pattern Rules::, for information on defining pattern rules.  The
  246. purpose of the `sed' command is to translate (for example):
  247.  
  248.      main.o : main.c defs.h
  249.  
  250. into:
  251.  
  252.      main.o main.d : main.c defs.h
  253.  
  254. This makes each `.d' file depend on all the source and header files
  255. that the corresponding `.o' file depends on.  `make' then knows it must
  256. regenerate the dependencies whenever any of the source or header files
  257. changes.
  258.  
  259.    Once you've defined the rule to remake the `.d' files, you then use
  260. the `include' directive to read them all in.  *Note Include::.  For
  261. example:
  262.  
  263.      sources = foo.c bar.c
  264.      
  265.      include $(sources:.c=.d)
  266.  
  267. (This example uses a substitution variable reference to translate the
  268. list of source files `foo.c bar.c' into a list of dependency makefiles,
  269. `foo.d bar.d'.  *Note Substitution Refs::, for full information on
  270. substitution references.)  Since the `.d' files are makefiles like any
  271. others, `make' will remake them as necessary with no further work from
  272. you.  *Note Remaking Makefiles::.
  273.  
  274. 
  275. File: make.info,  Node: Commands,  Next: Using Variables,  Prev: Rules,  Up: Top
  276.  
  277. Writing the Commands in Rules
  278. *****************************
  279.  
  280.    The commands of a rule consist of shell command lines to be executed
  281. one by one.  Each command line must start with a tab, except that the
  282. first command line may be attached to the target-and-dependencies line
  283. with a semicolon in between.  Blank lines and lines of just comments
  284. may appear among the command lines; they are ignored.
  285.  
  286.    Users use many different shell programs, but commands in makefiles
  287. are always interpreted by `/bin/sh' unless the makefile specifies
  288. otherwise.  *Note Command Execution: Execution.
  289.  
  290.    The shell that is in use determines whether comments can be written
  291. on command lines, and what syntax they use.  When the shell is
  292. `/bin/sh', a `#' starts a comment that extends to the end of the line.
  293. The `#' does not have to be at the beginning of a line.  Text on a line
  294. before a `#' is not part of the comment.
  295.  
  296. * Menu:
  297.  
  298. * Echoing::                     How to control when commands are echoed.
  299. * Execution::                   How commands are executed.
  300. * Parallel::                    How commands can be executed in parallel.
  301. * Errors::                      What happens after a command execution error.
  302. * Interrupts::                  What happens when a command is interrupted.
  303. * Recursion::                   Invoking `make' from makefiles.
  304. * Sequences::                   Defining canned sequences of commands.
  305. * Empty Commands::              Defining useful, do-nothing commands.
  306.  
  307. 
  308. File: make.info,  Node: Echoing,  Next: Execution,  Up: Commands
  309.  
  310. Command Echoing
  311. ===============
  312.  
  313.    Normally `make' prints each command line before it is executed.  We
  314. call this "echoing" because it gives the appearance that you are typing
  315. the commands yourself.
  316.  
  317.    When a line starts with `@', the echoing of that line is suppressed.
  318. The `@' is discarded before the command is passed to the shell.
  319. Typically you would use this for a command whose only effect is to print
  320. something, such as an `echo' command to indicate progress through the
  321. makefile:
  322.  
  323.      @echo About to make distribution files
  324.  
  325.    When `make' is given the flag `-n' or `--just-print', echoing is all
  326. that happens, no execution.  *Note Summary of Options: Options Summary.
  327. In this case and only this case, even the commands starting with `@'
  328. are printed.  This flag is useful for finding out which commands `make'
  329. thinks are necessary without actually doing them.
  330.  
  331.    The `-s' or `--silent' flag to `make' prevents all echoing, as if
  332. all commands started with `@'.  A rule in the makefile for the special
  333. target `.SILENT' has the same effect (*note Special Built-in Target
  334. Names: Special Targets.).  `.SILENT' is essentially obsolete since `@'
  335. is more flexible.
  336.  
  337. 
  338. File: make.info,  Node: Execution,  Next: Parallel,  Prev: Echoing,  Up: Commands
  339.  
  340. Command Execution
  341. =================
  342.  
  343.    When it is time to execute commands to update a target, they are
  344. executed by making a new subshell for each line.  (In practice, `make'
  345. may take shortcuts that do not affect the results.)
  346.  
  347.    *Please note:* this implies that shell commands such as `cd' that
  348. set variables local to each process will not affect the following
  349. command lines.  If you want to use `cd' to affect the next command, put
  350. the two on a single line with a semicolon between them.  Then `make'
  351. will consider them a single command and pass them, together, to a shell
  352. which will execute them in sequence.  For example:
  353.  
  354.      foo : bar/lose
  355.              cd bar; gobble lose > ../foo
  356.  
  357.    If you would like to split a single shell command into multiple
  358. lines of text, you must use a backslash at the end of all but the last
  359. subline.  Such a sequence of lines is combined into a single line, by
  360. deleting the backslash-newline sequences, before passing it to the
  361. shell.  Thus, the following is equivalent to the preceding example:
  362.  
  363.      foo : bar/lose
  364.              cd bar;  \
  365.              gobble lose > ../foo
  366.  
  367.    The program used as the shell is taken from the variable `SHELL'.
  368. By default, the program `/bin/sh' is used.
  369.  
  370.    Unlike most variables, the variable `SHELL' is never set from the
  371. environment.  This is because the `SHELL' environment variable is used
  372. to specify your personal choice of shell program for interactive use.
  373. It would be very bad for personal choices like this to affect the
  374. functioning of makefiles.  *Note Variables from the Environment:
  375. Environment.
  376.  
  377. 
  378. File: make.info,  Node: Parallel,  Next: Errors,  Prev: Execution,  Up: Commands
  379.  
  380. Parallel Execution
  381. ==================
  382.  
  383.    GNU `make' knows how to execute several commands at once.  Normally,
  384. `make' will execute only one command at a time, waiting for it to
  385. finish before executing the next.  However, the `-j' or `--jobs' option
  386. tells `make' to execute many commands simultaneously.
  387.  
  388.    If the `-j' option is followed by an integer, this is the number of
  389. commands to execute at once; this is called the number of "job slots".
  390. If there is nothing looking like an integer after the `-j' option,
  391. there is no limit on the number of job slots.  The default number of job
  392. slots is one, which means serial execution (one thing at a time).
  393.  
  394.    One unpleasant consequence of running several commands
  395. simultaneously is that output from all of the commands comes when the
  396. commands send it, so messages from different commands may be
  397. interspersed.
  398.  
  399.    Another problem is that two processes cannot both take input from the
  400. same device; so to make sure that only one command tries to take input
  401. from the terminal at once, `make' will invalidate the standard input
  402. streams of all but one running command.  This means that attempting to
  403. read from standard input will usually be a fatal error (a `Broken pipe'
  404. signal) for most child processes if there are several.
  405.  
  406.    It is unpredictable which command will have a valid standard input
  407. stream (which will come from the terminal, or wherever you redirect the
  408. standard input of `make').  The first command run will always get it
  409. first, and the first command started after that one finishes will get
  410. it next, and so on.
  411.  
  412.    We will change how this aspect of `make' works if we find a better
  413. alternative.  In the mean time, you should not rely on any command using
  414. standard input at all if you are using the parallel execution feature;
  415. but if you are not using this feature, then standard input works
  416. normally in all commands.
  417.  
  418.    If a command fails (is killed by a signal or exits with a nonzero
  419. status), and errors are not ignored for that command (*note Errors in
  420. Commands: Errors.), the remaining command lines to remake the same
  421. target will not be run.  If a command fails and the `-k' or
  422. `--keep-going' option was not given (*note Summary of Options: Options
  423. Summary.), `make' aborts execution.  If make terminates for any reason
  424. (including a signal) with child processes running, it waits for them to
  425. finish before actually exiting.
  426.  
  427.    When the system is heavily loaded, you will probably want to run
  428. fewer jobs than when it is lightly loaded.  You can use the `-l' option
  429. to tell `make' to limit the number of jobs to run at once, based on the
  430. load average.  The `-l' or `--max-load' option is followed by a
  431. floating-point number.  For example,
  432.  
  433.      -l 2.5
  434.  
  435. will not let `make' start more than one job if the load average is
  436. above 2.5.  The `-l' option with no following number removes the load
  437. limit, if one was given with a previous `-l' option.
  438.  
  439.    More precisely, when `make' goes to start up a job, and it already
  440. has at least one job running, it checks the current load average; if it
  441. is not lower than the limit given with `-l', `make' waits until the load
  442. average goes below that limit, or until all the other jobs finish.
  443.  
  444.    By default, there is no load limit.
  445.  
  446. 
  447. File: make.info,  Node: Errors,  Next: Interrupts,  Prev: Parallel,  Up: Commands
  448.  
  449. Errors in Commands
  450. ==================
  451.  
  452.    After each shell command returns, `make' looks at its exit status.
  453. If the command completed successfully, the next command line is executed
  454. in a new shell; after the last command line is finished, the rule is
  455. finished.
  456.  
  457.    If there is an error (the exit status is nonzero), `make' gives up on
  458. the current rule, and perhaps on all rules.
  459.  
  460.    Sometimes the failure of a certain command does not indicate a
  461. problem.  For example, you may use the `mkdir' command to ensure that a
  462. directory exists.  If the directory already exists, `mkdir' will report
  463. an error, but you probably want `make' to continue regardless.
  464.  
  465.    To ignore errors in a command line, write a `-' at the beginning of
  466. the line's text (after the initial tab).  The `-' is discarded before
  467. the command is passed to the shell for execution.
  468.  
  469.    For example,
  470.  
  471.      clean:
  472.              -rm -f *.o
  473.  
  474. This causes `rm' to continue even if it is unable to remove a file.
  475.  
  476.    When you run `make' with the `-i' or `--ignore-errors' flag, errors
  477. are ignored in all commands of all rules.  A rule in the makefile for
  478. the special target `.IGNORE' has the same effect.  These ways of
  479. ignoring errors are obsolete because `-' is more flexible.
  480.  
  481.    When errors are to be ignored, because of either a `-' or the `-i'
  482. flag, `make' treats an error return just like success, except that it
  483. prints out a message that tells you the status code the command exited
  484. with, and says that the error has been ignored.
  485.  
  486.    When an error happens that `make' has not been told to ignore, it
  487. implies that the current target cannot be correctly remade, and neither
  488. can any other that depends on it either directly or indirectly.  No
  489. further commands will be executed for these targets, since their
  490. preconditions have not been achieved.
  491.  
  492.    Normally `make' gives up immediately in this circumstance, returning
  493. a nonzero status.  However, if the `-k' or `--keep-going' flag is
  494. specified, `make' continues to consider the other dependencies of the
  495. pending targets, remaking them if necessary, before it gives up and
  496. returns nonzero status.  For example, after an error in compiling one
  497. object file, `make -k' will continue compiling other object files even
  498. though it already knows that linking them will be impossible.  *Note
  499. Summary of Options: Options Summary.
  500.  
  501.    The usual behavior assumes that your purpose is to get the specified
  502. targets up to date; once `make' learns that this is impossible, it
  503. might as well report the failure immediately.  The `-k' option says
  504. that the real purpose is to test as many of the changes made in the
  505. program as possible, perhaps to find several independent problems so
  506. that you can correct them all before the next attempt to compile.  This
  507. is why Emacs' `compile' command passes the `-k' flag by default.
  508.  
  509. 
  510. File: make.info,  Node: Interrupts,  Next: Recursion,  Prev: Errors,  Up: Commands
  511.  
  512. Interrupting or Killing `make'
  513. ==============================
  514.  
  515.    If `make' gets a fatal signal while a command is executing, it may
  516. delete the target file that the command was supposed to update.  This is
  517. done if the target file's last-modification time has changed since
  518. `make' first checked it.
  519.  
  520.    The purpose of deleting the target is to make sure that it is remade
  521. from scratch when `make' is next run.  Why is this?  Suppose you type
  522. `Ctrl-c' while a compiler is running, and it has begun to write an
  523. object file `foo.o'.  The `Ctrl-c' kills the compiler, resulting in an
  524. incomplete file whose last-modification time is newer than the source
  525. file `foo.c'.  But `make' also receives the `Ctrl-c' signal and deletes
  526. this incomplete file.  If `make' did not do this, the next invocation
  527. of `make' would think that `foo.o' did not require updating--resulting
  528. in a strange error message from the linker when it tries to link an
  529. object file half of which is missing.
  530.  
  531.    You can prevent the deletion of a target file in this way by making
  532. the special target `.PRECIOUS' depend on it.  Before remaking a target,
  533. `make' checks to see whether it appears on the dependencies of
  534. `.PRECIOUS', and thereby decides whether the target should be deleted
  535. if a signal happens.  Some reasons why you might do this are that the
  536. target is updated in some atomic fashion, or exists only to record a
  537. modification-time (its contents do not matter), or must exist at all
  538. times to prevent other sorts of trouble.
  539.  
  540. 
  541. File: make.info,  Node: Recursion,  Next: Sequences,  Prev: Interrupts,  Up: Commands
  542.  
  543. Recursive Use of `make'
  544. =======================
  545.  
  546.    Recursive use of `make' means using `make' as a command in a
  547. makefile.  This technique is useful when you want separate makefiles for
  548. various subsystems that compose a larger system.  For example, suppose
  549. you have a subdirectory `subdir' which has its own makefile, and you
  550. would like the containing directory's makefile to run `make' on the
  551. subdirectory.  You can do it by writing this:
  552.  
  553.      subsystem:
  554.              cd subdir; $(MAKE)
  555.  
  556. or, equivalently, this (*note Summary of Options: Options Summary.):
  557.  
  558.      subsystem:
  559.              $(MAKE) -C subdir
  560.  
  561.    You can write recursive `make' commands just by copying this example,
  562. but there are many things to know about how they work and why, and about
  563. how the sub-`make' relates to the top-level `make'.
  564.  
  565. * Menu:
  566.  
  567. * MAKE Variable::               The special effects of using `$(MAKE)'.
  568. * Variables/Recursion::         How to communicate variables to a sub-`make'.
  569. * Options/Recursion::           How to communicate options to a sub-`make'.
  570. * -w Option::                   How the `-w' or `--print-directory' option
  571.                                  helps debug use of recursive `make' commands.
  572.  
  573. 
  574. File: make.info,  Node: MAKE Variable,  Next: Variables/Recursion,  Up: Recursion
  575.  
  576. How the `MAKE' Variable Works
  577. -----------------------------
  578.  
  579.    Recursive `make' commands should always use the variable `MAKE', not
  580. the explicit command name `make', as shown here:
  581.  
  582.      subsystem:
  583.              cd subdir; $(MAKE)
  584.  
  585.    The value of this variable is the file name with which `make' was
  586. invoked.  If this file name was `/bin/make', then the command executed
  587. is `cd subdir; /bin/make'.  If you use a special version of `make' to
  588. run the top-level makefile, the same special version will be executed
  589. for recursive invocations.
  590.  
  591.    Also, any arguments that define variable values are added to `MAKE',
  592. so the sub-`make' gets them too.  Thus, if you do `make CFLAGS=-O', so
  593. that all C compilations will be optimized, the sub-`make' is run with
  594. `cd subdir; /bin/make CFLAGS=-O'.
  595.  
  596.    The `MAKE' variable actually just refers to two other variables
  597. which contain these special values.  In fact, `MAKE' is always defined
  598. as `$(MAKE_COMMAND) $(MAKEOVERRIDES)'.  The variable `MAKE_COMMAND' is
  599. the file name with which `make' was invoked (such as `/bin/make',
  600. above).  The variable `MAKEOVERRIDES' contains definitions for the
  601. variables defined on the command line; in the above example, its value
  602. is `CFLAGS=-O'.  If you *do not* want these variable definitions done
  603. in all recursive `make' invocations, you can redefine the
  604. `MAKEOVERRIDES' variable to remove them.  You do this in any of the
  605. normal ways for defining variables: in a makefile (*note Setting
  606. Variables: Setting.); on the command line with an argument like
  607. `MAKEOVERRIDES=' (*note Overriding Variables: Overriding.); or with an
  608. environment variable (*note Variables from the Environment:
  609. Environment.).
  610.  
  611.    As a special feature, using the variable `MAKE' in the commands of a
  612. rule alters the effects of the `-t' (`--touch'), `-n' (`--just-print'),
  613. or `-q' (`--question') option.  Using the `MAKE' variable has the same
  614. effect as using a `+' character at the beginning of the command line.
  615. *Note Instead of Executing the Commands: Instead of Execution.
  616.  
  617.    Consider the command `make -t' in the above example.  (The `-t'
  618. option marks targets as up to date without actually running any
  619. commands; see *Note Instead of Execution::.)  Following the usual
  620. definition of `-t', a `make -t' command in the example would create a
  621. file named `subsystem' and do nothing else.  What you really want it to
  622. do is run `cd subdir; make -t'; but that would require executing the
  623. command, and `-t' says not to execute commands.
  624.  
  625.    The special feature makes this do what you want: whenever a command
  626. line of a rule contains the variable `MAKE', the flags `-t', `-n' and
  627. `-q' do not apply to that line.  Command lines containing `MAKE' are
  628. executed normally despite the presence of a flag that causes most
  629. commands not to be run.  The usual `MAKEFLAGS' mechanism passes the
  630. flags to the sub-`make' (*note Communicating Options to a Sub-`make':
  631. Options/Recursion.), so your request to touch the files, or print the
  632. commands, is propagated to the subsystem.
  633.  
  634. 
  635. File: make.info,  Node: Variables/Recursion,  Next: Options/Recursion,  Prev: MAKE Variable,  Up: Recursion
  636.  
  637. Communicating Variables to a Sub-`make'
  638. ---------------------------------------
  639.  
  640.    Variable values of the top-level `make' can be passed to the
  641. sub-`make' through the environment by explicit request.  These
  642. variables are defined in the sub-`make' as defaults, but do not
  643. override what is specified in the sub-`make''s makefile unless you use
  644. the `-e' switch (*note Summary of Options: Options Summary.).
  645.  
  646.    To pass down, or "export", a variable, `make' adds the variable and
  647. its value to the environment for running each command.  The sub-`make',
  648. in turn, uses the environment to initialize its table of variable
  649. values.  *Note Variables from the Environment: Environment.
  650.  
  651.    Except by explicit request, `make' exports a variable only if it is
  652. either defined in the environment initially or set on the command line,
  653. and if its name consists only of letters, numbers, and underscores.
  654. Some shells cannot cope with environment variable names consisting of
  655. characters other than letters, numbers, and underscores.
  656.  
  657.    The special variables `SHELL' and `MAKEFLAGS' are always exported.
  658. `MAKEFILES' is exported if you set it to anything.
  659.  
  660.    Variables are *not* normally passed down if they were created by
  661. default by `make' (*note Variables Used by Implicit Rules: Implicit
  662. Variables.).  The sub-`make' will define these for itself.
  663.  
  664.    If you want to export specific variables to a sub-`make', use the
  665. `export' directive, like this:
  666.  
  667.      export VARIABLE ...
  668.  
  669. If you want to *prevent* a variable from being exported, use the
  670. `unexport' directive, like this:
  671.  
  672.      unexport VARIABLE ...
  673.  
  674. As a convenience, you can define a variable and export it at the same
  675. time by doing:
  676.  
  677.      export VARIABLE = value
  678.  
  679. has the same result as:
  680.  
  681.      VARIABLE = value
  682.      export VARIABLE
  683.  
  684. and
  685.  
  686.      export VARIABLE := value
  687.  
  688. has the same result as:
  689.  
  690.      VARIABLE := value
  691.      export VARIABLE
  692.  
  693.    Likewise,
  694.  
  695.      export VARIABLE += value
  696.  
  697. is just like:
  698.  
  699.      VARIABLE += value
  700.      export VARIABLE
  701.  
  702. *Note Appending More Text to Variables: Appending.
  703.  
  704.    You may notice that the `export' and `unexport' directives work in
  705. `make' in the same way they work in the shell, `sh'.
  706.  
  707.    If you want all variables to be exported by default, you can use
  708. `export' by itself:
  709.  
  710.      export
  711.  
  712. This tells `make' that variables which are not explicitly mentioned in
  713. an `export' or `unexport' directive should be exported.  Any variable
  714. given in an `unexport' directive will still *not* be exported.  If you
  715. use `export' by itself to export variables by default, variables whose
  716. names contain characters other than alphanumerics and underscores will
  717. not be exported unless specifically mentioned in an `export' directive.
  718.  
  719.    The behavior elicited by an `export' directive by itself was the
  720. default in older versions of GNU `make'.  If your makefiles depend on
  721. this behavior and you want to be compatible with old versions of
  722. `make', you can write a rule for the special target
  723. `.EXPORT_ALL_VARIABLES' instead of using the `export' directive.  This
  724. will be ignored by old `make's, while the `export' directive will cause
  725. a syntax error.
  726.  
  727.    Likewise, you can use `unexport' by itself to tell `make' *not* to
  728. export variables by default.  Since this is the default behavior, you
  729. would only need to do this if `export' had been used by itself earlier
  730. (in an included makefile, perhaps).  You *cannot* use `export' and
  731. `unexport' by themselves to have variables exported for some commands
  732. and not for others.  The last `export' or `unexport' directive that
  733. appears by itself determines the behavior for the entire run of `make'.
  734.  
  735.    As a special feature, the variable `MAKELEVEL' is changed when it is
  736. passed down from level to level.  This variable's value is a string
  737. which is the depth of the level as a decimal number.  The value is `0'
  738. for the top-level `make'; `1' for a sub-`make', `2' for a
  739. sub-sub-`make', and so on.  The incrementation happens when `make' sets
  740. up the environment for a command.
  741.  
  742.    The main use of `MAKELEVEL' is to test it in a conditional directive
  743. (*note Conditional Parts of Makefiles: Conditionals.); this way you can
  744. write a makefile that behaves one way if run recursively and another
  745. way if run directly by you.
  746.  
  747.    You can use the variable `MAKEFILES' to cause all sub-`make'
  748. commands to use additional makefiles.  The value of `MAKEFILES' is a
  749. whitespace-separated list of file names.  This variable, if defined in
  750. the outer-level makefile, is passed down through the environment; then
  751. it serves as a list of extra makefiles for the sub-`make' to read
  752. before the usual or specified ones.  *Note The Variable `MAKEFILES':
  753. MAKEFILES Variable.
  754.  
  755. 
  756. File: make.info,  Node: Options/Recursion,  Next: -w Option,  Prev: Variables/Recursion,  Up: Recursion
  757.  
  758. Communicating Options to a Sub-`make'
  759. -------------------------------------
  760.  
  761.    Flags such as `-s' and `-k' are passed automatically to the
  762. sub-`make' through the variable `MAKEFLAGS'.  This variable is set up
  763. automatically by `make' to contain the flag letters that `make'
  764. received.  Thus, if you do `make -ks' then `MAKEFLAGS' gets the value
  765. `ks'.
  766.  
  767.    As a consequence, every sub-`make' gets a value for `MAKEFLAGS' in
  768. its environment.  In response, it takes the flags from that value and
  769. processes them as if they had been given as arguments.  *Note Summary
  770. of Options: Options Summary.
  771.  
  772.    The options `-C', `-f', `-I', `-o', and `-W' are not put into
  773. `MAKEFLAGS'; these options are not passed down.
  774.  
  775.    The `-j' option is a special case (*note Parallel Execution:
  776. Parallel.).  If you set it to some numeric value, `-j 1' is always put
  777. into `MAKEFLAGS' instead of the value you specified.  This is because if
  778. the `-j' option were passed down to sub-`make's, you would get many
  779. more jobs running in parallel than you asked for.  If you give `-j'
  780. with no numeric argument, meaning to run as many jobs as possible in
  781. parallel, this is passed down, since multiple infinities are no more
  782. than one.
  783.  
  784.    If you do not want to pass the other flags down, you must change the
  785. value of `MAKEFLAGS', like this:
  786.  
  787.      MAKEFLAGS=
  788.      subsystem:
  789.              cd subdir; $(MAKE)
  790.  
  791.    or like this:
  792.  
  793.      subsystem:
  794.              cd subdir; $(MAKE) MAKEFLAGS=
  795.  
  796.    A similar variable `MFLAGS' exists also, for historical
  797. compatibility.  It has the same value as `MAKEFLAGS' except that a
  798. hyphen is added at the beginning if it is not empty.  `MFLAGS' was
  799. traditionally used explicitly in the recursive `make' command, like
  800. this:
  801.  
  802.      subsystem:
  803.              cd subdir; $(MAKE) $(MFLAGS)
  804.  
  805. but now `MAKEFLAGS' makes this usage redundant.
  806.  
  807.    The `MAKEFLAGS' and `MFLAGS' variables can also be useful if you
  808. want to have certain options, such as `-k' (*note Summary of Options:
  809. Options Summary.) set each time you run `make'.  Just put `MAKEFLAGS=k'
  810. or `MFLAGS=-k' in your environment.  These variables may also be set in
  811. makefiles, so a makefile can specify additional flags that should also
  812. be in effect for that makefile.
  813.  
  814.    If you do put `MAKEFLAGS' or `MFLAGS' in your environment, you
  815. should be sure not to include any options that will drastically affect
  816. the actions of `make' and undermine the purpose of makefiles and of
  817. `make' itself.  For instance, the `-t', `-n', and `-q' options, if put
  818. in one of these variables, could have disastrous consequences and would
  819. certainly have at least surprising and probably annoying effects.
  820.  
  821. 
  822. File: make.info,  Node: -w Option,  Prev: Options/Recursion,  Up: Recursion
  823.  
  824. The `--print-directory' Option
  825. ------------------------------
  826.  
  827.    If you use several levels of recursive `make' invocations, the `-w'
  828. or `--print-directory' option can make the output a lot easier to
  829. understand by showing each directory as `make' starts processing it and
  830. as `make' finishes processing it.  For example, if `make -w' is run in
  831. the directory `/u/gnu/make', `make' will print a line of the form:
  832.  
  833.      make: Entering directory `/u/gnu/make'.
  834.  
  835. before doing anything else, and a line of the form:
  836.  
  837.      make: Leaving directory `/u/gnu/make'.
  838.  
  839. when processing is completed.
  840.  
  841.    Normally, you do not need to specify this option because `make' does
  842. it for you: `-w' is turned on automatically when you use the `-C'
  843. option, and in sub-`make's.  `make' will not automatically turn on `-w'
  844. if you also use `-s', which says to be silent, or if you use
  845. `--no-print-directory' to explicitly disable it.
  846.  
  847. 
  848. File: make.info,  Node: Sequences,  Next: Empty Commands,  Prev: Recursion,  Up: Commands
  849.  
  850. Defining Canned Command Sequences
  851. =================================
  852.  
  853.    When the same sequence of commands is useful in making various
  854. targets, you can define it as a canned sequence with the `define'
  855. directive, and refer to the canned sequence from the rules for those
  856. targets.  The canned sequence is actually a variable, so the name must
  857. not conflict with other variable names.
  858.  
  859.    Here is an example of defining a canned sequence of commands:
  860.  
  861.      define run-yacc
  862.      yacc $(firstword $^)
  863.      mv y.tab.c $@
  864.      endef
  865.  
  866. Here `run-yacc' is the name of the variable being defined; `endef'
  867. marks the end of the definition; the lines in between are the commands.
  868. The `define' directive does not expand variable references and
  869. function calls in the canned sequence; the `$' characters, parentheses,
  870. variable names, and so on, all become part of the value of the variable
  871. you are defining.  *Note Defining Variables Verbatim: Defining, for a
  872. complete explanation of `define'.
  873.  
  874.    The first command in this example runs Yacc on the first dependency
  875. of whichever rule uses the canned sequence.  The output file from Yacc
  876. is always named `y.tab.c'.  The second command moves the output to the
  877. rule's target file name.
  878.  
  879.    To use the canned sequence, substitute the variable into the
  880. commands of a rule.  You can substitute it like any other variable
  881. (*note Basics of Variable References: Reference.).  Because variables
  882. defined by `define' are recursively expanded variables, all the
  883. variable references you wrote inside the `define' are expanded now.
  884. For example:
  885.  
  886.      foo.c : foo.y
  887.              $(run-yacc)
  888.  
  889. `foo.y' will be substituted for the variable `$^' when it occurs in
  890. `run-yacc''s value, and `foo.c' for `$@'.
  891.  
  892.    This is a realistic example, but this particular one is not needed in
  893. practice because `make' has an implicit rule to figure out these
  894. commands based on the file names involved (*note Using Implicit Rules:
  895. Implicit Rules.).
  896.  
  897.    In command execution, each line of a canned sequence is treated just
  898. as if the line appeared on its own in the rule, preceded by a tab.  In
  899. particular, `make' invokes a separate subshell for each line.  You can
  900. use the special prefix characters that affect command lines (`@', `-',
  901. and `+') on each line of a canned sequence.  *Note Writing the Commands
  902. in Rules: Commands.  For example, using this canned sequence:
  903.  
  904.      define frobnicate
  905.      @echo "frobnicating target $@"
  906.      frob-step-1 $< -o $@-step-1
  907.      frob-step-2 $@-step-1 -o $@
  908.      endef
  909.  
  910. `make' will not echo the first line, the `echo' command.  But it *will*
  911. echo the following two command lines.
  912.  
  913.    On the other hand, prefix characters on the command line that refers
  914. to a canned sequence apply to every line in the sequence.  So the rule:
  915.  
  916.      frob.out: frob.in
  917.          @$(frobnicate)
  918.  
  919. does not echo *any* commands.  (*Note Command Echoing: Echoing, for a
  920. full explanation of `@'.)
  921.  
  922. 
  923. File: make.info,  Node: Empty Commands,  Prev: Sequences,  Up: Commands
  924.  
  925. Using Empty Commands
  926. ====================
  927.  
  928.    It is sometimes useful to define commands which do nothing.  This is
  929. done simply by giving a command that consists of nothing but
  930. whitespace.  For example:
  931.  
  932.      target: ;
  933.  
  934. defines an empty command string for `target'.  You could also use a
  935. line beginning with a tab character to define an empty command string,
  936. but this would be confusing because such a line looks empty.
  937.  
  938.    You may be wondering why you would want to define a command string
  939. that does nothing.  The only reason this is useful is to prevent a
  940. target from getting implicit commands (from implicit rules or the
  941. `.DEFAULT' special target; *note Implicit Rules::. and *note Defining
  942. Last-Resort Default Rules: Last Resort.).
  943.  
  944.    You may be inclined to define empty command strings for targets that
  945. are not actual files, but only exist so that their dependencies can be
  946. remade.  However, this is not the best way to do that, because the
  947. dependencies may not be remade properly if the target file actually
  948. does exist.  *Note Phony Targets: Phony Targets, for a better way to do
  949. this.
  950.  
  951. 
  952. File: make.info,  Node: Using Variables,  Next: Conditionals,  Prev: Commands,  Up: Top
  953.  
  954. How to Use Variables
  955. ********************
  956.  
  957.    A "variable" is a name defined in a makefile to represent a string
  958. of text, called the variable's "value".  These values are substituted
  959. by explicit request into targets, dependencies, commands, and other
  960. parts of the makefile.  (In some other versions of `make', variables
  961. are called "macros".)
  962.  
  963.    Variables and functions in all parts of a makefile are expanded when
  964. read, except for the shell commands in rules, the right-hand sides of
  965. variable definitions using `=', and the bodies of variable definitions
  966. using the `define' directive.
  967.  
  968.    Variables can represent lists of file names, options to pass to
  969. compilers, programs to run, directories to look in for source files,
  970. directories to write output in, or anything else you can imagine.
  971.  
  972.    A variable name may be any sequence of characters not containing `:',
  973. `#', `=', or leading or trailing whitespace.  However, variable names
  974. containing characters other than letters, numbers, and underscores
  975. should be avoided, as they may be given special meanings in the future,
  976. and with some shells they cannot be passed through the environment to a
  977. sub-`make' (*note Communicating Variables to a Sub-`make':
  978. Variables/Recursion.).
  979.  
  980.    It is traditional to use upper case letters in variable names, but we
  981. recommend using lower case letters for variable names that serve
  982. internal purposes in the makefile, and reserving upper case for
  983. parameters that control implicit rules or for parameters that the user
  984. should override with command options (*note Overriding Variables:
  985. Overriding.).
  986.  
  987. * Menu:
  988.  
  989. * Reference::                   How to use the value of a variable.
  990. * Flavors::                     Variables come in two flavors.
  991. * Advanced::                    Advanced features for referencing a variable.
  992. * Values::                      All the ways variables get their values.
  993. * Setting::                     How to set a variable in the makefile.
  994. * Appending::                   How to append more text to the old value
  995.                                   of a variable.
  996. * Override Directive::          How to set a variable in the makefile even if
  997.                                   the user has set it with a command argument.
  998. * Defining::                    An alternate way to set a variable
  999.                                   to a verbatim string.
  1000. * Environment::                 Variable values can come from the environment.
  1001.  
  1002. 
  1003. File: make.info,  Node: Reference,  Next: Flavors,  Up: Using Variables
  1004.  
  1005. Basics of Variable References
  1006. =============================
  1007.  
  1008.    To substitute a variable's value, write a dollar sign followed by
  1009. the name of the variable in parentheses or braces: either `$(foo)' or
  1010. `${foo}' is a valid reference to the variable `foo'.  This special
  1011. significance of `$' is why you must write `$$' to have the effect of a
  1012. single dollar sign in a file name or command.
  1013.  
  1014.    Variable references can be used in any context: targets,
  1015. dependencies, commands, most directives, and new variable values.  Here
  1016. is an example of a common case, where a variable holds the names of all
  1017. the object files in a program:
  1018.  
  1019.      objects = program.o foo.o utils.o
  1020.      program : $(objects)
  1021.              cc -o program $(objects)
  1022.      
  1023.      $(objects) : defs.h
  1024.  
  1025.    Variable references work by strict textual substitution.  Thus, the
  1026. rule
  1027.  
  1028.      foo = c
  1029.      prog.o : prog.$(foo)
  1030.              $(foo)$(foo) -$(foo) prog.$(foo)
  1031.  
  1032. could be used to compile a C program `prog.c'.  Since spaces before the
  1033. variable value are ignored in variable assignments, the value of `foo'
  1034. is precisely `c'.  (Don't actually write your makefiles this way!)
  1035.  
  1036.    A dollar sign followed by a character other than a dollar sign,
  1037. open-parenthesis or open-brace treats that single character as the
  1038. variable name.  Thus, you could reference the variable `x' with `$x'.
  1039. However, this practice is strongly discouraged, except in the case of
  1040. the automatic variables (*note Automatic Variables: Automatic.).
  1041.  
  1042. 
  1043. File: make.info,  Node: Flavors,  Next: Advanced,  Prev: Reference,  Up: Using Variables
  1044.  
  1045. The Two Flavors of Variables
  1046. ============================
  1047.  
  1048.    There are two ways that a variable in GNU `make' can have a value;
  1049. we call them the two "flavors" of variables.  The two flavors are
  1050. distinguished in how they are defined and in what they do when expanded.
  1051.  
  1052.    The first flavor of variable is a "recursively expanded" variable.
  1053. Variables of this sort are defined by lines using `=' (*note Setting
  1054. Variables: Setting.) or by the `define' directive (*note Defining
  1055. Variables Verbatim: Defining.).  The value you specify is installed
  1056. verbatim; if it contains references to other variables, these
  1057. references are expanded whenever this variable is substituted (in the
  1058. course of expanding some other string).  When this happens, it is
  1059. called "recursive expansion".
  1060.  
  1061.    For example,
  1062.  
  1063.      foo = $(bar)
  1064.      bar = $(ugh)
  1065.      ugh = Huh?
  1066.      
  1067.      all:;echo $(foo)
  1068.  
  1069. will echo `Huh?': `$(foo)' expands to `$(bar)' which expands to
  1070. `$(ugh)' which finally expands to `Huh?'.
  1071.  
  1072.    This flavor of variable is the only sort supported by other versions
  1073. of `make'.  It has its advantages and its disadvantages.  An advantage
  1074. (most would say) is that:
  1075.  
  1076.      CFLAGS = $(include_dirs) -O
  1077.      include_dirs = -Ifoo -Ibar
  1078.  
  1079. will do what was intended: when `CFLAGS' is expanded in a command, it
  1080. will expand to `-Ifoo -Ibar -O'.  A major disadvantage is that you
  1081. cannot append something on the end of a variable, as in
  1082.  
  1083.      CFLAGS = $(CFLAGS) -O
  1084.  
  1085. because it will cause an infinite loop in the variable expansion.
  1086. (Actually `make' detects the infinite loop and reports an error.)
  1087.  
  1088.    Another disadvantage is that any functions (*note Functions for
  1089. Transforming Text: Functions.) referenced in the definition will be
  1090. executed every time the variable is expanded.  This makes `make' run
  1091. slower; worse, it causes the `wildcard' and `shell' functions to give
  1092. unpredictable results because you cannot easily control when they are
  1093. called, or even how many times.
  1094.  
  1095.    To avoid all the problems and inconveniences of recursively expanded
  1096. variables, there is another flavor: simply expanded variables.
  1097.  
  1098.    "Simply expanded variables" are defined by lines using `:=' (*note
  1099. Setting Variables: Setting.).  The value of a simply expanded variable
  1100. is scanned once and for all, expanding any references to other
  1101. variables and functions, when the variable is defined.  The actual
  1102. value of the simply expanded variable is the result of expanding the
  1103. text that you write.  It does not contain any references to other
  1104. variables; it contains their values *as of the time this variable was
  1105. defined*.  Therefore,
  1106.  
  1107.      x := foo
  1108.      y := $(x) bar
  1109.      x := later
  1110.  
  1111. is equivalent to
  1112.  
  1113.      y := foo bar
  1114.      x := later
  1115.  
  1116.    When a simply expanded variable is referenced, its value is
  1117. substituted verbatim.
  1118.  
  1119.    Here is a somewhat more complicated example, illustrating the use of
  1120. `:=' in conjunction with the `shell' function.  (*Note The `shell'
  1121. Function: Shell Function.)  This example also shows use of the variable
  1122. `MAKELEVEL', which is changed when it is passed down from level to
  1123. level.  (*Note Communicating Variables to a Sub-`make':
  1124. Variables/Recursion, for information about `MAKELEVEL'.)
  1125.  
  1126.      ifeq (0,${MAKELEVEL})
  1127.      cur-dir   := $(shell pwd)
  1128.      whoami    := $(shell whoami)
  1129.      host-type := $(shell arch)
  1130.      MAKE := ${MAKE} host-type=${host-type} whoami=${whoami}
  1131.      endif
  1132.  
  1133. An advantage of this use of `:=' is that a typical `descend into a
  1134. directory' command then looks like this:
  1135.  
  1136.      ${subdirs}:
  1137.            ${MAKE} cur-dir=${cur-dir}/$@ -C $@ all
  1138.  
  1139.    Simply expanded variables generally make complicated makefile
  1140. programming more predictable because they work like variables in most
  1141. programming languages.  They allow you to redefine a variable using its
  1142. own value (or its value processed in some way by one of the expansion
  1143. functions) and to use the expansion functions much more efficiently
  1144. (*note Functions for Transforming Text: Functions.).
  1145.  
  1146.    You can also use them to introduce controlled leading or trailing
  1147. spaces into variable values.  Such spaces are discarded from your input
  1148. before substitution of variable references and function calls; this
  1149. means you can include leading or trailing spaces in a variable value by
  1150. protecting them with variable references, like this:
  1151.  
  1152.      nullstring :=
  1153.      space := $(nullstring) $(nullstring)
  1154.  
  1155. Here the value of the variable `space' is precisely one space.
  1156.  
  1157. 
  1158. File: make.info,  Node: Advanced,  Next: Values,  Prev: Flavors,  Up: Using Variables
  1159.  
  1160. Advanced Features for Reference to Variables
  1161. ============================================
  1162.  
  1163.    This section describes some advanced features you can use to
  1164. reference variables in more flexible ways.
  1165.  
  1166. * Menu:
  1167.  
  1168. * Substitution Refs::           Referencing a variable with
  1169.                                   substitutions on the value.
  1170. * Computed Names::              Computing the name of the variable to refer to.
  1171.  
  1172. 
  1173. File: make.info,  Node: Substitution Refs,  Next: Computed Names,  Up: Advanced
  1174.  
  1175. Substitution References
  1176. -----------------------
  1177.  
  1178.    A "substitution reference" substitutes the value of a variable with
  1179. alterations that you specify.  It has the form `$(VAR:A=B)' (or
  1180. `${VAR:A=B}') and its meaning is to take the value of the variable VAR,
  1181. replace every A at the end of a word with B in that value, and
  1182. substitute the resulting string.
  1183.  
  1184.    When we say "at the end of a word", we mean that A must appear
  1185. either followed by whitespace or at the end of the value in order to be
  1186. replaced; other occurrences of A in the value are unaltered.  For
  1187. example:
  1188.  
  1189.      foo := a.o b.o c.o
  1190.      bar := $(foo:.o=.c)
  1191.  
  1192. sets `bar' to `a.c b.c c.c'.  *Note Setting Variables: Setting.
  1193.  
  1194.    A substitution reference is actually an abbreviation for use of the
  1195. `patsubst' expansion function (*note Functions for String Substitution
  1196. and Analysis: Text Functions.).  We provide substitution references as
  1197. well as `patsubst' for compatibility with other implementations of
  1198. `make'.
  1199.  
  1200.    Another type of substitution reference lets you use the full power of
  1201. the `patsubst' function.  It has the same form `$(VAR:A=B)' described
  1202. above, except that now A must contain a single `%' character.  This
  1203. case is equivalent to `$(patsubst A,B,$(VAR))'.  *Note Functions for
  1204. String Substitution and Analysis: Text Functions, for a description of
  1205. the `patsubst' function.
  1206.  
  1207. For example:
  1208.  
  1209.      foo := a.o b.o c.o
  1210.      bar := $(foo:%.o=%.c)
  1211.  
  1212. sets `bar' to `a.c b.c c.c'.
  1213.  
  1214.