home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / make-3.75-bin.lha / info / make.info-4 (.txt) < prev    next >
GNU Info File  |  1996-10-12  |  50KB  |  955 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: Override Directive,  Next: Defining,  Prev: Appending,  Up: Using Variables
  22. The `override' Directive
  23. ========================
  24.    If a variable has been set with a command argument (*note Overriding
  25. Variables: Overriding.), then ordinary assignments in the makefile are
  26. ignored.  If you want to set the variable in the makefile even though
  27. it was set with a command argument, you can use an `override'
  28. directive, which is a line that looks like this:
  29.      override VARIABLE = VALUE
  30.      override VARIABLE := VALUE
  31.    To append more text to a variable defined on the command line, use:
  32.      override VARIABLE += MORE TEXT
  33. *Note Appending More Text to Variables: Appending.
  34.    The `override' directive was not invented for escalation in the war
  35. between makefiles and command arguments.  It was invented so you can
  36. alter and add to values that the user specifies with command arguments.
  37.    For example, suppose you always want the `-g' switch when you run the
  38. C compiler, but you would like to allow the user to specify the other
  39. switches with a command argument just as usual.  You could use this
  40. `override' directive:
  41.      override CFLAGS += -g
  42.    You can also use `override' directives with `define' directives.
  43. This is done as you might expect:
  44.      override define foo
  45.      bar
  46.      endef
  47. *Note Defining Variables Verbatim: Defining.
  48. File: make.info,  Node: Defining,  Next: Environment,  Prev: Override Directive,  Up: Using Variables
  49. Defining Variables Verbatim
  50. ===========================
  51. Another way to set the value of a variable is to use the `define'
  52. directive.  This directive has an unusual syntax which allows newline
  53. characters to be included in the value, which is convenient for defining
  54. canned sequences of commands (*note Defining Canned Command Sequences:
  55. Sequences.).
  56.    The `define' directive is followed on the same line by the name of
  57. the variable and nothing more.  The value to give the variable appears
  58. on the following lines.  The end of the value is marked by a line
  59. containing just the word `endef'.  Aside from this difference in
  60. syntax, `define' works just like `=': it creates a recursively-expanded
  61. variable (*note The Two Flavors of Variables: Flavors.).  The variable
  62. name may contain function and variable references, which are expanded
  63. when the directive is read to find the actual variable name to use.
  64.      define two-lines
  65.      echo foo
  66.      echo $(bar)
  67.      endef
  68.    The value in an ordinary assignment cannot contain a newline; but the
  69. newlines that separate the lines of the value in a `define' become part
  70. of the variable's value (except for the final newline which precedes
  71. the `endef' and is not considered part of the value).
  72.    The previous example is functionally equivalent to this:
  73.      two-lines = echo foo; echo $(bar)
  74. since two commands separated by semicolon behave much like two separate
  75. shell commands.  However, note that using two separate lines means
  76. `make' will invoke the shell twice, running an independent subshell for
  77. each line.  *Note Command Execution: Execution.
  78.    If you want variable definitions made with `define' to take
  79. precedence over command-line variable definitions, you can use the
  80. `override' directive together with `define':
  81.      override define two-lines
  82.      foo
  83.      $(bar)
  84.      endef
  85. *Note The `override' Directive: Override Directive.
  86. File: make.info,  Node: Environment,  Prev: Defining,  Up: Using Variables
  87. Variables from the Environment
  88. ==============================
  89.    Variables in `make' can come from the environment in which `make' is
  90. run.  Every environment variable that `make' sees when it starts up is
  91. transformed into a `make' variable with the same name and value.  But
  92. an explicit assignment in the makefile, or with a command argument,
  93. overrides the environment.  (If the `-e' flag is specified, then values
  94. from the environment override assignments in the makefile.  *Note
  95. Summary of Options: Options Summary.  But this is not recommended
  96. practice.)
  97.    Thus, by setting the variable `CFLAGS' in your environment, you can
  98. cause all C compilations in most makefiles to use the compiler switches
  99. you prefer.  This is safe for variables with standard or conventional
  100. meanings because you know that no makefile will use them for other
  101. things.  (But this is not totally reliable; some makefiles set `CFLAGS'
  102. explicitly and therefore are not affected by the value in the
  103. environment.)
  104.    When `make' is invoked recursively, variables defined in the outer
  105. invocation can be passed to inner invocations through the environment
  106. (*note Recursive Use of `make': Recursion.).  By default, only
  107. variables that came from the environment or the command line are passed
  108. to recursive invocations.  You can use the `export' directive to pass
  109. other variables.  *Note Communicating Variables to a Sub-`make':
  110. Variables/Recursion, for full details.
  111.    Other use of variables from the environment is not recommended.  It
  112. is not wise for makefiles to depend for their functioning on
  113. environment variables set up outside their control, since this would
  114. cause different users to get different results from the same makefile.
  115. This is against the whole purpose of most makefiles.
  116.    Such problems would be especially likely with the variable `SHELL',
  117. which is normally present in the environment to specify the user's
  118. choice of interactive shell.  It would be very undesirable for this
  119. choice to affect `make'.  So `make' ignores the environment value of
  120. `SHELL'.
  121. File: make.info,  Node: Conditionals,  Next: Functions,  Prev: Using Variables,  Up: Top
  122. Conditional Parts of Makefiles
  123. ******************************
  124.    A "conditional" causes part of a makefile to be obeyed or ignored
  125. depending on the values of variables.  Conditionals can compare the
  126. value of one variable to another, or the value of a variable to a
  127. constant string.  Conditionals control what `make' actually "sees" in
  128. the makefile, so they *cannot* be used to control shell commands at the
  129. time of execution.
  130. * Menu:
  131. * Conditional Example::         Example of a conditional
  132. * Conditional Syntax::          The syntax of conditionals.
  133. * Testing Flags::               Conditionals that test flags.
  134. File: make.info,  Node: Conditional Example,  Next: Conditional Syntax,  Up: Conditionals
  135. Example of a Conditional
  136. ========================
  137.    The following example of a conditional tells `make' to use one set
  138. of libraries if the `CC' variable is `gcc', and a different set of
  139. libraries otherwise.  It works by controlling which of two command
  140. lines will be used as the command for a rule.  The result is that
  141. `CC=gcc' as an argument to `make' changes not only which compiler is
  142. used but also which libraries are linked.
  143.      libs_for_gcc = -lgnu
  144.      normal_libs =
  145.      
  146.      foo: $(objects)
  147.      ifeq ($(CC),gcc)
  148.              $(CC) -o foo $(objects) $(libs_for_gcc)
  149.      else
  150.              $(CC) -o foo $(objects) $(normal_libs)
  151.      endif
  152.    This conditional uses three directives: one `ifeq', one `else' and
  153. one `endif'.
  154.    The `ifeq' directive begins the conditional, and specifies the
  155. condition.  It contains two arguments, separated by a comma and
  156. surrounded by parentheses.  Variable substitution is performed on both
  157. arguments and then they are compared.  The lines of the makefile
  158. following the `ifeq' are obeyed if the two arguments match; otherwise
  159. they are ignored.
  160.    The `else' directive causes the following lines to be obeyed if the
  161. previous conditional failed.  In the example above, this means that the
  162. second alternative linking command is used whenever the first
  163. alternative is not used.  It is optional to have an `else' in a
  164. conditional.
  165.    The `endif' directive ends the conditional.  Every conditional must
  166. end with an `endif'.  Unconditional makefile text follows.
  167.    As this example illustrates, conditionals work at the textual level:
  168. the lines of the conditional are treated as part of the makefile, or
  169. ignored, according to the condition.  This is why the larger syntactic
  170. units of the makefile, such as rules, may cross the beginning or the
  171. end of the conditional.
  172.    When the variable `CC' has the value `gcc', the above example has
  173. this effect:
  174.      foo: $(objects)
  175.              $(CC) -o foo $(objects) $(libs_for_gcc)
  176. When the variable `CC' has any other value, the effect is this:
  177.      foo: $(objects)
  178.              $(CC) -o foo $(objects) $(normal_libs)
  179.    Equivalent results can be obtained in another way by
  180. conditionalizing a variable assignment and then using the variable
  181. unconditionally:
  182.      libs_for_gcc = -lgnu
  183.      normal_libs =
  184.      
  185.      ifeq ($(CC),gcc)
  186.        libs=$(libs_for_gcc)
  187.      else
  188.        libs=$(normal_libs)
  189.      endif
  190.      
  191.      foo: $(objects)
  192.              $(CC) -o foo $(objects) $(libs)
  193. File: make.info,  Node: Conditional Syntax,  Next: Testing Flags,  Prev: Conditional Example,  Up: Conditionals
  194. Syntax of Conditionals
  195. ======================
  196.    The syntax of a simple conditional with no `else' is as follows:
  197.      CONDITIONAL-DIRECTIVE
  198.      TEXT-IF-TRUE
  199.      endif
  200. The TEXT-IF-TRUE may be any lines of text, to be considered as part of
  201. the makefile if the condition is true.  If the condition is false, no
  202. text is used instead.
  203.    The syntax of a complex conditional is as follows:
  204.      CONDITIONAL-DIRECTIVE
  205.      TEXT-IF-TRUE
  206.      else
  207.      TEXT-IF-FALSE
  208.      endif
  209. If the condition is true, TEXT-IF-TRUE is used; otherwise,
  210. TEXT-IF-FALSE is used instead.  The TEXT-IF-FALSE can be any number of
  211. lines of text.
  212.    The syntax of the CONDITIONAL-DIRECTIVE is the same whether the
  213. conditional is simple or complex.  There are four different directives
  214. that test different conditions.  Here is a table of them:
  215. `ifeq (ARG1, ARG2)'
  216. `ifeq 'ARG1' 'ARG2''
  217. `ifeq "ARG1" "ARG2"'
  218. `ifeq "ARG1" 'ARG2''
  219. `ifeq 'ARG1' "ARG2"'
  220.      Expand all variable references in ARG1 and ARG2 and compare them.
  221.      If they are identical, the TEXT-IF-TRUE is effective; otherwise,
  222.      the TEXT-IF-FALSE, if any, is effective.
  223.      Often you want to test if a variable has a non-empty value.  When
  224.      the value results from complex expansions of variables and
  225.      functions, expansions you would consider empty may actually
  226.      contain whitespace characters and thus are not seen as empty.
  227.      However, you can use the `strip' function (*note Text
  228.      Functions::.) to avoid interpreting whitespace as a non-empty
  229.      value.  For example:
  230.           ifeq ($(strip $(foo)),)
  231.           TEXT-IF-EMPTY
  232.           endif
  233.      will evaluate TEXT-IF-EMPTY even if the expansion of `$(foo)'
  234.      contains whitespace characters.
  235. `ifneq (ARG1, ARG2)'
  236. `ifneq 'ARG1' 'ARG2''
  237. `ifneq "ARG1" "ARG2"'
  238. `ifneq "ARG1" 'ARG2''
  239. `ifneq 'ARG1' "ARG2"'
  240.      Expand all variable references in ARG1 and ARG2 and compare them.
  241.      If they are different, the TEXT-IF-TRUE is effective; otherwise,
  242.      the TEXT-IF-FALSE, if any, is effective.
  243. `ifdef VARIABLE-NAME'
  244.      If the variable VARIABLE-NAME has a non-empty value, the
  245.      TEXT-IF-TRUE is effective; otherwise, the TEXT-IF-FALSE, if any,
  246.      is effective.  Variables that have never been defined have an
  247.      empty value.
  248.      Note that `ifdef' only tests whether a variable has a value.  It
  249.      does not expand the variable to see if that value is nonempty.
  250.      Consequently, tests using `ifdef' return true for all definitions
  251.      except those like `foo ='.  To test for an empty value, use
  252.      `ifeq ($(foo),)'.  For example,
  253.           bar =
  254.           foo = $(bar)
  255.           ifdef foo
  256.           frobozz = yes
  257.           else
  258.           frobozz = no
  259.           endif
  260.      sets `frobozz' to `yes', while:
  261.           foo =
  262.           ifdef foo
  263.           frobozz = yes
  264.           else
  265.           frobozz = no
  266.           endif
  267.      sets `frobozz' to `no'.
  268. `ifndef VARIABLE-NAME'
  269.      If the variable VARIABLE-NAME has an empty value, the TEXT-IF-TRUE
  270.      is effective; otherwise, the TEXT-IF-FALSE, if any, is effective.
  271.    Extra spaces are allowed and ignored at the beginning of the
  272. conditional directive line, but a tab is not allowed.  (If the line
  273. begins with a tab, it will be considered a command for a rule.)  Aside
  274. from this, extra spaces or tabs may be inserted with no effect anywhere
  275. except within the directive name or within an argument.  A comment
  276. starting with `#' may appear at the end of the line.
  277.    The other two directives that play a part in a conditional are `else'
  278. and `endif'.  Each of these directives is written as one word, with no
  279. arguments.  Extra spaces are allowed and ignored at the beginning of the
  280. line, and spaces or tabs at the end.  A comment starting with `#' may
  281. appear at the end of the line.
  282.    Conditionals affect which lines of the makefile `make' uses.  If the
  283. condition is true, `make' reads the lines of the TEXT-IF-TRUE as part
  284. of the makefile; if the condition is false, `make' ignores those lines
  285. completely.  It follows that syntactic units of the makefile, such as
  286. rules, may safely be split across the beginning or the end of the
  287. conditional.
  288.    `make' evaluates conditionals when it reads a makefile.
  289. Consequently, you cannot use automatic variables in the tests of
  290. conditionals because they are not defined until commands are run (*note
  291. Automatic Variables: Automatic.).
  292.    To prevent intolerable confusion, it is not permitted to start a
  293. conditional in one makefile and end it in another.  However, you may
  294. write an `include' directive within a conditional, provided you do not
  295. attempt to terminate the conditional inside the included file.
  296. File: make.info,  Node: Testing Flags,  Prev: Conditional Syntax,  Up: Conditionals
  297. Conditionals that Test Flags
  298. ============================
  299.    You can write a conditional that tests `make' command flags such as
  300. `-t' by using the variable `MAKEFLAGS' together with the `findstring'
  301. function (*note Functions for String Substitution and Analysis: Text
  302. Functions.).  This is useful when `touch' is not enough to make a file
  303. appear up to date.
  304.    The `findstring' function determines whether one string appears as a
  305. substring of another.  If you want to test for the `-t' flag, use `t'
  306. as the first string and the value of `MAKEFLAGS' as the other.
  307.    For example, here is how to arrange to use `ranlib -t' to finish
  308. marking an archive file up to date:
  309.      archive.a: ...
  310.      ifneq (,$(findstring t,$(MAKEFLAGS)))
  311.              +touch archive.a
  312.              +ranlib -t archive.a
  313.      else
  314.              ranlib archive.a
  315.      endif
  316. The `+' prefix marks those command lines as "recursive" so that they
  317. will be executed despite use of the `-t' flag.  *Note Recursive Use of
  318. `make': Recursion.
  319. File: make.info,  Node: Functions,  Next: Running,  Prev: Conditionals,  Up: Top
  320. Functions for Transforming Text
  321. *******************************
  322.    "Functions" allow you to do text processing in the makefile to
  323. compute the files to operate on or the commands to use.  You use a
  324. function in a "function call", where you give the name of the function
  325. and some text (the "arguments") for the function to operate on.  The
  326. result of the function's processing is substituted into the makefile at
  327. the point of the call, just as a variable might be substituted.
  328. * Menu:
  329. * Syntax of Functions::         How to write a function call.
  330. * Text Functions::              General-purpose text manipulation functions.
  331. * Filename Functions::          Functions for manipulating file names.
  332. * Foreach Function::            Repeat some text with controlled variation.
  333. * Origin Function::             Find where a variable got its value.
  334. * Shell Function::              Substitute the output of a shell command.
  335. File: make.info,  Node: Syntax of Functions,  Next: Text Functions,  Up: Functions
  336. Function Call Syntax
  337. ====================
  338.    A function call resembles a variable reference.  It looks like this:
  339.      $(FUNCTION ARGUMENTS)
  340. or like this:
  341.      ${FUNCTION ARGUMENTS}
  342.    Here FUNCTION is a function name; one of a short list of names that
  343. are part of `make'.  There is no provision for defining new functions.
  344.    The ARGUMENTS are the arguments of the function.  They are separated
  345. from the function name by one or more spaces or tabs, and if there is
  346. more than one argument, then they are separated by commas.  Such
  347. whitespace and commas are not part of an argument's value.  The
  348. delimiters which you use to surround the function call, whether
  349. parentheses or braces, can appear in an argument only in matching pairs;
  350. the other kind of delimiters may appear singly.  If the arguments
  351. themselves contain other function calls or variable references, it is
  352. wisest to use the same kind of delimiters for all the references; write
  353. `$(subst a,b,$(x))', not `$(subst a,b,${x})'.  This is because it is
  354. clearer, and because only one type of delimiter is matched to find the
  355. end of the reference.
  356.    The text written for each argument is processed by substitution of
  357. variables and function calls to produce the argument value, which is
  358. the text on which the function acts.  The substitution is done in the
  359. order in which the arguments appear.
  360.    Commas and unmatched parentheses or braces cannot appear in the text
  361. of an argument as written; leading spaces cannot appear in the text of
  362. the first argument as written.  These characters can be put into the
  363. argument value by variable substitution.  First define variables
  364. `comma' and `space' whose values are isolated comma and space
  365. characters, then substitute these variables where such characters are
  366. wanted, like this:
  367.      comma:= ,
  368.      empty:=
  369.      space:= $(empty) $(empty)
  370.      foo:= a b c
  371.      bar:= $(subst $(space),$(comma),$(foo))
  372.      # bar is now `a,b,c'.
  373. Here the `subst' function replaces each space with a comma, through the
  374. value of `foo', and substitutes the result.
  375. File: make.info,  Node: Text Functions,  Next: Filename Functions,  Prev: Syntax of Functions,  Up: Functions
  376. Functions for String Substitution and Analysis
  377. ==============================================
  378.    Here are some functions that operate on strings:
  379. `$(subst FROM,TO,TEXT)'
  380.      Performs a textual replacement on the text TEXT: each occurrence
  381.      of FROM is replaced by TO.  The result is substituted for the
  382.      function call.  For example,
  383.           $(subst ee,EE,feet on the street)
  384.      substitutes the string `fEEt on the strEEt'.
  385. `$(patsubst PATTERN,REPLACEMENT,TEXT)'
  386.      Finds whitespace-separated words in TEXT that match PATTERN and
  387.      replaces them with REPLACEMENT.  Here PATTERN may contain a `%'
  388.      which acts as a wildcard, matching any number of any characters
  389.      within a word.  If REPLACEMENT also contains a `%', the `%' is
  390.      replaced by the text that matched the `%' in PATTERN.
  391.      `%' characters in `patsubst' function invocations can be quoted
  392.      with preceding backslashes (`\').  Backslashes that would
  393.      otherwise quote `%' characters can be quoted with more backslashes.
  394.      Backslashes that quote `%' characters or other backslashes are
  395.      removed from the pattern before it is compared file names or has a
  396.      stem substituted into it.  Backslashes that are not in danger of
  397.      quoting `%' characters go unmolested.  For example, the pattern
  398.      `the\%weird\\%pattern\\' has `the%weird\' preceding the operative
  399.      `%' character, and `pattern\\' following it.  The final two
  400.      backslashes are left alone because they cannot affect any `%'
  401.      character.
  402.      Whitespace between words is folded into single space characters;
  403.      leading and trailing whitespace is discarded.
  404.      For example,
  405.           $(patsubst %.c,%.o,x.c.c bar.c)
  406.      produces the value `x.c.o bar.o'.
  407.      Substitution references (*note Substitution References:
  408.      Substitution Refs.) are a simpler way to get the effect of the
  409.      `patsubst' function:
  410.           $(VAR:PATTERN=REPLACEMENT)
  411.      is equivalent to
  412.           $(patsubst PATTERN,REPLACEMENT,$(VAR))
  413.      The second shorthand simplifies one of the most common uses of
  414.      `patsubst': replacing the suffix at the end of file names.
  415.           $(VAR:SUFFIX=REPLACEMENT)
  416.      is equivalent to
  417.           $(patsubst %SUFFIX,%REPLACEMENT,$(VAR))
  418.      For example, you might have a list of object files:
  419.           objects = foo.o bar.o baz.o
  420.      To get the list of corresponding source files, you could simply
  421.      write:
  422.           $(objects:.o=.c)
  423.      instead of using the general form:
  424.           $(patsubst %.o,%.c,$(objects))
  425. `$(strip STRING)'
  426.      Removes leading and trailing whitespace from STRING and replaces
  427.      each internal sequence of one or more whitespace characters with a
  428.      single space.  Thus, `$(strip a b  c )' results in `a b c'.
  429.      The function `strip' can be very useful when used in conjunction
  430.      with conditionals.  When comparing something with the empty string
  431.      `' using `ifeq' or `ifneq', you usually want a string of just
  432.      whitespace to match the empty string (*note Conditionals::.).
  433.      Thus, the following may fail to have the desired results:
  434.           .PHONY: all
  435.           ifneq   "$(needs_made)" ""
  436.           all: $(needs_made)
  437.           else
  438.           all:;@echo 'Nothing to make!'
  439.           endif
  440.      Replacing the variable reference `$(needs_made)' with the function
  441.      call `$(strip $(needs_made))' in the `ifneq' directive would make
  442.      it more robust.
  443. `$(findstring FIND,IN)'
  444.      Searches IN for an occurrence of FIND.  If it occurs, the value is
  445.      FIND; otherwise, the value is empty.  You can use this function in
  446.      a conditional to test for the presence of a specific substring in
  447.      a given string.  Thus, the two examples,
  448.           $(findstring a,a b c)
  449.           $(findstring a,b c)
  450.      produce the values `a' and `' (the empty string), respectively.
  451.      *Note Testing Flags::, for a practical application of `findstring'.
  452. `$(filter PATTERN...,TEXT)'
  453.      Removes all whitespace-separated words in TEXT that do *not* match
  454.      any of the PATTERN words, returning only matching words.  The
  455.      patterns are written using `%', just like the patterns used in the
  456.      `patsubst' function above.
  457.      The `filter' function can be used to separate out different types
  458.      of strings (such as file names) in a variable.  For example:
  459.           sources := foo.c bar.c baz.s ugh.h
  460.           foo: $(sources)
  461.                   cc $(filter %.c %.s,$(sources)) -o foo
  462.      says that `foo' depends of `foo.c', `bar.c', `baz.s' and `ugh.h'
  463.      but only `foo.c', `bar.c' and `baz.s' should be specified in the
  464.      command to the compiler.
  465. `$(filter-out PATTERN...,TEXT)'
  466.      Removes all whitespace-separated words in TEXT that *do* match the
  467.      PATTERN words, returning only the words that *do not* match.  This
  468.      is the exact opposite of the `filter' function.
  469.      For example, given:
  470.           objects=main1.o foo.o main2.o bar.o
  471.           mains=main1.o main2.o
  472.      the following generates a list which contains all the object files
  473.      not in `mains':
  474.           $(filter-out $(mains),$(objects))
  475. `$(sort LIST)'
  476.      Sorts the words of LIST in lexical order, removing duplicate
  477.      words.  The output is a list of words separated by single spaces.
  478.      Thus,
  479.           $(sort foo bar lose)
  480.      returns the value `bar foo lose'.
  481.      Incidentally, since `sort' removes duplicate words, you can use it
  482.      for this purpose even if you don't care about the sort order.
  483.    Here is a realistic example of the use of `subst' and `patsubst'.
  484. Suppose that a makefile uses the `VPATH' variable to specify a list of
  485. directories that `make' should search for dependency files (*note
  486. `VPATH' Search Path for All Dependencies: General Search.).  This
  487. example shows how to tell the C compiler to search for header files in
  488. the same list of directories.
  489.    The value of `VPATH' is a list of directories separated by colons,
  490. such as `src:../headers'.  First, the `subst' function is used to
  491. change the colons to spaces:
  492.      $(subst :, ,$(VPATH))
  493. This produces `src ../headers'.  Then `patsubst' is used to turn each
  494. directory name into a `-I' flag.  These can be added to the value of
  495. the variable `CFLAGS', which is passed automatically to the C compiler,
  496. like this:
  497.      override CFLAGS += $(patsubst %,-I%,$(subst :, ,$(VPATH)))
  498. The effect is to append the text `-Isrc -I../headers' to the previously
  499. given value of `CFLAGS'.  The `override' directive is used so that the
  500. new value is assigned even if the previous value of `CFLAGS' was
  501. specified with a command argument (*note The `override' Directive:
  502. Override Directive.).
  503. File: make.info,  Node: Filename Functions,  Next: Foreach Function,  Prev: Text Functions,  Up: Functions
  504. Functions for File Names
  505. ========================
  506.    Several of the built-in expansion functions relate specifically to
  507. taking apart file names or lists of file names.
  508.    Each of the following functions performs a specific transformation
  509. on a file name.  The argument of the function is regarded as a series
  510. of file names, separated by whitespace.  (Leading and trailing
  511. whitespace is ignored.)  Each file name in the series is transformed in
  512. the same way and the results are concatenated with single spaces
  513. between them.
  514. `$(dir NAMES...)'
  515.      Extracts the directory-part of each file name in NAMES.  The
  516.      directory-part of the file name is everything up through (and
  517.      including) the last slash in it.  If the file name contains no
  518.      slash, the directory part is the string `./'.  For example,
  519.           $(dir src/foo.c hacks)
  520.      produces the result `src/ ./'.
  521. `$(notdir NAMES...)'
  522.      Extracts all but the directory-part of each file name in NAMES.
  523.      If the file name contains no slash, it is left unchanged.
  524.      Otherwise, everything through the last slash is removed from it.
  525.      A file name that ends with a slash becomes an empty string.  This
  526.      is unfortunate, because it means that the result does not always
  527.      have the same number of whitespace-separated file names as the
  528.      argument had; but we do not see any other valid alternative.
  529.      For example,
  530.           $(notdir src/foo.c hacks)
  531.      produces the result `foo.c hacks'.
  532. `$(suffix NAMES...)'
  533.      Extracts the suffix of each file name in NAMES.  If the file name
  534.      contains a period, the suffix is everything starting with the last
  535.      period.  Otherwise, the suffix is the empty string.  This
  536.      frequently means that the result will be empty when NAMES is not,
  537.      and if NAMES contains multiple file names, the result may contain
  538.      fewer file names.
  539.      For example,
  540.           $(suffix src/foo.c hacks)
  541.      produces the result `.c'.
  542. `$(basename NAMES...)'
  543.      Extracts all but the suffix of each file name in NAMES.  If the
  544.      file name contains a period, the basename is everything starting
  545.      up to (and not including) the last period.  Otherwise, the
  546.      basename is the entire file name.  For example,
  547.           $(basename src/foo.c hacks)
  548.      produces the result `src/foo hacks'.
  549. `$(addsuffix SUFFIX,NAMES...)'
  550.      The argument NAMES is regarded as a series of names, separated by
  551.      whitespace; SUFFIX is used as a unit.  The value of SUFFIX is
  552.      appended to the end of each individual name and the resulting
  553.      larger names are concatenated with single spaces between them.
  554.      For example,
  555.           $(addsuffix .c,foo bar)
  556.      produces the result `foo.c bar.c'.
  557. `$(addprefix PREFIX,NAMES...)'
  558.      The argument NAMES is regarded as a series of names, separated by
  559.      whitespace; PREFIX is used as a unit.  The value of PREFIX is
  560.      prepended to the front of each individual name and the resulting
  561.      larger names are concatenated with single spaces between them.
  562.      For example,
  563.           $(addprefix src/,foo bar)
  564.      produces the result `src/foo src/bar'.
  565. `$(join LIST1,LIST2)'
  566.      Concatenates the two arguments word by word: the two first words
  567.      (one from each argument) concatenated form the first word of the
  568.      result, the two second words form the second word of the result,
  569.      and so on.  So the Nth word of the result comes from the Nth word
  570.      of each argument.  If one argument has more words that the other,
  571.      the extra words are copied unchanged into the result.
  572.      For example, `$(join a b,.c .o)' produces `a.c b.o'.
  573.      Whitespace between the words in the lists is not preserved; it is
  574.      replaced with a single space.
  575.      This function can merge the results of the `dir' and `notdir'
  576.      functions, to produce the original list of files which was given
  577.      to those two functions.
  578. `$(word N,TEXT)'
  579.      Returns the Nth word of TEXT.  The legitimate values of N start
  580.      from 1.  If N is bigger than the number of words in TEXT, the
  581.      value is empty.  For example,
  582.           $(word 2, foo bar baz)
  583.      returns `bar'.
  584. `$(words TEXT)'
  585.      Returns the number of words in TEXT.  Thus, the last word of TEXT
  586.      is `$(word $(words TEXT),TEXT)'.
  587. `$(firstword NAMES...)'
  588.      The argument NAMES is regarded as a series of names, separated by
  589.      whitespace.  The value is the first name in the series.  The rest
  590.      of the names are ignored.
  591.      For example,
  592.           $(firstword foo bar)
  593.      produces the result `foo'.  Although `$(firstword TEXT)' is the
  594.      same as `$(word 1,TEXT)', the `firstword' function is retained for
  595.      its simplicity.
  596. `$(wildcard PATTERN)'
  597.      The argument PATTERN is a file name pattern, typically containing
  598.      wildcard characters (as in shell file name patterns).  The result
  599.      of `wildcard' is a space-separated list of the names of existing
  600.      files that match the pattern.  *Note Using Wildcard Characters in
  601.      File Names: Wildcards.
  602. File: make.info,  Node: Foreach Function,  Next: Origin Function,  Prev: Filename Functions,  Up: Functions
  603. The `foreach' Function
  604. ======================
  605.    The `foreach' function is very different from other functions.  It
  606. causes one piece of text to be used repeatedly, each time with a
  607. different substitution performed on it.  It resembles the `for' command
  608. in the shell `sh' and the `foreach' command in the C-shell `csh'.
  609.    The syntax of the `foreach' function is:
  610.      $(foreach VAR,LIST,TEXT)
  611. The first two arguments, VAR and LIST, are expanded before anything
  612. else is done; note that the last argument, TEXT, is *not* expanded at
  613. the same time.  Then for each word of the expanded value of LIST, the
  614. variable named by the expanded value of VAR is set to that word, and
  615. TEXT is expanded.  Presumably TEXT contains references to that
  616. variable, so its expansion will be different each time.
  617.    The result is that TEXT is expanded as many times as there are
  618. whitespace-separated words in LIST.  The multiple expansions of TEXT
  619. are concatenated, with spaces between them, to make the result of
  620. `foreach'.
  621.    This simple example sets the variable `files' to the list of all
  622. files in the directories in the list `dirs':
  623.      dirs := a b c d
  624.      files := $(foreach dir,$(dirs),$(wildcard $(dir)/*))
  625.    Here TEXT is `$(wildcard $(dir)/*)'.  The first repetition finds the
  626. value `a' for `dir', so it produces the same result as `$(wildcard
  627. a/*)'; the second repetition produces the result of `$(wildcard b/*)';
  628. and the third, that of `$(wildcard c/*)'.
  629.    This example has the same result (except for setting `dirs') as the
  630. following example:
  631.      files := $(wildcard a/* b/* c/* d/*)
  632.    When TEXT is complicated, you can improve readability by giving it a
  633. name, with an additional variable:
  634.      find_files = $(wildcard $(dir)/*)
  635.      dirs := a b c d
  636.      files := $(foreach dir,$(dirs),$(find_files))
  637. Here we use the variable `find_files' this way.  We use plain `=' to
  638. define a recursively-expanding variable, so that its value contains an
  639. actual function call to be reexpanded under the control of `foreach'; a
  640. simply-expanded variable would not do, since `wildcard' would be called
  641. only once at the time of defining `find_files'.
  642.    The `foreach' function has no permanent effect on the variable VAR;
  643. its value and flavor after the `foreach' function call are the same as
  644. they were beforehand.  The other values which are taken from LIST are
  645. in effect only temporarily, during the execution of `foreach'.  The
  646. variable VAR is a simply-expanded variable during the execution of
  647. `foreach'.  If VAR was undefined before the `foreach' function call, it
  648. is undefined after the call.  *Note The Two Flavors of Variables:
  649. Flavors.
  650.    You must take care when using complex variable expressions that
  651. result in variable names because many strange things are valid variable
  652. names, but are probably not what you intended.  For example,
  653.      files := $(foreach Esta escrito en espanol!,b c ch,$(find_files))
  654. might be useful if the value of `find_files' references the variable
  655. whose name is `Esta escrito en espanol!' (es un nombre bastante largo,
  656. no?), but it is more likely to be a mistake.
  657. File: make.info,  Node: Origin Function,  Next: Shell Function,  Prev: Foreach Function,  Up: Functions
  658. The `origin' Function
  659. =====================
  660.    The `origin' function is unlike most other functions in that it does
  661. not operate on the values of variables; it tells you something *about*
  662. a variable.  Specifically, it tells you where it came from.
  663.    The syntax of the `origin' function is:
  664.      $(origin VARIABLE)
  665.    Note that VARIABLE is the *name* of a variable to inquire about; not
  666. a *reference* to that variable.  Therefore you would not normally use a
  667. `$' or parentheses when writing it.  (You can, however, use a variable
  668. reference in the name if you want the name not to be a constant.)
  669.    The result of this function is a string telling you how the variable
  670. VARIABLE was defined:
  671. `undefined'
  672.      if VARIABLE was never defined.
  673. `default'
  674.      if VARIABLE has a default definition, as is usual with `CC' and so
  675.      on.  *Note Variables Used by Implicit Rules: Implicit Variables.
  676.      Note that if you have redefined a default variable, the `origin'
  677.      function will return the origin of the later definition.
  678. `environment'
  679.      if VARIABLE was defined as an environment variable and the `-e'
  680.      option is *not* turned on (*note Summary of Options: Options
  681.      Summary.).
  682. `environment override'
  683.      if VARIABLE was defined as an environment variable and the `-e'
  684.      option *is* turned on (*note Summary of Options: Options Summary.).
  685. `file'
  686.      if VARIABLE was defined in a makefile.
  687. `command line'
  688.      if VARIABLE was defined on the command line.
  689. `override'
  690.      if VARIABLE was defined with an `override' directive in a makefile
  691.      (*note The `override' Directive: Override Directive.).
  692. `automatic'
  693.      if VARIABLE is an automatic variable defined for the execution of
  694.      the commands for each rule (*note Automatic Variables: Automatic.).
  695.    This information is primarily useful (other than for your curiosity)
  696. to determine if you want to believe the value of a variable.  For
  697. example, suppose you have a makefile `foo' that includes another
  698. makefile `bar'.  You want a variable `bletch' to be defined in `bar' if
  699. you run the command `make -f bar', even if the environment contains a
  700. definition of `bletch'.  However, if `foo' defined `bletch' before
  701. including `bar', you do not want to override that definition.  This
  702. could be done by using an `override' directive in `foo', giving that
  703. definition precedence over the later definition in `bar';
  704. unfortunately, the `override' directive would also override any command
  705. line definitions.  So, `bar' could include:
  706.      ifdef bletch
  707.      ifeq "$(origin bletch)" "environment"
  708.      bletch = barf, gag, etc.
  709.      endif
  710.      endif
  711. If `bletch' has been defined from the environment, this will redefine
  712.    If you want to override a previous definition of `bletch' if it came
  713. from the environment, even under `-e', you could instead write:
  714.      ifneq "$(findstring environment,$(origin bletch))" ""
  715.      bletch = barf, gag, etc.
  716.      endif
  717.    Here the redefinition takes place if `$(origin bletch)' returns
  718. either `environment' or `environment override'.  *Note Functions for
  719. String Substitution and Analysis: Text Functions.
  720. File: make.info,  Node: Shell Function,  Prev: Origin Function,  Up: Functions
  721. The `shell' Function
  722. ====================
  723.    The `shell' function is unlike any other function except the
  724. `wildcard' function (*note The Function `wildcard': Wildcard Function.)
  725. in that it communicates with the world outside of `make'.
  726.    The `shell' function performs the same function that backquotes
  727. (``') perform in most shells: it does "command expansion".  This means
  728. that it takes an argument that is a shell command and returns the
  729. output of the command.  The only processing `make' does on the result,
  730. before substituting it into the surrounding text, is to convert
  731. newlines to spaces.
  732.    The commands run by calls to the `shell' function are run when the
  733. function calls are expanded.  In most cases, this is when the makefile
  734. is read in.  The exception is that function calls in the commands of
  735. the rules are expanded when the commands are run, and this applies to
  736. `shell' function calls like all others.
  737.    Here are some examples of the use of the `shell' function:
  738.      contents := $(shell cat foo)
  739. sets `contents' to the contents of the file `foo', with a space (rather
  740. than a newline) separating each line.
  741.      files := $(shell echo *.c)
  742. sets `files' to the expansion of `*.c'.  Unless `make' is using a very
  743. strange shell, this has the same result as `$(wildcard *.c)'.
  744. File: make.info,  Node: Running,  Next: Implicit Rules,  Prev: Functions,  Up: Top
  745. How to Run `make'
  746. *****************
  747.    A makefile that says how to recompile a program can be used in more
  748. than one way.  The simplest use is to recompile every file that is out
  749. of date.  Usually, makefiles are written so that if you run `make' with
  750. no arguments, it does just that.
  751.    But you might want to update only some of the files; you might want
  752. to use a different compiler or different compiler options; you might
  753. want just to find out which files are out of date without changing them.
  754.    By giving arguments when you run `make', you can do any of these
  755. things and many others.
  756.    The exit status of `make' is always one of three values:
  757.      The exit status is zero if `make' is successful.
  758.      The exit status is two if `make' encounters any errors.  It will
  759.      print messages describing the particular errors.
  760.      The exit status is one if you use the `-q' flag and `make'
  761.      determines that some target is not already up to date.  *Note
  762.      Instead of Executing the Commands: Instead of Execution.
  763. * Menu:
  764. * Makefile Arguments::          How to specify which makefile to use.
  765. * Goals::                       How to use goal arguments to specify which
  766.                                   parts of the makefile to use.
  767. * Instead of Execution::        How to use mode flags to specify what
  768.                                   kind of thing to do with the commands
  769.                                   in the makefile other than simply
  770.                                   execute them.
  771. * Avoiding Compilation::        How to avoid recompiling certain files.
  772. * Overriding::                  How to override a variable to specify
  773.                                   an alternate compiler and other things.
  774. * Testing::                     How to proceed past some errors, to
  775.                                   test compilation.
  776. * Options Summary::             Summary of Options
  777. File: make.info,  Node: Makefile Arguments,  Next: Goals,  Up: Running
  778. Arguments to Specify the Makefile
  779. =================================
  780.    The way to specify the name of the makefile is with the `-f' or
  781. `--file' option (`--makefile' also works).  For example, `-f altmake'
  782. says to use the file `altmake' as the makefile.
  783.    If you use the `-f' flag several times and follow each `-f' with an
  784. argument, all the specified files are used jointly as makefiles.
  785.    If you do not use the `-f' or `--file' flag, the default is to try
  786. `GNUmakefile', `makefile', and `Makefile', in that order, and use the
  787. first of these three which exists or can be made (*note Writing
  788. Makefiles: Makefiles.).
  789. File: make.info,  Node: Goals,  Next: Instead of Execution,  Prev: Makefile Arguments,  Up: Running
  790. Arguments to Specify the Goals
  791. ==============================
  792.    The "goals" are the targets that `make' should strive ultimately to
  793. update.  Other targets are updated as well if they appear as
  794. dependencies of goals, or dependencies of dependencies of goals, etc.
  795.    By default, the goal is the first target in the makefile (not
  796. counting targets that start with a period).  Therefore, makefiles are
  797. usually written so that the first target is for compiling the entire
  798. program or programs they describe.  If the first rule in the makefile
  799. has several targets, only the first target in the rule becomes the
  800. default goal, not the whole list.
  801.    You can specify a different goal or goals with arguments to `make'.
  802. Use the name of the goal as an argument.  If you specify several goals,
  803. `make' processes each of them in turn, in the order you name them.
  804.    Any target in the makefile may be specified as a goal (unless it
  805. starts with `-' or contains an `=', in which case it will be parsed as
  806. a switch or variable definition, respectively).  Even targets not in
  807. the makefile may be specified, if `make' can find implicit rules that
  808. say how to make them.
  809.    One use of specifying a goal is if you want to compile only a part of
  810. the program, or only one of several programs.  Specify as a goal each
  811. file that you wish to remake.  For example, consider a directory
  812. containing several programs, with a makefile that starts like this:
  813.      .PHONY: all
  814.      all: size nm ld ar as
  815.    If you are working on the program `size', you might want to say
  816. `make size' so that only the files of that program are recompiled.
  817.    Another use of specifying a goal is to make files that are not
  818. normally made.  For example, there may be a file of debugging output,
  819. or a version of the program that is compiled specially for testing,
  820. which has a rule in the makefile but is not a dependency of the default
  821. goal.
  822.    Another use of specifying a goal is to run the commands associated
  823. with a phony target (*note Phony Targets::.) or empty target (*note
  824. Empty Target Files to Record Events: Empty Targets.).  Many makefiles
  825. contain a phony target named `clean' which deletes everything except
  826. source files.  Naturally, this is done only if you request it
  827. explicitly with `make clean'.  Following is a list of typical phony and
  828. empty target names.  *Note Standard Targets::, for a detailed list of
  829. all the standard target names which GNU software packages use.
  830. `all'
  831.      Make all the top-level targets the makefile knows about.
  832. `clean'
  833.      Delete all files that are normally created by running `make'.
  834. `mostlyclean'
  835.      Like `clean', but may refrain from deleting a few files that people
  836.      normally don't want to recompile.  For example, the `mostlyclean'
  837.      target for GCC does not delete `libgcc.a', because recompiling it
  838.      is rarely necessary and takes a lot of time.
  839. `distclean'
  840. `realclean'
  841. `clobber'
  842.      Any of these targets might be defined to delete *more* files than
  843.      `clean' does.  For example, this would delete configuration files
  844.      or links that you would normally create as preparation for
  845.      compilation, even if the makefile itself cannot create these files.
  846. `install'
  847.      Copy the executable file into a directory that users typically
  848.      search for commands; copy any auxiliary files that the executable
  849.      uses into the directories where it will look for them.
  850. `print'
  851.      Print listings of the source files that have changed.
  852. `tar'
  853.      Create a tar file of the source files.
  854. `shar'
  855.      Create a shell archive (shar file) of the source files.
  856. `dist'
  857.      Create a distribution file of the source files.  This might be a
  858.      tar file, or a shar file, or a compressed version of one of the
  859.      above, or even more than one of the above.
  860. `TAGS'
  861.      Update a tags table for this program.
  862. `check'
  863. `test'
  864.      Perform self tests on the program this makefile builds.
  865. File: make.info,  Node: Instead of Execution,  Next: Avoiding Compilation,  Prev: Goals,  Up: Running
  866. Instead of Executing the Commands
  867. =================================
  868.    The makefile tells `make' how to tell whether a target is up to date,
  869. and how to update each target.  But updating the targets is not always
  870. what you want.  Certain options specify other activities for `make'.
  871. `--just-print'
  872. `--dry-run'
  873. `--recon'
  874.      "No-op".  The activity is to print what commands would be used to
  875.      make the targets up to date, but not actually execute them.
  876. `--touch'
  877.      "Touch".  The activity is to mark the targets as up to date without
  878.      actually changing them.  In other words, `make' pretends to compile
  879.      the targets but does not really change their contents.
  880. `--question'
  881.      "Question".  The activity is to find out silently whether the
  882.      targets are up to date already; but execute no commands in either
  883.      case.  In other words, neither compilation nor output will occur.
  884. `-W FILE'
  885. `--what-if=FILE'
  886. `--assume-new=FILE'
  887. `--new-file=FILE'
  888.      "What if".  Each `-W' flag is followed by a file name.  The given
  889.      files' modification times are recorded by `make' as being the
  890.      present time, although the actual modification times remain the
  891.      same.  You can use the `-W' flag in conjunction with the `-n' flag
  892.      to see what would happen if you were to modify specific files.
  893.    With the `-n' flag, `make' prints the commands that it would
  894. normally execute but does not execute them.
  895.    With the `-t' flag, `make' ignores the commands in the rules and
  896. uses (in effect) the command `touch' for each target that needs to be
  897. remade.  The `touch' command is also printed, unless `-s' or `.SILENT'
  898. is used.  For speed, `make' does not actually invoke the program
  899. `touch'.  It does the work directly.
  900.    With the `-q' flag, `make' prints nothing and executes no commands,
  901. but the exit status code it returns is zero if and only if the targets
  902. to be considered are already up to date.  If the exit status is one,
  903. then some updating needs to be done.  If `make' encounters an error,
  904. the exit status is two, so you can distinguish an error from a target
  905. that is not up to date.
  906.    It is an error to use more than one of these three flags in the same
  907. invocation of `make'.
  908.    The `-n', `-t', and `-q' options do not affect command lines that
  909. begin with `+' characters or contain the strings `$(MAKE)' or
  910. `${MAKE}'.  Note that only the line containing the `+' character or the
  911. strings `$(MAKE)' or `${MAKE}' is run regardless of these options.
  912. Other lines in the same rule are not run unless they too begin with `+'
  913. or contain `$(MAKE)' or `${MAKE}' (*Note How the `MAKE' Variable Works:
  914. MAKE Variable.)
  915.    The `-W' flag provides two features:
  916.    * If you also use the `-n' or `-q' flag, you can see what `make'
  917.      would do if you were to modify some files.
  918.    * Without the `-n' or `-q' flag, when `make' is actually executing
  919.      commands, the `-W' flag can direct `make' to act as if some files
  920.      had been modified, without actually modifying the files.
  921.    Note that the options `-p' and `-v' allow you to obtain other
  922. information about `make' or about the makefiles in use (*note Summary
  923. of Options: Options Summary.).
  924. File: make.info,  Node: Avoiding Compilation,  Next: Overriding,  Prev: Instead of Execution,  Up: Running
  925. Avoiding Recompilation of Some Files
  926. ====================================
  927.    Sometimes you may have changed a source file but you do not want to
  928. recompile all the files that depend on it.  For example, suppose you
  929. add a macro or a declaration to a header file that many other files
  930. depend on.  Being conservative, `make' assumes that any change in the
  931. header file requires recompilation of all dependent files, but you know
  932. that they do not need to be recompiled and you would rather not waste
  933. the time waiting for them to compile.
  934.    If you anticipate the problem before changing the header file, you
  935. can use the `-t' flag.  This flag tells `make' not to run the commands
  936. in the rules, but rather to mark the target up to date by changing its
  937. last-modification date.  You would follow this procedure:
  938.   1. Use the command `make' to recompile the source files that really
  939.      need recompilation.
  940.   2. Make the changes in the header files.
  941.   3. Use the command `make -t' to mark all the object files as up to
  942.      date.  The next time you run `make', the changes in the header
  943.      files will not cause any recompilation.
  944.    If you have already changed the header file at a time when some files
  945. do need recompilation, it is too late to do this.  Instead, you can use
  946. the `-o FILE' flag, which marks a specified file as "old" (*note
  947. Summary of Options: Options Summary.).  This means that the file itself
  948. will not be remade, and nothing else will be remade on its account.
  949. Follow this procedure:
  950.   1. Recompile the source files that need compilation for reasons
  951.      independent of the particular header file, with `make -o
  952.      HEADERFILE'.  If several header files are involved, use a separate
  953.      `-o' option for each header file.
  954.   2. Touch all the object files with `make -t'.
  955.