home *** CD-ROM | disk | FTP | other *** search
/ Gold Fish 2 / goldfish_vol2_cd1.bin / gnu / info / make.info-3 (.txt) < prev    next >
GNU Info File  |  1994-11-12  |  51KB  |  928 lines

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