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

  1. This is Info file make.info, produced by Makeinfo-1.64 from the input
  2. file /ade-src/fsf/make/make.texinfo.
  3.    This file documents the GNU Make utility, which determines
  4. automatically which pieces of a large program need to be recompiled,
  5. and issues the commands to recompile them.
  6.    This is Edition 0.51, last updated 9 May 1996, of `The GNU Make
  7. Manual', for `make', Version 3.75 Beta.
  8.    Copyright (C) 1988, '89, '90, '91, '92, '93, '94, '95, '96
  9. Free Software Foundation, Inc.
  10.    Permission is granted to make and distribute verbatim copies of this
  11. manual provided the copyright notice and this permission notice are
  12. preserved on all copies.
  13.    Permission is granted to copy and distribute modified versions of
  14. this manual under the conditions for verbatim copying, provided that
  15. the entire resulting derived work is distributed under the terms of a
  16. permission notice identical to this one.
  17.    Permission is granted to copy and distribute translations of this
  18. manual into another language, under the above conditions for modified
  19. versions, except that this permission notice may be stated in a
  20. translation approved by the Free Software Foundation.
  21. File: make.info,  Node: Top,  Next: Overview,  Prev: (dir),  Up: (dir)
  22.    The GNU `make' utility automatically determines which pieces of a
  23. large program need to be recompiled, and issues the commands to
  24. recompile them.
  25.    This is Edition 0.51 of the `GNU Make Manual', last updated 9 May
  26. 1996 for `make' Version 3.75 Beta.
  27.    This manual describes `make' and contains the following chapters:
  28. * Menu:
  29. * Overview::                    Overview of `make'.
  30. * Introduction::                An introduction to `make'.
  31. * Makefiles::                   Makefiles tell `make' what to do.
  32. * Rules::                       Rules describe when a file must be remade.
  33. * Commands::                    Commands say how to remake a file.
  34. * Using Variables::             You can use variables to avoid repetition.
  35. * Conditionals::                Use or ignore parts of the makefile based
  36.                                  on the values of variables.
  37. * Functions::                   Many powerful ways to manipulate text.
  38. * make Invocation: Running.     How to invoke `make' on the command line.
  39. * Implicit Rules::              Use implicit rules to treat many files alike,
  40.                                  based on their file names.
  41. * Archives::                    How `make' can update library archives.
  42. * Features::                    Features GNU `make' has over other `make's.
  43. * Missing::                     What GNU `make' lacks from other `make's.
  44. * Makefile Conventions::        Conventions for makefiles in GNU programs.
  45. * Quick Reference::             A quick reference for experienced users.
  46. * Complex Makefile::            A real example of a straightforward,
  47.                                  but nontrivial, makefile.
  48. * Concept Index::               Index of Concepts
  49. * Name Index::                  Index of Functions, Variables, & Directives
  50.  -- The Detailed Node Listing --
  51. Overview of `make'
  52. * Preparing::                   Preparing and Running Make
  53. * Reading::                     On Reading this Text
  54. * Bugs::                        Problems and Bugs
  55. An Introduction to Makefiles
  56. * Rule Introduction::           What a rule looks like.
  57. * Simple Makefile::             A Simple Makefile
  58. * How Make Works::              How `make' Processes This Makefile
  59. * Variables Simplify::          Variables Make Makefiles Simpler
  60. * make Deduces::                Letting `make' Deduce the Commands
  61. * Combine By Dependency::       Another Style of Makefile
  62. * Cleanup::                     Rules for Cleaning the Directory
  63. Writing Makefiles
  64. * Makefile Contents::           What makefiles contain.
  65. * Makefile Names::              How to name your makefile.
  66. * Include::                     How one makefile can use another makefile.
  67. * MAKEFILES Variable::          The environment can specify extra makefiles.
  68. * Remaking Makefiles::          How makefiles get remade.
  69. * Overriding Makefiles::        How to override part of one makefile
  70.                                  with another makefile.
  71. Writing Rules
  72. * Rule Example::                An example explained.
  73. * Rule Syntax::                 General syntax explained.
  74. * Wildcards::                   Using wildcard characters such as `*'.
  75. * Directory Search::            Searching other directories for source files.
  76. * Phony Targets::               Using a target that is not a real file's name.
  77. * Force Targets::               You can use a target without commands
  78.                                   or dependencies to mark other
  79.                                   targets as phony.
  80. * Empty Targets::               When only the date matters and the
  81.                                   files are empty.
  82. * Special Targets::             Targets with special built-in meanings.
  83. * Multiple Targets::            When to make use of several targets in a rule.
  84. * Multiple Rules::              How to use several rules with the same target.
  85. * Static Pattern::              Static pattern rules apply to multiple targets
  86.                                   and can vary the dependencies according to
  87.                                   the target name.
  88. * Double-Colon::                How to use a special kind of rule to allow
  89.                                   several independent rules for one target.
  90. * Automatic Dependencies::      How to automatically generate rules giving
  91.                                  dependencies from the source files themselves.
  92. Using Wildcard Characters in File Names
  93. * Wildcard Examples::           Several examples
  94. * Wildcard Pitfall::            Problems to avoid.
  95. * Wildcard Function::           How to cause wildcard expansion where
  96.                                   it does not normally take place.
  97. Searching Directories for Dependencies
  98. * General Search::              Specifying a search path that applies
  99.                                   to every dependency.
  100. * Selective Search::            Specifying a search path
  101.                                   for a specified class of names.
  102. * Commands/Search::             How to write shell commands that work together
  103.                                   with search paths.
  104. * Implicit/Search::             How search paths affect implicit rules.
  105. * Libraries/Search::            Directory search for link libraries.
  106. Static Pattern Rules
  107. * Static Usage::                The syntax of static pattern rules.
  108. * Static versus Implicit::      When are they better than implicit rules?
  109. Writing the Commands in Rules
  110. * Echoing::                     How to control when commands are echoed.
  111. * Execution::                   How commands are executed.
  112. * Parallel::                    How commands can be executed in parallel.
  113. * Errors::                      What happens after a command execution error.
  114. * Interrupts::                  What happens when a command is interrupted.
  115. * Recursion::                   Invoking `make' from makefiles.
  116. * Sequences::                   Defining canned sequences of commands.
  117. * Empty Commands::              Defining useful, do-nothing commands.
  118. Recursive Use of `make'
  119. * MAKE Variable::               The special effects of using `$(MAKE)'.
  120. * Variables/Recursion::         How to communicate variables to a sub-`make'.
  121. * Options/Recursion::           How to communicate options to a sub-`make'.
  122. * -w Option::                   How the `-w' or `--print-directory' option
  123.                                  helps debug use of recursive `make' commands.
  124. How to Use Variables
  125. * Reference::                   How to use the value of a variable.
  126. * Flavors::                     Variables come in two flavors.
  127. * Advanced::                    Advanced features for referencing a variable.
  128. * Values::                      All the ways variables get their values.
  129. * Setting::                     How to set a variable in the makefile.
  130. * Appending::                   How to append more text to the old value
  131.                                   of a variable.
  132. * Override Directive::          How to set a variable in the makefile even if
  133.                                   the user has set it with a command argument.
  134. * Defining::                    An alternate way to set a variable
  135.                                   to a verbatim string.
  136. * Environment::                 Variable values can come from the environment.
  137. Advanced Features for Reference to Variables
  138. * Substitution Refs::           Referencing a variable with
  139.                                   substitutions on the value.
  140. * Computed Names::              Computing the name of the variable to refer to.
  141. Conditional Parts of Makefiles
  142. * Conditional Example::         Example of a conditional
  143. * Conditional Syntax::          The syntax of conditionals.
  144. * Testing Flags::               Conditionals that test flags.
  145. Functions for Transforming Text
  146. * Syntax of Functions::         How to write a function call.
  147. * Text Functions::              General-purpose text manipulation functions.
  148. * Filename Functions::          Functions for manipulating file names.
  149. * Foreach Function::            Repeat some text with controlled variation.
  150. * Origin Function::             Find where a variable got its value.
  151. * Shell Function::              Substitute the output of a shell command.
  152. How to Run `make'
  153. * Makefile Arguments::          How to specify which makefile to use.
  154. * Goals::                       How to use goal arguments to specify which
  155.                                   parts of the makefile to use.
  156. * Instead of Execution::        How to use mode flags to specify what
  157.                                   kind of thing to do with the commands
  158.                                   in the makefile other than simply
  159.                                   execute them.
  160. * Avoiding Compilation::        How to avoid recompiling certain files.
  161. * Overriding::                  How to override a variable to specify
  162.                                   an alternate compiler and other things.
  163. * Testing::                     How to proceed past some errors, to
  164.                                   test compilation.
  165. * Options Summary::             Summary of Options
  166. Using Implicit Rules
  167. * Using Implicit::              How to use an existing implicit rule
  168.                                   to get the commands for updating a file.
  169. * Catalogue of Rules::          A list of built-in implicit rules.
  170. * Implicit Variables::          How to change what predefined rules do.
  171. * Chained Rules::               How to use a chain of implicit rules.
  172. * Pattern Rules::               How to define new implicit rules.
  173. * Last Resort::                 How to defining commands for rules
  174.                                   which cannot find any.
  175. * Suffix Rules::                The old-fashioned style of implicit rule.
  176. * Search Algorithm::            The precise algorithm for applying
  177.                                   implicit rules.
  178. Defining and Redefining Pattern Rules
  179. * Pattern Intro::               An introduction to pattern rules.
  180. * Pattern Examples::            Examples of pattern rules.
  181. * Automatic::                   How to use automatic variables in the
  182.                                   commands of implicit rules.
  183. * Pattern Match::               How patterns match.
  184. * Match-Anything Rules::        Precautions you should take prior to
  185.                                   defining rules that can match any
  186.                                   target file whatever.
  187. * Canceling Rules::             How to override or cancel built-in rules.
  188. Using `make' to Update Archive Files
  189. * Archive Members::             Archive members as targets.
  190. * Archive Update::              The implicit rule for archive member targets.
  191. * Archive Suffix Rules::        You can write a special kind of suffix rule
  192.                                   for updating archives.
  193. Implicit Rule for Archive Member Targets
  194. * Archive Symbols::             How to update archive symbol directories.
  195. File: make.info,  Node: Overview,  Next: Introduction,  Prev: Top,  Up: Top
  196. Overview of `make'
  197. ******************
  198.    The `make' utility automatically determines which pieces of a large
  199. program need to be recompiled, and issues commands to recompile them.
  200. This manual describes GNU `make', which was implemented by Richard
  201. Stallman and Roland McGrath.  GNU `make' conforms to section 6.2 of
  202. `IEEE Standard 1003.2-1992' (POSIX.2).
  203.    Our examples show C programs, since they are most common, but you
  204. can use `make' with any programming language whose compiler can be run
  205. with a shell command.  Indeed, `make' is not limited to programs.  You
  206. can use it to describe any task where some files must be updated
  207. automatically from others whenever the others change.
  208. * Menu:
  209. * Preparing::                   Preparing and Running Make
  210. * Reading::                     On Reading this Text
  211. * Bugs::                        Problems and Bugs
  212. File: make.info,  Node: Preparing,  Next: Reading,  Up: Overview
  213. Preparing and Running Make
  214. ==========================
  215.    To prepare to use `make', you must write a file called the
  216. "makefile" that describes the relationships among files in your program
  217. and provides commands for updating each file.  In a program, typically,
  218. the executable file is updated from object files, which are in turn
  219. made by compiling source files.
  220.    Once a suitable makefile exists, each time you change some source
  221. files, this simple shell command:
  222.      make
  223. suffices to perform all necessary recompilations.  The `make' program
  224. uses the makefile data base and the last-modification times of the
  225. files to decide which of the files need to be updated.  For each of
  226. those files, it issues the commands recorded in the data base.
  227.    You can provide command line arguments to `make' to control which
  228. files should be recompiled, or how.  *Note How to Run `make': Running.
  229. File: make.info,  Node: Reading,  Next: Bugs,  Prev: Preparing,  Up: Overview
  230. How to Read This Manual
  231. =======================
  232.    If you are new to `make', or are looking for a general introduction,
  233. read the first few sections of each chapter, skipping the later
  234. sections.  In each chapter, the first few sections contain introductory
  235. or general information and the later sections contain specialized or
  236. technical information.  The exception is the second chapter, *Note An
  237. Introduction to Makefiles: Introduction, all of which is introductory.
  238.    If you are familiar with other `make' programs, see *Note Features
  239. of GNU `make': Features, which lists the enhancements GNU `make' has,
  240. and *Note Incompatibilities and Missing Features: Missing, which
  241. explains the few things GNU `make' lacks that others have.
  242.    For a quick summary, see *Note Options Summary::, *Note Quick
  243. Reference::, and *Note Special Targets::.
  244. File: make.info,  Node: Bugs,  Prev: Reading,  Up: Overview
  245. Problems and Bugs
  246. =================
  247.    If you have problems with GNU `make' or think you've found a bug,
  248. please report it to the developers; we cannot promise to do anything but
  249. we might well want to fix it.
  250.    Before reporting a bug, make sure you've actually found a real bug.
  251. Carefully reread the documentation and see if it really says you can do
  252. what you're trying to do.  If it's not clear whether you should be able
  253. to do something or not, report that too; it's a bug in the
  254. documentation!
  255.    Before reporting a bug or trying to fix it yourself, try to isolate
  256. it to the smallest possible makefile that reproduces the problem.  Then
  257. send us the makefile and the exact results `make' gave you.  Also say
  258. what you expected to occur; this will help us decide whether the
  259. problem was really in the documentation.
  260.    Once you've got a precise problem, please send electronic mail either
  261. through the Internet or via UUCP:
  262.      Internet address:
  263.          bug-gnu-utils@prep.ai.mit.edu
  264.      
  265.      UUCP path:
  266.          mit-eddie!prep.ai.mit.edu!bug-gnu-utils
  267. Please include the version number of `make' you are using.  You can get
  268. this information with the command `make --version'.  Be sure also to
  269. include the type of machine and operating system you are using.  If
  270. possible, include the contents of the file `config.h' that is generated
  271. by the configuration process.
  272. File: make.info,  Node: Introduction,  Next: Makefiles,  Prev: Overview,  Up: Top
  273. An Introduction to Makefiles
  274. ****************************
  275.    You need a file called a "makefile" to tell `make' what to do.  Most
  276. often, the makefile tells `make' how to compile and link a program.
  277.    In this chapter, we will discuss a simple makefile that describes
  278. how to compile and link a text editor which consists of eight C source
  279. files and three header files.  The makefile can also tell `make' how to
  280. run miscellaneous commands when explicitly asked (for example, to remove
  281. certain files as a clean-up operation).  To see a more complex example
  282. of a makefile, see *Note Complex Makefile::.
  283.    When `make' recompiles the editor, each changed C source file must
  284. be recompiled.  If a header file has changed, each C source file that
  285. includes the header file must be recompiled to be safe.  Each
  286. compilation produces an object file corresponding to the source file.
  287. Finally, if any source file has been recompiled, all the object files,
  288. whether newly made or saved from previous compilations, must be linked
  289. together to produce the new executable editor.
  290. * Menu:
  291. * Rule Introduction::           What a rule looks like.
  292. * Simple Makefile::             A Simple Makefile
  293. * How Make Works::              How `make' Processes This Makefile
  294. * Variables Simplify::          Variables Make Makefiles Simpler
  295. * make Deduces::                Letting `make' Deduce the Commands
  296. * Combine By Dependency::       Another Style of Makefile
  297. * Cleanup::                     Rules for Cleaning the Directory
  298. File: make.info,  Node: Rule Introduction,  Next: Simple Makefile,  Up: Introduction
  299. What a Rule Looks Like
  300. ======================
  301.    A simple makefile consists of "rules" with the following shape:
  302.      TARGET ... : DEPENDENCIES ...
  303.              COMMAND
  304.              ...
  305.              ...
  306.    A "target" is usually the name of a file that is generated by a
  307. program; examples of targets are executable or object files.  A target
  308. can also be the name of an action to carry out, such as `clean' (*note
  309. Phony Targets::.).
  310.    A "dependency" is a file that is used as input to create the target.
  311. A target often depends on several files.
  312.    A "command" is an action that `make' carries out.  A rule may have
  313. more than one command, each on its own line.  *Please note:* you need
  314. to put a tab character at the beginning of every command line!  This is
  315. an obscurity that catches the unwary.
  316.    Usually a command is in a rule with dependencies and serves to
  317. create a target file if any of the dependencies change.  However, the
  318. rule that specifies commands for the target need not have dependencies.
  319. For example, the rule containing the delete command associated with the
  320. target `clean' does not have dependencies.
  321.    A "rule", then, explains how and when to remake certain files which
  322. are the targets of the particular rule.  `make' carries out the
  323. commands on the dependencies to create or update the target.  A rule
  324. can also explain how and when to carry out an action.  *Note Writing
  325. Rules: Rules.
  326.    A makefile may contain other text besides rules, but a simple
  327. makefile need only contain rules.  Rules may look somewhat more
  328. complicated than shown in this template, but all fit the pattern more
  329. or less.
  330. File: make.info,  Node: Simple Makefile,  Next: How Make Works,  Prev: Rule Introduction,  Up: Introduction
  331. A Simple Makefile
  332. =================
  333.    Here is a straightforward makefile that describes the way an
  334. executable file called `edit' depends on eight object files which, in
  335. turn, depend on eight C source and three header files.
  336.    In this example, all the C files include `defs.h', but only those
  337. defining editing commands include `command.h', and only low level files
  338. that change the editor buffer include `buffer.h'.
  339.      edit : main.o kbd.o command.o display.o \
  340.             insert.o search.o files.o utils.o
  341.              cc -o edit main.o kbd.o command.o display.o \
  342.                         insert.o search.o files.o utils.o
  343.      
  344.      main.o : main.c defs.h
  345.              cc -c main.c
  346.      kbd.o : kbd.c defs.h command.h
  347.              cc -c kbd.c
  348.      command.o : command.c defs.h command.h
  349.              cc -c command.c
  350.      display.o : display.c defs.h buffer.h
  351.              cc -c display.c
  352.      insert.o : insert.c defs.h buffer.h
  353.              cc -c insert.c
  354.      search.o : search.c defs.h buffer.h
  355.              cc -c search.c
  356.      files.o : files.c defs.h buffer.h command.h
  357.              cc -c files.c
  358.      utils.o : utils.c defs.h
  359.              cc -c utils.c
  360.      clean :
  361.              rm edit main.o kbd.o command.o display.o \
  362.                 insert.o search.o files.o utils.o
  363. We split each long line into two lines using backslash-newline; this is
  364. like using one long line, but is easier to read.
  365.    To use this makefile to create the executable file called `edit',
  366. type:
  367.      make
  368.    To use this makefile to delete the executable file and all the object
  369. files from the directory, type:
  370.      make clean
  371.    In the example makefile, the targets include the executable file
  372. `edit', and the object files `main.o' and `kbd.o'.  The dependencies
  373. are files such as `main.c' and `defs.h'.  In fact, each `.o' file is
  374. both a target and a dependency.  Commands include `cc -c main.c' and
  375. `cc -c kbd.c'.
  376.    When a target is a file, it needs to be recompiled or relinked if any
  377. of its dependencies change.  In addition, any dependencies that are
  378. themselves automatically generated should be updated first.  In this
  379. example, `edit' depends on each of the eight object files; the object
  380. file `main.o' depends on the source file `main.c' and on the header
  381. file `defs.h'.
  382.    A shell command follows each line that contains a target and
  383. dependencies.  These shell commands say how to update the target file.
  384. A tab character must come at the beginning of every command line to
  385. distinguish commands lines from other lines in the makefile.  (Bear in
  386. mind that `make' does not know anything about how the commands work.
  387. It is up to you to supply commands that will update the target file
  388. properly.  All `make' does is execute the commands in the rule you have
  389. specified when the target file needs to be updated.)
  390.    The target `clean' is not a file, but merely the name of an action.
  391. Since you normally do not want to carry out the actions in this rule,
  392. `clean' is not a dependency of any other rule.  Consequently, `make'
  393. never does anything with it unless you tell it specifically.  Note that
  394. this rule not only is not a dependency, it also does not have any
  395. dependencies, so the only purpose of the rule is to run the specified
  396. commands.  Targets that do not refer to files but are just actions are
  397. called "phony targets".  *Note Phony Targets::, for information about
  398. this kind of target.  *Note Errors in Commands: Errors, to see how to
  399. cause `make' to ignore errors from `rm' or any other command.
  400. File: make.info,  Node: How Make Works,  Next: Variables Simplify,  Prev: Simple Makefile,  Up: Introduction
  401. How `make' Processes a Makefile
  402. ===============================
  403.    By default, `make' starts with the first target (not targets whose
  404. names start with `.').  This is called the "default goal".  ("Goals"
  405. are the targets that `make' strives ultimately to update.  *Note
  406. Arguments to Specify the Goals: Goals.)
  407.    In the simple example of the previous section, the default goal is to
  408. update the executable program `edit'; therefore, we put that rule first.
  409.    Thus, when you give the command:
  410.      make
  411. `make' reads the makefile in the current directory and begins by
  412. processing the first rule.  In the example, this rule is for relinking
  413. `edit'; but before `make' can fully process this rule, it must process
  414. the rules for the files that `edit' depends on, which in this case are
  415. the object files.  Each of these files is processed according to its
  416. own rule.  These rules say to update each `.o' file by compiling its
  417. source file.  The recompilation must be done if the source file, or any
  418. of the header files named as dependencies, is more recent than the
  419. object file, or if the object file does not exist.
  420.    The other rules are processed because their targets appear as
  421. dependencies of the goal.  If some other rule is not depended on by the
  422. goal (or anything it depends on, etc.), that rule is not processed,
  423. unless you tell `make' to do so (with a command such as `make clean').
  424.    Before recompiling an object file, `make' considers updating its
  425. dependencies, the source file and header files.  This makefile does not
  426. specify anything to be done for them--the `.c' and `.h' files are not
  427. the targets of any rules--so `make' does nothing for these files.  But
  428. `make' would update automatically generated C programs, such as those
  429. made by Bison or Yacc, by their own rules at this time.
  430.    After recompiling whichever object files need it, `make' decides
  431. whether to relink `edit'.  This must be done if the file `edit' does
  432. not exist, or if any of the object files are newer than it.  If an
  433. object file was just recompiled, it is now newer than `edit', so `edit'
  434. is relinked.
  435.    Thus, if we change the file `insert.c' and run `make', `make' will
  436. compile that file to update `insert.o', and then link `edit'.  If we
  437. change the file `command.h' and run `make', `make' will recompile the
  438. object files `kbd.o', `command.o' and `files.o' and then link the file
  439. `edit'.
  440. File: make.info,  Node: Variables Simplify,  Next: make Deduces,  Prev: How Make Works,  Up: Introduction
  441. Variables Make Makefiles Simpler
  442. ================================
  443.    In our example, we had to list all the object files twice in the
  444. rule for `edit' (repeated here):
  445.      edit : main.o kbd.o command.o display.o \
  446.                    insert.o search.o files.o utils.o
  447.              cc -o edit main.o kbd.o command.o display.o \
  448.                         insert.o search.o files.o utils.o
  449.    Such duplication is error-prone; if a new object file is added to the
  450. system, we might add it to one list and forget the other.  We can
  451. eliminate the risk and simplify the makefile by using a variable.
  452. "Variables" allow a text string to be defined once and substituted in
  453. multiple places later (*note How to Use Variables: Using Variables.).
  454.    It is standard practice for every makefile to have a variable named
  455. `objects', `OBJECTS', `objs', `OBJS', `obj', or `OBJ' which is a list
  456. of all object file names.  We would define such a variable `objects'
  457. with a line like this in the makefile:
  458.      objects = main.o kbd.o command.o display.o \
  459.                insert.o search.o files.o utils.o
  460. Then, each place we want to put a list of the object file names, we can
  461. substitute the variable's value by writing `$(objects)' (*note How to
  462. Use Variables: Using Variables.).
  463.    Here is how the complete simple makefile looks when you use a
  464. variable for the object files:
  465.      objects = main.o kbd.o command.o display.o \
  466.                insert.o search.o files.o utils.o
  467.      
  468.      edit : $(objects)
  469.              cc -o edit $(objects)
  470.      main.o : main.c defs.h
  471.              cc -c main.c
  472.      kbd.o : kbd.c defs.h command.h
  473.              cc -c kbd.c
  474.      command.o : command.c defs.h command.h
  475.              cc -c command.c
  476.      display.o : display.c defs.h buffer.h
  477.              cc -c display.c
  478.      insert.o : insert.c defs.h buffer.h
  479.              cc -c insert.c
  480.      search.o : search.c defs.h buffer.h
  481.              cc -c search.c
  482.      files.o : files.c defs.h buffer.h command.h
  483.              cc -c files.c
  484.      utils.o : utils.c defs.h
  485.              cc -c utils.c
  486.      clean :
  487.              rm edit $(objects)
  488. File: make.info,  Node: make Deduces,  Next: Combine By Dependency,  Prev: Variables Simplify,  Up: Introduction
  489. Letting `make' Deduce the Commands
  490. ==================================
  491.    It is not necessary to spell out the commands for compiling the
  492. individual C source files, because `make' can figure them out: it has an
  493. "implicit rule" for updating a `.o' file from a correspondingly named
  494. `.c' file using a `cc -c' command.  For example, it will use the
  495. command `cc -c main.c -o main.o' to compile `main.c' into `main.o'.  We
  496. can therefore omit the commands from the rules for the object files.
  497. *Note Using Implicit Rules: Implicit Rules.
  498.    When a `.c' file is used automatically in this way, it is also
  499. automatically added to the list of dependencies.  We can therefore omit
  500. the `.c' files from the dependencies, provided we omit the commands.
  501.    Here is the entire example, with both of these changes, and a
  502. variable `objects' as suggested above:
  503.      objects = main.o kbd.o command.o display.o \
  504.                insert.o search.o files.o utils.o
  505.      
  506.      edit : $(objects)
  507.              cc -o edit $(objects)
  508.      
  509.      main.o : defs.h
  510.      kbd.o : defs.h command.h
  511.      command.o : defs.h command.h
  512.      display.o : defs.h buffer.h
  513.      insert.o : defs.h buffer.h
  514.      search.o : defs.h buffer.h
  515.      files.o : defs.h buffer.h command.h
  516.      utils.o : defs.h
  517.      
  518.      .PHONY : clean
  519.      clean :
  520.              -rm edit $(objects)
  521. This is how we would write the makefile in actual practice.  (The
  522. complications associated with `clean' are described elsewhere.  See
  523. *Note Phony Targets::, and *Note Errors in Commands: Errors.)
  524.    Because implicit rules are so convenient, they are important.  You
  525. will see them used frequently.
  526. File: make.info,  Node: Combine By Dependency,  Next: Cleanup,  Prev: make Deduces,  Up: Introduction
  527. Another Style of Makefile
  528. =========================
  529.    When the objects of a makefile are created only by implicit rules, an
  530. alternative style of makefile is possible.  In this style of makefile,
  531. you group entries by their dependencies instead of by their targets.
  532. Here is what one looks like:
  533.      objects = main.o kbd.o command.o display.o \
  534.                insert.o search.o files.o utils.o
  535.      
  536.      edit : $(objects)
  537.              cc -o edit $(objects)
  538.      
  539.      $(objects) : defs.h
  540.      kbd.o command.o files.o : command.h
  541.      display.o insert.o search.o files.o : buffer.h
  542. Here `defs.h' is given as a dependency of all the object files;
  543. `command.h' and `buffer.h' are dependencies of the specific object
  544. files listed for them.
  545.    Whether this is better is a matter of taste: it is more compact, but
  546. some people dislike it because they find it clearer to put all the
  547. information about each target in one place.
  548. File: make.info,  Node: Cleanup,  Prev: Combine By Dependency,  Up: Introduction
  549. Rules for Cleaning the Directory
  550. ================================
  551.    Compiling a program is not the only thing you might want to write
  552. rules for.  Makefiles commonly tell how to do a few other things besides
  553. compiling a program: for example, how to delete all the object files
  554. and executables so that the directory is `clean'.
  555.    Here is how we could write a `make' rule for cleaning our example
  556. editor:
  557.      clean:
  558.              rm edit $(objects)
  559.    In practice, we might want to write the rule in a somewhat more
  560. complicated manner to handle unanticipated situations.  We would do
  561. this:
  562.      .PHONY : clean
  563.      clean :
  564.              -rm edit $(objects)
  565. This prevents `make' from getting confused by an actual file called
  566. `clean' and causes it to continue in spite of errors from `rm'.  (See
  567. *Note Phony Targets::, and *Note Errors in Commands: Errors.)
  568. A rule such as this should not be placed at the beginning of the
  569. makefile, because we do not want it to run by default!  Thus, in the
  570. example makefile, we want the rule for `edit', which recompiles the
  571. editor, to remain the default goal.
  572.    Since `clean' is not a dependency of `edit', this rule will not run
  573. at all if we give the command `make' with no arguments.  In order to
  574. make the rule run, we have to type `make clean'.  *Note How to Run
  575. `make': Running.
  576. File: make.info,  Node: Makefiles,  Next: Rules,  Prev: Introduction,  Up: Top
  577. Writing Makefiles
  578. *****************
  579.    The information that tells `make' how to recompile a system comes
  580. from reading a data base called the "makefile".
  581. * Menu:
  582. * Makefile Contents::           What makefiles contain.
  583. * Makefile Names::              How to name your makefile.
  584. * Include::                     How one makefile can use another makefile.
  585. * MAKEFILES Variable::          The environment can specify extra makefiles.
  586. * Remaking Makefiles::          How makefiles get remade.
  587. * Overriding Makefiles::        How to override part of one makefile
  588.                                  with another makefile.
  589. File: make.info,  Node: Makefile Contents,  Next: Makefile Names,  Up: Makefiles
  590. What Makefiles Contain
  591. ======================
  592.    Makefiles contain five kinds of things: "explicit rules", "implicit
  593. rules", "variable definitions", "directives", and "comments".  Rules,
  594. variables, and directives are described at length in later chapters.
  595.    * An "explicit rule" says when and how to remake one or more files,
  596.      called the rule's targets.  It lists the other files that the
  597.      targets "depend on", and may also give commands to use to create
  598.      or update the targets.  *Note Writing Rules: Rules.
  599.    * An "implicit rule" says when and how to remake a class of files
  600.      based on their names.  It describes how a target may depend on a
  601.      file with a name similar to the target and gives commands to
  602.      create or update such a target.  *Note Using Implicit Rules:
  603.      Implicit Rules.
  604.    * A "variable definition" is a line that specifies a text string
  605.      value for a variable that can be substituted into the text later.
  606.      The simple makefile example shows a variable definition for
  607.      `objects' as a list of all object files (*note Variables Make
  608.      Makefiles Simpler: Variables Simplify.).
  609.    * A "directive" is a command for `make' to do something special while
  610.      reading the makefile.  These include:
  611.         * Reading another makefile (*note Including Other Makefiles:
  612.           Include.).
  613.         * Deciding (based on the values of variables) whether to use or
  614.           ignore a part of the makefile (*note Conditional Parts of
  615.           Makefiles: Conditionals.).
  616.         * Defining a variable from a verbatim string containing
  617.           multiple lines (*note Defining Variables Verbatim: Defining.).
  618.    * `#' in a line of a makefile starts a "comment".  It and the rest of
  619.      the line are ignored, except that a trailing backslash not escaped
  620.      by another backslash will continue the comment across multiple
  621.      lines.  Comments may appear on any of the lines in the makefile,
  622.      except within a `define' directive, and perhaps within commands
  623.      (where the shell decides what is a comment).  A line containing
  624.      just a comment (with perhaps spaces before it) is effectively
  625.      blank, and is ignored.
  626. File: make.info,  Node: Makefile Names,  Next: Include,  Prev: Makefile Contents,  Up: Makefiles
  627. What Name to Give Your Makefile
  628. ===============================
  629.    By default, when `make' looks for the makefile, it tries the
  630. following names, in order: `GNUmakefile', `makefile' and `Makefile'.
  631.    Normally you should call your makefile either `makefile' or
  632. `Makefile'.  (We recommend `Makefile' because it appears prominently
  633. near the beginning of a directory listing, right near other important
  634. files such as `README'.)  The first name checked, `GNUmakefile', is not
  635. recommended for most makefiles.  You should use this name if you have a
  636. makefile that is specific to GNU `make', and will not be understood by
  637. other versions of `make'.  Other `make' programs look for `makefile' and
  638. `Makefile', but not `GNUmakefile'.
  639.    If `make' finds none of these names, it does not use any makefile.
  640. Then you must specify a goal with a command argument, and `make' will
  641. attempt to figure out how to remake it using only its built-in implicit
  642. rules.  *Note Using Implicit Rules: Implicit Rules.
  643.    If you want to use a nonstandard name for your makefile, you can
  644. specify the makefile name with the `-f' or `--file' option.  The
  645. arguments `-f NAME' or `--file=NAME' tell `make' to read the file NAME
  646. as the makefile.  If you use more than one `-f' or `--file' option, you
  647. can specify several makefiles.  All the makefiles are effectively
  648. concatenated in the order specified.  The default makefile names
  649. `GNUmakefile', `makefile' and `Makefile' are not checked automatically
  650. if you specify `-f' or `--file'.
  651. File: make.info,  Node: Include,  Next: MAKEFILES Variable,  Prev: Makefile Names,  Up: Makefiles
  652. Including Other Makefiles
  653. =========================
  654.    The `include' directive tells `make' to suspend reading the current
  655. makefile and read one or more other makefiles before continuing.  The
  656. directive is a line in the makefile that looks like this:
  657.      include FILENAMES...
  658. FILENAMES can contain shell file name patterns.
  659.    Extra spaces are allowed and ignored at the beginning of the line,
  660. but a tab is not allowed.  (If the line begins with a tab, it will be
  661. considered a command line.)  Whitespace is required between `include'
  662. and the file names, and between file names; extra whitespace is ignored
  663. there and at the end of the directive.  A comment starting with `#' is
  664. allowed at the end of the line.  If the file names contain any variable
  665. or function references, they are expanded.  *Note How to Use Variables:
  666. Using Variables.
  667.    For example, if you have three `.mk' files, `a.mk', `b.mk', and
  668. `c.mk', and `$(bar)' expands to `bish bash', then the following
  669. expression
  670.      include foo *.mk $(bar)
  671.    is equivalent to
  672.      include foo a.mk b.mk c.mk bish bash
  673.    When `make' processes an `include' directive, it suspends reading of
  674. the containing makefile and reads from each listed file in turn.  When
  675. that is finished, `make' resumes reading the makefile in which the
  676. directive appears.
  677.    One occasion for using `include' directives is when several programs,
  678. handled by individual makefiles in various directories, need to use a
  679. common set of variable definitions (*note Setting Variables: Setting.)
  680. or pattern rules (*note Defining and Redefining Pattern Rules: Pattern
  681. Rules.).
  682.    Another such occasion is when you want to generate dependencies from
  683. source files automatically; the dependencies can be put in a file that
  684. is included by the main makefile.  This practice is generally cleaner
  685. than that of somehow appending the dependencies to the end of the main
  686. makefile as has been traditionally done with other versions of `make'.
  687. *Note Automatic Dependencies::.
  688.    If the specified name does not start with a slash, and the file is
  689. not found in the current directory, several other directories are
  690. searched.  First, any directories you have specified with the `-I' or
  691. `--include-dir' option are searched (*note Summary of Options: Options
  692. Summary.).  Then the following directories (if they exist) are
  693. searched, in this order: `PREFIX/include' (normally
  694. `/usr/local/include') `/usr/gnu/include', `/usr/local/include',
  695. `/usr/include'.
  696.    If an included makefile cannot be found in any of these directories,
  697. a warning message is generated, but it is not an immediately fatal
  698. error; processing of the makefile containing the `include' continues.
  699. Once it has finished reading makefiles, `make' will try to remake any
  700. that are out of date or don't exist.  *Note How Makefiles Are Remade:
  701. Remaking Makefiles.  Only after it has tried to find a way to remake a
  702. makefile and failed, will `make' diagnose the missing makefile as a
  703. fatal error.
  704.    If you want `make' to simply ignore a makefile which does not exist
  705. and cannot be remade, with no error message, use the `-include'
  706. directive instead of `include', like this:
  707.      -include FILENAMES...
  708.    This is acts like `include' in every way except that there is no
  709. error (not even a warning) if any of the FILENAMES do not exist.  For
  710. compatibility with some other `make' implementations, `sinclude' is
  711. another name for `-include'.
  712. File: make.info,  Node: MAKEFILES Variable,  Next: Remaking Makefiles,  Prev: Include,  Up: Makefiles
  713. The Variable `MAKEFILES'
  714. ========================
  715.    If the environment variable `MAKEFILES' is defined, `make' considers
  716. its value as a list of names (separated by whitespace) of additional
  717. makefiles to be read before the others.  This works much like the
  718. `include' directive: various directories are searched for those files
  719. (*note Including Other Makefiles: Include.).  In addition, the default
  720. goal is never taken from one of these makefiles and it is not an error
  721. if the files listed in `MAKEFILES' are not found.
  722.    The main use of `MAKEFILES' is in communication between recursive
  723. invocations of `make' (*note Recursive Use of `make': Recursion.).  It
  724. usually is not desirable to set the environment variable before a
  725. top-level invocation of `make', because it is usually better not to
  726. mess with a makefile from outside.  However, if you are running `make'
  727. without a specific makefile, a makefile in `MAKEFILES' can do useful
  728. things to help the built-in implicit rules work better, such as
  729. defining search paths (*note Directory Search::.).
  730.    Some users are tempted to set `MAKEFILES' in the environment
  731. automatically on login, and program makefiles to expect this to be done.
  732. This is a very bad idea, because such makefiles will fail to work if
  733. run by anyone else.  It is much better to write explicit `include'
  734. directives in the makefiles.  *Note Including Other Makefiles: Include.
  735. File: make.info,  Node: Remaking Makefiles,  Next: Overriding Makefiles,  Prev: MAKEFILES Variable,  Up: Makefiles
  736. How Makefiles Are Remade
  737. ========================
  738.    Sometimes makefiles can be remade from other files, such as RCS or
  739. SCCS files.  If a makefile can be remade from other files, you probably
  740. want `make' to get an up-to-date version of the makefile to read in.
  741.    To this end, after reading in all makefiles, `make' will consider
  742. each as a goal target and attempt to update it.  If a makefile has a
  743. rule which says how to update it (found either in that very makefile or
  744. in another one) or if an implicit rule applies to it (*note Using
  745. Implicit Rules: Implicit Rules.), it will be updated if necessary.
  746. After all makefiles have been checked, if any have actually been
  747. changed, `make' starts with a clean slate and reads all the makefiles
  748. over again.  (It will also attempt to update each of them over again,
  749. but normally this will not change them again, since they are already up
  750. to date.)
  751.    If the makefiles specify a double-colon rule to remake a file with
  752. commands but no dependencies, that file will always be remade (*note
  753. Double-Colon::.).  In the case of makefiles, a makefile that has a
  754. double-colon rule with commands but no dependencies will be remade every
  755. time `make' is run, and then again after `make' starts over and reads
  756. the makefiles in again.  This would cause an infinite loop: `make'
  757. would constantly remake the makefile, and never do anything else.  So,
  758. to avoid this, `make' will *not* attempt to remake makefiles which are
  759. specified as double-colon targets but have no dependencies.
  760.    If you do not specify any makefiles to be read with `-f' or `--file'
  761. options, `make' will try the default makefile names; *note What Name to
  762. Give Your Makefile: Makefile Names..  Unlike makefiles explicitly
  763. requested with `-f' or `--file' options, `make' is not certain that
  764. these makefiles should exist.  However, if a default makefile does not
  765. exist but can be created by running `make' rules, you probably want the
  766. rules to be run so that the makefile can be used.
  767.    Therefore, if none of the default makefiles exists, `make' will try
  768. to make each of them in the same order in which they are searched for
  769. (*note What Name to Give Your Makefile: Makefile Names.) until it
  770. succeeds in making one, or it runs out of names to try.  Note that it
  771. is not an error if `make' cannot find or make any makefile; a makefile
  772. is not always necessary.
  773.    When you use the `-t' or `--touch' option (*note Instead of
  774. Executing the Commands: Instead of Execution.), you would not want to
  775. use an out-of-date makefile to decide which targets to touch.  So the
  776. `-t' option has no effect on updating makefiles; they are really
  777. updated even if `-t' is specified.  Likewise, `-q' (or `--question')
  778. and `-n' (or `--just-print') do not prevent updating of makefiles,
  779. because an out-of-date makefile would result in the wrong output for
  780. other targets.  Thus, `make -f mfile -n foo' will update `mfile', read
  781. it in, and then print the commands to update `foo' and its dependencies
  782. without running them.  The commands printed for `foo' will be those
  783. specified in the updated contents of `mfile'.
  784.    However, on occasion you might actually wish to prevent updating of
  785. even the makefiles.  You can do this by specifying the makefiles as
  786. goals in the command line as well as specifying them as makefiles.
  787. When the makefile name is specified explicitly as a goal, the options
  788. `-t' and so on do apply to them.
  789.    Thus, `make -f mfile -n mfile foo' would read the makefile `mfile',
  790. print the commands needed to update it without actually running them,
  791. and then print the commands needed to update `foo' without running
  792. them.  The commands for `foo' will be those specified by the existing
  793. contents of `mfile'.
  794. File: make.info,  Node: Overriding Makefiles,  Prev: Remaking Makefiles,  Up: Makefiles
  795. Overriding Part of Another Makefile
  796. ===================================
  797.    Sometimes it is useful to have a makefile that is mostly just like
  798. another makefile.  You can often use the `include' directive to include
  799. one in the other, and add more targets or variable definitions.
  800. However, if the two makefiles give different commands for the same
  801. target, `make' will not let you just do this.  But there is another way.
  802.    In the containing makefile (the one that wants to include the other),
  803. you can use a match-anything pattern rule to say that to remake any
  804. target that cannot be made from the information in the containing
  805. makefile, `make' should look in another makefile.  *Note Pattern
  806. Rules::, for more information on pattern rules.
  807.    For example, if you have a makefile called `Makefile' that says how
  808. to make the target `foo' (and other targets), you can write a makefile
  809. called `GNUmakefile' that contains:
  810.      foo:
  811.              frobnicate > foo
  812.      
  813.      %: force
  814.              @$(MAKE) -f Makefile $@
  815.      force: ;
  816.    If you say `make foo', `make' will find `GNUmakefile', read it, and
  817. see that to make `foo', it needs to run the command `frobnicate > foo'.
  818. If you say `make bar', `make' will find no way to make `bar' in
  819. `GNUmakefile', so it will use the commands from the pattern rule: `make
  820. -f Makefile bar'.  If `Makefile' provides a rule for updating `bar',
  821. `make' will apply the rule.  And likewise for any other target that
  822. `GNUmakefile' does not say how to make.
  823.    The way this works is that the pattern rule has a pattern of just
  824. `%', so it matches any target whatever.  The rule specifies a
  825. dependency `force', to guarantee that the commands will be run even if
  826. the target file already exists.  We give `force' target empty commands
  827. to prevent `make' from searching for an implicit rule to build
  828. it--otherwise it would apply the same match-anything rule to `force'
  829. itself and create a dependency loop!
  830. File: make.info,  Node: Rules,  Next: Commands,  Prev: Makefiles,  Up: Top
  831. Writing Rules
  832. *************
  833.    A "rule" appears in the makefile and says when and how to remake
  834. certain files, called the rule's "targets" (most often only one per
  835. rule).  It lists the other files that are the "dependencies" of the
  836. target, and "commands" to use to create or update the target.
  837.    The order of rules is not significant, except for determining the
  838. "default goal": the target for `make' to consider, if you do not
  839. otherwise specify one.  The default goal is the target of the first
  840. rule in the first makefile.  If the first rule has multiple targets,
  841. only the first target is taken as the default.  There are two
  842. exceptions: a target starting with a period is not a default unless it
  843. contains one or more slashes, `/', as well; and, a target that defines
  844. a pattern rule has no effect on the default goal.  (*Note Defining and
  845. Redefining Pattern Rules: Pattern Rules.)
  846.    Therefore, we usually write the makefile so that the first rule is
  847. the one for compiling the entire program or all the programs described
  848. by the makefile (often with a target called `all').  *Note Arguments to
  849. Specify the Goals: Goals.
  850. * Menu:
  851. * Rule Example::                An example explained.
  852. * Rule Syntax::                 General syntax explained.
  853. * Wildcards::                   Using wildcard characters such as `*'.
  854. * Directory Search::            Searching other directories for source files.
  855. * Phony Targets::               Using a target that is not a real file's name.
  856. * Force Targets::               You can use a target without commands
  857.                                   or dependencies to mark other
  858.                                   targets as phony.
  859. * Empty Targets::               When only the date matters and the
  860.                                   files are empty.
  861. * Special Targets::             Targets with special built-in meanings.
  862. * Multiple Targets::            When to make use of several targets in a rule.
  863. * Multiple Rules::              How to use several rules with the same target.
  864. * Static Pattern::              Static pattern rules apply to multiple targets
  865.                                   and can vary the dependencies according to
  866.                                   the target name.
  867. * Double-Colon::                How to use a special kind of rule to allow
  868.                                   several independent rules for one target.
  869. * Automatic Dependencies::      How to automatically generate rules giving
  870.                                  dependencies from the source files themselves.
  871. File: make.info,  Node: Rule Example,  Next: Rule Syntax,  Up: Rules
  872. Rule Example
  873. ============
  874.    Here is an example of a rule:
  875.      foo.o : foo.c defs.h       # module for twiddling the frobs
  876.              cc -c -g foo.c
  877.    Its target is `foo.o' and its dependencies are `foo.c' and `defs.h'.
  878. It has one command, which is `cc -c -g foo.c'.  The command line
  879. starts with a tab to identify it as a command.
  880.    This rule says two things:
  881.    * How to decide whether `foo.o' is out of date: it is out of date if
  882.      it does not exist, or if either `foo.c' or `defs.h' is more recent
  883.      than it.
  884.    * How to update the file `foo.o': by running `cc' as stated.  The
  885.      command does not explicitly mention `defs.h', but we presume that
  886.      `foo.c' includes it, and that that is why `defs.h' was added to
  887.      the dependencies.
  888.