source: trunk/third/gmake/make.info-1 @ 15972

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