xref: /openbsd/usr.bin/make/make.1 (revision 09467b48)
1.\"	$OpenBSD: make.1,v 1.131 2020/01/26 12:40:50 espie Exp $
2.\"	$NetBSD: make.1,v 1.18 1997/03/10 21:19:53 christos Exp $
3.\"
4.\" Copyright (c) 1990, 1993
5.\"	The Regents of the University of California.  All rights reserved.
6.\"
7.\" Redistribution and use in source and binary forms, with or without
8.\" modification, are permitted provided that the following conditions
9.\" are met:
10.\" 1. Redistributions of source code must retain the above copyright
11.\"    notice, this list of conditions and the following disclaimer.
12.\" 2. Redistributions in binary form must reproduce the above copyright
13.\"    notice, this list of conditions and the following disclaimer in the
14.\"    documentation and/or other materials provided with the distribution.
15.\" 3. Neither the name of the University nor the names of its contributors
16.\"    may be used to endorse or promote products derived from this software
17.\"    without specific prior written permission.
18.\"
19.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29.\" SUCH DAMAGE.
30.\"
31.\"	from: @(#)make.1	8.4 (Berkeley) 3/19/94
32.\"
33.Dd $Mdocdate: January 26 2020 $
34.Dt MAKE 1
35.Os
36.Sh NAME
37.Nm make
38.Nd maintain program dependencies
39.Sh SYNOPSIS
40.Nm make
41.Op Fl BeiknpqrSst
42.Op Fl C Ar directory
43.Op Fl D Ar variable
44.Op Fl d Ar flags
45.Op Fl f Ar mk
46.Op Fl I Ar directory
47.Op Fl j Ar max_processes
48.Op Fl m Ar directory
49.Op Fl V Ar variable
50.Op Ar NAME Ns = Ns Ar value
51.Bk -words
52.Op Ar target ...
53.Ek
54.Sh DESCRIPTION
55.Nm
56is a program designed to simplify the maintenance of other programs.
57Its input is a
58.Em makefile :
59a list of specifications (target rules) describing build
60relationships between programs and other files.
61By default, the file
62.Pa makefile
63is used;
64if no such file is found, it tries
65.Pa Makefile .
66If neither of these exist,
67.Nm
68can still rely on a set of built-in system rules.
69.Pp
70If the file
71.Sq Pa .depend
72exists, it will also be read after the main
73.Ar makefile
74(see
75.Xr mkdep 1 ) .
76.Pp
77The handling of
78.Sq Pa .depend
79is a
80.Bx
81extension.
82.Pp
83If a list of
84.Ar target ...
85is specified,
86.Nm
87will build those targets.
88Otherwise a default target will be built:
89either a target explicitly marked with
90.Ic .MAIN
91or the first target encountered in the
92.Em makefile .
93.Pp
94Standard options are as follows:
95.Bl -tag -width Ds
96.It Fl e
97Environment variables override macro assignments within
98makefiles.
99.It Fl f Ar mk
100Read file
101.Ar mk
102instead of the default makefile.
103If
104.Ar mk
105is
106.Ql \- ,
107standard input is used.
108Multiple makefiles may be specified, and are read in the order specified.
109.It Fl i
110Ignore non-zero exit of shell commands in the makefile.
111Equivalent to specifying
112.Ql \-
113before each command line in the makefile.
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 p
121Print a dump of the target rules and variables on stdout.
122Do not build anything.
123.It Fl q
124Do not execute any commands, but exit with status 0 if the specified targets
125are up to date, and 1 otherwise.
126.It Fl r
127Do not use the built-in rules specified in the system makefile,
128.Pa <sys.mk> .
129.It Fl S
130Stop processing when an error is encountered.
131This is the default behavior.
132This is needed to negate the
133.Fl k
134option during recursive builds.
135.It Fl s
136Do not echo commands as they are executed.
137Equivalent to specifying
138.Sq Ic @
139before each command line in the makefile.
140.It Fl t
141Rather than re-building a target as specified in the makefile, create it
142or update its modification time to make it appear up to date, a bit like
143.Xr touch 1 .
144.It Ar NAME Ns = Ns Ar value
145Set the value of the variable
146.Ar NAME
147to
148.Ar value .
149.El
150.Pp
151Extended options are as follows:
152.Bl -tag -width Ds
153.It Fl B
154Try to be backwards compatible by executing the commands to make
155the prerequisites in a target rule in sequence.
156This is the default, in the absence of
157.Fl j Ar max_processes .
158.It Fl C Ar directory
159Enter
160.Ar directory
161before doing anything.
162.It Fl D Ar variable
163Define
164.Ar variable
165to be 1.
166.It Fl d Ar flags
167Turn on debugging, and specify which portions of
168.Nm
169are to print debugging information.
170.Ar flags
171is one or more of the following:
172.Bl -tag -width Ds
173.It Ar A
174Print all possible debugging information;
175equivalent to specifying all of the debugging flags.
176.It Ar a
177Print debugging information about archive searching and caching.
178.It Ar c
179Print debugging information about conditional evaluation.
180.It Ar d
181Print debugging information about directory searching and caching.
182.It Ar D
183Print warning messages about multiply defined command lists.
184.It Ar e
185Print debugging information about expensive command heuristics.
186.It Ar f
187Print debugging information about the expansion of for loops.
188.It Ar "g1"
189Print the input graph before making anything.
190.It Ar "g2"
191Print the input graph after making everything, or before exiting
192on error.
193.It Ar h
194Print information about jobs being held back because of sibling/target
195groups races.
196.It Ar j
197Print debugging information about forking processes to run commands.
198.It Ar k
199Print debugging information about manually killing processes.
200.It Ar l
201Print commands in Makefile targets regardless of whether or not they are
202prefixed by @.
203Also known as loud behavior.
204.It Ar m
205Print debugging information about making targets, including modification
206dates.
207.It Ar n
208Print debugging information about target names equivalence computations.
209.It Ar p
210Help finding concurrency issues for parallel make by adding some
211randomization.
212If
213.Va RANDOM_ORDER
214is defined,
215targets will be shuffled before being built.
216If
217.Va RANDOM_DELAY
218is defined,
219.Nm
220will wait between 0 and ${RANDOM_DELAY} seconds before starting a command.
221A given random seed can be forced by setting
222.Va RANDOM_SEED ,
223but this does not guarantee reproducibility.
224.It Ar q
225.Sq quick death
226option: after a fatal error, instead of waiting for other jobs to die,
227kill them right away.
228.It Ar s
229Print debugging information about inference (suffix) transformation rules.
230.It Ar t
231Print debugging information about target list maintenance.
232.It Ar T
233Print debugging information about target group determination.
234.It Ar v
235Print debugging information about variable assignment.
236.El
237.It Fl I Ar directory
238Specify a directory in which to search for makefiles and
239for "..."-style inclusions.
240Multiple directories can be added to form a search path.
241Furthermore, the system include path (see the
242.Fl m
243option) will be used after this search path.
244.It Fl j Ar max_processes
245Specify the maximum number of processes that
246.Nm
247may have running at any one time.
248.It Fl m Ar directory
249Specify a directory in which to search for system include files:
250.Pa sys.mk
251and <...>-style inclusions.
252Multiple directories can be added to form the system search path.
253Using
254.Fl m
255will override the default system include directory
256.Pa /usr/share/mk .
257.It Fl V Ar variable
258Print
259.Nm make Ns 's
260idea of the value of
261.Ar variable .
262Do not build any targets.
263Multiple instances of this option may be specified;
264the variables will be printed one per line,
265with a blank line for each null or undefined variable.
266.El
267.Pp
268There are seven different types of lines in a makefile: dependency
269lines, shell commands, variable assignments, include statements,
270conditional directives, for loops, and comments.
271Of these, include statements, conditional directives and for loops are
272extensions.
273.Pp
274A complete target rule is composed of a dependency line,
275followed by a list of shell commands.
276.Pp
277In general, lines may be continued from one line to the next by ending
278them with a backslash
279.Pq Ql \e .
280The trailing newline character and initial whitespace on the following
281line are compressed into a single space.
282.Sh DEPENDENCY LINES
283Dependency lines consist of one or more targets, an operator, and zero
284or more prerequisites:
285.Bd -ragged -offset indent
286.Ar target ... : Ns Op Ar prerequisite ...
287.Ed
288.Pp
289This creates a relationship where the targets
290.Dq depend
291on the prerequisites and are usually built from them.
292The exact relationship between targets and prerequisites is determined
293by the operator that separates them.
294.Pp
295It is an error to use different dependency operators for the same target.
296.Pp
297The operators are as follows:
298.Bl -tag -width flag
299.It Ic \&:
300A target is considered out of date if any of its prerequisites has
301been modified more recently than the target (that is, its modification time
302is less than that of any of its prerequisites).
303Thus, targets with no prerequisites are always out of date.
304.Pp
305.Nm
306will then execute the list of shell commands associated with that target.
307.Pp
308Additional prerequisites may be specified over additional dependency lines:
309.Nm
310will consider all prerequisites for determining out-of-date status.
311The target is removed if
312.Nm
313is interrupted.
314.It Ic \&!
315.Nm
316first examines all prerequisites and re-creates them as necessary.
317.Pp
318It will then always execute the list of shell commands associated with
319that target (as if the target always was out of date).
320.Pp
321Like
322.Ic \&: ,
323additional prerequisites may be specified over additional dependency lines,
324and the target is still removed if
325.Nm
326is interrupted.
327.It Ic \&::
328Each dependency line for a target is considered independently.
329A target is considered out of date for this target rule if any of its
330prerequisites in this dependency has been modified more recently than
331the target.
332.Pp
333.Nm
334will then execute the list of shell commands associated with that target.
335Target rules that specify no prerequisites are always executed.
336.Pp
337The target will not be removed if
338.Nm
339is interrupted.
340.El
341.Pp
342The
343.Ic \&:
344operator is the only standard operator.
345The
346.Ic \&::
347operator is a fairly standard extension,
348popularized by
349.Sy imake .
350The
351.Ic !\&
352operator is a
353.Bx
354extension.
355.Pp
356As an extension, targets and prerequisites may contain the shell wildcard
357expressions
358.Ql \&? ,
359.Ql * ,
360.Ql []
361and
362.Ql {} .
363The expressions
364.Ql \&? ,
365.Ql *
366and
367.Ql []
368may only be used as part of the final
369component of the target or prerequisite, and must be used to describe existing
370files.
371The expression
372.Ql {}
373need not necessarily be used to describe existing files.
374Expansion is in directory order, not alphabetically as done in the shell.
375.Pp
376For maximum portability, target names should only consist of periods,
377underscores, digits and alphabetic characters.
378.Pp
379The use of several targets can be a shorthand for duplicate rules.
380Specifically,
381.Bd -literal -offset indent
382target1 target2: reqa reqa
383	cmd1
384	cmd2
385.Ed
386.Pp
387may be replaced with
388.Bd -literal -offset indent
389target1: reqa reqa
390	cmd1
391	cmd2
392target2: reqa reqa
393	cmd1
394	cmd2
395.Ed
396.Pp
397in general.
398But
399.Nm
400is aware of parallel issues, and will not build those targets concurrently,
401if not appropriate.
402.Sh SHELL COMMANDS
403Each target may have associated with it a series of shell commands, normally
404used to build the target.
405While several dependency lines may name the same target, only one of
406these dependency lines should be followed by shell commands, and thus
407define a complete target rule (unless the
408.Sq Ic ::
409operator is used).
410Each of the shell commands in the target rule
411.Em must
412be preceded by a tab.
413.Pp
414If a command line begins with a combination of the characters,
415.Sq Ic @ ,
416.Sq Ic \-
417and/or
418.Sq Ic + ,
419the command is treated specially:
420.Bl -tag -width `@'
421.It Sq Ic @
422causes the command not to be echoed before it is executed.
423.It Sq Ic \-
424causes any non-zero exit status of the command line to be ignored.
425.It Sq Ic +
426causes the command to be executed even if
427.Fl n
428has been specified.
429(This can be useful to debug recursive Makefiles.)
430.El
431.Pp
432Commands are executed using
433.Pa /bin/sh
434in
435.Qq set -e
436mode, unless
437.Sq Ic \-
438is specified.
439.Pp
440As an optimization,
441.Nm
442may execute very simple commands without going through an extra shell
443process, as long as this does not change observable behavior.
444.Sh INFERENCE RULES
445.Nm
446also maintains a list of valid suffixes through the use of the
447.Ic .SUFFIXES
448special target.
449.Pp
450These suffixes can be used to write generic transformation rules called
451inference rules.
452.Pp
453If a target has the form
454.Sq \&.s1.s2 ,
455where .s1 and .s2 are currently valid suffixes, then it defines a
456transformation from *.s1 to *.s2 (double suffix inference).
457If a target has the form
458.Sq \&.s1 ,
459where .s1 is a currently valid suffix, then it defines a
460transformation from *.s1 to * (single suffix inference).
461.Pp
462A complete inference rule is a dependency line with such a target, the
463normal dependency operator, no prerequisites and a list of shell commands.
464.Pp
465When
466.Nm
467requires a target for which it has no complete target rule, it will try
468to apply a single active inference rule to create the target.
469.Pp
470For instance, with the following Makefile, describing a C program compiled
471from sources a.c and b.c, with header file a.h:
472.Bd -literal -offset indent
473\&.SUFFIXES: .c .o
474\&.c.o:
475	${CC} ${CFLAGS} -c $<
476
477prog: a.o b.o
478	${CC} ${CFLAGS} -o $@ a.o b.o
479
480a.o b.o: a.h
481
482b.o: b.c
483	${CC} -DFOO ${CFLAGS} -o $@ $<
484.Ed
485.Pp
486Consider b.o:
487there is a complete target rule re-creating it from b.c, so
488it will be compiled using ${CC} -DFOO.
489.Pp
490Consider a.o:
491there is no explicit target rule, so
492.Nm
493will consider valid transforms.
494Fortunately, there is an inference rule that can create a.o from a.c,
495so it will be compiled using ${CC}.
496.Pp
497Note that extra prerequisites are still taken into account, so both a.o
498and b.o depend on a.h for re-creation.
499.Pp
500Valid suffixes accumulate over
501.Ic .SUFFIXES
502lines.
503An empty
504.Ic .SUFFIXES
505can be used to reset the currently valid list of suffixes,
506but inference rules already read are still known by
507.Nm ,
508and they are marked as inactive.
509Redefining the corresponding suffix (or suffixes) will reactivate the rule.
510.Pp
511In case of duplicate inference rules with the same suffix combination,
512the new rule overrides the old one.
513.Pp
514For maximal portability, suffixes should start with a dot.
515.Sh VARIABLE ASSIGNMENTS
516Variables in
517.Nm
518are much like variables in the shell and, by tradition,
519consist of all upper-case letters.
520They are also called
521.Sq macros
522in various texts.
523For portability, only periods, underscores, digits and letters should be
524used for variable names.
525The following operators can be used to assign values to variables:
526.Bl -tag -width Ds
527.It Ic \&=
528Assign the value to the variable.
529Any previous value is overridden.
530.It Ic \&:=
531Assign with expansion, i.e., expand the value before assigning it
532to the variable (extension).
533.It Ic \&+=
534Append the value to the current value of the variable (extension).
535.It Ic \&?=
536Assign the value to the variable if it is not already defined
537.Po
538.Bx
539extension
540.Pc .
541Normally, expansion is not done until the variable is referenced.
542.It Ic \&!=
543Perform variable expansion and pass the result to the shell for
544execution on the spot, assigning the result to the variable.
545Any newlines in the result are also replaced with spaces
546.Po
547.Bx
548extension
549.Pc .
550.It Ic \&!!=
551Perform variable expansion on the spot and pass the result to the shell
552for execution only when the value is needed, assigning the result to
553the variable.
554.Pp
555This is almost identical to
556.Ic \&!=
557except that a shell is only run when the variable value is needed.
558Any newlines in the result are also replaced with spaces
559.Po
560.Ox
561extension
562.Pc .
563.El
564.Pp
565Any whitespace before the assigned
566.Ar value
567is removed; if the value is being appended, a single space is inserted
568between the previous contents of the variable and the appended value.
569.Pp
570Several extended assignment operators may be combined together.
571For instance,
572.Bd -literal -offset indent
573A ?!= cmd
574.Ed
575.Pp
576will only run
577.Qq cmd
578and put its output into
579.Va A
580if
581.Va A
582is not yet defined.
583.Pp
584Combinations that do not make sense, such as
585.Bd -literal -offset indent
586A +!!= cmd
587.Ed
588.Pp
589will not work.
590.Pp
591Variables are expanded by surrounding the variable name with either
592curly braces
593.Pq Ql {}
594or parentheses
595.Pq Ql ()
596and preceding it with
597a dollar sign
598.Pq Ql \&$ .
599If the variable name contains only a single letter, the surrounding
600braces or parentheses are not required.
601This shorter form is not recommended.
602.Pp
603Variable substitution occurs at two distinct times, depending on where
604the variable is being used.
605Variables in dependency lines are expanded as the line is read.
606Variables in shell commands are expanded when the shell command is
607executed.
608.Pp
609The four different classes of variables (in order of increasing precedence)
610are:
611.Bl -tag -width Ds
612.It Environment variables
613Variables defined as part of
614.Nm make Ns 's
615environment.
616.It Global variables
617Variables defined in the makefile or in included makefiles.
618.It Command line variables
619Variables defined as part of the command line.
620.It Local variables
621Variables that are defined specific to a certain target.
622Standard local variables are as follows:
623.Bl -tag -width ".ARCHIVE"
624.It Va @
625The name of the target.
626.It Va \&%
627The name of the archive member (only valid for library rules).
628.It Va \&!
629The name of the archive file (only valid for library rules).
630.It Va \&?
631The list of prerequisites for this target that were deemed out of date.
632.It Va \&<
633The name of the prerequisite from which this target is to be built, if a valid
634inference rule (suffix rule) is in scope.
635.It Va *
636The file prefix of the file, containing only the file portion,
637no suffix or preceding directory components.
638.El
639.Pp
640The six variables
641.Sq Va "@F" ,
642.Sq Va "@D" ,
643.Sq Va "<F" ,
644.Sq Va "<D" ,
645.Sq Va "*F" ,
646and
647.Sq Va "*D"
648yield the
649.Qq filename
650and
651.Qq directory
652parts of the corresponding macros.
653.Pp
654For maximum compatibility,
655.Sq Va \&<
656should only be used for actual inference rules.
657It is also set for normal target rules when there is an inference rule
658that matches the current target and prerequisite in scope.
659That is, in
660.Bd -literal -offset indent
661\&.SUFFIXES: .c .o
662file.o: file.c
663	cmd1 $<
664
665\&.c.o:
666	cmd2
667.Ed
668.Pp
669building
670.Pa file.o
671will execute
672.Qq cmd1 file.c .
673.Pp
674As an extension,
675.Nm
676supports the following local variables:
677.Bl -tag -width ".ARCHIVE"
678.It Va \&>
679The list of all prerequisites for this target.
680.It Va .ALLSRC
681Synonym for
682.Sq Va \&> .
683.It Va .ARCHIVE
684Synonym for
685.Sq Va \&! .
686.It Va .IMPSRC
687Synonym for
688.Sq Va \&< .
689.It Va .MEMBER
690Synonym for
691.Sq Va \&% .
692.It Va .OODATE
693Synonym for
694.Sq Va \&? .
695.It Va .PREFIX
696Synonym for
697.Sq Va * .
698.It Va .TARGET
699Synonym for
700.Sq Va @ .
701.El
702.Pp
703These variables may be used on the dependency half of dependency
704lines, when they make sense.
705.El
706.Pp
707In addition,
708.Nm
709sets or knows about the following internal variables, or environment
710variables:
711.Bl -tag -width MAKEFLAGS
712.It Va \&$
713A single dollar sign
714.Ql \&$ ,
715i.e.,
716.Ql \&$$
717expands to a single dollar
718sign.
719.It Va .MAKE
720The name that
721.Nm
722was executed with
723.Pq Va argv Ns Op 0 .
724.It Va .CURDIR
725A path to the directory where
726.Nm
727was executed.
728.It Va .OBJDIR
729Path to the directory where targets are built.
730At startup,
731.Nm
732searches for an alternate directory to place target files.
733.Nm
734tries to
735.Xr chdir 2
736into
737.Ev MAKEOBJDIR
738(or
739.Pa obj
740if
741.Ev MAKEOBJDIR
742is not defined),
743and sets
744.Va .OBJDIR
745accordingly.
746Should that fail,
747.Va .OBJDIR
748is set to
749.Va .CURDIR .
750.It Va MAKEFILE_LIST
751The list of files read by
752.Nm .
753.It Va .MAKEFLAGS
754The environment variable
755.Ev MAKEFLAGS
756may contain anything that
757may be specified on
758.Nm make Ns 's
759command line.
760Its contents are stored in
761.Nm make Ns 's
762.Va .MAKEFLAGS
763variable.
764Anything specified on
765.Nm make Ns 's
766command line is appended to the
767.Va .MAKEFLAGS
768variable which is then
769entered into the environment as
770.Ev MAKEFLAGS
771for all programs which
772.Nm
773executes.
774.It Va MFLAGS
775A shorter synonym for
776.Va .MAKEFLAGS .
777.It Ev PWD
778Alternate path to the current directory.
779.Nm
780normally sets
781.Sq Va .CURDIR
782to the canonical path given by
783.Xr getcwd 3 .
784However, if the environment variable
785.Ev PWD
786is set and gives a path to the current directory, then
787.Nm
788sets
789.Sq Va .CURDIR
790to the value of
791.Ev PWD
792instead.
793.Ev PWD
794is always set to the value of
795.Sq Va .OBJDIR
796for all programs which
797.Nm
798executes.
799.It Va .TARGETS
800List of targets
801.Nm
802is currently building.
803.It Va MACHINE
804Name of the machine architecture
805.Nm
806is running on, obtained from the
807.Ev MACHINE
808environment variable, or through
809.Xr uname 3
810if not defined.
811.It Va MACHINE_ARCH
812Name of the machine architecture
813.Nm
814was compiled for, obtained from the
815.Ev MACHINE_ARCH
816environment variable, or defined at compilation time.
817.It Va MACHINE_CPU
818Name of the machine processor
819.Nm
820was compiled for, obtained from the
821.Ev MACHINE_CPU
822environment variable, or defined at compilation time.
823On processors where only one endianness is possible, the value of this
824variable is always the same as
825.Ev MACHINE_ARCH .
826.It Va MAKEFILE
827Possibly the file name of the last makefile that has been read.
828It should not be used; see the
829.Sx BUGS
830section below.
831.El
832.Pp
833Variable expansion may be modified to select or modify each word of the
834variable (where
835.Dq word
836is a whitespace delimited sequence of characters).
837The general format of a variable expansion is as follows:
838.Pp
839.Dl {variable[:modifier[:...]]}
840.Pp
841Each modifier begins with a colon and one of the following
842special characters.
843The colon may be escaped with a backslash
844.Pq Ql \e .
845.Bl -tag -width Ds
846.It Cm :E
847Replaces each word in the variable with its suffix.
848.It Cm :H
849Replaces each word in the variable with everything but the last component.
850.It Cm :L
851Replaces each word in the variable with its lower case equivalent.
852.It Cm :U
853Replaces each word in the variable with its upper case equivalent.
854.It Cm :M Ns Ar pattern
855Select only those words that match the rest of the modifier.
856The standard shell wildcard characters
857.Pf ( Ql * ,
858.Ql \&? ,
859and
860.Ql [] )
861may
862be used.
863The wildcard characters may be escaped with a backslash
864.Pq Ql \e .
865.It Cm :N Ns Ar pattern
866This is identical to
867.Cm :M ,
868but selects all words which do not match
869the rest of the modifier.
870.It Cm :Q
871Quotes every shell meta-character in the variable, so that it can be passed
872safely through recursive invocations of
873.Nm make .
874.It Cm :QL
875Quote list: quotes every shell meta-character in the variable, except
876whitespace, so that it can be passed to a shell's
877.Sq for
878loops.
879.It Cm :R
880Replaces each word in the variable with everything but its suffix.
881.Sm off
882.It Cm :S No \&/ Ar old_string Xo
883.No \&/ Ar new_string
884.No \&/ Op Cm 1g
885.Xc
886.Sm on
887Modify the first occurrence of
888.Ar old_string
889in the variable's value, replacing it with
890.Ar new_string .
891If a
892.Ql g
893is appended to the last slash of the pattern, all occurrences
894in each word are replaced.
895If a
896.Ql 1
897is appended to the last slash of the pattern, only the first word
898is affected.
899If
900.Ar old_string
901begins with a caret
902.Pq Ql ^ ,
903.Ar old_string
904is anchored at the beginning of each word.
905If
906.Ar old_string
907ends with a dollar sign
908.Pq Ql \&$ ,
909it is anchored at the end of each word.
910Inside
911.Ar new_string ,
912an ampersand
913.Pq Ql &
914is replaced by
915.Ar old_string
916(without any
917.Ql ^
918or
919.Ql \&$ ) .
920Any character may be used as a delimiter for the parts of the modifier
921string.
922The anchoring, ampersand and delimiter characters may be escaped with a
923backslash
924.Pq Ql \e .
925.Pp
926Variable expansion occurs in the normal fashion inside both
927.Ar old_string
928and
929.Ar new_string
930with the single exception that a backslash is used to prevent the expansion
931of a dollar sign
932.Pq Ql \&$ ,
933not a preceding dollar sign as is usual.
934.Sm off
935.It Cm :C No \&/ Ar pattern Xo
936.No \&/ Ar replacement
937.No \&/ Op Cm 1g
938.Xc
939.Sm on
940The
941.Cm :C
942modifier is just like the
943.Cm :S
944modifier except that the old and new strings, instead of being
945simple strings, are an extended regular expression (see
946.Xr re_format 7 )
947and an
948.Xr ed 1 Ns \-style
949replacement string.
950Normally, the first occurrence of the pattern in
951each word of the value is changed.
952The
953.Ql 1
954modifier causes the substitution to apply to at most one word; the
955.Ql g
956modifier causes the substitution to apply to as many instances of the
957search pattern as occur in the word or words it is found in.
958Note that
959.Ql 1
960and
961.Ql g
962are orthogonal; the former specifies whether multiple words are
963potentially affected, the latter whether multiple substitutions can
964potentially occur within each affected word.
965.It Cm :T
966Replaces each word in the variable with its last component.
967.It Ar :old_string Ns = Ns Ar new_string
968This is the
969.At V
970style variable substitution.
971It must be the last modifier specified.
972If
973.Ar old_string
974or
975.Ar new_string
976do not contain the pattern matching character
977.Sq %
978then it is assumed that they are
979anchored at the end of each word, so only suffixes or entire
980words may be replaced.
981Otherwise
982.Sq %
983is the substring of
984.Ar old_string
985to be replaced in
986.Ar new_string .
987The right hand side
988.Pq Ar new_string
989may contain variable values, which will be expanded.
990To put an actual single dollar, just double it.
991.El
992.Pp
993All modifiers are
994.Bx
995extensions, except for the standard
996.At V
997style variable substitution.
998.Pp
999The interpretation of
1000.Sq %
1001and
1002.Sq $
1003in
1004.At V
1005variable substitutions is not mandated by POSIX, though it is
1006fairly common.
1007.Sh INCLUDE STATEMENTS, CONDITIONALS AND FOR LOOPS
1008Makefile inclusion, conditional structures and for loops reminiscent
1009of the C programming language are provided in
1010.Nm make .
1011All such structures are identified by a line beginning with a single
1012dot
1013.Pq Ql \&.
1014character.
1015Whitespace characters may follow this dot, e.g.,
1016.Bd -literal -offset indent
1017\&.include <file>
1018.Ed
1019and
1020.Bd -literal -offset indent -compact
1021\&.   include <file>
1022.Ed
1023.Pp
1024are identical constructs.
1025Files are included with either
1026.Ql .include <file>
1027or
1028.Ql .include Qq file .
1029Variables between the angle brackets or double quotes are expanded
1030to form the file name.
1031If angle brackets are used, the included makefile is expected to be in
1032the system makefile directory.
1033If double quotes are used, the including makefile's directory and any
1034directories specified using the
1035.Fl I
1036option are searched before the system
1037makefile directory.
1038.Pp
1039Conditional expressions are also preceded by a single dot as the first
1040character of a line.
1041The possible conditionals are as follows:
1042.Bl -tag -width Ds
1043.It Ic .undef Ar variable
1044Un-define the specified global variable.
1045Only global variables may be un-defined.
1046.It Ic .poison Ar variable
1047Poison the specified global variable.
1048Any further reference to
1049.Ar variable
1050will be flagged as an error.
1051.It Ic .poison !defined Pq Ar variable
1052It is an error to try to use the value of
1053.Ar variable
1054in a context where it is not defined.
1055.It Ic .poison empty Pq Ar variable
1056It is an error to try to use the value of
1057.Ar variable
1058in a context where it is not defined or is empty.
1059.It Xo
1060.Ic \&.if
1061.Oo \&! Oc Ns Ar expression
1062.Op Ar operator expression ...
1063.Xc
1064Test the value of an expression.
1065.It Xo
1066.Ic .ifdef
1067.Oo \&! Oc Ns Ar variable
1068.Op Ar operator variable ...
1069.Xc
1070Test the value of a variable.
1071.It Xo
1072.Ic .ifndef
1073.Oo \&! Oc Ns Ar variable
1074.Op Ar operator variable ...
1075.Xc
1076Test the value of a variable.
1077.It Xo
1078.Ic .ifmake
1079.Oo \&! Oc Ns Ar target
1080.Op Ar operator target ...
1081.Xc
1082Test the target being built.
1083.It Xo
1084.Ic .ifnmake
1085.Oo \&! Oc Ar target
1086.Op Ar operator target ...
1087.Xc
1088Test the target being built.
1089.It Ic .else
1090Reverse the sense of the last conditional.
1091.It Xo
1092.Ic .elif
1093.Oo \&! Oc Ar expression
1094.Op Ar operator expression ...
1095.Xc
1096A combination of
1097.Sq Ic .else
1098followed by
1099.Sq Ic .if .
1100.It Xo
1101.Ic .elifdef
1102.Oo \&! Oc Ns Ar variable
1103.Op Ar operator variable ...
1104.Xc
1105A combination of
1106.Sq Ic .else
1107followed by
1108.Sq Ic .ifdef .
1109.It Xo
1110.Ic .elifndef
1111.Oo \&! Oc Ns Ar variable
1112.Op Ar operator variable ...
1113.Xc
1114A combination of
1115.Sq Ic .else
1116followed by
1117.Sq Ic .ifndef .
1118.It Xo
1119.Ic .elifmake
1120.Oo \&! Oc Ns Ar target
1121.Op Ar operator target ...
1122.Xc
1123A combination of
1124.Sq Ic .else
1125followed by
1126.Sq Ic .ifmake .
1127.It Xo
1128.Ic .elifnmake
1129.Oo \&! Oc Ns Ar target
1130.Op Ar operator target ...
1131.Xc
1132A combination of
1133.Sq Ic .else
1134followed by
1135.Sq Ic .ifnmake .
1136.It Ic .endif
1137End the body of the conditional.
1138.El
1139.Pp
1140The
1141.Ar operator
1142may be any one of the following:
1143.Bl -tag -width "Cm XX"
1144.It Cm ||
1145logical OR
1146.It Cm \&&&
1147Logical AND; of higher precedence than
1148.Cm || .
1149.El
1150.Pp
1151As in C,
1152.Nm
1153will only evaluate a conditional as far as is necessary to determine
1154its value.
1155Parentheses may be used to change the order of evaluation.
1156The boolean operator
1157.Sq Ic \&!
1158may be used to logically negate an entire
1159conditional.
1160It is of higher precedence than
1161.Sq Ic \&&& .
1162.Pp
1163The value of
1164.Ar expression
1165may be any of the following:
1166.Bl -tag -width commands
1167.It Ic commands
1168Takes a target name as an argument and evaluates to true if the target
1169has been defined and has shell commands associated with it.
1170.It Ic defined
1171Takes a variable name as an argument and evaluates to true if the variable
1172has been defined.
1173.It Ic make
1174Takes a target name as an argument and evaluates to true if the target
1175was specified as part of
1176.Nm make Ns 's
1177command line or was declared the default target (either implicitly or
1178explicitly, see
1179.Va .MAIN )
1180before the line containing the conditional.
1181.It Ic empty
1182Takes a variable, with possible modifiers, and evaluates to true if
1183the expansion of the variable would result in an empty string.
1184.It Ic exists
1185Takes a file name as an argument and evaluates to true if the file exists.
1186The file is searched for on the system search path (see
1187.Va .PATH ) .
1188.It Ic target
1189Takes a target name as an argument and evaluates to true if the target
1190has been defined.
1191.El
1192.Pp
1193.Ar expression
1194may also be an arithmetic or string comparison.
1195Variable expansion is
1196performed on both sides of the comparison, after which the integral
1197values are compared.
1198A value is interpreted as hexadecimal if it is
1199preceded by 0x, otherwise it is decimal; octal numbers are not supported.
1200The standard C relational operators are all supported.
1201If after
1202variable expansion, either the left or right hand side of a
1203.Sq Ic ==
1204or
1205.Sq Ic "!="
1206operator is not an integral value, then
1207string comparison is performed between the expanded
1208variables.
1209If no relational operator is given, it is assumed that the expanded
1210variable is being compared against 0.
1211.Pp
1212When
1213.Nm
1214is evaluating one of these conditional expressions, and it encounters
1215a word it doesn't recognize, either the
1216.Dq make
1217or
1218.Dq defined
1219expression is applied to it, depending on the form of the conditional.
1220If the form is
1221.Sq Ic .ifdef
1222or
1223.Sq Ic .ifndef ,
1224the
1225.Dq defined
1226expression is applied.
1227Similarly, if the form is
1228.Sq Ic .ifmake
1229or
1230.Sq Ic .ifnmake ,
1231the
1232.Dq make
1233expression is applied.
1234.Pp
1235If the conditional evaluates to true the parsing of the makefile continues
1236as before.
1237If it evaluates to false, the following lines are skipped.
1238In both cases this continues until a
1239.Sq Ic .else
1240or
1241.Sq Ic .endif
1242is found.
1243.Pp
1244For loops are typically used to apply a set of rules to a list of files.
1245The syntax of a for loop is:
1246.Bd -unfilled -offset indent
1247.Ic .for Ar variable Oo Ar variable ... Oc Ic in Ar expression
1248	<make-rules>
1249.Ic .endfor
1250.Ed
1251.Pp
1252After the for
1253.Ar expression
1254is evaluated, it is split into words.
1255On each iteration of the loop, one word is assigned to each
1256.Ar variable ,
1257in order,
1258and these
1259.Ar variables
1260are substituted in the
1261.Ic make-rules
1262inside the body of the for loop.
1263The number of words must match the number of iteration variables;
1264that is, if there are three iteration variables, the number of words
1265must be a multiple of three.
1266.Pp
1267Loops and conditional expressions may nest arbitrarily, but
1268they may not cross include file boundaries.
1269.Pp
1270.Nm
1271also supports
1272.Ic sinclude
1273and
1274.Ic -include
1275for compatibility with other implementations.
1276Both use the same syntax:
1277.Bd -unfilled -offset indent
1278.Ic sinclude Pa file
1279.Ic -include Pa file
1280.Ed
1281.Pp
1282.Po
1283note no quotes around
1284.Pa file
1285.Pc
1286and will include
1287.Pa file ,
1288but without any error if it does not exist.
1289.Sh COMMENTS
1290Comments begin with a hash
1291.Pq Ql \&#
1292character, anywhere but in a shell
1293command line, and continue to the end of the line
1294(but a
1295.Pq Ql \&#
1296character in a shell command line will be interpreted as a comment by
1297the shell).
1298.Sh TARGET ATTRIBUTES
1299Some targets may be tagged with some specific attributes by one
1300of the
1301.Sx SPECIAL TARGETS
1302or
1303.Sx SPECIAL PREREQUISITES
1304described below.
1305.Bl -tag -width "Ignoring errors"
1306.It Dq Always build
1307Run the commands associated with this target even if the
1308.Fl n
1309or
1310.Fl t
1311options were specified.
1312Can be used to mark recursive
1313.Nm make Ns 's ,
1314but prefer standard
1315.Sq Ic + Ns Ar cmd .
1316.It Dq Cheap
1317In parallel mode, don't scan the commands for occurrences of
1318.Nm ,
1319thus letting normal recursive
1320.Fl j
1321behavior apply.
1322.It Dq Expensive
1323In parallel mode, assume commands will invoke recursive commands.
1324Once
1325.Nm
1326starts building an expensive target, it won't start building anything else
1327until that target has finished building.
1328.It Dq Ignoring errors
1329Ignore any errors generating by running shell commands, exactly
1330as if they were all preceded by a dash
1331.Pq Ql \- .
1332.It Dq Phony
1333A phony target is a target that does not correspond to any object in the
1334file system (more like a placeholder for a list of commands).
1335.Pp
1336Phony targets are always out of date at the start of a run, but
1337.Nm
1338still keeps track of when they are built (that is, when the associated
1339command list finishes running).
1340.It Dq Precious
1341Don't remove the target if
1342.Nm
1343is interrupted in the middle of building it.
1344.It Dq Silent
1345Do not display shell commands before running them, exactly as
1346if they were all preceded by a
1347.Sq @ .
1348.El
1349.Sh SPECIAL TARGETS
1350.Nm
1351recognizes standard special targets:
1352.Bl -tag -width ".NOTPARALLEL"
1353.It Ic .DEFAULT
1354If there is a
1355.Ic .DEFAULT
1356target rule, with commands but no prerequisites, and
1357.Nm
1358can't figure out another way to build a target, it will use that
1359list of commands, setting
1360.Va \&<
1361and
1362.Va @
1363appropriately.
1364.It Ic .IGNORE
1365Mark its prerequisites as
1366.Dq Ignoring errors .
1367.Pp
1368If the list of prerequisites is empty, apply that to all targets, exactly
1369like the
1370.Fl i
1371command-line option.
1372.It Ic .PRECIOUS
1373Mark its prerequisites as
1374.Dq Precious .
1375.Pp
1376If the list of prerequisites is empty, apply that to all targets.
1377.It Ic .SILENT
1378Mark its prerequisites as
1379.Dq Silent .
1380.Pp
1381If the list of prerequisites is empty, apply that to all targets, exactly
1382like the
1383.Fl s
1384command-line option.
1385.It Ic .SUFFIXES
1386See
1387.Sx INFERENCE RULES .
1388.El
1389.Pp
1390and also some other special targets as an extension:
1391.Bl -tag -width ".NOTPARALLEL"
1392.It Ic .BEGIN
1393Command lines attached to this target are executed before anything
1394else is done.
1395.It Ic .CHEAP
1396Mark its prerequisites as
1397.Dq Cheap .
1398.It Ic .END
1399Command lines attached to this target are executed at the end of a successful
1400run.
1401.It Ic .EXPENSIVE
1402Mark its prerequisites as
1403.Dq Expensive .
1404.It Ic .INTERRUPT
1405Command lines attached to this target are executed if
1406.Nm
1407is interrupted by a SIGINT.
1408.It Ic .MAKE
1409Mark its prerequisites as
1410.Dq Always build .
1411Prefer standard
1412.Sq Ic + Ns Ar cmd .
1413.It Ic .MAIN
1414If no target is specified when
1415.Nm
1416is invoked, this target will be built.
1417This is always set, either
1418explicitly, or implicitly when
1419.Nm
1420selects the default target, to give the user a way to refer to the default
1421target on the command line.
1422.It Ic .MAKEFLAGS
1423This target provides a way to specify flags for
1424.Nm
1425when the makefile is used.
1426The flags are as if typed to the shell, though the
1427.Fl f
1428option will have
1429no effect.
1430.It Ic .NOTPARALLEL
1431Disable parallel mode for the current makefile.
1432The
1433.Fl j
1434option is still passed to submakes.
1435.It Ic .NO_PARALLEL
1436Same as above, for compatibility with other pmake variants.
1437.It Ic .ORDER
1438The list of prerequisites should be built in sequence.
1439.It Ic .PATH
1440The prerequisites define a search path: directories that will be searched
1441for files not found in the current directory.
1442If no prerequisites are specified, any previously specified directories are
1443deleted.
1444.It Ic .PATH\fI.suffix\fR
1445This target is only valid if .suffix is a currently valid suffix.
1446The prerequisites defines a search path for files ending in that suffix.
1447For files not found in the current directory,
1448.Nm
1449will first look in that path, before reverting to the default search path.
1450.It Ic .PHONY
1451Mark its prerequisites as
1452.Dq Phony
1453targets.
1454.El
1455.Pp
1456It is an error to use several special targets, or a special target and
1457normal targets, in a single dependency line.
1458.Sh SPECIAL PREREQUISITES
1459Of the special targets described in the previous
1460section, the ones that tag prerequisites can also be used as prerequisites,
1461in which case the corresponding targets will be tagged accordingly.
1462.Pp
1463This is an extension, even for standard special targets.
1464.Pp
1465.Nm
1466also recognizes some other prerequisites:
1467.Bl -tag -width ".PRECIOUS"
1468.It Ic .NOTMAIN
1469Normally
1470.Nm
1471selects the first target it encounters as the default target to be built
1472if no target was specified.
1473This prerequisite prevents this target from being selected.
1474.It Ic .OPTIONAL
1475If a target is marked with this attribute and
1476.Nm
1477can't figure out how to create it, it will ignore this fact and assume
1478the file isn't needed or already exists.
1479.It Ic .USE
1480Turn the target into
1481.Nm make Ns 's
1482version of a macro.
1483When the target is used as a prerequisite for another target, the other target
1484acquires the commands, prerequisites, and attributes (except for
1485.Ic .USE )
1486of the
1487prerequisite.
1488If the target already has commands, the
1489.Ic .USE
1490target's commands are appended
1491to them.
1492.It Ic .WAIT
1493If
1494.Ic .WAIT
1495appears in a dependency line, the prerequisites that precede it are
1496made before the prerequisites that follow it in the line.
1497Loops are not
1498detected and targets that form loops will be silently ignored.
1499.El
1500.Sh ENVIRONMENT
1501.Nm
1502uses the following environment variables, if they exist:
1503.Ev MACHINE ,
1504.Ev MACHINE_ARCH ,
1505.Ev MACHINE_CPU ,
1506.Ev MAKEFLAGS ,
1507.Ev MAKEOBJDIR
1508and
1509.Ev PWD .
1510.Nm
1511also ignores and unsets
1512.Ev CDPATH .
1513.Sh FILES
1514.Bl -tag -width /usr/share/mk -compact
1515.It Pa .depend
1516list of dependencies
1517.It Pa makefile
1518default makefile
1519.It Pa Makefile
1520default makefile if
1521.Pa makefile
1522does not exist
1523.It Pa sys.mk
1524system makefile
1525.It Pa /usr/share/mk
1526system makefile directory
1527.El
1528.Sh EXIT STATUS
1529If
1530.Fl q
1531was specified, the
1532.Nm
1533utility exits with one of the following values:
1534.Pp
1535.Bl -tag -width Ds -offset indent -compact
1536.It 0
1537Normal behavior.
1538.It 1
1539The target was not up to date.
1540.It >1
1541An error occurred.
1542.El
1543.Pp
1544Otherwise, the
1545.Nm
1546utility exits with a value of 0 on success, and >0 if an error occurred.
1547.Sh SEE ALSO
1548.Xr ed 1 ,
1549.Xr mkdep 1 ,
1550.Xr sh 1 ,
1551.Xr getcwd 3 ,
1552.Xr uname 3 ,
1553.Xr re_format 7
1554.Sh STANDARDS
1555The
1556.Nm
1557utility is mostly compliant with the
1558.St -p1003.1-2008
1559specification,
1560though its presence is optional.
1561.Pp
1562The flags
1563.Op Fl BCDdIjmV
1564are extensions to that specification.
1565.Pp
1566Older versions of
1567.Nm
1568used
1569.Ev MAKE
1570instead of
1571.Ev MAKEFLAGS .
1572This was removed for POSIX compatibility.
1573The internal variable
1574.Va MAKE
1575is set to the same value as
1576.Va .MAKE .
1577Support for this may be removed in the future.
1578.Pp
1579Most of the more esoteric features of
1580.Nm
1581should probably be avoided for greater compatibility.
1582.Sh HISTORY
1583A
1584.Nm
1585command appeared in
1586.At v7 .
1587.Pp
1588This implementation is a distant derivative of
1589.Nm pmake ,
1590originally written by Adam de Boor.
1591.Sh BUGS
1592If the same target is specified several times in complete target rules,
1593.Nm
1594silently ignores all commands after the first non empty set of commands,
1595e.g., in
1596.Bd -literal -offset indent
1597a:
1598	@echo "Executed"
1599a:
1600	@echo "Bad luck"
1601.Ed
1602.Pp
1603@echo "Bad luck" will be ignored.
1604.Pp
1605.Va .TARGETS
1606is not set to the default target when
1607.Nm
1608is invoked without a target name and no
1609.Ic MAIN
1610special target exists.
1611.Pp
1612The evaluation of
1613.Ar expression
1614in a test is somewhat simplistic.
1615Variables don't need to be quoted, but strings do:
1616Tests like
1617.Ql .if ${VAR} == "string" ,
1618.Ql .if ${VAR} >= 5 ,
1619.Ql .if 5 <= 10 ,
1620and
1621.Ql .if "string" == ${VAR}
1622do work, but
1623.Ql .if string = ${VAR}
1624doesn't.
1625.Pp
1626For loops are expanded before tests, so a fragment such as:
1627.Bd -literal -offset indent
1628\&.for TMACHINE in ${SHARED_ARCHS}
1629\&.if "${TMACHINE}" == ${MACHINE}
1630     ...
1631\&.endif
1632\&.endfor
1633.Ed
1634.Pp
1635requires the quotes.
1636.Pp
1637When handling
1638.Pf pre- Bx 4.4
1639archives,
1640.Nm
1641may erroneously mark archive members as out of date if the archive name
1642was truncated.
1643.Pp
1644The handling of
1645.Sq ;\&
1646and other special characters in tests may be utterly bogus.
1647For instance, in
1648.Bd -literal -offset indent
1649\&A=abcd;c.c
1650\&.if ${A:R} == "abcd;c"
1651.Ed
1652.Pp
1653the test will never match, even though the value is correct.
1654.Pp
1655In a .for loop, only the variable value is used; assignments will be
1656evaluated later, e.g., in
1657.Bd -literal -offset indent
1658\&.for I in a b c d
1659I:=${I:S/a/z/}
1660A+=$I
1661\&.endfor
1662.Ed
1663.Pp
1664.Sq A
1665will evaluate to a b c d after the loop, not z b c d.
1666.Pp
1667.Ic ORDER
1668is currently only used in parallel mode, so
1669keep prerequisites ordered for sequential mode!
1670.Pp
1671Distinct target names are treated separately, even though they might
1672correspond to the same file in the file system.
1673This can cause excessive rebuilds of some targets, and bogus
1674races in parallel mode.
1675This can also prevent
1676.Nm
1677from finding a rule to solve a dependency if the target name is not
1678exactly the same as the dependency.
1679.Pp
1680In parallel mode,
1681.Fl j Ar n
1682only limits the number of direct children of
1683.Nm .
1684During recursive invocations, each level may multiply the total number
1685of processes by
1686.Ar n .
1687However,
1688.Nm
1689includes some heuristics to try to prevent catastrophic behavior:
1690if a command is marked as expensive, or preceded by
1691.Sq + ,
1692or seems to
1693invoke a program that looks sufficiently like
1694.Sq make ,
1695.Nm
1696will assume recursive invocation, and not start any new process until
1697said command has finished running.
1698Thus the number of processes run directly or indirectly by
1699.Nm
1700will increase linearly with each level of recursion instead of exponentially.
1701.Pp
1702The
1703.Va MAKEFILE
1704variable cannot be used reliably.
1705It is a compatibility feature and may get set to the last makefile
1706specified, as it is set by System V make.
1707