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