xref: /original-bsd/usr.bin/make/make.1 (revision 58b1b499)
1.\" Copyright (c) 1990, 1993
2.\"	The Regents of the University of California.  All rights reserved.
3.\"
4.\" %sccs.include.redist.roff%
5.\"
6.\"	@(#)make.1	8.5 (Berkeley) 03/14/95
7.\"
8.Dd
9.Dt MAKE 1
10.Os
11.Sh NAME
12.Nm make
13.Nd maintain program dependencies
14.Sh SYNOPSIS
15.Nm make
16.Op Fl eiknqrstv
17.Op Fl D Ar variable
18.Op Fl d Ar flags
19.Op Fl f Ar makefile
20.Op Fl I Ar directory
21.Bk -words
22.Op Fl j Ar max_jobs
23.Ek
24.Op Ar variable=value
25.Op Ar target ...
26.Sh DESCRIPTION
27.Nm Make
28is a program designed to simplify the maintenance of other programs.
29Its input is a list of specifications as to the files upon which programs
30and other files depend.
31If the file
32.Ql Pa makefile
33exists, it is read for this list of specifications.
34If it does not exist, the file
35.Ql Pa Makefile
36is read.
37If the file
38.Ql Pa .depend
39exists, it is read (see
40.Xr mkdep 1) .
41.Pp
42This manual page is intended as a reference document only.
43For a more thorough description of
44.Nm make
45and makefiles, please refer to
46.%T "Make \- A Tutorial" .
47.Pp
48The options are as follows:
49.Bl -tag -width Ds
50.It Fl D Ar variable
51Define
52.Ar variable
53to be 1, in the global context.
54.It Fl d Ar flags
55Turn on debugging, and specify which portions of
56.Nm make
57are to print debugging information.
58.Ar Flags
59is one or more of the following:
60.Bl -tag -width Ds
61.It Ar A
62Print all possible debugging information;
63equivalent to specifying all of the debugging flags.
64.It Ar a
65Print debugging information about archive searching and caching.
66.It Ar c
67Print debugging information about conditional evaluation.
68.It Ar d
69Print debugging information about directory searching and caching.
70.It Ar "g1"
71Print the input graph before making anything.
72.It Ar "g2"
73Print the input graph after making everything, or before exiting
74on error.
75.It Ar j
76Print debugging information about running multiple shells.
77.It Ar m
78Print debugging information about making targets, including modification
79dates.
80.It Ar s
81Print debugging information about suffix-transformation rules.
82.It Ar t
83Print debugging information about target list maintenance.
84.It Ar v
85Print debugging information about variable assignment.
86.El
87.It Fl e
88Specify that environmental variables override macro assignments within
89makefiles.
90.It Fl f Ar makefile
91Specify a makefile to read instead of the default
92.Ql Pa makefile
93and
94.Ql Pa Makefile .
95If
96.Ar makefile
97is
98.Ql Fl ,
99standard input is read.
100Multiple makefile's may be specified, and are read in the order specified.
101.It Fl I Ar directory
102Specify a directory in which to search for makefiles and included makefiles.
103The system makefile directory is automatically included as part of this
104list.
105.It Fl i
106Ignore non-zero exit of shell commands in the makefile.
107Equivalent to specifying
108.Ql Fl
109before each command line in the makefile.
110.It Fl j Ar max_jobs
111Specify the maximum number of jobs that
112.Nm make
113may have running at any one time.
114.It Fl k
115Continue processing after errors are encountered, but only on those targets
116that do not depend on the target whose creation caused the error.
117.It Fl n
118Display the commands that would have been executed, but do not actually
119execute them.
120.It Fl q
121Do not execute any commands, but exit 0 if the specified targets are
122up-to-date and 1, otherwise.
123.It Fl r
124Do not use the built-in rules specified in the system makefile.
125.It Fl s
126Do not echo any commands as they are executed.
127Equivalent to specifying
128.Ql Ic @
129before each command line in the makefile.
130.It Fl t
131Rather than re-building a target as specified in the makefile, create it
132or update its modification time to make it appear up-to-date.
133.It Ar variable=value
134Set the value of the variable
135.Ar variable
136to
137.Ar value .
138.El
139.Pp
140There are seven different types of lines in a makefile: file dependency
141specifications, shell commands, variable assignments, include statements,
142conditional directives, for loops, and comments.
143.Pp
144In general, lines may be continued from one line to the next by ending
145them with a backslash
146.Pq Ql \e .
147The trailing newline character and initial whitespace on the following
148line are compressed into a single space.
149.Sh FILE DEPENDENCY SPECIFICATIONS
150Dependency lines consist of one or more targets, an operator, and zero
151or more sources.
152This creates a relationship where the targets ``depend'' on the sources
153and are usually created from them.
154The exact relationship between the target and the source is determined
155by the operator that separates them.
156The three operators are as follows:
157.Bl -tag -width flag
158.It Ic \&:
159A target is considered out-of-date if its modification time is less than
160those of any of its sources.
161Sources for a target accumulate over dependency lines when this operator
162is used.
163The target is removed if
164.Nm make
165is interrupted.
166.It Ic \&!
167Targets are always re-created, but not until all sources have been
168examined and re-created as necessary.
169Sources for a target accumulate over dependency lines when this operator
170is used.
171The target is removed if
172.Nm make
173is interrupted.
174.It Ic \&::
175If no sources are specified, the target is always re-created.
176Otherwise, a target is considered out-of-date if any of its sources has
177been modified more recently than the target.
178Sources for a target do not accumulate over dependency lines when this
179operator is used.
180The target will not be removed if
181.Nm make
182is interrupted.
183.El
184.Pp
185Targets and sources may contain the shell wildcard values
186.Ql ? ,
187.Ql * ,
188.Ql []
189and
190.Ql {} .
191The values
192.Ql ? ,
193.Ql *
194and
195.Ql []
196may only be used as part of the final
197component of the target or source, and must be used to describe existing
198files.
199The value
200.Ql {}
201need not necessarily be used to describe existing files.
202Expansion is in directory order, not alphabetically as done in the shell.
203.Sh SHELL COMMANDS
204Each target may have associated with it a series of shell commands, normally
205used to create the target.
206Each of the commands in this script
207.Em must
208be preceded by a tab.
209While any target may appear on a dependency line, only one of these
210dependencies may be followed by a creation script, unless the
211.Ql Ic ::
212operator is used.
213.Pp
214If the first or first two characters of the command line are
215.Ql Ic @
216and/or
217.Ql Ic \- ,
218the command is treated specially.
219A
220.Ql Ic @
221causes the command not to be echoed before it is executed.
222A
223.Ql Ic \-
224causes any non-zero exit status of the command line to be ignored.
225.Sh VARIABLE ASSIGNMENTS
226Variables in make are much like variables in the shell, and, by tradition,
227consist of all upper-case letters.
228The five operators that can be used to assign values to variables are as
229follows:
230.Bl -tag -width Ds
231.It Ic \&=
232Assign the value to the variable.
233Any previous value is overridden.
234.It Ic \&+=
235Append the value to the current value of the variable.
236.It Ic \&?=
237Assign the value to the variable if it is not already defined.
238.It Ic \&:=
239Assign with expansion, i.e. expand the value before assigning it
240to the variable.
241Normally, expansion is not done until the variable is referenced.
242.It Ic \&!=
243Expand the value and pass it to the shell for execution and assign
244the result to the variable.
245Any newlines in the result are replaced with spaces.
246.El
247.Pp
248Any white-space before the assigned
249.Ar value
250is removed; if the value is being appended, a single space is inserted
251between the previous contents of the variable and the appended value.
252.Pp
253Variables are expanded by surrounding the variable name with either
254curly braces
255.Pq Ql {}
256or parenthesis
257.Pq Ql ()
258and preceding it with
259a dollar sign
260.Pq Ql \&$ .
261If the variable name contains only a single letter, the surrounding
262braces or parenthesis are not required.
263This shorter form is not recommended.
264.Pp
265Variable substitution occurs at two distinct times, depending on where
266the variable is being used.
267Variables in dependency lines are expanded as the line is read.
268Variables in shell commands are expanded when the shell command is
269executed.
270.Pp
271The four different classes of variables (in order of increasing precedence)
272are:
273.Bl -tag -width Ds
274.It Environment variables
275Variables defined as part of
276.Nm make Ns 's
277environment.
278.It Global variables
279Variables defined in the makefile or in included makefiles.
280.It Command line variables
281Variables defined as part of the command line.
282.It Local variables
283Variables that are defined specific to a certain target.
284The seven local variables are as follows:
285.Bl -tag -width ".ARCHIVE"
286.It Va .ALLSRC
287The list of all sources for this target; also known as
288.Ql Va \&> .
289.It Va .ARCHIVE
290The name of the archive file.
291.It Va .IMPSRC
292The name/path of the source from which the target is to be transformed
293(the ``implied'' source); also known as
294.Ql Va \&< .
295.It Va .MEMBER
296The name of the archive member.
297.It Va .OODATE
298The list of sources for this target that were deemed out-of-date; also
299known as
300.Ql Va \&? .
301.It Va .PREFIX
302The file prefix of the file, containing only the file portion, no suffix
303or preceding directory components; also known as
304.Ql Va * .
305.It Va .TARGET
306The name of the target; also known as
307.Ql Va @ .
308.El
309.Pp
310The shorter forms
311.Ql Va @ ,
312.Ql Va ? ,
313.Ql Va \&>
314and
315.Ql Va *
316are permitted for backward
317compatibility with historical makefiles and are not recommended.
318The six variables
319.Ql Va "@F" ,
320.Ql Va "@D" ,
321.Ql Va "<F" ,
322.Ql Va "<D" ,
323.Ql Va "*F"
324and
325.Ql Va "*D"
326are
327permitted for compatibility with
328.At V
329makefiles and are not recommended.
330.Pp
331Four of the local variables may be used in sources on dependency lines
332because they expand to the proper value for each target on the line.
333These variables are
334.Ql Va .TARGET ,
335.Ql Va .PREFIX ,
336.Ql Va .ARCHIVE ,
337and
338.Ql Va .MEMBER .
339.Pp
340In addition,
341.Nm make
342sets or knows about the following variables:
343.Bl -tag -width MAKEFLAGS
344.It Va \&$
345A single dollar sign
346.Ql \&$ ,
347i.e.
348.Ql \&$$
349expands to a single dollar
350sign.
351.It Va .MAKE
352The name that
353.Nm make
354was executed with
355.Pq Va argv Op 0
356.It Va .CURDIR
357A path to the directory where
358.Nm make
359was executed.
360.It Va .OBJDIR
361A path to the directory where the targets are built.
362.It Ev MAKEFLAGS
363The environment variable
364.Ql Ev MAKEFLAGS
365may contain anything that
366may be specified on
367.Nm make Ns 's
368command line.
369Anything specified on
370.Nm make Ns 's
371command line is appended to the
372.Ql Ev MAKEFLAGS
373variable which is then
374entered into the environment for all programs which
375.Nm make
376executes.
377.El
378.Pp
379Variable expansion may be modified to select or modify each word of the
380variable (where a ``word'' is white-space delimited sequence of characters).
381The general format of a variable expansion is as follows:
382.Pp
383.Dl {variable[:modifier[:...]]}
384.Pp
385Each modifier begins with a colon and one of the following
386special characters.
387The colon may be escaped with a backslash
388.Pq Ql \e .
389.Bl -tag -width Cm E\&
390.It Cm E
391Replaces each word in the variable with its suffix.
392.It Cm H
393Replaces each word in the variable with everything but the last component.
394.It Cm M Ns Ar pattern
395Select only those words that match the rest of the modifier.
396The standard shell wildcard characters
397.Pf ( Ql * ,
398.Ql ? ,
399and
400.Ql Op )
401may
402be used.
403The wildcard characters may be escaped with a backslash
404.Pq Ql \e .
405.It Cm N Ns Ar pattern
406This is identical to
407.Ql Cm M ,
408but selects all words which do not match
409the rest of the modifier.
410.It Cm R
411Replaces each word in the variable with everything but its suffix.
412.Sm off
413.It Cm S No \&/ Ar old_pattern Xo
414.No \&/ Ar new_pattern
415.No \&/ Op Cm g
416.Xc
417.Sm on
418Modify the first occurrence of
419.Ar old_pattern
420in each word to be replaced with
421.Ar new_pattern .
422If a
423.Ql g
424is appended to the last slash of the pattern, all occurrences
425in each word are replaced.
426If
427.Ar old_pattern
428begins with a carat
429.Pq Ql ^ ,
430.Ar old_pattern
431is anchored at the beginning of each word.
432If
433.Ar old_pattern
434ends with a dollar sign
435.Pq Ql \&$ ,
436it is anchored at the end of each word.
437Inside
438.Ar new_string ,
439an ampersand
440.Pq Ql &
441is replaced by
442.Ar old_pattern .
443Any character may be used as a delimiter for the parts of the modifier
444string.
445The anchoring, ampersand and delimiter characters may be escaped with a
446backslash
447.Pq Ql \e .
448.Pp
449Variable expansion occurs in the normal fashion inside both
450.Ar old_string
451and
452.Ar new_string
453with the single exception that a backslash is used to prevent the expansion
454of a dollar sign
455.Pq Ql \&$
456not a preceding dollar sign as is usual.
457.It Cm T
458Replaces each word in the variable with its last component.
459.It Ar old_string=new_string
460This is the
461.At V
462style variable substitution.
463It must be the last modifier specified.
464If
465.Ar old_string
466or
467.Ar new_string
468do not contain the pattern matching character
469.Ar %
470then it is assumed that they are
471anchored at the end of each word, so only suffixes or entire
472words may be replaced. Otherwise
473.Ar %
474is the substring of
475.Ar old_string
476to be replaced in
477.Ar new_string
478.El
479.Sh INCLUDE STATEMENTS, CONDITIONALS AND FOR LOOPS
480Makefile inclusion, conditional structures and for loops  reminiscent
481of the C programming language are provided in
482.Nm make .
483All such structures are identified by a line beginning with a single
484dot
485.Pq Ql \&.
486character.
487Files are included with either
488.Ql .include <file>
489or
490.Ql .include \*qfile\*q .
491Variables between the angle brackets or double quotes are expanded
492to form the file name.
493If angle brackets are used, the included makefile is expected to be in
494the system makefile directory.
495If double quotes are used, the including makefile's directory and any
496directories specified using the
497.Fl I
498option are searched before the system
499makefile directory.
500.Pp
501Conditional expressions are also preceded by a single dot as the first
502character of a line.
503The possible conditionals are as follows:
504.Bl -tag -width Ds
505.It Ic .undef Ar variable
506Un-define the specified global variable.
507Only global variables may be un-defined.
508.It Xo
509.Ic \&.if
510.Oo \&! Oc Ns Ar expression
511.Op Ar operator expression ...
512.Xc
513Test the value of an expression.
514.It Xo
515.Ic .ifdef
516.Oo \&! Oc Ns Ar variable
517.Op Ar operator variable ...
518.Xc
519Test the value of a variable.
520.It Xo
521.Ic .ifndef
522.Oo \&! Oc Ns Ar variable
523.Op Ar operator variable ...
524.Xc
525Test the value of a variable.
526.It Xo
527.Ic .ifmake
528.Oo \&! Oc Ns Ar target
529.Op Ar operator target ...
530.Xc
531Test the the target being built.
532.It Xo
533.Ic .ifnmake
534.Oo \&! Oc Ar target
535.Op Ar operator target ...
536.Xc
537Test the target being built.
538.It Ic .else
539Reverse the sense of the last conditional.
540.It Xo
541.Ic .elif
542.Oo \&! Oc Ar expression
543.Op Ar operator expression ...
544.Xc
545A combination of
546.Ql Ic .else
547followed by
548.Ql Ic .if .
549.It Xo
550.Ic .elifdef
551.Oo \&! Oc Ns Ar variable
552.Op Ar operator variable ...
553.Xc
554A combination of
555.Ql Ic .else
556followed by
557.Ql Ic .ifdef .
558.It Xo
559.Ic .elifndef
560.Oo \&! Oc Ns Ar variable
561.Op Ar operator variable ...
562.Xc
563A combination of
564.Ql Ic .else
565followed by
566.Ql Ic .ifndef .
567.It Xo
568.Ic .elifmake
569.Oo \&! Oc Ns Ar target
570.Op Ar operator target ...
571.Xc
572A combination of
573.Ql Ic .else
574followed by
575.Ql Ic .ifmake .
576.It Xo
577.Ic .elifnmake
578.Oo \&! Oc Ns Ar target
579.Op Ar operator target ...
580.Xc
581A combination of
582.Ql Ic .else
583followed by
584.Ql Ic .ifnmake .
585.It Ic .endif
586End the body of the conditional.
587.El
588.Pp
589The
590.Ar operator
591may be any one of the following:
592.Bl -tag -width "Cm XX"
593.It Cm \&|\&|
594logical OR
595.It Cm \&&&
596Logical
597.Tn AND ;
598of higher precedence than
599.Dq .
600.El
601.Pp
602As in C,
603.Nm make
604will only evaluate a conditional as far as is necessary to determine
605its value.
606Parentheses may be used to change the order of evaluation.
607The boolean operator
608.Ql Ic \&!
609may be used to logically negate an entire
610conditional.
611It is of higher precedence than
612.Ql Ic \&&& .
613.Pp
614The value of
615.Ar expression
616may be any of the following:
617.Bl -tag -width Ic defined
618.It Ic defined
619Takes a variable name as an argument and evaluates to true if the variable
620has been defined.
621.It Ic make
622Takes a target name as an argument and evaluates to true if the target
623was specified as part of
624.Nm make Ns 's
625command line or was declared the default target (either implicitly or
626explicitly, see
627.Va .MAIN )
628before the line containing the conditional.
629.It Ic empty
630Takes a variable, with possible modifiers, and evaluates to true if
631the expansion of the variable would result in an empty string.
632.It Ic exists
633Takes a file name as an argument and evaluates to true if the file exists.
634The file is searched for on the system search path (see
635.Va .PATH ) .
636.It Ic target
637Takes a target name as an argument and evaluates to true if the target
638has been defined.
639.El
640.Pp
641.Ar Expression
642may also be an arithmetic or string comparison.  Variable expansion is
643performed on both sides of the comparison, after which the integral
644values are compared.  A value is interpreted as hexadecimal if it is
645preceded by 0x, otherwise it is decimal; octal numbers are not supported.
646The standard C relational operators are all supported.  If after
647variable expansion, either the left or right hand side of a
648.Ql Ic ==
649or
650.Ql Ic "!="
651operator is not an integral value, then
652string comparison is performed between the expanded
653variables.
654If no relational operator is given, it is assumed that the expanded
655variable is being compared against 0.
656.Pp
657When
658.Nm make
659is evaluating one of these conditional expression, and it encounters
660a word it doesn't recognize, either the ``make'' or ``defined''
661expression is applied to it, depending on the form of the conditional.
662If the form is
663.Ql Ic .ifdef
664or
665.Ql Ic .ifndef ,
666the ``defined'' expression
667is applied.
668Similarly, if the form is
669.Ql Ic .ifmake
670or
671.Ql Ic .ifnmake , the ``make''
672expression is applied.
673.Pp
674If the conditional evaluates to true the parsing of the makefile continues
675as before.
676If it evaluates to false, the following lines are skipped.
677In both cases this continues until a
678.Ql Ic .else
679or
680.Ql Ic .endif
681is found.
682.Pp
683For loops are typically used to apply a set of rules to a list of files.
684The syntax of a for loop is:
685.Bl -tag -width Ds
686.It Xo
687.Ic \&.for
688.Ar variable
689.Ic in
690.Ar expression
691.Xc
692.It Xo
693<make-rules>
694.Xc
695.It Xo
696.Ic \&.endfor
697.Xc
698.El
699After the for
700.Ic expression
701is evaluated, it is split into words. The
702iteration
703.Ic variable
704is successively set to each word, and substituted in the
705.Ic make-rules
706inside the body of the for loop.
707.Sh COMMENTS
708Comments begin with a hash
709.Pq Ql \&#
710character, anywhere but in a shell
711command line, and continue to the end of the line.
712.Sh SPECIAL SOURCES
713.Bl -tag -width Ic .IGNORE
714.It Ic .IGNORE
715Ignore any errors from the commands associated with this target, exactly
716as if they all were preceded by a dash
717.Pq Ql \- .
718.It Ic .MAKE
719Execute the commands associated with this target even if the
720.Fl n
721or
722.Fl t
723options were specified.
724Normally used to mark recursive
725.Nm make Ns 's .
726.It Ic .NOTMAIN
727Normally
728.Nm make
729selects the first target it encounters as the default target to be built
730if no target was specified.
731This source prevents this target from being selected.
732.It Ic .OPTIONAL
733If a target is marked with this attribute and
734.Nm make
735can't figure out how to create it, it will ignore this fact and assume
736the file isn't needed or already exists.
737.It Ic .PRECIOUS
738When
739.Nm make
740is interrupted, it removes any partially made targets.
741This source prevents the target from being removed.
742.It Ic .SILENT
743Do not echo any of the commands associated with this target, exactly
744as if they all were preceded by an at sign
745.Pq Ql @ .
746.It Ic .USE
747Turn the target into
748.Nm make Ns 's .
749version of a macro.
750When the target is used as a source for another target, the other target
751acquires the commands, sources, and attributes (except for
752.Ic .USE )
753of the
754source.
755If the target already has commands, the
756.Ic .USE
757target's commands are appended
758to them.
759.El
760.Sh "SPECIAL TARGETS"
761Special targets may not be included with other targets, i.e. they must be
762the only target specified.
763.Bl -tag -width Ic .BEGIN
764.It Ic .BEGIN
765Any command lines attached to this target are executed before anything
766else is done.
767.It Ic .DEFAULT
768This is sort of a
769.Ic .USE
770rule for any target (that was used only as a
771source) that
772.Nm make
773can't figure out any other way to create.
774Only the shell script is used.
775The
776.Ic .IMPSRC
777variable of a target that inherits
778.Ic .DEFAULT Ns 's
779commands is set
780to the target's own name.
781.It Ic .END
782Any command lines attached to this target are executed after everything
783else is done.
784.It Ic .IGNORE
785Mark each of the sources with the
786.Ic .IGNORE
787attribute.
788If no sources are specified, this is the equivalent of specifying the
789.Fl i
790option.
791.It Ic .INTERRUPT
792If
793.Nm make
794is interrupted, the commands for this target will be executed.
795.It Ic .MAIN
796If no target is specified when
797.Nm make
798is invoked, this target will be built.
799.It Ic .MAKEFLAGS
800This target provides a way to specify flags for
801.Nm make
802when the makefile is used.
803The flags are as if typed to the shell, though the
804.Fl f
805option will have
806no effect.
807.It Ic .PATH
808The sources are directories which are to be searched for files not
809found in the current directory.
810If no sources are specified, any previously specified directories are
811deleted.
812.It Ic .PRECIOUS
813Apply the
814.Ic .PRECIOUS
815attribute to any specified sources.
816If no sources are specified, the
817.Ic .PRECIOUS
818attribute is applied to every
819target in the file.
820.It Ic .SILENT
821Apply the
822.Ic .SILENT
823attribute to any specified sources.
824If no sources are specified, the
825.Ic .SILENT
826attribute is applied to every
827command in the file.
828.It Ic .SUFFIXES
829Each source specifies a suffix to
830.Nm make .
831If no sources are specified, any previous specified suffices are deleted.
832.Sh ENVIRONMENT
833.Nm Make
834utilizes the following environment variables, if they exist:
835.Ev MAKE ,
836.Ev MAKEFLAGS
837and
838.Ev MAKEOBJDIR .
839.Sh FILES
840.Bl -tag -width /usr/share/mk -compact
841.It .depend
842list of dependencies
843.It Makefile
844list of dependencies
845.It makefile
846list of dependencies
847.It sys.mk
848system makefile
849.It /usr/share/mk
850system makefile directory
851.El
852.Sh SEE ALSO
853.Xr mkdep 1
854.Sh HISTORY
855A
856.Nm Make
857command appeared in
858.At v7 .
859