home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / gnu / make-3.70-bin.lha / info / make.info-3 (.txt) < prev    next >
GNU Info File  |  1994-02-21  |  49KB  |  914 lines

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