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

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