home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / make-3.75-bin.lha / info / make.info-2 (.txt) < prev    next >
GNU Info File  |  1996-10-12  |  48KB  |  879 lines

  1. This is Info file make.info, produced by Makeinfo-1.64 from the input
  2. file /ade-src/fsf/make/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.51, last updated 9 May 1996, of `The GNU Make
  7. Manual', for `make', Version 3.75 Beta.
  8.    Copyright (C) 1988, '89, '90, '91, '92, '93, '94, '95, '96
  9. Free Software 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: Rule Syntax,  Next: Wildcards,  Prev: Rule Example,  Up: Rules
  22. Rule Syntax
  23. ===========
  24.    In general, a rule looks like this:
  25.      TARGETS : DEPENDENCIES
  26.              COMMAND
  27.              ...
  28. or like this:
  29.      TARGETS : DEPENDENCIES ; COMMAND
  30.              COMMAND
  31.              ...
  32.    The TARGETS are file names, separated by spaces.  Wildcard
  33. characters may be used (*note Using Wildcard Characters in File Names:
  34. Wildcards.) and a name of the form `A(M)' represents member M in
  35. archive file A (*note Archive Members as Targets: Archive Members.).
  36. Usually there is only one target per rule, but occasionally there is a
  37. reason to have more (*note Multiple Targets in a Rule: Multiple
  38. Targets.).
  39.    The COMMAND lines start with a tab character.  The first command may
  40. appear on the line after the dependencies, with a tab character, or may
  41. appear on the same line, with a semicolon.  Either way, the effect is
  42. the same.  *Note Writing the Commands in Rules: Commands.
  43.    Because dollar signs are used to start variable references, if you
  44. really want a dollar sign in a rule you must write two of them, `$$'
  45. (*note How to Use Variables: Using Variables.).  You may split a long
  46. line by inserting a backslash followed by a newline, but this is not
  47. required, as `make' places no limit on the length of a line in a
  48. makefile.
  49.    A rule tells `make' two things: when the targets are out of date,
  50. and how to update them when necessary.
  51.    The criterion for being out of date is specified in terms of the
  52. DEPENDENCIES, which consist of file names separated by spaces.
  53. (Wildcards and archive members (*note Archives::.) are allowed here
  54. too.) A target is out of date if it does not exist or if it is older
  55. than any of the dependencies (by comparison of last-modification
  56. times).  The idea is that the contents of the target file are computed
  57. based on information in the dependencies, so if any of the dependencies
  58. changes, the contents of the existing target file are no longer
  59. necessarily valid.
  60.    How to update is specified by COMMANDS.  These are lines to be
  61. executed by the shell (normally `sh'), but with some extra features
  62. (*note Writing the Commands in Rules: Commands.).
  63. File: make.info,  Node: Wildcards,  Next: Directory Search,  Prev: Rule Syntax,  Up: Rules
  64. Using Wildcard Characters in File Names
  65. =======================================
  66.    A single file name can specify many files using "wildcard
  67. characters".  The wildcard characters in `make' are `*', `?' and
  68. `[...]', the same as in the Bourne shell.  For example, `*.c' specifies
  69. a list of all the files (in the working directory) whose names end in
  70. `.c'.
  71.    The character `~' at the beginning of a file name also has special
  72. significance.  If alone, or followed by a slash, it represents your home
  73. directory.  For example `~/bin' expands to `/home/you/bin'.  If the `~'
  74. is followed by a word, the string represents the home directory of the
  75. user named by that word.  For example `~john/bin' expands to
  76. `/home/john/bin'.
  77.    Wildcard expansion happens automatically in targets, in dependencies,
  78. and in commands (where the shell does the expansion).  In other
  79. contexts, wildcard expansion happens only if you request it explicitly
  80. with the `wildcard' function.
  81.    The special significance of a wildcard character can be turned off by
  82. preceding it with a backslash.  Thus, `foo\*bar' would refer to a
  83. specific file whose name consists of `foo', an asterisk, and `bar'.
  84. * Menu:
  85. * Wildcard Examples::           Several examples
  86. * Wildcard Pitfall::            Problems to avoid.
  87. * Wildcard Function::           How to cause wildcard expansion where
  88.                                   it does not normally take place.
  89. File: make.info,  Node: Wildcard Examples,  Next: Wildcard Pitfall,  Up: Wildcards
  90. Wildcard Examples
  91. -----------------
  92.    Wildcards can be used in the commands of a rule, where they are
  93. expanded by the shell.  For example, here is a rule to delete all the
  94. object files:
  95.      clean:
  96.              rm -f *.o
  97.    Wildcards are also useful in the dependencies of a rule.  With the
  98. following rule in the makefile, `make print' will print all the `.c'
  99. files that have changed since the last time you printed them:
  100.      print: *.c
  101.              lpr -p $?
  102.              touch print
  103. This rule uses `print' as an empty target file; see *Note Empty Target
  104. Files to Record Events: Empty Targets.  (The automatic variable `$?' is
  105. used to print only those files that have changed; see *Note Automatic
  106. Variables: Automatic.)
  107.    Wildcard expansion does not happen when you define a variable.
  108. Thus, if you write this:
  109.      objects = *.o
  110. then the value of the variable `objects' is the actual string `*.o'.
  111. However, if you use the value of `objects' in a target, dependency or
  112. command, wildcard expansion will take place at that time.  To set
  113. `objects' to the expansion, instead use:
  114.      objects := $(wildcard *.o)
  115. *Note Wildcard Function::.
  116. File: make.info,  Node: Wildcard Pitfall,  Next: Wildcard Function,  Prev: Wildcard Examples,  Up: Wildcards
  117. Pitfalls of Using Wildcards
  118. ---------------------------
  119.    Now here is an example of a naive way of using wildcard expansion,
  120. that does not do what you would intend.  Suppose you would like to say
  121. that the executable file `foo' is made from all the object files in the
  122. directory, and you write this:
  123.      objects = *.o
  124.      
  125.      foo : $(objects)
  126.              cc -o foo $(CFLAGS) $(objects)
  127. The value of `objects' is the actual string `*.o'.  Wildcard expansion
  128. happens in the rule for `foo', so that each *existing* `.o' file
  129. becomes a dependency of `foo' and will be recompiled if necessary.
  130.    But what if you delete all the `.o' files?  When a wildcard matches
  131. no files, it is left as it is, so then `foo' will depend on the
  132. oddly-named file `*.o'.  Since no such file is likely to exist, `make'
  133. will give you an error saying it cannot figure out how to make `*.o'.
  134. This is not what you want!
  135.    Actually it is possible to obtain the desired result with wildcard
  136. expansion, but you need more sophisticated techniques, including the
  137. `wildcard' function and string substitution.  *Note The Function
  138. `wildcard': Wildcard Function.
  139. File: make.info,  Node: Wildcard Function,  Prev: Wildcard Pitfall,  Up: Wildcards
  140. The Function `wildcard'
  141. -----------------------
  142.    Wildcard expansion happens automatically in rules.  But wildcard
  143. expansion does not normally take place when a variable is set, or
  144. inside the arguments of a function.  If you want to do wildcard
  145. expansion in such places, you need to use the `wildcard' function, like
  146. this:
  147.      $(wildcard PATTERN...)
  148. This string, used anywhere in a makefile, is replaced by a
  149. space-separated list of names of existing files that match one of the
  150. given file name patterns.  If no existing file name matches a pattern,
  151. then that pattern is omitted from the output of the `wildcard'
  152. function.  Note that this is different from how unmatched wildcards
  153. behave in rules, where they are used verbatim rather than ignored
  154. (*note Wildcard Pitfall::.).
  155.    One use of the `wildcard' function is to get a list of all the C
  156. source files in a directory, like this:
  157.      $(wildcard *.c)
  158.    We can change the list of C source files into a list of object files
  159. by replacing the `.c' suffix with `.o' in the result, like this:
  160.      $(patsubst %.c,%.o,$(wildcard *.c))
  161. (Here we have used another function, `patsubst'.  *Note Functions for
  162. String Substitution and Analysis: Text Functions.)
  163.    Thus, a makefile to compile all C source files in the directory and
  164. then link them together could be written as follows:
  165.      objects := $(patsubst %.c,%.o,$(wildcard *.c))
  166.      
  167.      foo : $(objects)
  168.              cc -o foo $(objects)
  169. (This takes advantage of the implicit rule for compiling C programs, so
  170. there is no need to write explicit rules for compiling the files.
  171. *Note The Two Flavors of Variables: Flavors, for an explanation of
  172. `:=', which is a variant of `='.)
  173. File: make.info,  Node: Directory Search,  Next: Phony Targets,  Prev: Wildcards,  Up: Rules
  174. Searching Directories for Dependencies
  175. ======================================
  176.    For large systems, it is often desirable to put sources in a separate
  177. directory from the binaries.  The "directory search" features of `make'
  178. facilitate this by searching several directories automatically to find
  179. a dependency.  When you redistribute the files among directories, you
  180. do not need to change the individual rules, just the search paths.
  181. * Menu:
  182. * General Search::              Specifying a search path that applies
  183.                                   to every dependency.
  184. * Selective Search::            Specifying a search path
  185.                                   for a specified class of names.
  186. * Commands/Search::             How to write shell commands that work together
  187.                                   with search paths.
  188. * Implicit/Search::             How search paths affect implicit rules.
  189. * Libraries/Search::            Directory search for link libraries.
  190. File: make.info,  Node: General Search,  Next: Selective Search,  Up: Directory Search
  191. `VPATH': Search Path for All Dependencies
  192. -----------------------------------------
  193.    The value of the `make' variable `VPATH' specifies a list of
  194. directories that `make' should search.  Most often, the directories are
  195. expected to contain dependency files that are not in the current
  196. directory; however, `VPATH' specifies a search list that `make' applies
  197. for all files, including files which are targets of rules.
  198.    Thus, if a file that is listed as a target or dependency does not
  199. exist in the current directory, `make' searches the directories listed
  200. in `VPATH' for a file with that name.  If a file is found in one of
  201. them, that file becomes the dependency.  Rules may then specify the
  202. names of source files in the dependencies as if they all existed in the
  203. current directory.  *Note Writing Shell Commands with Directory Search:
  204. Commands/Search.
  205.    In the `VPATH' variable, directory names are separated by colons or
  206. blanks.  The order in which directories are listed is the order followed
  207. by `make' in its search.
  208.    For example,
  209.      VPATH = src:../headers
  210. specifies a path containing two directories, `src' and `../headers',
  211. which `make' searches in that order.
  212.    With this value of `VPATH', the following rule,
  213.      foo.o : foo.c
  214. is interpreted as if it were written like this:
  215.      foo.o : src/foo.c
  216. assuming the file `foo.c' does not exist in the current directory but
  217. is found in the directory `src'.
  218. File: make.info,  Node: Selective Search,  Next: Commands/Search,  Prev: General Search,  Up: Directory Search
  219. The `vpath' Directive
  220. ---------------------
  221.    Similar to the `VPATH' variable but more selective is the `vpath'
  222. directive (note lower case), which allows you to specify a search path
  223. for a particular class of file names, those that match a particular
  224. pattern.  Thus you can supply certain search directories for one class
  225. of file names and other directories (or none) for other file names.
  226.    There are three forms of the `vpath' directive:
  227. `vpath PATTERN DIRECTORIES'
  228.      Specify the search path DIRECTORIES for file names that match
  229.      PATTERN.
  230.      The search path, DIRECTORIES, is a list of directories to be
  231.      searched, separated by colons or blanks, just like the search path
  232.      used in the `VPATH' variable.
  233. `vpath PATTERN'
  234.      Clear out the search path associated with PATTERN.
  235. `vpath'
  236.      Clear all search paths previously specified with `vpath'
  237.      directives.
  238.    A `vpath' pattern is a string containing a `%' character.  The
  239. string must match the file name of a dependency that is being searched
  240. for, the `%' character matching any sequence of zero or more characters
  241. (as in pattern rules; *note Defining and Redefining Pattern Rules:
  242. Pattern Rules.).  For example, `%.h' matches files that end in `.h'.
  243. (If there is no `%', the pattern must match the dependency exactly,
  244. which is not useful very often.)
  245.    `%' characters in a `vpath' directive's pattern can be quoted with
  246. preceding backslashes (`\').  Backslashes that would otherwise quote
  247. `%' characters can be quoted with more backslashes.  Backslashes that
  248. quote `%' characters or other backslashes are removed from the pattern
  249. before it is compared to file names.  Backslashes that are not in
  250. danger of quoting `%' characters go unmolested.
  251.    When a dependency fails to exist in the current directory, if the
  252. PATTERN in a `vpath' directive matches the name of the dependency file,
  253. then the DIRECTORIES in that directive are searched just like (and
  254. before) the directories in the `VPATH' variable.
  255.    For example,
  256.      vpath %.h ../headers
  257. tells `make' to look for any dependency whose name ends in `.h' in the
  258. directory `../headers' if the file is not found in the current
  259. directory.
  260.    If several `vpath' patterns match the dependency file's name, then
  261. `make' processes each matching `vpath' directive one by one, searching
  262. all the directories mentioned in each directive.  `make' handles
  263. multiple `vpath' directives in the order in which they appear in the
  264. makefile; multiple directives with the same pattern are independent of
  265. each other.
  266.    Thus,
  267.      vpath %.c foo
  268.      vpath %   blish
  269.      vpath %.c bar
  270. will look for a file ending in `.c' in `foo', then `blish', then `bar',
  271. while
  272.      vpath %.c foo:bar
  273.      vpath %   blish
  274. will look for a file ending in `.c' in `foo', then `bar', then `blish'.
  275. File: make.info,  Node: Commands/Search,  Next: Implicit/Search,  Prev: Selective Search,  Up: Directory Search
  276. Writing Shell Commands with Directory Search
  277. --------------------------------------------
  278.    When a dependency is found in another directory through directory
  279. search, this cannot change the commands of the rule; they will execute
  280. as written.  Therefore, you must write the commands with care so that
  281. they will look for the dependency in the directory where `make' finds
  282.    This is done with the "automatic variables" such as `$^' (*note
  283. Automatic Variables: Automatic.).  For instance, the value of `$^' is a
  284. list of all the dependencies of the rule, including the names of the
  285. directories in which they were found, and the value of `$@' is the
  286. target.  Thus:
  287.      foo.o : foo.c
  288.              cc -c $(CFLAGS) $^ -o $@
  289. (The variable `CFLAGS' exists so you can specify flags for C
  290. compilation by implicit rules; we use it here for consistency so it will
  291. affect all C compilations uniformly; *note Variables Used by Implicit
  292. Rules: Implicit Variables..)
  293.    Often the dependencies include header files as well, which you do not
  294. want to mention in the commands.  The automatic variable `$<' is just
  295. the first dependency:
  296.      VPATH = src:../headers
  297.      foo.o : foo.c defs.h hack.h
  298.              cc -c $(CFLAGS) $< -o $@
  299. File: make.info,  Node: Implicit/Search,  Next: Libraries/Search,  Prev: Commands/Search,  Up: Directory Search
  300. Directory Search and Implicit Rules
  301. -----------------------------------
  302.    The search through the directories specified in `VPATH' or with
  303. `vpath' also happens during consideration of implicit rules (*note
  304. Using Implicit Rules: Implicit Rules.).
  305.    For example, when a file `foo.o' has no explicit rule, `make'
  306. considers implicit rules, such as the built-in rule to compile `foo.c'
  307. if that file exists.  If such a file is lacking in the current
  308. directory, the appropriate directories are searched for it.  If `foo.c'
  309. exists (or is mentioned in the makefile) in any of the directories, the
  310. implicit rule for C compilation is applied.
  311.    The commands of implicit rules normally use automatic variables as a
  312. matter of necessity; consequently they will use the file names found by
  313. directory search with no extra effort.
  314. File: make.info,  Node: Libraries/Search,  Prev: Implicit/Search,  Up: Directory Search
  315. Directory Search for Link Libraries
  316. -----------------------------------
  317.    Directory search applies in a special way to libraries used with the
  318. linker.  This special feature comes into play when you write a
  319. dependency whose name is of the form `-lNAME'.  (You can tell something
  320. strange is going on here because the dependency is normally the name of
  321. a file, and the *file name* of the library looks like `libNAME.a', not
  322. like `-lNAME'.)
  323.    When a dependency's name has the form `-lNAME', `make' handles it
  324. specially by searching for the file `libNAME.a' in the current
  325. directory, in directories specified by matching `vpath' search paths
  326. and the `VPATH' search path, and then in the directories `/lib',
  327. `/usr/lib', and `PREFIX/lib' (normally `/usr/local/lib').
  328.    For example,
  329.      foo : foo.c -lcurses
  330.              cc $^ -o $@
  331. would cause the command `cc foo.c /usr/lib/libcurses.a -o foo' to be
  332. executed when `foo' is older than `foo.c' or than
  333. `/usr/lib/libcurses.a'.
  334. File: make.info,  Node: Phony Targets,  Next: Force Targets,  Prev: Directory Search,  Up: Rules
  335. Phony Targets
  336. =============
  337.    A phony target is one that is not really the name of a file.  It is
  338. just a name for some commands to be executed when you make an explicit
  339. request.  There are two reasons to use a phony target: to avoid a
  340. conflict with a file of the same name, and to improve performance.
  341.    If you write a rule whose commands will not create the target file,
  342. the commands will be executed every time the target comes up for
  343. remaking.  Here is an example:
  344.      clean:
  345.              rm *.o temp
  346. Because the `rm' command does not create a file named `clean', probably
  347. no such file will ever exist.  Therefore, the `rm' command will be
  348. executed every time you say `make clean'.
  349.    The phony target will cease to work if anything ever does create a
  350. file named `clean' in this directory.  Since it has no dependencies, the
  351. file `clean' would inevitably be considered up to date, and its
  352. commands would not be executed.  To avoid this problem, you can
  353. explicitly declare the target to be phony, using the special target
  354. `.PHONY' (*note Special Built-in Target Names: Special Targets.) as
  355. follows:
  356.      .PHONY : clean
  357. Once this is done, `make clean' will run the commands regardless of
  358. whether there is a file named `clean'.
  359.    Since it knows that phony targets do not name actual files that
  360. could be remade from other files, `make' skips the implicit rule search
  361. for phony targets (*note Implicit Rules::.).  This is why declaring a
  362. target phony is good for performance, even if you are not worried about
  363. the actual file existing.
  364.    Thus, you first write the line that states that `clean' is a phony
  365. target, then you write the rule, like this:
  366.      .PHONY: clean
  367.      clean:
  368.              rm *.o temp
  369.    A phony target should not be a dependency of a real target file; if
  370. it is, its commands are run every time `make' goes to update that file.
  371. As long as a phony target is never a dependency of a real target, the
  372. phony target commands will be executed only when the phony target is a
  373. specified goal (*note Arguments to Specify the Goals: Goals.).
  374.    Phony targets can have dependencies.  When one directory contains
  375. multiple programs, it is most convenient to describe all of the
  376. programs in one makefile `./Makefile'.  Since the target remade by
  377. default will be the first one in the makefile, it is common to make
  378. this a phony target named `all' and give it, as dependencies, all the
  379. individual programs.  For example:
  380.      all : prog1 prog2 prog3
  381.      .PHONY : all
  382.      
  383.      prog1 : prog1.o utils.o
  384.              cc -o prog1 prog1.o utils.o
  385.      
  386.      prog2 : prog2.o
  387.              cc -o prog2 prog2.o
  388.      
  389.      prog3 : prog3.o sort.o utils.o
  390.              cc -o prog3 prog3.o sort.o utils.o
  391. Now you can say just `make' to remake all three programs, or specify as
  392. arguments the ones to remake (as in `make prog1 prog3').
  393.    When one phony target is a dependency of another, it serves as a
  394. subroutine of the other.  For example, here `make cleanall' will delete
  395. the object files, the difference files, and the file `program':
  396.      .PHONY: cleanall cleanobj cleandiff
  397.      
  398.      cleanall : cleanobj cleandiff
  399.              rm program
  400.      
  401.      cleanobj :
  402.              rm *.o
  403.      
  404.      cleandiff :
  405.              rm *.diff
  406. File: make.info,  Node: Force Targets,  Next: Empty Targets,  Prev: Phony Targets,  Up: Rules
  407. Rules without Commands or Dependencies
  408. ======================================
  409.    If a rule has no dependencies or commands, and the target of the rule
  410. is a nonexistent file, then `make' imagines this target to have been
  411. updated whenever its rule is run.  This implies that all targets
  412. depending on this one will always have their commands run.
  413.    An example will illustrate this:
  414.      clean: FORCE
  415.              rm $(objects)
  416.      FORCE:
  417.    Here the target `FORCE' satisfies the special conditions, so the
  418. target `clean' that depends on it is forced to run its commands.  There
  419. is nothing special about the name `FORCE', but that is one name
  420. commonly used this way.
  421.    As you can see, using `FORCE' this way has the same results as using
  422. `.PHONY: clean'.
  423.    Using `.PHONY' is more explicit and more efficient.  However, other
  424. versions of `make' do not support `.PHONY'; thus `FORCE' appears in
  425. many makefiles.  *Note Phony Targets::.
  426. File: make.info,  Node: Empty Targets,  Next: Special Targets,  Prev: Force Targets,  Up: Rules
  427. Empty Target Files to Record Events
  428. ===================================
  429.    The "empty target" is a variant of the phony target; it is used to
  430. hold commands for an action that you request explicitly from time to
  431. time.  Unlike a phony target, this target file can really exist; but
  432. the file's contents do not matter, and usually are empty.
  433.    The purpose of the empty target file is to record, with its
  434. last-modification time, when the rule's commands were last executed.  It
  435. does so because one of the commands is a `touch' command to update the
  436. target file.
  437.    The empty target file must have some dependencies.  When you ask to
  438. remake the empty target, the commands are executed if any dependency is
  439. more recent than the target; in other words, if a dependency has
  440. changed since the last time you remade the target.  Here is an example:
  441.      print: foo.c bar.c
  442.              lpr -p $?
  443.              touch print
  444. With this rule, `make print' will execute the `lpr' command if either
  445. source file has changed since the last `make print'.  The automatic
  446. variable `$?' is used to print only those files that have changed
  447. (*note Automatic Variables: Automatic.).
  448. File: make.info,  Node: Special Targets,  Next: Multiple Targets,  Prev: Empty Targets,  Up: Rules
  449. Special Built-in Target Names
  450. =============================
  451.    Certain names have special meanings if they appear as targets.
  452. `.PHONY'
  453.      The dependencies of the special target `.PHONY' are considered to
  454.      be phony targets.  When it is time to consider such a target,
  455.      `make' will run its commands unconditionally, regardless of
  456.      whether a file with that name exists or what its last-modification
  457.      time is.  *Note Phony Targets: Phony Targets.
  458. `.SUFFIXES'
  459.      The dependencies of the special target `.SUFFIXES' are the list of
  460.      suffixes to be used in checking for suffix rules.  *Note
  461.      Old-Fashioned Suffix Rules: Suffix Rules.
  462. `.DEFAULT'
  463.      The commands specified for `.DEFAULT' are used for any target for
  464.      which no rules are found (either explicit rules or implicit rules).
  465.      *Note Last Resort::.  If `.DEFAULT' commands are specified, every
  466.      file mentioned as a dependency, but not as a target in a rule,
  467.      will have these commands executed on its behalf.  *Note Implicit
  468.      Rule Search Algorithm: Search Algorithm.
  469. `.PRECIOUS'
  470.      The targets which `.PRECIOUS' depends on are given the following
  471.      special treatment: if `make' is killed or interrupted during the
  472.      execution of their commands, the target is not deleted.  *Note
  473.      Interrupting or Killing `make': Interrupts.  Also, if the target
  474.      is an intermediate file, it will not be deleted after it is no
  475.      longer needed, as is normally done.  *Note Chains of Implicit
  476.      Rules: Chained Rules.
  477.      You can also list the target pattern of an implicit rule (such as
  478.      `%.o') as a dependency file of the special target `.PRECIOUS' to
  479.      preserve intermediate files created by rules whose target patterns
  480.      match that file's name.
  481. `.INTERMEDIATE'
  482.      The targets which `.INTERMEDIATE' depends on are treated as
  483.      intermediate files.  *Note Chains of Implicit Rules: Chained Rules.
  484.      `.INTERMEDIATE' with no dependencies marks all file targets
  485.      mentioned in the makefile as intermediate.
  486. `.SECONDARY'
  487.      The targets which `.SECONDARY' depends on are treated as
  488.      intermediate files, except that they are never automatically
  489.      deleted.  *Note Chains of Implicit Rules: Chained Rules.
  490.      `.SECONDARY' with no dependencies marks all file targets mentioned
  491.      in the makefile as secondary.
  492. `.IGNORE'
  493.      If you specify dependencies for `.IGNORE', then `make' will ignore
  494.      errors in execution of the commands run for those particular
  495.      files.  The commands for `.IGNORE' are not meaningful.
  496.      If mentioned as a target with no dependencies, `.IGNORE' says to
  497.      ignore errors in execution of commands for all files.  This usage
  498.      of `.IGNORE' is supported only for historical compatibility.  Since
  499.      this affects every command in the makefile, it is not very useful;
  500.      we recommend you use the more selective ways to ignore errors in
  501.      specific commands.  *Note Errors in Commands: Errors.
  502. `.SILENT'
  503.      If you specify dependencies for `.SILENT', then `make' will not
  504.      the print commands to remake those particular files before
  505.      executing them.  The commands for `.SILENT' are not meaningful.
  506.      If mentioned as a target with no dependencies, `.SILENT' says not
  507.      to print any commands before executing them.  This usage of
  508.      `.SILENT' is supported only for historical compatibility.  We
  509.      recommend you use the more selective ways to silence specific
  510.      commands.  *Note Command Echoing: Echoing.  If you want to silence
  511.      all commands for a particular run of `make', use the `-s' or
  512.      `--silent' option (*note Options Summary::.).
  513. `.EXPORT_ALL_VARIABLES'
  514.      Simply by being mentioned as a target, this tells `make' to export
  515.      all variables to child processes by default.  *Note Communicating
  516.      Variables to a Sub-`make': Variables/Recursion.
  517.    Any defined implicit rule suffix also counts as a special target if
  518. it appears as a target, and so does the concatenation of two suffixes,
  519. such as `.c.o'.  These targets are suffix rules, an obsolete way of
  520. defining implicit rules (but a way still widely used).  In principle,
  521. any target name could be special in this way if you break it in two and
  522. add both pieces to the suffix list.  In practice, suffixes normally
  523. begin with `.', so these special target names also begin with `.'.
  524. *Note Old-Fashioned Suffix Rules: Suffix Rules.
  525. File: make.info,  Node: Multiple Targets,  Next: Multiple Rules,  Prev: Special Targets,  Up: Rules
  526. Multiple Targets in a Rule
  527. ==========================
  528.    A rule with multiple targets is equivalent to writing many rules,
  529. each with one target, and all identical aside from that.  The same
  530. commands apply to all the targets, but their effects may vary because
  531. you can substitute the actual target name into the command using `$@'.
  532. The rule contributes the same dependencies to all the targets also.
  533.    This is useful in two cases.
  534.    * You want just dependencies, no commands.  For example:
  535.           kbd.o command.o files.o: command.h
  536.      gives an additional dependency to each of the three object files
  537.      mentioned.
  538.    * Similar commands work for all the targets.  The commands do not
  539.      need to be absolutely identical, since the automatic variable `$@'
  540.      can be used to substitute the particular target to be remade into
  541.      the commands (*note Automatic Variables: Automatic.).  For example:
  542.           bigoutput littleoutput : text.g
  543.                   generate text.g -$(subst output,,$@) > $@
  544.      is equivalent to
  545.           bigoutput : text.g
  546.                   generate text.g -big > bigoutput
  547.           littleoutput : text.g
  548.                   generate text.g -little > littleoutput
  549.      Here we assume the hypothetical program `generate' makes two types
  550.      of output, one if given `-big' and one if given `-little'.  *Note
  551.      Functions for String Substitution and Analysis: Text Functions,
  552.      for an explanation of the `subst' function.
  553.    Suppose you would like to vary the dependencies according to the
  554. target, much as the variable `$@' allows you to vary the commands.  You
  555. cannot do this with multiple targets in an ordinary rule, but you can
  556. do it with a "static pattern rule".  *Note Static Pattern Rules: Static
  557. Pattern.
  558. File: make.info,  Node: Multiple Rules,  Next: Static Pattern,  Prev: Multiple Targets,  Up: Rules
  559. Multiple Rules for One Target
  560. =============================
  561.    One file can be the target of several rules.  All the dependencies
  562. mentioned in all the rules are merged into one list of dependencies for
  563. the target.  If the target is older than any dependency from any rule,
  564. the commands are executed.
  565.    There can only be one set of commands to be executed for a file.  If
  566. more than one rule gives commands for the same file, `make' uses the
  567. last set given and prints an error message.  (As a special case, if the
  568. file's name begins with a dot, no error message is printed.  This odd
  569. behavior is only for compatibility with other implementations of
  570. `make'.) There is no reason to write your makefiles this way; that is
  571. why `make' gives you an error message.
  572.    An extra rule with just dependencies can be used to give a few extra
  573. dependencies to many files at once.  For example, one usually has a
  574. variable named `objects' containing a list of all the compiler output
  575. files in the system being made.  An easy way to say that all of them
  576. must be recompiled if `config.h' changes is to write the following:
  577.      objects = foo.o bar.o
  578.      foo.o : defs.h
  579.      bar.o : defs.h test.h
  580.      $(objects) : config.h
  581.    This could be inserted or taken out without changing the rules that
  582. really specify how to make the object files, making it a convenient
  583. form to use if you wish to add the additional dependency intermittently.
  584.    Another wrinkle is that the additional dependencies could be
  585. specified with a variable that you set with a command argument to `make'
  586. (*note Overriding Variables: Overriding.).  For example,
  587.      extradeps=
  588.      $(objects) : $(extradeps)
  589. means that the command `make extradeps=foo.h' will consider `foo.h' as
  590. a dependency of each object file, but plain `make' will not.
  591.    If none of the explicit rules for a target has commands, then `make'
  592. searches for an applicable implicit rule to find some commands *note
  593. Using Implicit Rules: Implicit Rules.).
  594. File: make.info,  Node: Static Pattern,  Next: Double-Colon,  Prev: Multiple Rules,  Up: Rules
  595. Static Pattern Rules
  596. ====================
  597.    "Static pattern rules" are rules which specify multiple targets and
  598. construct the dependency names for each target based on the target name.
  599. They are more general than ordinary rules with multiple targets because
  600. the targets do not have to have identical dependencies.  Their
  601. dependencies must be *analogous*, but not necessarily *identical*.
  602. * Menu:
  603. * Static Usage::                The syntax of static pattern rules.
  604. * Static versus Implicit::      When are they better than implicit rules?
  605. File: make.info,  Node: Static Usage,  Next: Static versus Implicit,  Up: Static Pattern
  606. Syntax of Static Pattern Rules
  607. ------------------------------
  608.    Here is the syntax of a static pattern rule:
  609.      TARGETS ...: TARGET-PATTERN: DEP-PATTERNS ...
  610.              COMMANDS
  611.              ...
  612. The TARGETS list specifies the targets that the rule applies to.  The
  613. targets can contain wildcard characters, just like the targets of
  614. ordinary rules (*note Using Wildcard Characters in File Names:
  615. Wildcards.).
  616.    The TARGET-PATTERN and DEP-PATTERNS say how to compute the
  617. dependencies of each target.  Each target is matched against the
  618. TARGET-PATTERN to extract a part of the target name, called the "stem".
  619. This stem is substituted into each of the DEP-PATTERNS to make the
  620. dependency names (one from each DEP-PATTERN).
  621.    Each pattern normally contains the character `%' just once.  When the
  622. TARGET-PATTERN matches a target, the `%' can match any part of the
  623. target name; this part is called the "stem".  The rest of the pattern
  624. must match exactly.  For example, the target `foo.o' matches the
  625. pattern `%.o', with `foo' as the stem.  The targets `foo.c' and
  626. `foo.out' do not match that pattern.
  627.    The dependency names for each target are made by substituting the
  628. stem for the `%' in each dependency pattern.  For example, if one
  629. dependency pattern is `%.c', then substitution of the stem `foo' gives
  630. the dependency name `foo.c'.  It is legitimate to write a dependency
  631. pattern that does not contain `%'; then this dependency is the same for
  632. all targets.
  633.    `%' characters in pattern rules can be quoted with preceding
  634. backslashes (`\').  Backslashes that would otherwise quote `%'
  635. characters can be quoted with more backslashes.  Backslashes that quote
  636. `%' characters or other backslashes are removed from the pattern before
  637. it is compared to file names or has a stem substituted into it.
  638. Backslashes that are not in danger of quoting `%' characters go
  639. unmolested.  For example, the pattern `the\%weird\\%pattern\\' has
  640. `the%weird\' preceding the operative `%' character, and `pattern\\'
  641. following it.  The final two backslashes are left alone because they
  642. cannot affect any `%' character.
  643.    Here is an example, which compiles each of `foo.o' and `bar.o' from
  644. the corresponding `.c' file:
  645.      objects = foo.o bar.o
  646.      
  647.      $(objects): %.o: %.c
  648.              $(CC) -c $(CFLAGS) $< -o $@
  649. Here `$<' is the automatic variable that holds the name of the
  650. dependency and `$@' is the automatic variable that holds the name of
  651. the target; see *Note Automatic Variables: Automatic.
  652.    Each target specified must match the target pattern; a warning is
  653. issued for each target that does not.  If you have a list of files,
  654. only some of which will match the pattern, you can use the `filter'
  655. function to remove nonmatching file names (*note Functions for String
  656. Substitution and Analysis: Text Functions.):
  657.      files = foo.elc bar.o lose.o
  658.      
  659.      $(filter %.o,$(files)): %.o: %.c
  660.              $(CC) -c $(CFLAGS) $< -o $@
  661.      $(filter %.elc,$(files)): %.elc: %.el
  662.              emacs -f batch-byte-compile $<
  663. In this example the result of `$(filter %.o,$(files))' is `bar.o
  664. lose.o', and the first static pattern rule causes each of these object
  665. files to be updated by compiling the corresponding C source file.  The
  666. result of `$(filter %.elc,$(files))' is `foo.elc', so that file is made
  667. from `foo.el'.
  668.    Another example shows how to use `$*' in static pattern rules:
  669.      bigoutput littleoutput : %output : text.g
  670.              generate text.g -$* > $@
  671. When the `generate' command is run, `$*' will expand to the stem,
  672. either `big' or `little'.
  673. File: make.info,  Node: Static versus Implicit,  Prev: Static Usage,  Up: Static Pattern
  674. Static Pattern Rules versus Implicit Rules
  675. ------------------------------------------
  676.    A static pattern rule has much in common with an implicit rule
  677. defined as a pattern rule (*note Defining and Redefining Pattern Rules:
  678. Pattern Rules.).  Both have a pattern for the target and patterns for
  679. constructing the names of dependencies.  The difference is in how
  680. `make' decides *when* the rule applies.
  681.    An implicit rule *can* apply to any target that matches its pattern,
  682. but it *does* apply only when the target has no commands otherwise
  683. specified, and only when the dependencies can be found.  If more than
  684. one implicit rule appears applicable, only one applies; the choice
  685. depends on the order of rules.
  686.    By contrast, a static pattern rule applies to the precise list of
  687. targets that you specify in the rule.  It cannot apply to any other
  688. target and it invariably does apply to each of the targets specified.
  689. If two conflicting rules apply, and both have commands, that's an error.
  690.    The static pattern rule can be better than an implicit rule for these
  691. reasons:
  692.    * You may wish to override the usual implicit rule for a few files
  693.      whose names cannot be categorized syntactically but can be given
  694.      in an explicit list.
  695.    * If you cannot be sure of the precise contents of the directories
  696.      you are using, you may not be sure which other irrelevant files
  697.      might lead `make' to use the wrong implicit rule.  The choice
  698.      might depend on the order in which the implicit rule search is
  699.      done.  With static pattern rules, there is no uncertainty: each
  700.      rule applies to precisely the targets specified.
  701. File: make.info,  Node: Double-Colon,  Next: Automatic Dependencies,  Prev: Static Pattern,  Up: Rules
  702. Double-Colon Rules
  703. ==================
  704.    "Double-colon" rules are rules written with `::' instead of `:'
  705. after the target names.  They are handled differently from ordinary
  706. rules when the same target appears in more than one rule.
  707.    When a target appears in multiple rules, all the rules must be the
  708. same type: all ordinary, or all double-colon.  If they are
  709. double-colon, each of them is independent of the others.  Each
  710. double-colon rule's commands are executed if the target is older than
  711. any dependencies of that rule.  This can result in executing none, any,
  712. or all of the double-colon rules.
  713.    Double-colon rules with the same target are in fact completely
  714. separate from one another.  Each double-colon rule is processed
  715. individually, just as rules with different targets are processed.
  716.    The double-colon rules for a target are executed in the order they
  717. appear in the makefile.  However, the cases where double-colon rules
  718. really make sense are those where the order of executing the commands
  719. would not matter.
  720.    Double-colon rules are somewhat obscure and not often very useful;
  721. they provide a mechanism for cases in which the method used to update a
  722. target differs depending on which dependency files caused the update,
  723. and such cases are rare.
  724.    Each double-colon rule should specify commands; if it does not, an
  725. implicit rule will be used if one applies.  *Note Using Implicit Rules:
  726. Implicit Rules.
  727. File: make.info,  Node: Automatic Dependencies,  Prev: Double-Colon,  Up: Rules
  728. Generating Dependencies Automatically
  729. =====================================
  730.    In the makefile for a program, many of the rules you need to write
  731. often say only that some object file depends on some header file.  For
  732. example, if `main.c' uses `defs.h' via an `#include', you would write:
  733.      main.o: defs.h
  734. You need this rule so that `make' knows that it must remake `main.o'
  735. whenever `defs.h' changes.  You can see that for a large program you
  736. would have to write dozens of such rules in your makefile.  And, you
  737. must always be very careful to update the makefile every time you add
  738. or remove an `#include'.
  739.    To avoid this hassle, most modern C compilers can write these rules
  740. for you, by looking at the `#include' lines in the source files.
  741. Usually this is done with the `-M' option to the compiler.  For
  742. example, the command:
  743.      cc -M main.c
  744. generates the output:
  745.      main.o : main.c defs.h
  746. Thus you no longer have to write all those rules yourself.  The
  747. compiler will do it for you.
  748.    Note that such a dependency constitutes mentioning `main.o' in a
  749. makefile, so it can never be considered an intermediate file by implicit
  750. rule search.  This means that `make' won't ever remove the file after
  751. using it; *note Chains of Implicit Rules: Chained Rules..
  752.    With old `make' programs, it was traditional practice to use this
  753. compiler feature to generate dependencies on demand with a command like
  754. `make depend'.  That command would create a file `depend' containing
  755. all the automatically-generated dependencies; then the makefile could
  756. use `include' to read them in (*note Include::.).
  757.    In GNU `make', the feature of remaking makefiles makes this practice
  758. obsolete--you need never tell `make' explicitly to regenerate the
  759. dependencies, because it always regenerates any makefile that is out of
  760. date.  *Note Remaking Makefiles::.
  761.    The practice we recommend for automatic dependency generation is to
  762. have one makefile corresponding to each source file.  For each source
  763. file `NAME.c' there is a makefile `NAME.d' which lists what files the
  764. object file `NAME.o' depends on.  That way only the source files that
  765. have changed need to be rescanned to produce the new dependencies.
  766.    Here is the pattern rule to generate a file of dependencies (i.e., a
  767. makefile) called `NAME.d' from a C source file called `NAME.c':
  768.      %.d: %.c
  769.              $(SHELL) -ec '$(CC) -M $(CPPFLAGS) $< \
  770.                            | sed '\''s/\($*\)\.o[ :]*/\1 $@/g'\'' > $@'
  771. *Note Pattern Rules::, for information on defining pattern rules.  The
  772. `-e' flag to the shell makes it exit immediately if the `$(CC)' command
  773. fails (exits with a nonzero status).  Normally the shell exits with the
  774. status of the last command in the pipeline (`sed' in this case), so
  775. `make' would not notice a nonzero status from the compiler.
  776.    With the GNU C compiler, you may wish to use the `-MM' flag instead
  777. of `-M'.  This omits dependencies on system header files.  *Note
  778. Options Controlling the Preprocessor: (gcc.info)Preprocessor Options,
  779. for details.
  780.    The purpose of the `sed' command is to translate (for example):
  781.      main.o : main.c defs.h
  782. into:
  783.      main.o main.d : main.c defs.h
  784. This makes each `.d' file depend on all the source and header files
  785. that the corresponding `.o' file depends on.  `make' then knows it must
  786. regenerate the dependencies whenever any of the source or header files
  787. changes.
  788.    Once you've defined the rule to remake the `.d' files, you then use
  789. the `include' directive to read them all in.  *Note Include::.  For
  790. example:
  791.      sources = foo.c bar.c
  792.      
  793.      include $(sources:.c=.d)
  794. (This example uses a substitution variable reference to translate the
  795. list of source files `foo.c bar.c' into a list of dependency makefiles,
  796. `foo.d bar.d'.  *Note Substitution Refs::, for full information on
  797. substitution references.)  Since the `.d' files are makefiles like any
  798. others, `make' will remake them as necessary with no further work from
  799. you.  *Note Remaking Makefiles::.
  800. File: make.info,  Node: Commands,  Next: Using Variables,  Prev: Rules,  Up: Top
  801. Writing the Commands in Rules
  802. *****************************
  803.    The commands of a rule consist of shell command lines to be executed
  804. one by one.  Each command line must start with a tab, except that the
  805. first command line may be attached to the target-and-dependencies line
  806. with a semicolon in between.  Blank lines and lines of just comments
  807. may appear among the command lines; they are ignored.  (But beware, an
  808. apparently "blank" line that begins with a tab is *not* blank!  It is an
  809. empty command; *note Empty Commands::..)
  810.    Users use many different shell programs, but commands in makefiles
  811. are always interpreted by `/bin/sh' unless the makefile specifies
  812. otherwise.  *Note Command Execution: Execution.
  813.    The shell that is in use determines whether comments can be written
  814. on command lines, and what syntax they use.  When the shell is
  815. `/bin/sh', a `#' starts a comment that extends to the end of the line.
  816. The `#' does not have to be at the beginning of a line.  Text on a line
  817. before a `#' is not part of the comment.
  818. * Menu:
  819. * Echoing::                     How to control when commands are echoed.
  820. * Execution::                   How commands are executed.
  821. * Parallel::                    How commands can be executed in parallel.
  822. * Errors::                      What happens after a command execution error.
  823. * Interrupts::                  What happens when a command is interrupted.
  824. * Recursion::                   Invoking `make' from makefiles.
  825. * Sequences::                   Defining canned sequences of commands.
  826. * Empty Commands::              Defining useful, do-nothing commands.
  827. File: make.info,  Node: Echoing,  Next: Execution,  Up: Commands
  828. Command Echoing
  829. ===============
  830.    Normally `make' prints each command line before it is executed.  We
  831. call this "echoing" because it gives the appearance that you are typing
  832. the commands yourself.
  833.    When a line starts with `@', the echoing of that line is suppressed.
  834. The `@' is discarded before the command is passed to the shell.
  835. Typically you would use this for a command whose only effect is to print
  836. something, such as an `echo' command to indicate progress through the
  837. makefile:
  838.      @echo About to make distribution files
  839.    When `make' is given the flag `-n' or `--just-print', echoing is all
  840. that happens, no execution.  *Note Summary of Options: Options Summary.
  841. In this case and only this case, even the commands starting with `@'
  842. are printed.  This flag is useful for finding out which commands `make'
  843. thinks are necessary without actually doing them.
  844.    The `-s' or `--silent' flag to `make' prevents all echoing, as if
  845. all commands started with `@'.  A rule in the makefile for the special
  846. target `.SILENT' without dependencies has the same effect (*note
  847. Special Built-in Target Names: Special Targets.).  `.SILENT' is
  848. essentially obsolete since `@' is more flexible.
  849. File: make.info,  Node: Execution,  Next: Parallel,  Prev: Echoing,  Up: Commands
  850. Command Execution
  851. =================
  852.    When it is time to execute commands to update a target, they are
  853. executed by making a new subshell for each line.  (In practice, `make'
  854. may take shortcuts that do not affect the results.)
  855.    *Please note:* this implies that shell commands such as `cd' that
  856. set variables local to each process will not affect the following
  857. command lines.  If you want to use `cd' to affect the next command, put
  858. the two on a single line with a semicolon between them.  Then `make'
  859. will consider them a single command and pass them, together, to a shell
  860. which will execute them in sequence.  For example:
  861.      foo : bar/lose
  862.              cd bar; gobble lose > ../foo
  863.    If you would like to split a single shell command into multiple
  864. lines of text, you must use a backslash at the end of all but the last
  865. subline.  Such a sequence of lines is combined into a single line, by
  866. deleting the backslash-newline sequences, before passing it to the
  867. shell.  Thus, the following is equivalent to the preceding example:
  868.      foo : bar/lose
  869.              cd bar;  \
  870.              gobble lose > ../foo
  871.    The program used as the shell is taken from the variable `SHELL'.
  872. By default, the program `/bin/sh' is used.
  873.    Unlike most variables, the variable `SHELL' is never set from the
  874. environment.  This is because the `SHELL' environment variable is used
  875. to specify your personal choice of shell program for interactive use.
  876. It would be very bad for personal choices like this to affect the
  877. functioning of makefiles.  *Note Variables from the Environment:
  878. Environment.
  879.