home *** CD-ROM | disk | FTP | other *** search
/ TOS Silver 2000 / TOS Silver 2000.iso / programm / GNU_C++ / LIB / MAKE3761.LZH / info / make.info-5 < prev    next >
Encoding:
GNU Info File  |  1998-07-20  |  50.9 KB  |  1,223 lines

  1. This is Info file make.info, produced by Makeinfo version 1.67 from the
  2. input file make.texinfo.
  3.  
  4. INFO-DIR-SECTION The GNU make utility
  5. START-INFO-DIR-ENTRY
  6. * GNU make: (make.info).           The GNU make utility.
  7. END-INFO-DIR-ENTRY
  8.  
  9.    This file documents the GNU Make utility, which determines
  10. automatically which pieces of a large program need to be recompiled,
  11. and issues the commands to recompile them.
  12.  
  13.    This is Edition 0.51, last updated 26 Aug 1997, of `The GNU Make
  14. Manual', for `make', Version 3.76 Beta.
  15.  
  16.    Copyright (C) 1988, '89, '90, '91, '92, '93, '94, '95, '96, '97     Free
  17. Software Foundation, Inc.
  18.  
  19.    Permission is granted to make and distribute verbatim copies of this
  20. manual provided the copyright notice and this permission notice are
  21. preserved on all copies.
  22.  
  23.    Permission is granted to copy and distribute modified versions of
  24. this manual under the conditions for verbatim copying, provided that
  25. the entire resulting derived work is distributed under the terms of a
  26. permission notice identical to this one.
  27.  
  28.    Permission is granted to copy and distribute translations of this
  29. manual into another language, under the above conditions for modified
  30. versions, except that this permission notice may be stated in a
  31. translation approved by the Free Software Foundation.
  32.  
  33. File: make.info,  Node: Goals,  Next: Instead of Execution,  Prev: Makefile Arguments,  Up: Running
  34.  
  35. Arguments to Specify the Goals
  36. ==============================
  37.  
  38.    The "goals" are the targets that `make' should strive ultimately to
  39. update.  Other targets are updated as well if they appear as
  40. dependencies of goals, or dependencies of dependencies of goals, etc.
  41.  
  42.    By default, the goal is the first target in the makefile (not
  43. counting targets that start with a period).  Therefore, makefiles are
  44. usually written so that the first target is for compiling the entire
  45. program or programs they describe.  If the first rule in the makefile
  46. has several targets, only the first target in the rule becomes the
  47. default goal, not the whole list.
  48.  
  49.    You can specify a different goal or goals with arguments to `make'.
  50. Use the name of the goal as an argument.  If you specify several goals,
  51. `make' processes each of them in turn, in the order you name them.
  52.  
  53.    Any target in the makefile may be specified as a goal (unless it
  54. starts with `-' or contains an `=', in which case it will be parsed as
  55. a switch or variable definition, respectively).  Even targets not in
  56. the makefile may be specified, if `make' can find implicit rules that
  57. say how to make them.
  58.  
  59.    `Make' will set the special variable `MAKECMDGOALS' to the list of
  60. goals you specified on the command line.  If no goals were given on the
  61. command line, this variable is empty.  Note that this variable should
  62. be used only in special circumstances.
  63.  
  64.    An example of appropriate use is to avoid including `.d' files
  65. during `clean' rules (*note Automatic Dependencies::.), so `make' won't
  66. create them only to immediately remove them again:
  67.  
  68.      sources = foo.c bar.c
  69.      
  70.      ifneq ($(MAKECMDGOALS),clean)
  71.      include $(sources:.c=.d)
  72.      endif
  73.  
  74.    One use of specifying a goal is if you want to compile only a part of
  75. the program, or only one of several programs.  Specify as a goal each
  76. file that you wish to remake.  For example, consider a directory
  77. containing several programs, with a makefile that starts like this:
  78.  
  79.      .PHONY: all
  80.      all: size nm ld ar as
  81.  
  82.    If you are working on the program `size', you might want to say
  83. `make size' so that only the files of that program are recompiled.
  84.  
  85.    Another use of specifying a goal is to make files that are not
  86. normally made.  For example, there may be a file of debugging output,
  87. or a version of the program that is compiled specially for testing,
  88. which has a rule in the makefile but is not a dependency of the default
  89. goal.
  90.  
  91.    Another use of specifying a goal is to run the commands associated
  92. with a phony target (*note Phony Targets::.) or empty target (*note
  93. Empty Target Files to Record Events: Empty Targets.).  Many makefiles
  94. contain a phony target named `clean' which deletes everything except
  95. source files.  Naturally, this is done only if you request it
  96. explicitly with `make clean'.  Following is a list of typical phony and
  97. empty target names.  *Note Standard Targets::, for a detailed list of
  98. all the standard target names which GNU software packages use.
  99.  
  100. `all'
  101.      Make all the top-level targets the makefile knows about.
  102.  
  103. `clean'
  104.      Delete all files that are normally created by running `make'.
  105.  
  106. `mostlyclean'
  107.      Like `clean', but may refrain from deleting a few files that people
  108.      normally don't want to recompile.  For example, the `mostlyclean'
  109.      target for GCC does not delete `libgcc.a', because recompiling it
  110.      is rarely necessary and takes a lot of time.
  111.  
  112. `distclean'
  113. `realclean'
  114. `clobber'
  115.      Any of these targets might be defined to delete *more* files than
  116.      `clean' does.  For example, this would delete configuration files
  117.      or links that you would normally create as preparation for
  118.      compilation, even if the makefile itself cannot create these files.
  119.  
  120. `install'
  121.      Copy the executable file into a directory that users typically
  122.      search for commands; copy any auxiliary files that the executable
  123.      uses into the directories where it will look for them.
  124.  
  125. `print'
  126.      Print listings of the source files that have changed.
  127.  
  128. `tar'
  129.      Create a tar file of the source files.
  130.  
  131. `shar'
  132.      Create a shell archive (shar file) of the source files.
  133.  
  134. `dist'
  135.      Create a distribution file of the source files.  This might be a
  136.      tar file, or a shar file, or a compressed version of one of the
  137.      above, or even more than one of the above.
  138.  
  139. `TAGS'
  140.      Update a tags table for this program.
  141.  
  142. `check'
  143. `test'
  144.      Perform self tests on the program this makefile builds.
  145.  
  146. File: make.info,  Node: Instead of Execution,  Next: Avoiding Compilation,  Prev: Goals,  Up: Running
  147.  
  148. Instead of Executing the Commands
  149. =================================
  150.  
  151.    The makefile tells `make' how to tell whether a target is up to date,
  152. and how to update each target.  But updating the targets is not always
  153. what you want.  Certain options specify other activities for `make'.
  154.  
  155. `-n'
  156. `--just-print'
  157. `--dry-run'
  158. `--recon'
  159.      "No-op".  The activity is to print what commands would be used to
  160.      make the targets up to date, but not actually execute them.
  161.  
  162. `-t'
  163. `--touch'
  164.      "Touch".  The activity is to mark the targets as up to date without
  165.      actually changing them.  In other words, `make' pretends to compile
  166.      the targets but does not really change their contents.
  167.  
  168. `-q'
  169. `--question'
  170.      "Question".  The activity is to find out silently whether the
  171.      targets are up to date already; but execute no commands in either
  172.      case.  In other words, neither compilation nor output will occur.
  173.  
  174. `-W FILE'
  175. `--what-if=FILE'
  176. `--assume-new=FILE'
  177. `--new-file=FILE'
  178.      "What if".  Each `-W' flag is followed by a file name.  The given
  179.      files' modification times are recorded by `make' as being the
  180.      present time, although the actual modification times remain the
  181.      same.  You can use the `-W' flag in conjunction with the `-n' flag
  182.      to see what would happen if you were to modify specific files.
  183.  
  184.    With the `-n' flag, `make' prints the commands that it would
  185. normally execute but does not execute them.
  186.  
  187.    With the `-t' flag, `make' ignores the commands in the rules and
  188. uses (in effect) the command `touch' for each target that needs to be
  189. remade.  The `touch' command is also printed, unless `-s' or `.SILENT'
  190. is used.  For speed, `make' does not actually invoke the program
  191. `touch'.  It does the work directly.
  192.  
  193.    With the `-q' flag, `make' prints nothing and executes no commands,
  194. but the exit status code it returns is zero if and only if the targets
  195. to be considered are already up to date.  If the exit status is one,
  196. then some updating needs to be done.  If `make' encounters an error,
  197. the exit status is two, so you can distinguish an error from a target
  198. that is not up to date.
  199.  
  200.    It is an error to use more than one of these three flags in the same
  201. invocation of `make'.
  202.  
  203.    The `-n', `-t', and `-q' options do not affect command lines that
  204. begin with `+' characters or contain the strings `$(MAKE)' or
  205. `${MAKE}'.  Note that only the line containing the `+' character or the
  206. strings `$(MAKE)' or `${MAKE}' is run regardless of these options.
  207. Other lines in the same rule are not run unless they too begin with `+'
  208. or contain `$(MAKE)' or `${MAKE}' (*Note How the `MAKE' Variable Works:
  209. MAKE Variable.)
  210.  
  211.    The `-W' flag provides two features:
  212.  
  213.    * If you also use the `-n' or `-q' flag, you can see what `make'
  214.      would do if you were to modify some files.
  215.  
  216.    * Without the `-n' or `-q' flag, when `make' is actually executing
  217.      commands, the `-W' flag can direct `make' to act as if some files
  218.      had been modified, without actually modifying the files.
  219.  
  220.    Note that the options `-p' and `-v' allow you to obtain other
  221. information about `make' or about the makefiles in use (*note Summary
  222. of Options: Options Summary.).
  223.  
  224. File: make.info,  Node: Avoiding Compilation,  Next: Overriding,  Prev: Instead of Execution,  Up: Running
  225.  
  226. Avoiding Recompilation of Some Files
  227. ====================================
  228.  
  229.    Sometimes you may have changed a source file but you do not want to
  230. recompile all the files that depend on it.  For example, suppose you
  231. add a macro or a declaration to a header file that many other files
  232. depend on.  Being conservative, `make' assumes that any change in the
  233. header file requires recompilation of all dependent files, but you know
  234. that they do not need to be recompiled and you would rather not waste
  235. the time waiting for them to compile.
  236.  
  237.    If you anticipate the problem before changing the header file, you
  238. can use the `-t' flag.  This flag tells `make' not to run the commands
  239. in the rules, but rather to mark the target up to date by changing its
  240. last-modification date.  You would follow this procedure:
  241.  
  242.   1. Use the command `make' to recompile the source files that really
  243.      need recompilation.
  244.  
  245.   2. Make the changes in the header files.
  246.  
  247.   3. Use the command `make -t' to mark all the object files as up to
  248.      date.  The next time you run `make', the changes in the header
  249.      files will not cause any recompilation.
  250.  
  251.    If you have already changed the header file at a time when some files
  252. do need recompilation, it is too late to do this.  Instead, you can use
  253. the `-o FILE' flag, which marks a specified file as "old" (*note
  254. Summary of Options: Options Summary.).  This means that the file itself
  255. will not be remade, and nothing else will be remade on its account.
  256. Follow this procedure:
  257.  
  258.   1. Recompile the source files that need compilation for reasons
  259.      independent of the particular header file, with `make -o
  260.      HEADERFILE'.  If several header files are involved, use a separate
  261.      `-o' option for each header file.
  262.  
  263.   2. Touch all the object files with `make -t'.
  264.  
  265. File: make.info,  Node: Overriding,  Next: Testing,  Prev: Avoiding Compilation,  Up: Running
  266.  
  267. Overriding Variables
  268. ====================
  269.  
  270.    An argument that contains `=' specifies the value of a variable:
  271. `V=X' sets the value of the variable V to X.  If you specify a value in
  272. this way, all ordinary assignments of the same variable in the makefile
  273. are ignored; we say they have been "overridden" by the command line
  274. argument.
  275.  
  276.    The most common way to use this facility is to pass extra flags to
  277. compilers.  For example, in a properly written makefile, the variable
  278. `CFLAGS' is included in each command that runs the C compiler, so a
  279. file `foo.c' would be compiled something like this:
  280.  
  281.      cc -c $(CFLAGS) foo.c
  282.  
  283.    Thus, whatever value you set for `CFLAGS' affects each compilation
  284. that occurs.  The makefile probably specifies the usual value for
  285. `CFLAGS', like this:
  286.  
  287.      CFLAGS=-g
  288.  
  289.    Each time you run `make', you can override this value if you wish.
  290. For example, if you say `make CFLAGS='-g -O'', each C compilation will
  291. be done with `cc -c -g -O'.  (This illustrates how you can use quoting
  292. in the shell to enclose spaces and other special characters in the
  293. value of a variable when you override it.)
  294.  
  295.    The variable `CFLAGS' is only one of many standard variables that
  296. exist just so that you can change them this way.  *Note Variables Used
  297. by Implicit Rules: Implicit Variables, for a complete list.
  298.  
  299.    You can also program the makefile to look at additional variables of
  300. your own, giving the user the ability to control other aspects of how
  301. the makefile works by changing the variables.
  302.  
  303.    When you override a variable with a command argument, you can define
  304. either a recursively-expanded variable or a simply-expanded variable.
  305. The examples shown above make a recursively-expanded variable; to make a
  306. simply-expanded variable, write `:=' instead of `='.  But, unless you
  307. want to include a variable reference or function call in the *value*
  308. that you specify, it makes no difference which kind of variable you
  309. create.
  310.  
  311.    There is one way that the makefile can change a variable that you
  312. have overridden.  This is to use the `override' directive, which is a
  313. line that looks like this: `override VARIABLE = VALUE' (*note The
  314. `override' Directive: Override Directive.).
  315.  
  316. File: make.info,  Node: Testing,  Next: Options Summary,  Prev: Overriding,  Up: Running
  317.  
  318. Testing the Compilation of a Program
  319. ====================================
  320.  
  321.    Normally, when an error happens in executing a shell command, `make'
  322. gives up immediately, returning a nonzero status.  No further commands
  323. are executed for any target.  The error implies that the goal cannot be
  324. correctly remade, and `make' reports this as soon as it knows.
  325.  
  326.    When you are compiling a program that you have just changed, this is
  327. not what you want.  Instead, you would rather that `make' try compiling
  328. every file that can be tried, to show you as many compilation errors as
  329. possible.
  330.  
  331.    On these occasions, you should use the `-k' or `--keep-going' flag.
  332. This tells `make' to continue to consider the other dependencies of the
  333. pending targets, remaking them if necessary, before it gives up and
  334. returns nonzero status.  For example, after an error in compiling one
  335. object file, `make -k' will continue compiling other object files even
  336. though it already knows that linking them will be impossible.  In
  337. addition to continuing after failed shell commands, `make -k' will
  338. continue as much as possible after discovering that it does not know
  339. how to make a target or dependency file.  This will always cause an
  340. error message, but without `-k', it is a fatal error (*note Summary of
  341. Options: Options Summary.).
  342.  
  343.    The usual behavior of `make' assumes that your purpose is to get the
  344. goals up to date; once `make' learns that this is impossible, it might
  345. as well report the failure immediately.  The `-k' flag says that the
  346. real purpose is to test as much as possible of the changes made in the
  347. program, perhaps to find several independent problems so that you can
  348. correct them all before the next attempt to compile.  This is why Emacs'
  349. `M-x compile' command passes the `-k' flag by default.
  350.  
  351. File: make.info,  Node: Options Summary,  Prev: Testing,  Up: Running
  352.  
  353. Summary of Options
  354. ==================
  355.  
  356.    Here is a table of all the options `make' understands:
  357.  
  358. `-b'
  359. `-m'
  360.      These options are ignored for compatibility with other versions of
  361.      `make'.
  362.  
  363. `-C DIR'
  364. `--directory=DIR'
  365.      Change to directory DIR before reading the makefiles.  If multiple
  366.      `-C' options are specified, each is interpreted relative to the
  367.      previous one: `-C / -C etc' is equivalent to `-C /etc'.  This is
  368.      typically used with recursive invocations of `make' (*note
  369.      Recursive Use of `make': Recursion.).
  370.  
  371. `-d'
  372. `--debug'
  373.      Print debugging information in addition to normal processing.  The
  374.      debugging information says which files are being considered for
  375.      remaking, which file-times are being compared and with what
  376.      results, which files actually need to be remade, which implicit
  377.      rules are considered and which are applied--everything interesting
  378.      about how `make' decides what to do.
  379.  
  380. `-e'
  381. `--environment-overrides'
  382.      Give variables taken from the environment precedence over
  383.      variables from makefiles.  *Note Variables from the Environment:
  384.      Environment.
  385.  
  386. `-f FILE'
  387. `--file=FILE'
  388. `--makefile=FILE'
  389.      Read the file named FILE as a makefile.  *Note Writing Makefiles:
  390.      Makefiles.
  391.  
  392. `-h'
  393. `--help'
  394.      Remind you of the options that `make' understands and then exit.
  395.  
  396. `-i'
  397. `--ignore-errors'
  398.      Ignore all errors in commands executed to remake files.  *Note
  399.      Errors in Commands: Errors.
  400.  
  401. `-I DIR'
  402. `--include-dir=DIR'
  403.      Specifies a directory DIR to search for included makefiles.  *Note
  404.      Including Other Makefiles: Include.  If several `-I' options are
  405.      used to specify several directories, the directories are searched
  406.      in the order specified.
  407.  
  408. `-j [JOBS]'
  409. `--jobs=[JOBS]'
  410.      Specifies the number of jobs (commands) to run simultaneously.
  411.      With no argument, `make' runs as many jobs simultaneously as
  412.      possible.  If there is more than one `-j' option, the last one is
  413.      effective.  *Note Parallel Execution: Parallel, for more
  414.      information on how commands are run.  Note that this option is
  415.      ignored on MS-DOS.
  416.  
  417. `-k'
  418. `--keep-going'
  419.      Continue as much as possible after an error.  While the target that
  420.      failed, and those that depend on it, cannot be remade, the other
  421.      dependencies of these targets can be processed all the same.
  422.      *Note Testing the Compilation of a Program: Testing.
  423.  
  424. `-l [LOAD]'
  425. `--load-average[=LOAD]'
  426. `--max-load[=LOAD]'
  427.      Specifies that no new jobs (commands) should be started if there
  428.      are other jobs running and the load average is at least LOAD (a
  429.      floating-point number).  With no argument, removes a previous load
  430.      limit.  *Note Parallel Execution: Parallel.
  431.  
  432. `-n'
  433. `--just-print'
  434. `--dry-run'
  435. `--recon'
  436.      Print the commands that would be executed, but do not execute them.
  437.      *Note Instead of Executing the Commands: Instead of Execution.
  438.  
  439. `-o FILE'
  440. `--old-file=FILE'
  441. `--assume-old=FILE'
  442.      Do not remake the file FILE even if it is older than its
  443.      dependencies, and do not remake anything on account of changes in
  444.      FILE.  Essentially the file is treated as very old and its rules
  445.      are ignored.  *Note Avoiding Recompilation of Some Files: Avoiding
  446.      Compilation.
  447.  
  448. `-p'
  449. `--print-data-base'
  450.      Print the data base (rules and variable values) that results from
  451.      reading the makefiles; then execute as usual or as otherwise
  452.      specified.  This also prints the version information given by the
  453.      `-v' switch (see below).  To print the data base without trying to
  454.      remake any files, use `make -p -f /dev/null'.
  455.  
  456. `-q'
  457. `--question'
  458.      "Question mode".  Do not run any commands, or print anything; just
  459.      return an exit status that is zero if the specified targets are
  460.      already up to date, one if any remaking is required, or two if an
  461.      error is encountered.  *Note Instead of Executing the Commands:
  462.      Instead of Execution.
  463.  
  464. `-r'
  465. `--no-builtin-rules'
  466.      Eliminate use of the built-in implicit rules (*note Using Implicit
  467.      Rules: Implicit Rules.).  You can still define your own by writing
  468.      pattern rules (*note Defining and Redefining Pattern Rules:
  469.      Pattern Rules.).  The `-r' option also clears out the default list
  470.      of suffixes for suffix rules (*note Old-Fashioned Suffix Rules:
  471.      Suffix Rules.).  But you can still define your own suffixes with a
  472.      rule for `.SUFFIXES', and then define your own suffix rules.
  473.  
  474. `-s'
  475. `--silent'
  476. `--quiet'
  477.      Silent operation; do not print the commands as they are executed.
  478.      *Note Command Echoing: Echoing.
  479.  
  480. `-S'
  481. `--no-keep-going'
  482. `--stop'
  483.      Cancel the effect of the `-k' option.  This is never necessary
  484.      except in a recursive `make' where `-k' might be inherited from
  485.      the top-level `make' via `MAKEFLAGS' (*note Recursive Use of
  486.      `make': Recursion.) or if you set `-k' in `MAKEFLAGS' in your
  487.      environment.
  488.  
  489. `-t'
  490. `--touch'
  491.      Touch files (mark them up to date without really changing them)
  492.      instead of running their commands.  This is used to pretend that
  493.      the commands were done, in order to fool future invocations of
  494.      `make'.  *Note Instead of Executing the Commands: Instead of
  495.      Execution.
  496.  
  497. `-v'
  498. `--version'
  499.      Print the version of the `make' program plus a copyright, a list
  500.      of authors, and a notice that there is no warranty; then exit.
  501.  
  502. `-w'
  503. `--print-directory'
  504.      Print a message containing the working directory both before and
  505.      after executing the makefile.  This may be useful for tracking
  506.      down errors from complicated nests of recursive `make' commands.
  507.      *Note Recursive Use of `make': Recursion.  (In practice, you
  508.      rarely need to specify this option since `make' does it for you;
  509.      see *Note The `--print-directory' Option: -w Option.)
  510.  
  511. `--no-print-directory'
  512.      Disable printing of the working directory under `-w'.  This option
  513.      is useful when `-w' is turned on automatically, but you do not
  514.      want to see the extra messages.  *Note The `--print-directory'
  515.      Option: -w Option.
  516.  
  517. `-W FILE'
  518. `--what-if=FILE'
  519. `--new-file=FILE'
  520. `--assume-new=FILE'
  521.      Pretend that the target FILE has just been modified.  When used
  522.      with the `-n' flag, this shows you what would happen if you were
  523.      to modify that file.  Without `-n', it is almost the same as
  524.      running a `touch' command on the given file before running `make',
  525.      except that the modification time is changed only in the
  526.      imagination of `make'.  *Note Instead of Executing the Commands:
  527.      Instead of Execution.
  528.  
  529. `--warn-undefined-variables'
  530.      Issue a warning message whenever `make' sees a reference to an
  531.      undefined variable.  This can be helpful when you are trying to
  532.      debug makefiles which use variables in complex ways.
  533.  
  534. File: make.info,  Node: Implicit Rules,  Next: Archives,  Prev: Running,  Up: Top
  535.  
  536. Using Implicit Rules
  537. ********************
  538.  
  539.    Certain standard ways of remaking target files are used very often.
  540. For example, one customary way to make an object file is from a C
  541. source file using the C compiler, `cc'.
  542.  
  543.    "Implicit rules" tell `make' how to use customary techniques so that
  544. you do not have to specify them in detail when you want to use them.
  545. For example, there is an implicit rule for C compilation.  File names
  546. determine which implicit rules are run.  For example, C compilation
  547. typically takes a `.c' file and makes a `.o' file.  So `make' applies
  548. the implicit rule for C compilation when it sees this combination of
  549. file name endings.
  550.  
  551.    A chain of implicit rules can apply in sequence; for example, `make'
  552. will remake a `.o' file from a `.y' file by way of a `.c' file.
  553.  
  554.    The built-in implicit rules use several variables in their commands
  555. so that, by changing the values of the variables, you can change the
  556. way the implicit rule works.  For example, the variable `CFLAGS'
  557. controls the flags given to the C compiler by the implicit rule for C
  558. compilation.
  559.  
  560.    You can define your own implicit rules by writing "pattern rules".
  561.  
  562.    "Suffix rules" are a more limited way to define implicit rules.
  563. Pattern rules are more general and clearer, but suffix rules are
  564. retained for compatibility.
  565.  
  566. * Menu:
  567.  
  568. * Using Implicit::              How to use an existing implicit rule
  569.                                   to get the commands for updating a file.
  570. * Catalogue of Rules::          A list of built-in implicit rules.
  571. * Implicit Variables::          How to change what predefined rules do.
  572. * Chained Rules::               How to use a chain of implicit rules.
  573. * Pattern Rules::               How to define new implicit rules.
  574. * Last Resort::                 How to defining commands for rules
  575.                                   which cannot find any.
  576. * Suffix Rules::                The old-fashioned style of implicit rule.
  577. * Implicit Rule Search::        The precise algorithm for applying
  578.                                   implicit rules.
  579.  
  580. File: make.info,  Node: Using Implicit,  Next: Catalogue of Rules,  Up: Implicit Rules
  581.  
  582. Using Implicit Rules
  583. ====================
  584.  
  585.    To allow `make' to find a customary method for updating a target
  586. file, all you have to do is refrain from specifying commands yourself.
  587. Either write a rule with no command lines, or don't write a rule at
  588. all.  Then `make' will figure out which implicit rule to use based on
  589. which kind of source file exists or can be made.
  590.  
  591.    For example, suppose the makefile looks like this:
  592.  
  593.      foo : foo.o bar.o
  594.              cc -o foo foo.o bar.o $(CFLAGS) $(LDFLAGS)
  595.  
  596. Because you mention `foo.o' but do not give a rule for it, `make' will
  597. automatically look for an implicit rule that tells how to update it.
  598. This happens whether or not the file `foo.o' currently exists.
  599.  
  600.    If an implicit rule is found, it can supply both commands and one or
  601. more dependencies (the source files).  You would want to write a rule
  602. for `foo.o' with no command lines if you need to specify additional
  603. dependencies, such as header files, that the implicit rule cannot
  604. supply.
  605.  
  606.    Each implicit rule has a target pattern and dependency patterns.
  607. There may be many implicit rules with the same target pattern.  For
  608. example, numerous rules make `.o' files: one, from a `.c' file with the
  609. C compiler; another, from a `.p' file with the Pascal compiler; and so
  610. on.  The rule that actually applies is the one whose dependencies exist
  611. or can be made.  So, if you have a file `foo.c', `make' will run the C
  612. compiler; otherwise, if you have a file `foo.p', `make' will run the
  613. Pascal compiler; and so on.
  614.  
  615.    Of course, when you write the makefile, you know which implicit rule
  616. you want `make' to use, and you know it will choose that one because you
  617. know which possible dependency files are supposed to exist.  *Note
  618. Catalogue of Implicit Rules: Catalogue of Rules, for a catalogue of all
  619. the predefined implicit rules.
  620.  
  621.    Above, we said an implicit rule applies if the required dependencies
  622. "exist or can be made".  A file "can be made" if it is mentioned
  623. explicitly in the makefile as a target or a dependency, or if an
  624. implicit rule can be recursively found for how to make it.  When an
  625. implicit dependency is the result of another implicit rule, we say that
  626. "chaining" is occurring.  *Note Chains of Implicit Rules: Chained Rules.
  627.  
  628.    In general, `make' searches for an implicit rule for each target, and
  629. for each double-colon rule, that has no commands.  A file that is
  630. mentioned only as a dependency is considered a target whose rule
  631. specifies nothing, so implicit rule search happens for it.  *Note
  632. Implicit Rule Search Algorithm: Implicit Rule Search, for the details
  633. of how the search is done.
  634.  
  635.    Note that explicit dependencies do not influence implicit rule
  636. search.  For example, consider this explicit rule:
  637.  
  638.      foo.o: foo.p
  639.  
  640. The dependency on `foo.p' does not necessarily mean that `make' will
  641. remake `foo.o' according to the implicit rule to make an object file, a
  642. `.o' file, from a Pascal source file, a `.p' file.  For example, if
  643. `foo.c' also exists, the implicit rule to make an object file from a C
  644. source file is used instead, because it appears before the Pascal rule
  645. in the list of predefined implicit rules (*note Catalogue of Implicit
  646. Rules: Catalogue of Rules.).
  647.  
  648.    If you do not want an implicit rule to be used for a target that has
  649. no commands, you can give that target empty commands by writing a
  650. semicolon (*note Defining Empty Commands: Empty Commands.).
  651.  
  652. File: make.info,  Node: Catalogue of Rules,  Next: Implicit Variables,  Prev: Using Implicit,  Up: Implicit Rules
  653.  
  654. Catalogue of Implicit Rules
  655. ===========================
  656.  
  657.    Here is a catalogue of predefined implicit rules which are always
  658. available unless the makefile explicitly overrides or cancels them.
  659. *Note Canceling Implicit Rules: Canceling Rules, for information on
  660. canceling or overriding an implicit rule.  The `-r' or
  661. `--no-builtin-rules' option cancels all predefined rules.
  662.  
  663.    Not all of these rules will always be defined, even when the `-r'
  664. option is not given.  Many of the predefined implicit rules are
  665. implemented in `make' as suffix rules, so which ones will be defined
  666. depends on the "suffix list" (the list of dependencies of the special
  667. target `.SUFFIXES').  The default suffix list is: `.out', `.a', `.ln',
  668. `.o', `.c', `.cc', `.C', `.p', `.f', `.F', `.r', `.y', `.l', `.s',
  669. `.S', `.mod', `.sym', `.def', `.h', `.info', `.dvi', `.tex', `.texinfo',
  670. `.texi', `.txinfo', `.w', `.ch' `.web', `.sh', `.elc', `.el'.  All of
  671. the implicit rules described below whose dependencies have one of these
  672. suffixes are actually suffix rules.  If you modify the suffix list, the
  673. only predefined suffix rules in effect will be those named by one or
  674. two of the suffixes that are on the list you specify; rules whose
  675. suffixes fail to be on the list are disabled.  *Note Old-Fashioned
  676. Suffix Rules: Suffix Rules, for full details on suffix rules.
  677.  
  678. Compiling C programs
  679.      `N.o' is made automatically from `N.c' with a command of the form
  680.      `$(CC) -c $(CPPFLAGS) $(CFLAGS)'.
  681.  
  682. Compiling C++ programs
  683.      `N.o' is made automatically from `N.cc' or `N.C' with a command of
  684.      the form `$(CXX) -c $(CPPFLAGS) $(CXXFLAGS)'.  We encourage you to
  685.      use the suffix `.cc' for C++ source files instead of `.C'.
  686.  
  687. Compiling Pascal programs
  688.      `N.o' is made automatically from `N.p' with the command `$(PC) -c
  689.      $(PFLAGS)'.
  690.  
  691. Compiling Fortran and Ratfor programs
  692.      `N.o' is made automatically from `N.r', `N.F' or `N.f' by running
  693.      the Fortran compiler.  The precise command used is as follows:
  694.  
  695.     `.f'
  696.           `$(FC) -c $(FFLAGS)'.
  697.  
  698.     `.F'
  699.           `$(FC) -c $(FFLAGS) $(CPPFLAGS)'.
  700.  
  701.     `.r'
  702.           `$(FC) -c $(FFLAGS) $(RFLAGS)'.
  703.  
  704. Preprocessing Fortran and Ratfor programs
  705.      `N.f' is made automatically from `N.r' or `N.F'.  This rule runs
  706.      just the preprocessor to convert a Ratfor or preprocessable
  707.      Fortran program into a strict Fortran program.  The precise
  708.      command used is as follows:
  709.  
  710.     `.F'
  711.           `$(FC) -F $(CPPFLAGS) $(FFLAGS)'.
  712.  
  713.     `.r'
  714.           `$(FC) -F $(FFLAGS) $(RFLAGS)'.
  715.  
  716. Compiling Modula-2 programs
  717.      `N.sym' is made from `N.def' with a command of the form `$(M2C)
  718.      $(M2FLAGS) $(DEFFLAGS)'.  `N.o' is made from `N.mod'; the form is:
  719.      `$(M2C) $(M2FLAGS) $(MODFLAGS)'.
  720.  
  721. Assembling and preprocessing assembler programs
  722.      `N.o' is made automatically from `N.s' by running the assembler,
  723.      `as'.  The precise command is `$(AS) $(ASFLAGS)'.
  724.  
  725.      `N.s' is made automatically from `N.S' by running the C
  726.      preprocessor, `cpp'.  The precise command is `$(CPP) $(CPPFLAGS)'.
  727.  
  728. Linking a single object file
  729.      `N' is made automatically from `N.o' by running the linker
  730.      (usually called `ld') via the C compiler.  The precise command
  731.      used is `$(CC) $(LDFLAGS) N.o $(LOADLIBES)'.
  732.  
  733.      This rule does the right thing for a simple program with only one
  734.      source file.  It will also do the right thing if there are multiple
  735.      object files (presumably coming from various other source files),
  736.      one of which has a name matching that of the executable file.
  737.      Thus,
  738.  
  739.           x: y.o z.o
  740.  
  741.      when `x.c', `y.c' and `z.c' all exist will execute:
  742.  
  743.           cc -c x.c -o x.o
  744.           cc -c y.c -o y.o
  745.           cc -c z.c -o z.o
  746.           cc x.o y.o z.o -o x
  747.           rm -f x.o
  748.           rm -f y.o
  749.           rm -f z.o
  750.  
  751.      In more complicated cases, such as when there is no object file
  752.      whose name derives from the executable file name, you must write
  753.      an explicit command for linking.
  754.  
  755.      Each kind of file automatically made into `.o' object files will
  756.      be automatically linked by using the compiler (`$(CC)', `$(FC)' or
  757.      `$(PC)'; the C compiler `$(CC)' is used to assemble `.s' files)
  758.      without the `-c' option.  This could be done by using the `.o'
  759.      object files as intermediates, but it is faster to do the
  760.      compiling and linking in one step, so that's how it's done.
  761.  
  762. Yacc for C programs
  763.      `N.c' is made automatically from `N.y' by running Yacc with the
  764.      command `$(YACC) $(YFLAGS)'.
  765.  
  766. Lex for C programs
  767.      `N.c' is made automatically from `N.l' by by running Lex.  The
  768.      actual command is `$(LEX) $(LFLAGS)'.
  769.  
  770. Lex for Ratfor programs
  771.      `N.r' is made automatically from `N.l' by by running Lex.  The
  772.      actual command is `$(LEX) $(LFLAGS)'.
  773.  
  774.      The convention of using the same suffix `.l' for all Lex files
  775.      regardless of whether they produce C code or Ratfor code makes it
  776.      impossible for `make' to determine automatically which of the two
  777.      languages you are using in any particular case.  If `make' is
  778.      called upon to remake an object file from a `.l' file, it must
  779.      guess which compiler to use.  It will guess the C compiler, because
  780.      that is more common.  If you are using Ratfor, make sure `make'
  781.      knows this by mentioning `N.r' in the makefile.  Or, if you are
  782.      using Ratfor exclusively, with no C files, remove `.c' from the
  783.      list of implicit rule suffixes with:
  784.  
  785.           .SUFFIXES:
  786.           .SUFFIXES: .o .r .f .l ...
  787.  
  788. Making Lint Libraries from C, Yacc, or Lex programs
  789.      `N.ln' is made from `N.c' by running `lint'.  The precise command
  790.      is `$(LINT) $(LINTFLAGS) $(CPPFLAGS) -i'.  The same command is
  791.      used on the C code produced from `N.y' or `N.l'.
  792.  
  793. TeX and Web
  794.      `N.dvi' is made from `N.tex' with the command `$(TEX)'.  `N.tex'
  795.      is made from `N.web' with `$(WEAVE)', or from `N.w' (and from
  796.      `N.ch' if it exists or can be made) with `$(CWEAVE)'.  `N.p' is
  797.      made from `N.web' with `$(TANGLE)' and `N.c' is made from `N.w'
  798.      (and from `N.ch' if it exists or can be made) with `$(CTANGLE)'.
  799.  
  800. Texinfo and Info
  801.      `N.dvi' is made from `N.texinfo', `N.texi', or `N.txinfo', with
  802.      the command `$(TEXI2DVI) $(TEXI2DVI_FLAGS)'.  `N.info' is made from
  803.      `N.texinfo', `N.texi', or `N.txinfo', with the command
  804.      `$(MAKEINFO) $(MAKEINFO_FLAGS)'.
  805.  
  806. RCS
  807.      Any file `N' is extracted if necessary from an RCS file named
  808.      either `N,v' or `RCS/N,v'.  The precise command used is
  809.      `$(CO) $(COFLAGS)'.  `N' will not be extracted from RCS if it
  810.      already exists, even if the RCS file is newer.  The rules for RCS
  811.      are terminal (*note Match-Anything Pattern Rules: Match-Anything
  812.      Rules.), so RCS files cannot be generated from another source;
  813.      they must actually exist.
  814.  
  815. SCCS
  816.      Any file `N' is extracted if necessary from an SCCS file named
  817.      either `s.N' or `SCCS/s.N'.  The precise command used is
  818.      `$(GET) $(GFLAGS)'.  The rules for SCCS are terminal (*note
  819.      Match-Anything Pattern Rules: Match-Anything Rules.), so SCCS
  820.      files cannot be generated from another source; they must actually
  821.      exist.
  822.  
  823.      For the benefit of SCCS, a file `N' is copied from `N.sh' and made
  824.      executable (by everyone).  This is for shell scripts that are
  825.      checked into SCCS.  Since RCS preserves the execution permission
  826.      of a file, you do not need to use this feature with RCS.
  827.  
  828.      We recommend that you avoid using of SCCS.  RCS is widely held to
  829.      be superior, and is also free.  By choosing free software in place
  830.      of comparable (or inferior) proprietary software, you support the
  831.      free software movement.
  832.  
  833.    Usually, you want to change only the variables listed in the table
  834. above, which are documented in the following section.
  835.  
  836.    However, the commands in built-in implicit rules actually use
  837. variables such as `COMPILE.c', `LINK.p', and `PREPROCESS.S', whose
  838. values contain the commands listed above.
  839.  
  840.    `make' follows the convention that the rule to compile a `.X' source
  841. file uses the variable `COMPILE.X'.  Similarly, the rule to produce an
  842. executable from a `.X' file uses `LINK.X'; and the rule to preprocess a
  843. `.X' file uses `PREPROCESS.X'.
  844.  
  845.    Every rule that produces an object file uses the variable
  846. `OUTPUT_OPTION'.  `make' defines this variable either to contain `-o
  847. $@', or to be empty, depending on a compile-time option.  You need the
  848. `-o' option to ensure that the output goes into the right file when the
  849. source file is in a different directory, as when using `VPATH' (*note
  850. Directory Search::.).  However, compilers on some systems do not accept
  851. a `-o' switch for object files.  If you use such a system, and use
  852. `VPATH', some compilations will put their output in the wrong place.  A
  853. possible workaround for this problem is to give `OUTPUT_OPTION' the
  854. value `; mv $*.o $@'.
  855.  
  856. File: make.info,  Node: Implicit Variables,  Next: Chained Rules,  Prev: Catalogue of Rules,  Up: Implicit Rules
  857.  
  858. Variables Used by Implicit Rules
  859. ================================
  860.  
  861.    The commands in built-in implicit rules make liberal use of certain
  862. predefined variables.  You can alter these variables in the makefile,
  863. with arguments to `make', or in the environment to alter how the
  864. implicit rules work without redefining the rules themselves.
  865.  
  866.    For example, the command used to compile a C source file actually
  867. says `$(CC) -c $(CFLAGS) $(CPPFLAGS)'.  The default values of the
  868. variables used are `cc' and nothing, resulting in the command `cc -c'.
  869. By redefining `CC' to `ncc', you could cause `ncc' to be used for all C
  870. compilations performed by the implicit rule.  By redefining `CFLAGS' to
  871. be `-g', you could pass the `-g' option to each compilation.  *All*
  872. implicit rules that do C compilation use `$(CC)' to get the program
  873. name for the compiler and *all* include `$(CFLAGS)' among the arguments
  874. given to the compiler.
  875.  
  876.    The variables used in implicit rules fall into two classes: those
  877. that are names of programs (like `CC') and those that contain arguments
  878. for the programs (like `CFLAGS').  (The "name of a program" may also
  879. contain some command arguments, but it must start with an actual
  880. executable program name.)  If a variable value contains more than one
  881. argument, separate them with spaces.
  882.  
  883.    Here is a table of variables used as names of programs in built-in
  884. rules:
  885.  
  886. `AR'
  887.      Archive-maintaining program; default `ar'.
  888.  
  889. `AS'
  890.      Program for doing assembly; default `as'.
  891.  
  892. `CC'
  893.      Program for compiling C programs; default `cc'.
  894.  
  895. `CXX'
  896.      Program for compiling C++ programs; default `g++'.
  897.  
  898. `CO'
  899.      Program for extracting a file from RCS; default `co'.
  900.  
  901. `CPP'
  902.      Program for running the C preprocessor, with results to standard
  903.      output; default `$(CC) -E'.
  904.  
  905. `FC'
  906.      Program for compiling or preprocessing Fortran and Ratfor programs;
  907.      default `f77'.
  908.  
  909. `GET'
  910.      Program for extracting a file from SCCS; default `get'.
  911.  
  912. `LEX'
  913.      Program to use to turn Lex grammars into C programs or Ratfor
  914.      programs; default `lex'.
  915.  
  916. `PC'
  917.      Program for compiling Pascal programs; default `pc'.
  918.  
  919. `YACC'
  920.      Program to use to turn Yacc grammars into C programs; default
  921.      `yacc'.
  922.  
  923. `YACCR'
  924.      Program to use to turn Yacc grammars into Ratfor programs; default
  925.      `yacc -r'.
  926.  
  927. `MAKEINFO'
  928.      Program to convert a Texinfo source file into an Info file; default
  929.      `makeinfo'.
  930.  
  931. `TEX'
  932.      Program to make TeX DVI files from TeX source; default `tex'.
  933.  
  934. `TEXI2DVI'
  935.      Program to make TeX DVI files from Texinfo source; default
  936.      `texi2dvi'.
  937.  
  938. `WEAVE'
  939.      Program to translate Web into TeX; default `weave'.
  940.  
  941. `CWEAVE'
  942.      Program to translate C Web into TeX; default `cweave'.
  943.  
  944. `TANGLE'
  945.      Program to translate Web into Pascal; default `tangle'.
  946.  
  947. `CTANGLE'
  948.      Program to translate C Web into C; default `ctangle'.
  949.  
  950. `RM'
  951.      Command to remove a file; default `rm -f'.
  952.  
  953.    Here is a table of variables whose values are additional arguments
  954. for the programs above.  The default values for all of these is the
  955. empty string, unless otherwise noted.
  956.  
  957. `ARFLAGS'
  958.      Flags to give the archive-maintaining program; default `rv'.
  959.  
  960. `ASFLAGS'
  961.      Extra flags to give to the assembler (when explicitly invoked on a
  962.      `.s' or `.S' file).
  963.  
  964. `CFLAGS'
  965.      Extra flags to give to the C compiler.
  966.  
  967. `CXXFLAGS'
  968.      Extra flags to give to the C++ compiler.
  969.  
  970. `COFLAGS'
  971.      Extra flags to give to the RCS `co' program.
  972.  
  973. `CPPFLAGS'
  974.      Extra flags to give to the C preprocessor and programs that use it
  975.      (the C and Fortran compilers).
  976.  
  977. `FFLAGS'
  978.      Extra flags to give to the Fortran compiler.
  979.  
  980. `GFLAGS'
  981.      Extra flags to give to the SCCS `get' program.
  982.  
  983. `LDFLAGS'
  984.      Extra flags to give to compilers when they are supposed to invoke
  985.      the linker, `ld'.
  986.  
  987. `LFLAGS'
  988.      Extra flags to give to Lex.
  989.  
  990. `PFLAGS'
  991.      Extra flags to give to the Pascal compiler.
  992.  
  993. `RFLAGS'
  994.      Extra flags to give to the Fortran compiler for Ratfor programs.
  995.  
  996. `YFLAGS'
  997.      Extra flags to give to Yacc.
  998.  
  999. File: make.info,  Node: Chained Rules,  Next: Pattern Rules,  Prev: Implicit Variables,  Up: Implicit Rules
  1000.  
  1001. Chains of Implicit Rules
  1002. ========================
  1003.  
  1004.    Sometimes a file can be made by a sequence of implicit rules.  For
  1005. example, a file `N.o' could be made from `N.y' by running first Yacc
  1006. and then `cc'.  Such a sequence is called a "chain".
  1007.  
  1008.    If the file `N.c' exists, or is mentioned in the makefile, no
  1009. special searching is required: `make' finds that the object file can be
  1010. made by C compilation from `N.c'; later on, when considering how to
  1011. make `N.c', the rule for running Yacc is used.  Ultimately both `N.c'
  1012. and `N.o' are updated.
  1013.  
  1014.    However, even if `N.c' does not exist and is not mentioned, `make'
  1015. knows how to envision it as the missing link between `N.o' and `N.y'!
  1016. In this case, `N.c' is called an "intermediate file".  Once `make' has
  1017. decided to use the intermediate file, it is entered in the data base as
  1018. if it had been mentioned in the makefile, along with the implicit rule
  1019. that says how to create it.
  1020.  
  1021.    Intermediate files are remade using their rules just like all other
  1022. files.  But intermediate files are treated differently in two ways.
  1023.  
  1024.    The first difference is what happens if the intermediate file does
  1025. not exist.  If an ordinary file B does not exist, and `make' considers
  1026. a target that depends on B, it invariably creates B and then updates
  1027. the target from B.  But if B is an intermediate file, then `make' can
  1028. leave well enough alone.  It won't bother updating B, or the ultimate
  1029. target, unless some dependency of B is newer than that target or there
  1030. is some other reason to update that target.
  1031.  
  1032.    The second difference is that if `make' *does* create B in order to
  1033. update something else, it deletes B later on after it is no longer
  1034. needed.  Therefore, an intermediate file which did not exist before
  1035. `make' also does not exist after `make'.  `make' reports the deletion
  1036. to you by printing a `rm -f' command showing which file it is deleting.
  1037.  
  1038.    Ordinarily, a file cannot be intermediate if it is mentioned in the
  1039. makefile as a target or dependency.  However, you can explicitly mark a
  1040. file as intermediate by listing it as a dependency of the special target
  1041. `.INTERMEDIATE'.  This takes effect even if the file is mentioned
  1042. explicitly in some other way.
  1043.  
  1044.    You can prevent automatic deletion of an intermediate file by
  1045. marking it as a "secondary" file.  To do this, list it as a dependency
  1046. of the special target `.SECONDARY'.  When a file is secondary, `make'
  1047. will not create the file merely because it does not already exist, but
  1048. `make' does not automatically delete the file.  Marking a file as
  1049. secondary also marks it as intermediate.
  1050.  
  1051.    You can list the target pattern of an implicit rule (such as `%.o')
  1052. as a dependency of the special target `.PRECIOUS' to preserve
  1053. intermediate files made by implicit rules whose target patterns match
  1054. that file's name; see *Note Interrupts::.
  1055.  
  1056.    A chain can involve more than two implicit rules.  For example, it is
  1057. possible to make a file `foo' from `RCS/foo.y,v' by running RCS, Yacc
  1058. and `cc'.  Then both `foo.y' and `foo.c' are intermediate files that
  1059. are deleted at the end.
  1060.  
  1061.    No single implicit rule can appear more than once in a chain.  This
  1062. means that `make' will not even consider such a ridiculous thing as
  1063. making `foo' from `foo.o.o' by running the linker twice.  This
  1064. constraint has the added benefit of preventing any infinite loop in the
  1065. search for an implicit rule chain.
  1066.  
  1067.    There are some special implicit rules to optimize certain cases that
  1068. would otherwise be handled by rule chains.  For example, making `foo'
  1069. from `foo.c' could be handled by compiling and linking with separate
  1070. chained rules, using `foo.o' as an intermediate file.  But what
  1071. actually happens is that a special rule for this case does the
  1072. compilation and linking with a single `cc' command.  The optimized rule
  1073. is used in preference to the step-by-step chain because it comes
  1074. earlier in the ordering of rules.
  1075.  
  1076. File: make.info,  Node: Pattern Rules,  Next: Last Resort,  Prev: Chained Rules,  Up: Implicit Rules
  1077.  
  1078. Defining and Redefining Pattern Rules
  1079. =====================================
  1080.  
  1081.    You define an implicit rule by writing a "pattern rule".  A pattern
  1082. rule looks like an ordinary rule, except that its target contains the
  1083. character `%' (exactly one of them).  The target is considered a
  1084. pattern for matching file names; the `%' can match any nonempty
  1085. substring, while other characters match only themselves.  The
  1086. dependencies likewise use `%' to show how their names relate to the
  1087. target name.
  1088.  
  1089.    Thus, a pattern rule `%.o : %.c' says how to make any file `STEM.o'
  1090. from another file `STEM.c'.
  1091.  
  1092.    Note that expansion using `%' in pattern rules occurs *after* any
  1093. variable or function expansions, which take place when the makefile is
  1094. read.  *Note How to Use Variables: Using Variables, and *Note Functions
  1095. for Transforming Text: Functions.
  1096.  
  1097. * Menu:
  1098.  
  1099. * Pattern Intro::               An introduction to pattern rules.
  1100. * Pattern Examples::            Examples of pattern rules.
  1101. * Automatic::                   How to use automatic variables in the
  1102.                                   commands of implicit rules.
  1103. * Pattern Match::               How patterns match.
  1104. * Match-Anything Rules::        Precautions you should take prior to
  1105.                                   defining rules that can match any
  1106.                                   target file whatever.
  1107. * Canceling Rules::             How to override or cancel built-in rules.
  1108.  
  1109. File: make.info,  Node: Pattern Intro,  Next: Pattern Examples,  Up: Pattern Rules
  1110.  
  1111. Introduction to Pattern Rules
  1112. -----------------------------
  1113.  
  1114.    A pattern rule contains the character `%' (exactly one of them) in
  1115. the target; otherwise, it looks exactly like an ordinary rule.  The
  1116. target is a pattern for matching file names; the `%' matches any
  1117. nonempty substring, while other characters match only themselves.
  1118.  
  1119.    For example, `%.c' as a pattern matches any file name that ends in
  1120. `.c'.  `s.%.c' as a pattern matches any file name that starts with
  1121. `s.', ends in `.c' and is at least five characters long.  (There must
  1122. be at least one character to match the `%'.)  The substring that the
  1123. `%' matches is called the "stem".
  1124.  
  1125.    `%' in a dependency of a pattern rule stands for the same stem that
  1126. was matched by the `%' in the target.  In order for the pattern rule to
  1127. apply, its target pattern must match the file name under consideration,
  1128. and its dependency patterns must name files that exist or can be made.
  1129. These files become dependencies of the target.
  1130.  
  1131.    Thus, a rule of the form
  1132.  
  1133.      %.o : %.c ; COMMAND...
  1134.  
  1135. specifies how to make a file `N.o', with another file `N.c' as its
  1136. dependency, provided that `N.c' exists or can be made.
  1137.  
  1138.    There may also be dependencies that do not use `%'; such a dependency
  1139. attaches to every file made by this pattern rule.  These unvarying
  1140. dependencies are useful occasionally.
  1141.  
  1142.    A pattern rule need not have any dependencies that contain `%', or
  1143. in fact any dependencies at all.  Such a rule is effectively a general
  1144. wildcard.  It provides a way to make any file that matches the target
  1145. pattern.  *Note Last Resort::.
  1146.  
  1147.    Pattern rules may have more than one target.  Unlike normal rules,
  1148. this does not act as many different rules with the same dependencies and
  1149. commands.  If a pattern rule has multiple targets, `make' knows that
  1150. the rule's commands are responsible for making all of the targets.  The
  1151. commands are executed only once to make all the targets.  When searching
  1152. for a pattern rule to match a target, the target patterns of a rule
  1153. other than the one that matches the target in need of a rule are
  1154. incidental: `make' worries only about giving commands and dependencies
  1155. to the file presently in question.  However, when this file's commands
  1156. are run, the other targets are marked as having been updated themselves.
  1157.  
  1158.    The order in which pattern rules appear in the makefile is important
  1159. since this is the order in which they are considered.  Of equally
  1160. applicable rules, only the first one found is used.  The rules you
  1161. write take precedence over those that are built in.  Note however, that
  1162. a rule whose dependencies actually exist or are mentioned always takes
  1163. priority over a rule with dependencies that must be made by chaining
  1164. other implicit rules.
  1165.  
  1166. File: make.info,  Node: Pattern Examples,  Next: Automatic,  Prev: Pattern Intro,  Up: Pattern Rules
  1167.  
  1168. Pattern Rule Examples
  1169. ---------------------
  1170.  
  1171.    Here are some examples of pattern rules actually predefined in
  1172. `make'.  First, the rule that compiles `.c' files into `.o' files:
  1173.  
  1174.      %.o : %.c
  1175.              $(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o $@
  1176.  
  1177. defines a rule that can make any file `X.o' from `X.c'.  The command
  1178. uses the automatic variables `$@' and `$<' to substitute the names of
  1179. the target file and the source file in each case where the rule applies
  1180. (*note Automatic Variables: Automatic.).
  1181.  
  1182.    Here is a second built-in rule:
  1183.  
  1184.      % :: RCS/%,v
  1185.              $(CO) $(COFLAGS) $<
  1186.  
  1187. defines a rule that can make any file `X' whatsoever from a
  1188. corresponding file `X,v' in the subdirectory `RCS'.  Since the target
  1189. is `%', this rule will apply to any file whatever, provided the
  1190. appropriate dependency file exists.  The double colon makes the rule
  1191. "terminal", which means that its dependency may not be an intermediate
  1192. file (*note Match-Anything Pattern Rules: Match-Anything Rules.).
  1193.  
  1194.    This pattern rule has two targets:
  1195.  
  1196.      %.tab.c %.tab.h: %.y
  1197.              bison -d $<
  1198.  
  1199. This tells `make' that the command `bison -d X.y' will make both
  1200. `X.tab.c' and `X.tab.h'.  If the file `foo' depends on the files
  1201. `parse.tab.o' and `scan.o' and the file `scan.o' depends on the file
  1202. `parse.tab.h', when `parse.y' is changed, the command `bison -d parse.y'
  1203. will be executed only once, and the dependencies of both `parse.tab.o'
  1204. and `scan.o' will be satisfied.  (Presumably the file `parse.tab.o'
  1205. will be recompiled from `parse.tab.c' and the file `scan.o' from
  1206. `scan.c', while `foo' is linked from `parse.tab.o', `scan.o', and its
  1207. other dependencies, and it will execute happily ever after.)
  1208.  
  1209.