home *** CD-ROM | disk | FTP | other *** search
/ Openstep 4.2 (Developer) / Openstep Developer 4.2.iso / NextDeveloper / Source / GNU / make / make-3.74 / make.info-3 < prev    next >
Encoding:
GNU Info File  |  1995-08-01  |  49.5 KB  |  1,229 lines

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