BSD 3 development
[unix-history] / usr / doc / make
CommitLineData
2074ceed
BJ
1.....TR 57
2.ND August 15, 1978
3.RP
4.de IT
5.if n .ul
6\&\\$3\f2\\$1\fR\^\&\\$2
7..
8.TL
9Make \(em A Program for Maintaining Computer Programs
10.AU
11S. I. Feldman
12.AI
13.MH
14.AB
15.PP
16In a programming project, it is easy to lose track of which files need
17to be reprocessed or recompiled after a change is made in some part of the source.
18.I Make
19provides a simple mechanism for maintaining up-to-date versions of programs that result
20from many operations on a number of files.
21It is possible to tell
22.I Make
23the sequence of commands that create certain files,
24and the list of files that require other files to be current before the operations can be done.
25Whenever a change is made in any part of the program,
26the
27.I Make
28command will create the proper files simply, correctly,
29and with a minimum amount of effort.
30.PP
31The basic operation of
32.I Make
33is to find the name of a needed target in the description, ensure that all of the files on which it depends exist and
34are up to date, and then create the target if it has not been modified since its generators were.
35The description file really defines the graph of dependencies;
36.I Make
37does a depth-first search of this graph
38to determine what work is really necessary.
39.PP
40.I Make
41also provides a simple macro substitution facility
42and the ability to encapsulate commands in a single file
43for convenient administration.
44.AE
45.SH
46Introduction
47.PP
48It is common practice to divide large programs into smaller, more manageable pieces.
49The pieces may require quite different treatments:
50some may need to be run through a macro processor, some may need to be processed by
51a sophisticated program generator (e.g., Yacc[1] or Lex[2]).
52The outputs of these generators may then have to be compiled with special options and with
53certain definitions and declarations.
54The code resulting from these transformations may then need to be loaded together with
55certain libraries under the control of special options.
56Related maintenance activities involve running complicated test scripts
57and installing validated modules.
58Unfortunately, it is very easy for a programmer to forget which files depend on which others,
59which files have been modified recently, and the exact sequence of operations
60needed to make or exercise a new version of the program.
61After a long editing session, one may easily lose track of which files have been changed
62and which object modules are still valid,
63since a change to a declaration can obsolete a dozen other files.
64Forgetting to compile a routine that has been changed or that uses changed declarations will result in
65a program that will not work, and a bug that can be very hard to track down.
66On the other hand, recompiling everything in sight just to be safe is very wasteful.
67.PP
68The program described in this report mechanizes many of the activities of program development
69and maintenance.
70If the information on inter-file dependences and command sequences is stored in a file, the simple command
71.DS
72make
73.DE
74is frequently sufficient to update the interesting files,
75regardless of the number that have been edited since the last ``make''.
76In most cases, the description file is easy to write and changes infrequently.
77It is usually easier to type the
78.IT make
79command than to issue even one of the needed operations, so the typical cycle of program development operations becomes
80.DS
81think \(em edit \(em \fImake\fR \(em test . . .
82.DE
83.PP
84.IT Make
85is most useful for medium-sized programming projects;
86it does not solve the problems of maintaining multiple source versions
87or of describing huge programs.
88.IT Make
89was designed for use on Unix, but a version runs on GCOS.
90.SH
91Basic Features
92.PP
93The basic operation of
94.IT make
95is to update a target file by ensuring
96that all of the files on which it depends exist and are up to date,
97then creating the target if it has not been modified since its dependents were.
98.IT Make
99does a depth-first search of the graph of dependences.
100The operation of the command depends on the ability to find the date and time
101that a file was last modified.
102.PP
103To illustrate, let us consider a simple example:
104A program named
105.IT prog
106is made by compiling and loading three C-language files
107.IT x.c ,
108.IT y.c ,
109and
110.IT z.c
111with the
112.IT lS
113library.
114By convention, the output of the C compilations will be found in files named
115.IT x.o ,
116.IT y.o ,
117and
118.IT z.o .
119Assume that the files
120.IT x.c
121and
122.IT y.c
123share some declarations in a file named
124.IT defs ,
125but that
126.IT z.c
127does not.
128That is,
129.IT x.c
130and
131.IT y.c
132have the line
133.DS
134#include "defs"
135.DE
136The following text describes the relationships and operations:
137.DS
138prog : x.o y.o z.o
139 cc x.o y.o z.o \-lS \-o prog
140.sp .5
141x.o y.o : defs
142.DE
143If this information were stored in a file named
144.IT makefile ,
145the command
146.DS
147make
148.DE
149would perform the operations needed to recreate
150.IT prog
151after any changes had been made to any of the four source files
152.IT x.c ,
153.IT y.c ,
154.IT z.c ,
155or
156.IT defs .
157.PP
158.IT Make
159operates using three sources of information:
160a user-supplied description file (as above),
161file names and ``last-modified'' times from the file system,
162and built-in rules to bridge some of the gaps.
163In our example, the first line says that
164.IT prog
165depends on three ``\fI.o\fR'' files.
166Once these object files are current, the second line describes how to load them to create
167.IT prog .
168The third line says that
169.IT x.o
170and
171.IT y.o
172depend on the file
173.IT defs .
174From the file system,
175.IT make
176discovers that there are three ``\fI.c\fR'' files corresponding to the needed ``\fI.o\fR'' files,
177and uses built-in information on how to generate an object from a source file
178(\fIi.e.,\fR issue a ``cc\ \-c'' command).
179.PP
180The following long-winded description file is equivalent to the one above, but
181takes no advantage of
182.IT make 's
183innate knowledge:
184.DS
185prog : x.o y.o z.o
186 cc x.o y.o z.o \-lS \-o prog
187.sp .3
188x.o : x.c defs
189 cc \-c x.c
190y.o : y.c defs
191 cc \-c y.c
192z.o : z.c
193 cc \-c z.c
194.DE
195.PP
196If none of the source or object files had changed since the last time
197.IT prog
198was made, all of the files would be current, and
199the command
200.DS
201make
202.DE
203would just announce this fact and stop.
204If, however, the
205.IT defs
206file had been edited,
207.IT x.c
208and
209.IT y.c
210(but not
211.IT z.c )
212would be recompiled, and then
213.IT prog
214would be created from the new ``\fI.o\fR'' files.
215If only the file
216.IT y.c
217had changed, only it would be recompiled, but it would still be necessary to reload
218.IT prog .
219.PP
220If no target name is given on the
221.IT make
222command line, the first target mentioned in the description is created;
223otherwise the specified targets are made.
224The command
225.DS
226make x.o
227.DE
228would recompile
229.IT x.o
230if
231.IT x.c
232or
233.IT defs
234had changed.
235.PP
236If the file exists after the commands are executed,
237its time of last modification is used in further decisions;
238otherwise the current time is used.
239It is often quite useful to include rules with mnemonic names and commands that do not
240actually produce a file with that name.
241These entries can take advantage of
242.IT make 's
243ability to generate files and substitute macros.
244Thus, an entry
245``save''
246might be included to copy a certain set of files, or an entry
247``cleanup''
248might be used to throw away unneeded intermediate files.
249In other cases one may maintain a zero-length file purely to keep track
250of the time at which certain actions were performed.
251This technique is useful for maintaining remote archives and listings.
252.PP
253.IT Make
254has a simple macro mechanism for substituting in dependency lines and command strings.
255Macros are defined by command arguments or description file lines with embedded equal signs.
256A macro is invoked by preceding the name by a dollar sign;
257macro names longer than one character must be parenthesized.
258The name of the macro is either the single character after the dollar sign or a name inside parentheses.
259The following are valid macro invocations:
260.DS
261$(CFLAGS)
262$2
263$(xy)
264$Z
265$(Z)
266.DE
267The last two invocations are identical.
268$$ is a dollar sign.
269All of these macros are assigned values during input, as shown below.
270Four special macros change values during the execution of the command:
271$\(**, $@, $?, and $<.
272They will be discussed later.
273The following fragment shows the use:
274.DS
275OBJECTS = x.o y.o z.o
276LIBES = \-lS
277prog: $(OBJECTS)
278 cc $(OBJECTS) $(LIBES) \-o prog
279 . . .
280.DE
281The command
282.DS
283make
284.DE
285loads the three object files with the
286.IT lS
287library. The command
288.DS
289make "LIBES= \-ll \-lS"
290.DE
291loads them with both the Lex (``\-ll'') and the Standard (``\-lS'') libraries,
292since macro definitions on the command line override definitions in the description.
293(It is necessary to quote arguments with embedded blanks in
294.UX
295commands.)
296.PP
297The following sections detail the form of description files and the command line,
298and discuss options and built-in rules in more detail.
299.SH
300Description Files and Substitutions
301.PP
302A description file contains three types of information:
303macro definitions,
304dependency information,
305and executable commands.
306There is also a comment convention:
307all characters after a sharp (#) are ignored, as is the sharp itself.
308Blank lines and lines beginning with a sharp are totally ignored.
309If a non-comment line is too long, it can be continued using a backslash.
310If the last character of a line is a backslash, the backslash, newline,
311and following blanks and tabs are replaced by a single blank.
312.PP
313A macro definition is a line containing an equal sign not preceded by a colon or a tab.
314The name (string of letters and digits) to the left of the equal sign
315(trailing blanks and tabs are stripped) is assigned the string of characters following the equal sign
316(leading blanks and tabs are stripped.)
317The following are valid macro definitions:
318.DS
3192 = xyz
320abc = \-ll \-ly \-lS
321LIBES =
322.DE
323The last definition assigns LIBES the null string.
324A macro that is never explicitly defined has the null string as value.
325Macro definitions may also appear on the
326.IT make
327command line (see below).
328.PP
329Other lines give information about target files.
330The general form of an entry is:
331.DS
332target1 [target2 . . .] :[:] [dependent1 . . .] [; commands] [# . . .]
333[\fI(tab)\fR commands] [# . . .]
334 . . .
335.DE
336Items inside brackets may be omitted.
337Targets and dependents are strings of letters, digits, periods, and slashes.
338(Shell metacharacters ``\(**'' and ``?'' are expanded.)
339A command is any string of characters not including a sharp (except in quotes)
340or newline.
341Commands may appear either after a semicolon on a dependency line
342or on lines beginning with a tab immediately following a dependency line.
343.PP
344A dependency line may have either a single or a double colon.
345A target name may appear on more than one dependency line, but all of those lines must be of the
346same (single or double colon) type.
347.IP 1.
348For the usual single-colon case,
349at most one of these dependency lines may have a command sequence associated with it.
350If the target is out of date with any of the dependents on any of the lines,
351and a command sequence is specified (even a null one following a semicolon or tab),
352it is executed; otherwise a default creation rule may be invoked.
353.IP 2.
354In the double-colon case, a command sequence may be associated with each dependency line;
355if the target is out of date with any of the files on a particular line, the associated
356commands are executed.
357A built-in rule may also be executed.
358This detailed form is of particular value in updating archive-type files.
359.PP
360If a target must be created, the sequence of commands is executed.
361Normally, each command line is printed and then
362passed to a separate invocation of the Shell after substituting for macros.
363(The printing is suppressed in silent mode or if the command line begins with an @ sign).
364.IT Make
365normally stops if any command signals an error by returning a non-zero error code.
366(Errors are ignored if the ``\-i'' flags has been specified on the
367.IT make
368command line,
369if the fake target name ``.IGNORE'' appears in the description file,
370or if the command string in the description file begins with a hyphen.
371Some
372.UX
373commands return meaningless status).
374Because each command line is passed to a separate invocation of the Shell,
375care must be taken with certain commands (e.g., \fIcd\fR and Shell control commands) that have meaning only
376within a single Shell process;
377the results are forgotten before the next line is executed.
378.PP
379Before issuing any command, certain macros are set.
380$@ is set to the name of the file to be ``made''.
381$? is set to the string of names that were found to be younger than the target.
382If the command was generated by an implicit rule (see below),
383$< is the name of the related file that caused the action, and
384$\(** is the prefix shared by the current and the dependent file names.
385.PP
386If a file must be made but there are no explicit commands or relevant
387built-in rules,
388the commands associated with the name ``.DEFAULT'' are used.
389If there is no such name,
390.IT make
391prints a message and stops.
392.SH
393Command Usage
394.PP
395The
396.IT make
397command takes four kinds of arguments:
398macro definitions, flags, description file names, and target file names.
399.DS
400make [ flags ] [ macro definitions ] [ targets ]
401.DE
402The following summary of the operation of the command explains how these arguments are interpreted.
403.PP
404First, all macro definition arguments (arguments with embedded equal signs) are analyzed
405and the assignments made.
406Command-line macros override corresponding definitions found in the description files.
407.PP
408Next, the flag arguments are examined.
409The permissible flags are
410.IP \-i
411Ignore error codes returned by invoked commands.
412This mode is entered if the fake target name ``.IGNORE'' appears in the description file.
413.IP \-s
414Silent mode. Do not print command lines before executing.
415This mode is also entered if the fake target name ``.SILENT'' appears in the description file.
416.IP \-r
417Do not use the built-in rules.
418.IP \-n
419No execute mode. Print commands, but do not execute them.
420Even lines beginning with an ``@'' sign are printed.
421.IP \-t
422Touch the target files (causing them to be up to date) rather than issue the usual commands.
423.IP \-q
424Question.
425The
426.IT make
427command returns a zero or non-zero status code depending on whether the target file
428is or is not up to date.
429.IP \-p
430Print out the complete set of macro definitions and target descriptions
431.IP \-d
432Debug mode. Print out detailed information on files and times examined.
433.IP \-f
434Description file name. The next argument is assumed to be the name of a description file.
435A file name of ``\-'' denotes the standard input.
436If there are no ``\-f\|'' arguments, the file named
437.IT makefile
438or
439.IT Makefile
440in the current directory is read.
441The contents of the description files override the built-in rules if they are present).
442.PP
443Finally, the remaining arguments are assumed to be the names of targets to be made;
444they are done in left to right order.
445If there are no such arguments, the first name in the description files that does not
446begin with a period is ``made''.
447.SH
448Implicit Rules
449.PP
450The
451.ul
452make
453program uses a table of interesting suffixes and a set
454of transformation rules to supply default dependency
455information and implied commands.
456(The Appendix describes these tables and means of overriding
457them.)
458The default suffix list is:
459.KS
460.sp
461.nf
462.ta 0.5i 1.5i
463 \fI.o\fR Object file
464 \fI.c\fR C source file
465 \fI.e\fR Efl source file
466 \fI.r\fR Ratfor source file
467 \fI.f\fR Fortran source file
468 \fI.s\fR Assembler source file
469 \fI.y\fR Yacc-C source grammar
470 \fI.yr\fR Yacc-Ratfor source grammar
471 \fI.ye\fR Yacc-Efl source grammar
472 \fI.l\fR Lex source grammar
473.fi
474.sp
475.KE
476The following diagram summarizes the default transformation paths.
477If there are two paths connecting a pair of suffixes, the longer
478one is used only if the intermediate file exists or is
479named in the description.
480.KS
481.sp
482.ft I
483.ta 2i
484 .o
485.sp 2
486.ta 0.75i 1.25i 1.6i 2.1i
487 .c .r .e .f .s .y .yr .ye .l .d
488.sp 2
489.ta 0.6i 1.25i 1.6i
490 .y .l .yr .ye
491.ft R
492.sp
493.KE
494.PP
495If the file
496.ul
497x.o
498were needed and there were an
499.ul
500x.c
501in the description or directory, it would be compiled.
502If there were also an
503.IT x.l ,
504that grammar would be run through Lex before compiling the result.
505However, if there were no
506.ul
507x.c
508but there were an
509.IT x.l ,
510.IT make
511would discard the intermediate C-language file and use the
512direct link in the graph above.
513.PP
514It is possible to change the names of some of the compilers used in the
515default, or the flag arguments with which they are invoked by knowing
516the macro names used.
517The compiler names are the macros AS, CC, RC, EC, YACC, YACCR, YACCE, and LEX.
518The command
519.DS
520make CC=newcc
521.DE
522will cause the ``newcc'' command to be used instead of the
523usual C compiler.
524The macros CFLAGS, RFLAGS, EFLAGS, YFLAGS, and LFLAGS may be set to
525cause these commands to be issued with optional flags.
526Thus,
527.DS
528make "CFLAGS=\|\(miO"
529.DE
530causes the optimizing C compiler to be used.
531.SH
532Example
533.PP
534As an example of the use of
535.ul
536make,
537we will present the description file used to maintain
538the
539.ul
540make
541command itself.
542The code for
543.ul
544make
545is spread over a number of C source files and a Yacc grammar.
546The description file contains:
547.DS
548# Description file for the Make command
549.sp .3
550P = und \-3 | opr \-r2 # send to GCOS to be printed
551FILES = Makefile version.c defs main.c doname.c misc.c files.c dosys.c\
552 gram.y lex.c gcos.c
553OBJECTS = version.o main.o doname.o misc.o files.o dosys.o gram.o
554LIBES= \-lS
555LINT = lint \-p
556CFLAGS = \-O
557.sp .3
558make: $(OBJECTS)
559 cc $(CFLAGS) $(OBJECTS) $(LIBES) \-o make
560 size make
561.sp .3
562$(OBJECTS): defs
563gram.o: lex.c
564.sp .3
565cleanup:
566 -rm *.o gram.c
567 -du
568.sp .3
569install:
570 @size make /usr/bin/make
571 cp make /usr/bin/make ; rm make
572.sp .3
573print: $(FILES) # print recently changed files
574 pr $? | $P
575 touch print
576.sp .3
577test:
578 make \-dp | grep \-v TIME >1zap
579 /usr/bin/make \-dp | grep \-v TIME >2zap
580 diff 1zap 2zap
581 rm 1zap 2zap
582.sp .3
583lint : dosys.c doname.c files.c main.c misc.c version.c gram.c
584 $(LINT) dosys.c doname.c files.c main.c misc.c version.c gram.c
585 rm gram.c
586.sp .3
587arch:
588 ar uv /sys/source/s2/make.a $(FILES)
589.DE
590.IT Make
591usually prints out each command before issuing it.
592The following output results from typing the simple command
593.DS
594make
595.DE
596in a directory containing only the source and description file:
597.DS
598 cc \-c version.c
599 cc \-c main.c
600 cc \-c doname.c
601 cc \-c misc.c
602 cc \-c files.c
603 cc \-c dosys.c
604 yacc gram.y
605 mv y.tab.c gram.c
606 cc \-c gram.c
607 cc version.o main.o doname.o misc.o files.o dosys.o gram.o \-lS \-o make
608 13188+3348+3044 = 19580b = 046174b
609.DE
610Although none of the source files or grammars were mentioned
611by name in the description file,
612.IT make
613found them using its suffix rules and issued the needed commands.
614The string of digits results from the ``size make''
615command; the printing of the command line itself was
616suppressed by an @ sign.
617The @ sign on the
618.IT size
619command in the description file suppressed the printing of the command,
620so only the sizes are written.
621.PP
622The last few entries in the description file are useful maintenance sequences.
623The ``print'' entry prints only the files that have been changed since the last
624``make print'' command.
625A zero-length file
626.IT print
627is maintained to keep track of the time of the printing;
628the $? macro in the command line then picks up only the names of the files
629changed since
630.IT print
631was touched.
632The printed output can be sent to a different printer or to a file by changing the definition of the
633.IT P
634macro:
635.DS
636make print "P = opr \-sp"
637 \fIor\fR
638make print "P= cat >zap"
639.DE
640.SH
641Suggestions and Warnings
642.PP
643The most common difficulties arise from
644.IT make 's
645specific meaning of dependency.
646If file
647.IT x.c
648has a ``#include "defs"''
649line, then the object file
650.IT x.o
651depends on
652.IT defs ;
653the source file
654.IT x.c
655does not.
656(If
657.IT defs
658is changed, it is not necessary to do anything
659to the file
660.IT x.c ,
661while it is necessary to recreate
662.IT x.o .)
663.PP
664To discover what
665.IT make
666would do, the ``\-n'' option is very useful.
667The command
668.DS
669make \-n
670.DE
671orders
672.IT make
673to print out the commands it would issue without actually taking the time to execute them.
674If a change to a file is absolutely certain to be benign
675(e.g., adding a new definition to an include file),
676the ``\-t'' (touch) option
677can save a lot of time:
678instead of issuing a large number of superfluous recompilations,
679.IT make
680updates the modification times on the affected file.
681Thus, the command
682.DS
683make \-ts
684.DE
685(``touch silently'') causes the relevant files to appear up to date.
686Obvious care is necessary, since this mode of operation subverts
687the intention of
688.IT make
689and destroys all memory of the previous relationships.
690.PP
691The debugging flag (``\-d'') causes
692.IT make
693to print out a very detailed description of what it is doing, including the
694file times. The output is verbose, and recommended only as a last resort.
695.SH
696Acknowledgments
697.PP
698I would like to thank S. C. Johnson for suggesting this approach
699to program maintenance control.
700I would like to thank S. C. Johnson and H. Gajewska for being
701the prime guinea pigs during development of
702.IT make .
703.SH
704References
705.IP 1.
706S. C. Johnson,
707``Yacc \(em Yet Another Compiler-Compiler'',
708Bell Laboratories
709Computing Science Technical Report #32,
710July 1978.
711.IP 2.
712M. E. Lesk,
713``Lex \(em A Lexical Analyzer Generator'',
714Computing Science Technical Report #39,
715October 1975.
716.bp
717.SH
718Appendix. Suffixes and Transformation Rules
719.PP
720The
721.ul
722make
723program itself does not know what file name suffixes are interesting
724or how to transform a file with one suffix into a file with another
725suffix.
726This information is stored in an internal table that has the form of a description file.
727If the ``\-r'' flag is used, this table is not used.
728.PP
729The list of suffixes is actually the dependency list for the name
730``.SUFFIXES'';
731.ul
732make
733looks for a file with any of the suffixes on the list.
734If such a file exists, and if there is a transformation
735rule for that combination,
736.ul
737make
738acts as described earlier.
739The transformation rule names are the concatenation of the
740two suffixes.
741The name of the rule to transform a ``\fI.r\fR'' file to a ``\fI.o\fR'' file
742is thus ``\fI.r.o\fR''.
743If the rule is present and no explicit command sequence
744has been given in the user's description files, the command
745sequence for the rule ``.r.o'' is used.
746If a command is generated by using one of these suffixing rules,
747the macro $\(** is given the value of the stem
748(everything but the suffix) of the name of the file to be made,
749and the macro $< is the name of the dependent that caused the action.
750.PP
751The order of the suffix list is significant, since it is scanned from
752left to right, and the first name that is formed that has both a file
753and a rule associated with it is used.
754If new names are to be appended, the user can just add an entry for
755``.SUFFIXES'' in his own description file; the dependents will be added to the usual list.
756A ``.SUFFIXES'' line without any dependents deletes the current list.
757(It is necessary to clear the current list if the order of names is to be changed).
758.PP
759The following is an excerpt from the default rules file:
760.DS
761.ta .5i 1i
762 .SUFFIXES : .o .c .e .r .f .y .yr .ye .l .s
763 YACC=yacc
764 YACCR=yacc \-r
765 YACCE=yacc \-e
766 YFLAGS=
767 LEX=lex
768 LFLAGS=
769 CC=cc
770 AS=as \-
771 CFLAGS=
772 RC=ec
773 RFLAGS=
774 EC=ec
775 EFLAGS=
776 FFLAGS=
777 .c.o :
778 $(CC) $(CFLAGS) \-c $<
779 .e.o .r.o .f.o :
780 $(EC) $(RFLAGS) $(EFLAGS) $(FFLAGS) \-c $<
781 .s.o :
782 $(AS) \-o $@ $<
783 .y.o :
784 $(YACC) $(YFLAGS) $<
785 $(CC) $(CFLAGS) \-c y.tab.c
786 rm y.tab.c
787 mv y.tab.o $@
788 .y.c :
789 $(YACC) $(YFLAGS) $<
790 mv y.tab.c $@
791.DE