home *** CD-ROM | disk | FTP | other *** search
/ TOS Silver 2000 / TOS Silver 2000.iso / programm / GNU_C++ / LIB / MAKE3761.LZH / info / make.info-3 < prev    next >
Encoding:
GNU Info File  |  1998-07-20  |  51.1 KB  |  1,223 lines

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