home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / GCC / GCC258_3.LHA / gcc / info / make.info-3 < prev    next >
Encoding:
GNU Info File  |  1993-12-07  |  49.5 KB  |  1,262 lines

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