xref: /freebsd/contrib/bmake/make.1 (revision 907b59d76938e654f0d040a888e8dfca3de1e222)
1.\"	$NetBSD: make.1,v 1.259 2016/06/03 07:07:37 wiz Exp $
2.\"
3.\" Copyright (c) 1990, 1993
4.\"	The Regents of the University of California.  All rights reserved.
5.\"
6.\" Redistribution and use in source and binary forms, with or without
7.\" modification, are permitted provided that the following conditions
8.\" are met:
9.\" 1. Redistributions of source code must retain the above copyright
10.\"    notice, this list of conditions and the following disclaimer.
11.\" 2. Redistributions in binary form must reproduce the above copyright
12.\"    notice, this list of conditions and the following disclaimer in the
13.\"    documentation and/or other materials provided with the distribution.
14.\" 3. Neither the name of the University nor the names of its contributors
15.\"    may be used to endorse or promote products derived from this software
16.\"    without specific prior written permission.
17.\"
18.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28.\" SUCH DAMAGE.
29.\"
30.\"	from: @(#)make.1	8.4 (Berkeley) 3/19/94
31.\"
32.Dd June 2, 2016
33.Dt MAKE 1
34.Os
35.Sh NAME
36.Nm make
37.Nd maintain program dependencies
38.Sh SYNOPSIS
39.Nm
40.Op Fl BeikNnqrstWwX
41.Op Fl C Ar directory
42.Op Fl D Ar variable
43.Op Fl d Ar flags
44.Op Fl f Ar makefile
45.Op Fl I Ar directory
46.Op Fl J Ar private
47.Op Fl j Ar max_jobs
48.Op Fl m Ar directory
49.Op Fl T Ar file
50.Op Fl V Ar variable
51.Op Ar variable=value
52.Op Ar target ...
53.Sh DESCRIPTION
54.Nm
55is a program designed to simplify the maintenance of other programs.
56Its input is a list of specifications as to the files upon which programs
57and other files depend.
58If no
59.Fl f Ar makefile
60makefile option is given,
61.Nm
62will try to open
63.Ql Pa makefile
64then
65.Ql Pa Makefile
66in order to find the specifications.
67If the file
68.Ql Pa .depend
69exists, it is read (see
70.Xr mkdep 1 ) .
71.Pp
72This manual page is intended as a reference document only.
73For a more thorough description of
74.Nm
75and makefiles, please refer to
76.%T "PMake \- A Tutorial" .
77.Pp
78.Nm
79will prepend the contents of the
80.Va MAKEFLAGS
81environment variable to the command line arguments before parsing them.
82.Pp
83The options are as follows:
84.Bl -tag -width Ds
85.It Fl B
86Try to be backwards compatible by executing a single shell per command and
87by executing the commands to make the sources of a dependency line in sequence.
88.It Fl C Ar directory
89Change to
90.Ar directory
91before reading the makefiles or doing anything else.
92If multiple
93.Fl C
94options are specified, each is interpreted relative to the previous one:
95.Fl C Pa / Fl C Pa etc
96is equivalent to
97.Fl C Pa /etc .
98.It Fl D Ar variable
99Define
100.Ar variable
101to be 1, in the global context.
102.It Fl d Ar [-]flags
103Turn on debugging, and specify which portions of
104.Nm
105are to print debugging information.
106Unless the flags are preceded by
107.Ql \-
108they are added to the
109.Va MAKEFLAGS
110environment variable and will be processed by any child make processes.
111By default, debugging information is printed to standard error,
112but this can be changed using the
113.Ar F
114debugging flag.
115The debugging output is always unbuffered; in addition, if debugging
116is enabled but debugging output is not directed to standard output,
117then the standard output is line buffered.
118.Ar Flags
119is one or more of the following:
120.Bl -tag -width Ds
121.It Ar A
122Print all possible debugging information;
123equivalent to specifying all of the debugging flags.
124.It Ar a
125Print debugging information about archive searching and caching.
126.It Ar C
127Print debugging information about current working directory.
128.It Ar c
129Print debugging information about conditional evaluation.
130.It Ar d
131Print debugging information about directory searching and caching.
132.It Ar e
133Print debugging information about failed commands and targets.
134.It Ar F Ns Oo Sy \&+ Oc Ns Ar filename
135Specify where debugging output is written.
136This must be the last flag, because it consumes the remainder of
137the argument.
138If the character immediately after the
139.Ql F
140flag is
141.Ql \&+ ,
142then the file will be opened in append mode;
143otherwise the file will be overwritten.
144If the file name is
145.Ql stdout
146or
147.Ql stderr
148then debugging output will be written to the
149standard output or standard error output file descriptors respectively
150(and the
151.Ql \&+
152option has no effect).
153Otherwise, the output will be written to the named file.
154If the file name ends
155.Ql .%d
156then the
157.Ql %d
158is replaced by the pid.
159.It Ar f
160Print debugging information about loop evaluation.
161.It Ar "g1"
162Print the input graph before making anything.
163.It Ar "g2"
164Print the input graph after making everything, or before exiting
165on error.
166.It Ar "g3"
167Print the input graph before exiting on error.
168.It Ar j
169Print debugging information about running multiple shells.
170.It Ar l
171Print commands in Makefiles regardless of whether or not they are prefixed by
172.Ql @
173or other "quiet" flags.
174Also known as "loud" behavior.
175.It Ar M
176Print debugging information about "meta" mode decisions about targets.
177.It Ar m
178Print debugging information about making targets, including modification
179dates.
180.It Ar n
181Don't delete the temporary command scripts created when running commands.
182These temporary scripts are created in the directory
183referred to by the
184.Ev TMPDIR
185environment variable, or in
186.Pa /tmp
187if
188.Ev TMPDIR
189is unset or set to the empty string.
190The temporary scripts are created by
191.Xr mkstemp 3 ,
192and have names of the form
193.Pa makeXXXXXX .
194.Em NOTE :
195This can create many files in
196.Ev TMPDIR
197or
198.Pa /tmp ,
199so use with care.
200.It Ar p
201Print debugging information about makefile parsing.
202.It Ar s
203Print debugging information about suffix-transformation rules.
204.It Ar t
205Print debugging information about target list maintenance.
206.It Ar V
207Force the
208.Fl V
209option to print raw values of variables.
210.It Ar v
211Print debugging information about variable assignment.
212.It Ar x
213Run shell commands with
214.Fl x
215so the actual commands are printed as they are executed.
216.El
217.It Fl e
218Specify that environment variables override macro assignments within
219makefiles.
220.It Fl f Ar makefile
221Specify a makefile to read instead of the default
222.Ql Pa makefile .
223If
224.Ar makefile
225is
226.Ql Fl ,
227standard input is read.
228Multiple makefiles may be specified, and are read in the order specified.
229.It Fl I Ar directory
230Specify a directory in which to search for makefiles and included makefiles.
231The system makefile directory (or directories, see the
232.Fl m
233option) is automatically included as part of this list.
234.It Fl i
235Ignore non-zero exit of shell commands in the makefile.
236Equivalent to specifying
237.Ql Fl
238before each command line in the makefile.
239.It Fl J Ar private
240This option should
241.Em not
242be specified by the user.
243.Pp
244When the
245.Ar j
246option is in use in a recursive build, this option is passed by a make
247to child makes to allow all the make processes in the build to
248cooperate to avoid overloading the system.
249.It Fl j Ar max_jobs
250Specify the maximum number of jobs that
251.Nm
252may have running at any one time.
253The value is saved in
254.Va .MAKE.JOBS .
255Turns compatibility mode off, unless the
256.Ar B
257flag is also specified.
258When compatibility mode is off, all commands associated with a
259target are executed in a single shell invocation as opposed to the
260traditional one shell invocation per line.
261This can break traditional scripts which change directories on each
262command invocation and then expect to start with a fresh environment
263on the next line.
264It is more efficient to correct the scripts rather than turn backwards
265compatibility on.
266.It Fl k
267Continue processing after errors are encountered, but only on those targets
268that do not depend on the target whose creation caused the error.
269.It Fl m Ar directory
270Specify a directory in which to search for sys.mk and makefiles included
271via the
272.Ao Ar file Ac Ns -style
273include statement.
274The
275.Fl m
276option can be used multiple times to form a search path.
277This path will override the default system include path: /usr/share/mk.
278Furthermore the system include path will be appended to the search path used
279for
280.Qo Ar file Qc Ns -style
281include statements (see the
282.Fl I
283option).
284.Pp
285If a file or directory name in the
286.Fl m
287argument (or the
288.Ev MAKESYSPATH
289environment variable) starts with the string
290.Qq \&.../
291then
292.Nm
293will search for the specified file or directory named in the remaining part
294of the argument string.
295The search starts with the current directory of
296the Makefile and then works upward towards the root of the file system.
297If the search is successful, then the resulting directory replaces the
298.Qq \&.../
299specification in the
300.Fl m
301argument.
302If used, this feature allows
303.Nm
304to easily search in the current source tree for customized sys.mk files
305(e.g., by using
306.Qq \&.../mk/sys.mk
307as an argument).
308.It Fl n
309Display the commands that would have been executed, but do not
310actually execute them unless the target depends on the .MAKE special
311source (see below).
312.It Fl N
313Display the commands which would have been executed, but do not
314actually execute any of them; useful for debugging top-level makefiles
315without descending into subdirectories.
316.It Fl q
317Do not execute any commands, but exit 0 if the specified targets are
318up-to-date and 1, otherwise.
319.It Fl r
320Do not use the built-in rules specified in the system makefile.
321.It Fl s
322Do not echo any commands as they are executed.
323Equivalent to specifying
324.Ql Ic @
325before each command line in the makefile.
326.It Fl T Ar tracefile
327When used with the
328.Fl j
329flag,
330append a trace record to
331.Ar tracefile
332for each job started and completed.
333.It Fl t
334Rather than re-building a target as specified in the makefile, create it
335or update its modification time to make it appear up-to-date.
336.It Fl V Ar variable
337Print
338.Nm Ns 's
339idea of the value of
340.Ar variable ,
341in the global context.
342Do not build any targets.
343Multiple instances of this option may be specified;
344the variables will be printed one per line,
345with a blank line for each null or undefined variable.
346If
347.Ar variable
348contains a
349.Ql \&$
350then the value will be expanded before printing.
351.It Fl W
352Treat any warnings during makefile parsing as errors.
353.It Fl w
354Print entering and leaving directory messages, pre and post processing.
355.It Fl X
356Don't export variables passed on the command line to the environment
357individually.
358Variables passed on the command line are still exported
359via the
360.Va MAKEFLAGS
361environment variable.
362This option may be useful on systems which have a small limit on the
363size of command arguments.
364.It Ar variable=value
365Set the value of the variable
366.Ar variable
367to
368.Ar value .
369Normally, all values passed on the command line are also exported to
370sub-makes in the environment.
371The
372.Fl X
373flag disables this behavior.
374Variable assignments should follow options for POSIX compatibility
375but no ordering is enforced.
376.El
377.Pp
378There are seven different types of lines in a makefile: file dependency
379specifications, shell commands, variable assignments, include statements,
380conditional directives, for loops, and comments.
381.Pp
382In general, lines may be continued from one line to the next by ending
383them with a backslash
384.Pq Ql \e .
385The trailing newline character and initial whitespace on the following
386line are compressed into a single space.
387.Sh FILE DEPENDENCY SPECIFICATIONS
388Dependency lines consist of one or more targets, an operator, and zero
389or more sources.
390This creates a relationship where the targets
391.Dq depend
392on the sources
393and are usually created from them.
394The exact relationship between the target and the source is determined
395by the operator that separates them.
396The three operators are as follows:
397.Bl -tag -width flag
398.It Ic \&:
399A target is considered out-of-date if its modification time is less than
400those of any of its sources.
401Sources for a target accumulate over dependency lines when this operator
402is used.
403The target is removed if
404.Nm
405is interrupted.
406.It Ic \&!
407Targets are always re-created, but not until all sources have been
408examined and re-created as necessary.
409Sources for a target accumulate over dependency lines when this operator
410is used.
411The target is removed if
412.Nm
413is interrupted.
414.It Ic \&::
415If no sources are specified, the target is always re-created.
416Otherwise, a target is considered out-of-date if any of its sources has
417been modified more recently than the target.
418Sources for a target do not accumulate over dependency lines when this
419operator is used.
420The target will not be removed if
421.Nm
422is interrupted.
423.El
424.Pp
425Targets and sources may contain the shell wildcard values
426.Ql \&? ,
427.Ql * ,
428.Ql [] ,
429and
430.Ql {} .
431The values
432.Ql \&? ,
433.Ql * ,
434and
435.Ql []
436may only be used as part of the final
437component of the target or source, and must be used to describe existing
438files.
439The value
440.Ql {}
441need not necessarily be used to describe existing files.
442Expansion is in directory order, not alphabetically as done in the shell.
443.Sh SHELL COMMANDS
444Each target may have associated with it one or more lines of shell
445commands, normally
446used to create the target.
447Each of the lines in this script
448.Em must
449be preceded by a tab.
450(For historical reasons, spaces are not accepted.)
451While targets can appear in many dependency lines if desired, by
452default only one of these rules may be followed by a creation
453script.
454If the
455.Ql Ic \&::
456operator is used, however, all rules may include scripts and the
457scripts are executed in the order found.
458.Pp
459Each line is treated as a separate shell command, unless the end of
460line is escaped with a backslash
461.Pq Ql \e
462in which case that line and the next are combined.
463.\" The escaped newline is retained and passed to the shell, which
464.\" normally ignores it.
465.\" However, the tab at the beginning of the following line is removed.
466If the first characters of the command are any combination of
467.Ql Ic @ ,
468.Ql Ic + ,
469or
470.Ql Ic \- ,
471the command is treated specially.
472A
473.Ql Ic @
474causes the command not to be echoed before it is executed.
475A
476.Ql Ic +
477causes the command to be executed even when
478.Fl n
479is given.
480This is similar to the effect of the .MAKE special source,
481except that the effect can be limited to a single line of a script.
482A
483.Ql Ic \-
484in compatibility mode
485causes any non-zero exit status of the command line to be ignored.
486.Pp
487When
488.Nm
489is run in jobs mode with
490.Fl j Ar max_jobs ,
491the entire script for the target is fed to a
492single instance of the shell.
493In compatibility (non-jobs) mode, each command is run in a separate process.
494If the command contains any shell meta characters
495.Pq Ql #=|^(){};&<>*?[]:$`\e\en
496it will be passed to the shell; otherwise
497.Nm
498will attempt direct execution.
499If a line starts with
500.Ql Ic \-
501and the shell has ErrCtl enabled then failure of the command line
502will be ignored as in compatibility mode.
503Otherwise
504.Ql Ic \-
505affects the entire job;
506the script will stop at the first command line that fails,
507but the target will not be deemed to have failed.
508.Pp
509Makefiles should be written so that the mode of
510.Nm
511operation does not change their behavior.
512For example, any command which needs to use
513.Dq cd
514or
515.Dq chdir
516without potentially changing the directory for subsequent commands
517should be put in parentheses so it executes in a subshell.
518To force the use of one shell, escape the line breaks so as to make
519the whole script one command.
520For example:
521.Bd -literal -offset indent
522avoid-chdir-side-effects:
523	@echo Building $@ in `pwd`
524	@(cd ${.CURDIR} && ${MAKE} $@)
525	@echo Back in `pwd`
526
527ensure-one-shell-regardless-of-mode:
528	@echo Building $@ in `pwd`; \e
529	(cd ${.CURDIR} && ${MAKE} $@); \e
530	echo Back in `pwd`
531.Ed
532.Pp
533Since
534.Nm
535will
536.Xr chdir 2
537to
538.Ql Va .OBJDIR
539before executing any targets, each child process
540starts with that as its current working directory.
541.Sh VARIABLE ASSIGNMENTS
542Variables in make are much like variables in the shell, and, by tradition,
543consist of all upper-case letters.
544.Ss Variable assignment modifiers
545The five operators that can be used to assign values to variables are as
546follows:
547.Bl -tag -width Ds
548.It Ic \&=
549Assign the value to the variable.
550Any previous value is overridden.
551.It Ic \&+=
552Append the value to the current value of the variable.
553.It Ic \&?=
554Assign the value to the variable if it is not already defined.
555.It Ic \&:=
556Assign with expansion, i.e. expand the value before assigning it
557to the variable.
558Normally, expansion is not done until the variable is referenced.
559.Em NOTE :
560References to undefined variables are
561.Em not
562expanded.
563This can cause problems when variable modifiers are used.
564.It Ic \&!=
565Expand the value and pass it to the shell for execution and assign
566the result to the variable.
567Any newlines in the result are replaced with spaces.
568.El
569.Pp
570Any white-space before the assigned
571.Ar value
572is removed; if the value is being appended, a single space is inserted
573between the previous contents of the variable and the appended value.
574.Pp
575Variables are expanded by surrounding the variable name with either
576curly braces
577.Pq Ql {}
578or parentheses
579.Pq Ql ()
580and preceding it with
581a dollar sign
582.Pq Ql \&$ .
583If the variable name contains only a single letter, the surrounding
584braces or parentheses are not required.
585This shorter form is not recommended.
586.Pp
587If the variable name contains a dollar, then the name itself is expanded first.
588This allows almost arbitrary variable names, however names containing dollar,
589braces, parenthesis, or whitespace are really best avoided!
590.Pp
591If the result of expanding a variable contains a dollar sign
592.Pq Ql \&$
593the string is expanded again.
594.Pp
595Variable substitution occurs at three distinct times, depending on where
596the variable is being used.
597.Bl -enum
598.It
599Variables in dependency lines are expanded as the line is read.
600.It
601Variables in shell commands are expanded when the shell command is
602executed.
603.It
604.Dq .for
605loop index variables are expanded on each loop iteration.
606Note that other variables are not expanded inside loops so
607the following example code:
608.Bd -literal -offset indent
609
610.Dv .for i in 1 2 3
611a+=     ${i}
612j=      ${i}
613b+=     ${j}
614.Dv .endfor
615
616all:
617	@echo ${a}
618	@echo ${b}
619
620.Ed
621will print:
622.Bd -literal -offset indent
6231 2 3
6243 3 3
625
626.Ed
627Because while ${a} contains
628.Dq 1 2 3
629after the loop is executed, ${b}
630contains
631.Dq ${j} ${j} ${j}
632which expands to
633.Dq 3 3 3
634since after the loop completes ${j} contains
635.Dq 3 .
636.El
637.Ss Variable classes
638The four different classes of variables (in order of increasing precedence)
639are:
640.Bl -tag -width Ds
641.It Environment variables
642Variables defined as part of
643.Nm Ns 's
644environment.
645.It Global variables
646Variables defined in the makefile or in included makefiles.
647.It Command line variables
648Variables defined as part of the command line.
649.It Local variables
650Variables that are defined specific to a certain target.
651.El
652.Pp
653Local variables are all built in and their values vary magically from
654target to target.
655It is not currently possible to define new local variables.
656The seven local variables are as follows:
657.Bl -tag -width ".ARCHIVE" -offset indent
658.It Va .ALLSRC
659The list of all sources for this target; also known as
660.Ql Va \&\*[Gt] .
661.It Va .ARCHIVE
662The name of the archive file; also known as
663.Ql Va \&! .
664.It Va .IMPSRC
665In suffix-transformation rules, the name/path of the source from which the
666target is to be transformed (the
667.Dq implied
668source); also known as
669.Ql Va \&\*[Lt] .
670It is not defined in explicit rules.
671.It Va .MEMBER
672The name of the archive member; also known as
673.Ql Va % .
674.It Va .OODATE
675The list of sources for this target that were deemed out-of-date; also
676known as
677.Ql Va \&? .
678.It Va .PREFIX
679The file prefix of the target, containing only the file portion, no suffix
680or preceding directory components; also known as
681.Ql Va * .
682The suffix must be one of the known suffixes declared with
683.Ic .SUFFIXES
684or it will not be recognized.
685.It Va .TARGET
686The name of the target; also known as
687.Ql Va @ .
688For compatibility with other makes this is an alias for
689.Ic .ARCHIVE
690in archive member rules.
691.El
692.Pp
693The shorter forms
694.Ql ( Va \*[Gt] ,
695.Ql Va \&! ,
696.Ql Va \*[Lt] ,
697.Ql Va % ,
698.Ql Va \&? ,
699.Ql Va * ,
700and
701.Ql Va @ )
702are permitted for backward
703compatibility with historical makefiles and legacy POSIX make and are
704not recommended.
705.Pp
706Variants of these variables with the punctuation followed immediately by
707.Ql D
708or
709.Ql F ,
710e.g.
711.Ql Va $(@D) ,
712are legacy forms equivalent to using the
713.Ql :H
714and
715.Ql :T
716modifiers.
717These forms are accepted for compatibility with
718.At V
719makefiles and POSIX but are not recommended.
720.Pp
721Four of the local variables may be used in sources on dependency lines
722because they expand to the proper value for each target on the line.
723These variables are
724.Ql Va .TARGET ,
725.Ql Va .PREFIX ,
726.Ql Va .ARCHIVE ,
727and
728.Ql Va .MEMBER .
729.Ss Additional built-in variables
730In addition,
731.Nm
732sets or knows about the following variables:
733.Bl -tag -width .MAKEOVERRIDES
734.It Va \&$
735A single dollar sign
736.Ql \&$ ,
737i.e.
738.Ql \&$$
739expands to a single dollar
740sign.
741.It Va .ALLTARGETS
742The list of all targets encountered in the Makefile.
743If evaluated during
744Makefile parsing, lists only those targets encountered thus far.
745.It Va .CURDIR
746A path to the directory where
747.Nm
748was executed.
749Refer to the description of
750.Ql Ev PWD
751for more details.
752.It Va .INCLUDEDFROMDIR
753The directory of the file this Makefile was included from.
754.It Va .INCLUDEDFROMFILE
755The filename of the file this Makefile was included from.
756.It Ev MAKE
757The name that
758.Nm
759was executed with
760.Pq Va argv[0] .
761For compatibility
762.Nm
763also sets
764.Va .MAKE
765with the same value.
766The preferred variable to use is the environment variable
767.Ev MAKE
768because it is more compatible with other versions of
769.Nm
770and cannot be confused with the special target with the same name.
771.It Va .MAKE.ALWAYS_PASS_JOB_QUEUE
772Tells
773.Nm
774whether to pass the descriptors of the job token queue
775even if the target is not tagged with
776.Ic .MAKE
777The default is
778.Ql Pa yes
779for backwards compatability with
780.Fx 9.0
781and earlier.
782.It Va .MAKE.DEPENDFILE
783Names the makefile (default
784.Ql Pa .depend )
785from which generated dependencies are read.
786.It Va .MAKE.EXPAND_VARIABLES
787A boolean that controls the default behavior of the
788.Fl V
789option.
790.It Va .MAKE.EXPORTED
791The list of variables exported by
792.Nm .
793.It Va .MAKE.JOBS
794The argument to the
795.Fl j
796option.
797.It Va .MAKE.JOB.PREFIX
798If
799.Nm
800is run with
801.Ar j
802then output for each target is prefixed with a token
803.Ql --- target ---
804the first part of which can be controlled via
805.Va .MAKE.JOB.PREFIX .
806If
807.Va .MAKE.JOB.PREFIX
808is empty, no token is printed.
809.br
810For example:
811.Li .MAKE.JOB.PREFIX=${.newline}---${.MAKE:T}[${.MAKE.PID}]
812would produce tokens like
813.Ql ---make[1234] target ---
814making it easier to track the degree of parallelism being achieved.
815.It Ev MAKEFLAGS
816The environment variable
817.Ql Ev MAKEFLAGS
818may contain anything that
819may be specified on
820.Nm Ns 's
821command line.
822Anything specified on
823.Nm Ns 's
824command line is appended to the
825.Ql Ev MAKEFLAGS
826variable which is then
827entered into the environment for all programs which
828.Nm
829executes.
830.It Va .MAKE.LEVEL
831The recursion depth of
832.Nm .
833The initial instance of
834.Nm
835will be 0, and an incremented value is put into the environment
836to be seen by the next generation.
837This allows tests like:
838.Li .if ${.MAKE.LEVEL} == 0
839to protect things which should only be evaluated in the initial instance of
840.Nm .
841.It Va .MAKE.MAKEFILE_PREFERENCE
842The ordered list of makefile names
843(default
844.Ql Pa makefile ,
845.Ql Pa Makefile )
846that
847.Nm
848will look for.
849.It Va .MAKE.MAKEFILES
850The list of makefiles read by
851.Nm ,
852which is useful for tracking dependencies.
853Each makefile is recorded only once, regardless of the number of times read.
854.It Va .MAKE.MODE
855Processed after reading all makefiles.
856Can affect the mode that
857.Nm
858runs in.
859It can contain a number of keywords:
860.Bl -hang -width missing-filemon=bf.
861.It Pa compat
862Like
863.Fl B ,
864puts
865.Nm
866into "compat" mode.
867.It Pa meta
868Puts
869.Nm
870into "meta" mode, where meta files are created for each target
871to capture the command run, the output generated and if
872.Xr filemon 4
873is available, the system calls which are of interest to
874.Nm .
875The captured output can be very useful when diagnosing errors.
876.It Pa curdirOk= Ar bf
877Normally
878.Nm
879will not create .meta files in
880.Ql Va .CURDIR .
881This can be overridden by setting
882.Va bf
883to a value which represents True.
884.It Pa missing-meta= Ar bf
885If
886.Va bf
887is True, then a missing .meta file makes the target out-of-date.
888.It Pa missing-filemon= Ar bf
889If
890.Va bf
891is True, then missing filemon data makes the target out-of-date.
892.It Pa nofilemon
893Do not use
894.Xr filemon 4 .
895.It Pa env
896For debugging, it can be useful to include the environment
897in the .meta file.
898.It Pa verbose
899If in "meta" mode, print a clue about the target being built.
900This is useful if the build is otherwise running silently.
901The message printed the value of:
902.Va .MAKE.META.PREFIX .
903.It Pa ignore-cmd
904Some makefiles have commands which are simply not stable.
905This keyword causes them to be ignored for
906determining whether a target is out of date in "meta" mode.
907See also
908.Ic .NOMETA_CMP .
909.It Pa silent= Ar bf
910If
911.Va bf
912is True, when a .meta file is created, mark the target
913.Ic .SILENT .
914.El
915.It Va .MAKE.META.BAILIWICK
916In "meta" mode, provides a list of prefixes which
917match the directories controlled by
918.Nm .
919If a file that was generated outside of
920.Va .OBJDIR
921but within said bailiwick is missing,
922the current target is considered out-of-date.
923.It Va .MAKE.META.CREATED
924In "meta" mode, this variable contains a list of all the meta files
925updated.
926If not empty, it can be used to trigger processing of
927.Va .MAKE.META.FILES .
928.It Va .MAKE.META.FILES
929In "meta" mode, this variable contains a list of all the meta files
930used (updated or not).
931This list can be used to process the meta files to extract dependency
932information.
933.It Va .MAKE.META.IGNORE_PATHS
934Provides a list of path prefixes that should be ignored;
935because the contents are expected to change over time.
936The default list includes:
937.Ql Pa /dev /etc /proc /tmp /var/run /var/tmp
938.It Va .MAKE.META.IGNORE_PATTERNS
939Provides a list of patterns to match against pathnames.
940Ignore any that match.
941.It Va .MAKE.META.PREFIX
942Defines the message printed for each meta file updated in "meta verbose" mode.
943The default value is:
944.Dl Building ${.TARGET:H:tA}/${.TARGET:T}
945.It Va .MAKEOVERRIDES
946This variable is used to record the names of variables assigned to
947on the command line, so that they may be exported as part of
948.Ql Ev MAKEFLAGS .
949This behavior can be disabled by assigning an empty value to
950.Ql Va .MAKEOVERRIDES
951within a makefile.
952Extra variables can be exported from a makefile
953by appending their names to
954.Ql Va .MAKEOVERRIDES .
955.Ql Ev MAKEFLAGS
956is re-exported whenever
957.Ql Va .MAKEOVERRIDES
958is modified.
959.It Va .MAKE.PATH_FILEMON
960If
961.Nm
962was built with
963.Xr filemon 4
964support, this is set to the path of the device node.
965This allows makefiles to test for this support.
966.It Va .MAKE.PID
967The process-id of
968.Nm .
969.It Va .MAKE.PPID
970The parent process-id of
971.Nm .
972.It Va .MAKE.SAVE_DOLLARS
973value should be a boolean that controls whether
974.Ql $$
975are preserved when doing
976.Ql :=
977assignments.
978The default is false, for backwards compatibility.
979Set to true for compatability with other makes.
980If set to false,
981.Ql $$
982becomes
983.Ql $
984per normal evaluation rules.
985.It Va MAKE_PRINT_VAR_ON_ERROR
986When
987.Nm
988stops due to an error, it prints its name and the value of
989.Ql Va .CURDIR
990as well as the value of any variables named in
991.Ql Va MAKE_PRINT_VAR_ON_ERROR .
992.It Va .newline
993This variable is simply assigned a newline character as its value.
994This allows expansions using the
995.Cm \&:@
996modifier to put a newline between
997iterations of the loop rather than a space.
998For example, the printing of
999.Ql Va MAKE_PRINT_VAR_ON_ERROR
1000could be done as ${MAKE_PRINT_VAR_ON_ERROR:@v@$v='${$v}'${.newline}@}.
1001.It Va .OBJDIR
1002A path to the directory where the targets are built.
1003Its value is determined by trying to
1004.Xr chdir 2
1005to the following directories in order and using the first match:
1006.Bl -enum
1007.It
1008.Ev ${MAKEOBJDIRPREFIX}${.CURDIR}
1009.Pp
1010(Only if
1011.Ql Ev MAKEOBJDIRPREFIX
1012is set in the environment or on the command line.)
1013.It
1014.Ev ${MAKEOBJDIR}
1015.Pp
1016(Only if
1017.Ql Ev MAKEOBJDIR
1018is set in the environment or on the command line.)
1019.It
1020.Ev ${.CURDIR} Ns Pa /obj. Ns Ev ${MACHINE}
1021.It
1022.Ev ${.CURDIR} Ns Pa /obj
1023.It
1024.Pa /usr/obj/ Ns Ev ${.CURDIR}
1025.It
1026.Ev ${.CURDIR}
1027.El
1028.Pp
1029Variable expansion is performed on the value before it's used,
1030so expressions such as
1031.Dl ${.CURDIR:S,^/usr/src,/var/obj,}
1032may be used.
1033This is especially useful with
1034.Ql Ev MAKEOBJDIR .
1035.Pp
1036.Ql Va .OBJDIR
1037may be modified in the makefile via the special target
1038.Ql Ic .OBJDIR .
1039In all cases,
1040.Nm
1041will
1042.Xr chdir 2
1043to the specified directory if it exists, and set
1044.Ql Va .OBJDIR
1045and
1046.Ql Ev PWD
1047to that directory before executing any targets.
1048.
1049.It Va .PARSEDIR
1050A path to the directory of the current
1051.Ql Pa Makefile
1052being parsed.
1053.It Va .PARSEFILE
1054The basename of the current
1055.Ql Pa Makefile
1056being parsed.
1057This variable and
1058.Ql Va .PARSEDIR
1059are both set only while the
1060.Ql Pa Makefiles
1061are being parsed.
1062If you want to retain their current values, assign them to a variable
1063using assignment with expansion:
1064.Pq Ql Cm \&:= .
1065.It Va .PATH
1066A variable that represents the list of directories that
1067.Nm
1068will search for files.
1069The search list should be updated using the target
1070.Ql Va .PATH
1071rather than the variable.
1072.It Ev PWD
1073Alternate path to the current directory.
1074.Nm
1075normally sets
1076.Ql Va .CURDIR
1077to the canonical path given by
1078.Xr getcwd 3 .
1079However, if the environment variable
1080.Ql Ev PWD
1081is set and gives a path to the current directory, then
1082.Nm
1083sets
1084.Ql Va .CURDIR
1085to the value of
1086.Ql Ev PWD
1087instead.
1088This behavior is disabled if
1089.Ql Ev MAKEOBJDIRPREFIX
1090is set or
1091.Ql Ev MAKEOBJDIR
1092contains a variable transform.
1093.Ql Ev PWD
1094is set to the value of
1095.Ql Va .OBJDIR
1096for all programs which
1097.Nm
1098executes.
1099.It Ev .TARGETS
1100The list of targets explicitly specified on the command line, if any.
1101.It Ev VPATH
1102Colon-separated
1103.Pq Dq \&:
1104lists of directories that
1105.Nm
1106will search for files.
1107The variable is supported for compatibility with old make programs only,
1108use
1109.Ql Va .PATH
1110instead.
1111.El
1112.Ss Variable modifiers
1113Variable expansion may be modified to select or modify each word of the
1114variable (where a
1115.Dq word
1116is white-space delimited sequence of characters).
1117The general format of a variable expansion is as follows:
1118.Pp
1119.Dl ${variable[:modifier[:...]]}
1120.Pp
1121Each modifier begins with a colon,
1122which may be escaped with a backslash
1123.Pq Ql \e .
1124.Pp
1125A set of modifiers can be specified via a variable, as follows:
1126.Pp
1127.Dl modifier_variable=modifier[:...]
1128.Dl ${variable:${modifier_variable}[:...]}
1129.Pp
1130In this case the first modifier in the modifier_variable does not
1131start with a colon, since that must appear in the referencing
1132variable.
1133If any of the modifiers in the modifier_variable contain a dollar sign
1134.Pq Ql $ ,
1135these must be doubled to avoid early expansion.
1136.Pp
1137The supported modifiers are:
1138.Bl -tag -width EEE
1139.It Cm \&:E
1140Replaces each word in the variable with its suffix.
1141.It Cm \&:H
1142Replaces each word in the variable with everything but the last component.
1143.It Cm \&:M Ns Ar pattern
1144Select only those words that match
1145.Ar pattern .
1146The standard shell wildcard characters
1147.Pf ( Ql * ,
1148.Ql \&? ,
1149and
1150.Ql Oo Oc )
1151may
1152be used.
1153The wildcard characters may be escaped with a backslash
1154.Pq Ql \e .
1155As a consequence of the way values are split into words, matched,
1156and then joined, a construct like
1157.Dl ${VAR:M*}
1158will normalize the inter-word spacing, removing all leading and
1159trailing space, and converting multiple consecutive spaces
1160to single spaces.
1161.
1162.It Cm \&:N Ns Ar pattern
1163This is identical to
1164.Ql Cm \&:M ,
1165but selects all words which do not match
1166.Ar pattern .
1167.It Cm \&:O
1168Order every word in variable alphabetically.
1169To sort words in
1170reverse order use the
1171.Ql Cm \&:O:[-1..1]
1172combination of modifiers.
1173.It Cm \&:Ox
1174Randomize words in variable.
1175The results will be different each time you are referring to the
1176modified variable; use the assignment with expansion
1177.Pq Ql Cm \&:=
1178to prevent such behavior.
1179For example,
1180.Bd -literal -offset indent
1181LIST=			uno due tre quattro
1182RANDOM_LIST=		${LIST:Ox}
1183STATIC_RANDOM_LIST:=	${LIST:Ox}
1184
1185all:
1186	@echo "${RANDOM_LIST}"
1187	@echo "${RANDOM_LIST}"
1188	@echo "${STATIC_RANDOM_LIST}"
1189	@echo "${STATIC_RANDOM_LIST}"
1190.Ed
1191may produce output similar to:
1192.Bd -literal -offset indent
1193quattro due tre uno
1194tre due quattro uno
1195due uno quattro tre
1196due uno quattro tre
1197.Ed
1198.It Cm \&:Q
1199Quotes every shell meta-character in the variable, so that it can be passed
1200safely through recursive invocations of
1201.Nm .
1202.It Cm \&:R
1203Replaces each word in the variable with everything but its suffix.
1204.It Cm \&:gmtime
1205The value is a format string for
1206.Xr strftime 3 ,
1207using the current
1208.Xr gmtime 3 .
1209.It Cm \&:hash
1210Compute a 32-bit hash of the value and encode it as hex digits.
1211.It Cm \&:localtime
1212The value is a format string for
1213.Xr strftime 3 ,
1214using the current
1215.Xr localtime 3 .
1216.It Cm \&:tA
1217Attempt to convert variable to an absolute path using
1218.Xr realpath 3 ,
1219if that fails, the value is unchanged.
1220.It Cm \&:tl
1221Converts variable to lower-case letters.
1222.It Cm \&:ts Ns Ar c
1223Words in the variable are normally separated by a space on expansion.
1224This modifier sets the separator to the character
1225.Ar c .
1226If
1227.Ar c
1228is omitted, then no separator is used.
1229The common escapes (including octal numeric codes), work as expected.
1230.It Cm \&:tu
1231Converts variable to upper-case letters.
1232.It Cm \&:tW
1233Causes the value to be treated as a single word
1234(possibly containing embedded white space).
1235See also
1236.Ql Cm \&:[*] .
1237.It Cm \&:tw
1238Causes the value to be treated as a sequence of
1239words delimited by white space.
1240See also
1241.Ql Cm \&:[@] .
1242.Sm off
1243.It Cm \&:S No \&/ Ar old_string No \&/ Ar new_string No \&/ Op Cm 1gW
1244.Sm on
1245Modify the first occurrence of
1246.Ar old_string
1247in the variable's value, replacing it with
1248.Ar new_string .
1249If a
1250.Ql g
1251is appended to the last slash of the pattern, all occurrences
1252in each word are replaced.
1253If a
1254.Ql 1
1255is appended to the last slash of the pattern, only the first word
1256is affected.
1257If a
1258.Ql W
1259is appended to the last slash of the pattern,
1260then the value is treated as a single word
1261(possibly containing embedded white space).
1262If
1263.Ar old_string
1264begins with a caret
1265.Pq Ql ^ ,
1266.Ar old_string
1267is anchored at the beginning of each word.
1268If
1269.Ar old_string
1270ends with a dollar sign
1271.Pq Ql \&$ ,
1272it is anchored at the end of each word.
1273Inside
1274.Ar new_string ,
1275an ampersand
1276.Pq Ql \*[Am]
1277is replaced by
1278.Ar old_string
1279(without any
1280.Ql ^
1281or
1282.Ql \&$ ) .
1283Any character may be used as a delimiter for the parts of the modifier
1284string.
1285The anchoring, ampersand and delimiter characters may be escaped with a
1286backslash
1287.Pq Ql \e .
1288.Pp
1289Variable expansion occurs in the normal fashion inside both
1290.Ar old_string
1291and
1292.Ar new_string
1293with the single exception that a backslash is used to prevent the expansion
1294of a dollar sign
1295.Pq Ql \&$ ,
1296not a preceding dollar sign as is usual.
1297.Sm off
1298.It Cm \&:C No \&/ Ar pattern No \&/ Ar replacement No \&/ Op Cm 1gW
1299.Sm on
1300The
1301.Cm \&:C
1302modifier is just like the
1303.Cm \&:S
1304modifier except that the old and new strings, instead of being
1305simple strings, are an extended regular expression (see
1306.Xr regex 3 )
1307string
1308.Ar pattern
1309and an
1310.Xr ed 1 Ns \-style
1311string
1312.Ar replacement .
1313Normally, the first occurrence of the pattern
1314.Ar pattern
1315in each word of the value is substituted with
1316.Ar replacement .
1317The
1318.Ql 1
1319modifier causes the substitution to apply to at most one word; the
1320.Ql g
1321modifier causes the substitution to apply to as many instances of the
1322search pattern
1323.Ar pattern
1324as occur in the word or words it is found in; the
1325.Ql W
1326modifier causes the value to be treated as a single word
1327(possibly containing embedded white space).
1328Note that
1329.Ql 1
1330and
1331.Ql g
1332are orthogonal; the former specifies whether multiple words are
1333potentially affected, the latter whether multiple substitutions can
1334potentially occur within each affected word.
1335.Pp
1336As for the
1337.Cm \&:S
1338modifier, the
1339.Ar pattern
1340and
1341.Ar replacement
1342are subjected to variable expansion before being parsed as
1343regular expressions.
1344.It Cm \&:T
1345Replaces each word in the variable with its last component.
1346.It Cm \&:u
1347Remove adjacent duplicate words (like
1348.Xr uniq 1 ) .
1349.Sm off
1350.It Cm \&:\&? Ar true_string Cm \&: Ar false_string
1351.Sm on
1352If the variable name (not its value), when parsed as a .if conditional
1353expression, evaluates to true, return as its value the
1354.Ar true_string ,
1355otherwise return the
1356.Ar false_string .
1357Since the variable name is used as the expression, \&:\&? must be the
1358first modifier after the variable name itself - which will, of course,
1359usually contain variable expansions.
1360A common error is trying to use expressions like
1361.Dl ${NUMBERS:M42:?match:no}
1362which actually tests defined(NUMBERS),
1363to determine is any words match "42" you need to use something like:
1364.Dl ${"${NUMBERS:M42}" != \&"\&":?match:no} .
1365.It Ar :old_string=new_string
1366This is the
1367.At V
1368style variable substitution.
1369It must be the last modifier specified.
1370If
1371.Ar old_string
1372or
1373.Ar new_string
1374do not contain the pattern matching character
1375.Ar %
1376then it is assumed that they are
1377anchored at the end of each word, so only suffixes or entire
1378words may be replaced.
1379Otherwise
1380.Ar %
1381is the substring of
1382.Ar old_string
1383to be replaced in
1384.Ar new_string .
1385.Pp
1386Variable expansion occurs in the normal fashion inside both
1387.Ar old_string
1388and
1389.Ar new_string
1390with the single exception that a backslash is used to prevent the
1391expansion of a dollar sign
1392.Pq Ql \&$ ,
1393not a preceding dollar sign as is usual.
1394.Sm off
1395.It Cm \&:@ Ar temp Cm @ Ar string Cm @
1396.Sm on
1397This is the loop expansion mechanism from the OSF Development
1398Environment (ODE) make.
1399Unlike
1400.Cm \&.for
1401loops expansion occurs at the time of
1402reference.
1403Assign
1404.Ar temp
1405to each word in the variable and evaluate
1406.Ar string .
1407The ODE convention is that
1408.Ar temp
1409should start and end with a period.
1410For example.
1411.Dl ${LINKS:@.LINK.@${LN} ${TARGET} ${.LINK.}@}
1412.Pp
1413However a single character variable is often more readable:
1414.Dl ${MAKE_PRINT_VAR_ON_ERROR:@v@$v='${$v}'${.newline}@}
1415.It Cm \&:U Ns Ar newval
1416If the variable is undefined
1417.Ar newval
1418is the value.
1419If the variable is defined, the existing value is returned.
1420This is another ODE make feature.
1421It is handy for setting per-target CFLAGS for instance:
1422.Dl ${_${.TARGET:T}_CFLAGS:U${DEF_CFLAGS}}
1423If a value is only required if the variable is undefined, use:
1424.Dl ${VAR:D:Unewval}
1425.It Cm \&:D Ns Ar newval
1426If the variable is defined
1427.Ar newval
1428is the value.
1429.It Cm \&:L
1430The name of the variable is the value.
1431.It Cm \&:P
1432The path of the node which has the same name as the variable
1433is the value.
1434If no such node exists or its path is null, then the
1435name of the variable is used.
1436In order for this modifier to work, the name (node) must at least have
1437appeared on the rhs of a dependency.
1438.Sm off
1439.It Cm \&:\&! Ar cmd Cm \&!
1440.Sm on
1441The output of running
1442.Ar cmd
1443is the value.
1444.It Cm \&:sh
1445If the variable is non-empty it is run as a command and the output
1446becomes the new value.
1447.It Cm \&::= Ns Ar str
1448The variable is assigned the value
1449.Ar str
1450after substitution.
1451This modifier and its variations are useful in
1452obscure situations such as wanting to set a variable when shell commands
1453are being parsed.
1454These assignment modifiers always expand to
1455nothing, so if appearing in a rule line by themselves should be
1456preceded with something to keep
1457.Nm
1458happy.
1459.Pp
1460The
1461.Ql Cm \&::
1462helps avoid false matches with the
1463.At V
1464style
1465.Cm \&:=
1466modifier and since substitution always occurs the
1467.Cm \&::=
1468form is vaguely appropriate.
1469.It Cm \&::?= Ns Ar str
1470As for
1471.Cm \&::=
1472but only if the variable does not already have a value.
1473.It Cm \&::+= Ns Ar str
1474Append
1475.Ar str
1476to the variable.
1477.It Cm \&::!= Ns Ar cmd
1478Assign the output of
1479.Ar cmd
1480to the variable.
1481.It Cm \&:\&[ Ns Ar range Ns Cm \&]
1482Selects one or more words from the value,
1483or performs other operations related to the way in which the
1484value is divided into words.
1485.Pp
1486Ordinarily, a value is treated as a sequence of words
1487delimited by white space.
1488Some modifiers suppress this behavior,
1489causing a value to be treated as a single word
1490(possibly containing embedded white space).
1491An empty value, or a value that consists entirely of white-space,
1492is treated as a single word.
1493For the purposes of the
1494.Ql Cm \&:[]
1495modifier, the words are indexed both forwards using positive integers
1496(where index 1 represents the first word),
1497and backwards using negative integers
1498(where index \-1 represents the last word).
1499.Pp
1500The
1501.Ar range
1502is subjected to variable expansion, and the expanded result is
1503then interpreted as follows:
1504.Bl -tag -width index
1505.\" :[n]
1506.It Ar index
1507Selects a single word from the value.
1508.\" :[start..end]
1509.It Ar start Ns Cm \&.. Ns Ar end
1510Selects all words from
1511.Ar start
1512to
1513.Ar end ,
1514inclusive.
1515For example,
1516.Ql Cm \&:[2..-1]
1517selects all words from the second word to the last word.
1518If
1519.Ar start
1520is greater than
1521.Ar end ,
1522then the words are output in reverse order.
1523For example,
1524.Ql Cm \&:[-1..1]
1525selects all the words from last to first.
1526.\" :[*]
1527.It Cm \&*
1528Causes subsequent modifiers to treat the value as a single word
1529(possibly containing embedded white space).
1530Analogous to the effect of
1531\&"$*\&"
1532in Bourne shell.
1533.\" :[0]
1534.It 0
1535Means the same as
1536.Ql Cm \&:[*] .
1537.\" :[*]
1538.It Cm \&@
1539Causes subsequent modifiers to treat the value as a sequence of words
1540delimited by white space.
1541Analogous to the effect of
1542\&"$@\&"
1543in Bourne shell.
1544.\" :[#]
1545.It Cm \&#
1546Returns the number of words in the value.
1547.El \" :[range]
1548.El
1549.Sh INCLUDE STATEMENTS, CONDITIONALS AND FOR LOOPS
1550Makefile inclusion, conditional structures and for loops  reminiscent
1551of the C programming language are provided in
1552.Nm .
1553All such structures are identified by a line beginning with a single
1554dot
1555.Pq Ql \&.
1556character.
1557Files are included with either
1558.Cm \&.include Aq Ar file
1559or
1560.Cm \&.include Pf \*q Ar file Ns \*q .
1561Variables between the angle brackets or double quotes are expanded
1562to form the file name.
1563If angle brackets are used, the included makefile is expected to be in
1564the system makefile directory.
1565If double quotes are used, the including makefile's directory and any
1566directories specified using the
1567.Fl I
1568option are searched before the system
1569makefile directory.
1570For compatibility with other versions of
1571.Nm
1572.Ql include file ...
1573is also accepted.
1574.Pp
1575If the include statement is written as
1576.Cm .-include
1577or as
1578.Cm .sinclude
1579then errors locating and/or opening include files are ignored.
1580.Pp
1581If the include statement is written as
1582.Cm .dinclude
1583not only are errors locating and/or opening include files ignored,
1584but stale dependencies within the included file will be ignored
1585just like
1586.Va .MAKE.DEPENDFILE .
1587.Pp
1588Conditional expressions are also preceded by a single dot as the first
1589character of a line.
1590The possible conditionals are as follows:
1591.Bl -tag -width Ds
1592.It Ic .error Ar message
1593The message is printed along with the name of the makefile and line number,
1594then
1595.Nm
1596will exit.
1597.It Ic .export Ar variable ...
1598Export the specified global variable.
1599If no variable list is provided, all globals are exported
1600except for internal variables (those that start with
1601.Ql \&. ) .
1602This is not affected by the
1603.Fl X
1604flag, so should be used with caution.
1605For compatibility with other
1606.Nm
1607programs
1608.Ql export variable=value
1609is also accepted.
1610.Pp
1611Appending a variable name to
1612.Va .MAKE.EXPORTED
1613is equivalent to exporting a variable.
1614.It Ic .export-env Ar variable ...
1615The same as
1616.Ql .export ,
1617except that the variable is not appended to
1618.Va .MAKE.EXPORTED .
1619This allows exporting a value to the environment which is different from that
1620used by
1621.Nm
1622internally.
1623.It Ic .export-literal Ar variable ...
1624The same as
1625.Ql .export-env ,
1626except that variables in the value are not expanded.
1627.It Ic .info Ar message
1628The message is printed along with the name of the makefile and line number.
1629.It Ic .undef Ar variable
1630Un-define the specified global variable.
1631Only global variables may be un-defined.
1632.It Ic .unexport Ar variable ...
1633The opposite of
1634.Ql .export .
1635The specified global
1636.Va variable
1637will be removed from
1638.Va .MAKE.EXPORTED .
1639If no variable list is provided, all globals are unexported,
1640and
1641.Va .MAKE.EXPORTED
1642deleted.
1643.It Ic .unexport-env
1644Unexport all globals previously exported and
1645clear the environment inherited from the parent.
1646This operation will cause a memory leak of the original environment,
1647so should be used sparingly.
1648Testing for
1649.Va .MAKE.LEVEL
1650being 0, would make sense.
1651Also note that any variables which originated in the parent environment
1652should be explicitly preserved if desired.
1653For example:
1654.Bd -literal -offset indent
1655.Li .if ${.MAKE.LEVEL} == 0
1656PATH := ${PATH}
1657.Li .unexport-env
1658.Li .export PATH
1659.Li .endif
1660.Pp
1661.Ed
1662Would result in an environment containing only
1663.Ql Ev PATH ,
1664which is the minimal useful environment.
1665Actually
1666.Ql Ev .MAKE.LEVEL
1667will also be pushed into the new environment.
1668.It Ic .warning Ar message
1669The message prefixed by
1670.Ql Pa warning:
1671is printed along with the name of the makefile and line number.
1672.It Ic \&.if Oo \&! Oc Ns Ar expression Op Ar operator expression ...
1673Test the value of an expression.
1674.It Ic .ifdef Oo \&! Oc Ns Ar variable Op Ar operator variable ...
1675Test the value of a variable.
1676.It Ic .ifndef Oo \&! Oc Ns Ar variable Op Ar operator variable ...
1677Test the value of a variable.
1678.It Ic .ifmake Oo \&! Oc Ns Ar target Op Ar operator target ...
1679Test the target being built.
1680.It Ic .ifnmake Oo \&! Ns Oc Ar target Op Ar operator target ...
1681Test the target being built.
1682.It Ic .else
1683Reverse the sense of the last conditional.
1684.It Ic .elif Oo \&! Ns Oc Ar expression Op Ar operator expression ...
1685A combination of
1686.Ql Ic .else
1687followed by
1688.Ql Ic .if .
1689.It Ic .elifdef Oo \&! Oc Ns Ar variable Op Ar operator variable ...
1690A combination of
1691.Ql Ic .else
1692followed by
1693.Ql Ic .ifdef .
1694.It Ic .elifndef Oo \&! Oc Ns Ar variable Op Ar operator variable ...
1695A combination of
1696.Ql Ic .else
1697followed by
1698.Ql Ic .ifndef .
1699.It Ic .elifmake Oo \&! Oc Ns Ar target Op Ar operator target ...
1700A combination of
1701.Ql Ic .else
1702followed by
1703.Ql Ic .ifmake .
1704.It Ic .elifnmake Oo \&! Oc Ns Ar target Op Ar operator target ...
1705A combination of
1706.Ql Ic .else
1707followed by
1708.Ql Ic .ifnmake .
1709.It Ic .endif
1710End the body of the conditional.
1711.El
1712.Pp
1713The
1714.Ar operator
1715may be any one of the following:
1716.Bl -tag -width "Cm XX"
1717.It Cm \&|\&|
1718Logical OR.
1719.It Cm \&\*[Am]\*[Am]
1720Logical
1721.Tn AND ;
1722of higher precedence than
1723.Dq \&|\&| .
1724.El
1725.Pp
1726As in C,
1727.Nm
1728will only evaluate a conditional as far as is necessary to determine
1729its value.
1730Parentheses may be used to change the order of evaluation.
1731The boolean operator
1732.Ql Ic \&!
1733may be used to logically negate an entire
1734conditional.
1735It is of higher precedence than
1736.Ql Ic \&\*[Am]\*[Am] .
1737.Pp
1738The value of
1739.Ar expression
1740may be any of the following:
1741.Bl -tag -width defined
1742.It Ic defined
1743Takes a variable name as an argument and evaluates to true if the variable
1744has been defined.
1745.It Ic make
1746Takes a target name as an argument and evaluates to true if the target
1747was specified as part of
1748.Nm Ns 's
1749command line or was declared the default target (either implicitly or
1750explicitly, see
1751.Va .MAIN )
1752before the line containing the conditional.
1753.It Ic empty
1754Takes a variable, with possible modifiers, and evaluates to true if
1755the expansion of the variable would result in an empty string.
1756.It Ic exists
1757Takes a file name as an argument and evaluates to true if the file exists.
1758The file is searched for on the system search path (see
1759.Va .PATH ) .
1760.It Ic target
1761Takes a target name as an argument and evaluates to true if the target
1762has been defined.
1763.It Ic commands
1764Takes a target name as an argument and evaluates to true if the target
1765has been defined and has commands associated with it.
1766.El
1767.Pp
1768.Ar Expression
1769may also be an arithmetic or string comparison.
1770Variable expansion is
1771performed on both sides of the comparison, after which the integral
1772values are compared.
1773A value is interpreted as hexadecimal if it is
1774preceded by 0x, otherwise it is decimal; octal numbers are not supported.
1775The standard C relational operators are all supported.
1776If after
1777variable expansion, either the left or right hand side of a
1778.Ql Ic ==
1779or
1780.Ql Ic "!="
1781operator is not an integral value, then
1782string comparison is performed between the expanded
1783variables.
1784If no relational operator is given, it is assumed that the expanded
1785variable is being compared against 0 or an empty string in the case
1786of a string comparison.
1787.Pp
1788When
1789.Nm
1790is evaluating one of these conditional expressions, and it encounters
1791a (white-space separated) word it doesn't recognize, either the
1792.Dq make
1793or
1794.Dq defined
1795expression is applied to it, depending on the form of the conditional.
1796If the form is
1797.Ql Ic .ifdef ,
1798.Ql Ic .ifndef ,
1799or
1800.Ql Ic .if
1801the
1802.Dq defined
1803expression is applied.
1804Similarly, if the form is
1805.Ql Ic .ifmake
1806or
1807.Ql Ic .ifnmake , the
1808.Dq make
1809expression is applied.
1810.Pp
1811If the conditional evaluates to true the parsing of the makefile continues
1812as before.
1813If it evaluates to false, the following lines are skipped.
1814In both cases this continues until a
1815.Ql Ic .else
1816or
1817.Ql Ic .endif
1818is found.
1819.Pp
1820For loops are typically used to apply a set of rules to a list of files.
1821The syntax of a for loop is:
1822.Pp
1823.Bl -tag -compact -width Ds
1824.It Ic \&.for Ar variable Oo Ar variable ... Oc Ic in Ar expression
1825.It Aq make-rules
1826.It Ic \&.endfor
1827.El
1828.Pp
1829After the for
1830.Ic expression
1831is evaluated, it is split into words.
1832On each iteration of the loop, one word is taken and assigned to each
1833.Ic variable ,
1834in order, and these
1835.Ic variables
1836are substituted into the
1837.Ic make-rules
1838inside the body of the for loop.
1839The number of words must come out even; that is, if there are three
1840iteration variables, the number of words provided must be a multiple
1841of three.
1842.Sh COMMENTS
1843Comments begin with a hash
1844.Pq Ql \&#
1845character, anywhere but in a shell
1846command line, and continue to the end of an unescaped new line.
1847.Sh SPECIAL SOURCES (ATTRIBUTES)
1848.Bl -tag -width .IGNOREx
1849.It Ic .EXEC
1850Target is never out of date, but always execute commands anyway.
1851.It Ic .IGNORE
1852Ignore any errors from the commands associated with this target, exactly
1853as if they all were preceded by a dash
1854.Pq Ql \- .
1855.\" .It Ic .INVISIBLE
1856.\" XXX
1857.\" .It Ic .JOIN
1858.\" XXX
1859.It Ic .MADE
1860Mark all sources of this target as being up-to-date.
1861.It Ic .MAKE
1862Execute the commands associated with this target even if the
1863.Fl n
1864or
1865.Fl t
1866options were specified.
1867Normally used to mark recursive
1868.Nm Ns s .
1869.It Ic .META
1870Create a meta file for the target, even if it is flagged as
1871.Ic .PHONY ,
1872.Ic .MAKE ,
1873or
1874.Ic .SPECIAL .
1875Usage in conjunction with
1876.Ic .MAKE
1877is the most likely case.
1878In "meta" mode, the target is out-of-date if the meta file is missing.
1879.It Ic .NOMETA
1880Do not create a meta file for the target.
1881Meta files are also not created for
1882.Ic .PHONY ,
1883.Ic .MAKE ,
1884or
1885.Ic .SPECIAL
1886targets.
1887.It Ic .NOMETA_CMP
1888Ignore differences in commands when deciding if target is out of date.
1889This is useful if the command contains a value which always changes.
1890If the number of commands change, though, the target will still be out of date.
1891The same effect applies to any command line that uses the variable
1892.Va .OODATE ,
1893which can be used for that purpose even when not otherwise needed or desired:
1894.Bd -literal -offset indent
1895
1896skip-compare-for-some:
1897	@echo this will be compared
1898	@echo this will not ${.OODATE:M.NOMETA_CMP}
1899	@echo this will also be compared
1900
1901.Ed
1902The
1903.Cm \&:M
1904pattern suppresses any expansion of the unwanted variable.
1905.It Ic .NOPATH
1906Do not search for the target in the directories specified by
1907.Ic .PATH .
1908.It Ic .NOTMAIN
1909Normally
1910.Nm
1911selects the first target it encounters as the default target to be built
1912if no target was specified.
1913This source prevents this target from being selected.
1914.It Ic .OPTIONAL
1915If a target is marked with this attribute and
1916.Nm
1917can't figure out how to create it, it will ignore this fact and assume
1918the file isn't needed or already exists.
1919.It Ic .PHONY
1920The target does not
1921correspond to an actual file; it is always considered to be out of date,
1922and will not be created with the
1923.Fl t
1924option.
1925Suffix-transformation rules are not applied to
1926.Ic .PHONY
1927targets.
1928.It Ic .PRECIOUS
1929When
1930.Nm
1931is interrupted, it normally removes any partially made targets.
1932This source prevents the target from being removed.
1933.It Ic .RECURSIVE
1934Synonym for
1935.Ic .MAKE .
1936.It Ic .SILENT
1937Do not echo any of the commands associated with this target, exactly
1938as if they all were preceded by an at sign
1939.Pq Ql @ .
1940.It Ic .USE
1941Turn the target into
1942.Nm Ns 's
1943version of a macro.
1944When the target is used as a source for another target, the other target
1945acquires the commands, sources, and attributes (except for
1946.Ic .USE )
1947of the
1948source.
1949If the target already has commands, the
1950.Ic .USE
1951target's commands are appended
1952to them.
1953.It Ic .USEBEFORE
1954Exactly like
1955.Ic .USE ,
1956but prepend the
1957.Ic .USEBEFORE
1958target commands to the target.
1959.It Ic .WAIT
1960If
1961.Ic .WAIT
1962appears in a dependency line, the sources that precede it are
1963made before the sources that succeed it in the line.
1964Since the dependents of files are not made until the file itself
1965could be made, this also stops the dependents being built unless they
1966are needed for another branch of the dependency tree.
1967So given:
1968.Bd -literal
1969x: a .WAIT b
1970	echo x
1971a:
1972	echo a
1973b: b1
1974	echo b
1975b1:
1976	echo b1
1977
1978.Ed
1979the output is always
1980.Ql a ,
1981.Ql b1 ,
1982.Ql b ,
1983.Ql x .
1984.br
1985The ordering imposed by
1986.Ic .WAIT
1987is only relevant for parallel makes.
1988.El
1989.Sh SPECIAL TARGETS
1990Special targets may not be included with other targets, i.e. they must be
1991the only target specified.
1992.Bl -tag -width .BEGINx
1993.It Ic .BEGIN
1994Any command lines attached to this target are executed before anything
1995else is done.
1996.It Ic .DEFAULT
1997This is sort of a
1998.Ic .USE
1999rule for any target (that was used only as a
2000source) that
2001.Nm
2002can't figure out any other way to create.
2003Only the shell script is used.
2004The
2005.Ic .IMPSRC
2006variable of a target that inherits
2007.Ic .DEFAULT Ns 's
2008commands is set
2009to the target's own name.
2010.It Ic .END
2011Any command lines attached to this target are executed after everything
2012else is done.
2013.It Ic .ERROR
2014Any command lines attached to this target are executed when another target fails.
2015The
2016.Ic .ERROR_TARGET
2017variable is set to the target that failed.
2018See also
2019.Ic MAKE_PRINT_VAR_ON_ERROR .
2020.It Ic .IGNORE
2021Mark each of the sources with the
2022.Ic .IGNORE
2023attribute.
2024If no sources are specified, this is the equivalent of specifying the
2025.Fl i
2026option.
2027.It Ic .INTERRUPT
2028If
2029.Nm
2030is interrupted, the commands for this target will be executed.
2031.It Ic .MAIN
2032If no target is specified when
2033.Nm
2034is invoked, this target will be built.
2035.It Ic .MAKEFLAGS
2036This target provides a way to specify flags for
2037.Nm
2038when the makefile is used.
2039The flags are as if typed to the shell, though the
2040.Fl f
2041option will have
2042no effect.
2043.\" XXX: NOT YET!!!!
2044.\" .It Ic .NOTPARALLEL
2045.\" The named targets are executed in non parallel mode.
2046.\" If no targets are
2047.\" specified, then all targets are executed in non parallel mode.
2048.It Ic .NOPATH
2049Apply the
2050.Ic .NOPATH
2051attribute to any specified sources.
2052.It Ic .NOTPARALLEL
2053Disable parallel mode.
2054.It Ic .NO_PARALLEL
2055Synonym for
2056.Ic .NOTPARALLEL ,
2057for compatibility with other pmake variants.
2058.It Ic .OBJDIR
2059The source is a new value for
2060.Ql Va .OBJDIR .
2061If it exists,
2062.Nm
2063will
2064.Xr chdir 2
2065to it and update the value of
2066.Ql Va .OBJDIR .
2067.It Ic .ORDER
2068The named targets are made in sequence.
2069This ordering does not add targets to the list of targets to be made.
2070Since the dependents of a target do not get built until the target itself
2071could be built, unless
2072.Ql a
2073is built by another part of the dependency graph,
2074the following is a dependency loop:
2075.Bd -literal
2076\&.ORDER: b a
2077b: a
2078.Ed
2079.Pp
2080The ordering imposed by
2081.Ic .ORDER
2082is only relevant for parallel makes.
2083.\" XXX: NOT YET!!!!
2084.\" .It Ic .PARALLEL
2085.\" The named targets are executed in parallel mode.
2086.\" If no targets are
2087.\" specified, then all targets are executed in parallel mode.
2088.It Ic .PATH
2089The sources are directories which are to be searched for files not
2090found in the current directory.
2091If no sources are specified, any previously specified directories are
2092deleted.
2093If the source is the special
2094.Ic .DOTLAST
2095target, then the current working
2096directory is searched last.
2097.It Ic .PATH. Ns Va suffix
2098Like
2099.Ic .PATH
2100but applies only to files with a particular suffix.
2101The suffix must have been previously declared with
2102.Ic .SUFFIXES .
2103.It Ic .PHONY
2104Apply the
2105.Ic .PHONY
2106attribute to any specified sources.
2107.It Ic .PRECIOUS
2108Apply the
2109.Ic .PRECIOUS
2110attribute to any specified sources.
2111If no sources are specified, the
2112.Ic .PRECIOUS
2113attribute is applied to every
2114target in the file.
2115.It Ic .SHELL
2116Sets the shell that
2117.Nm
2118will use to execute commands.
2119The sources are a set of
2120.Ar field=value
2121pairs.
2122.Bl -tag -width hasErrCtls
2123.It Ar name
2124This is the minimal specification, used to select one of the built-in
2125shell specs;
2126.Ar sh ,
2127.Ar ksh ,
2128and
2129.Ar csh .
2130.It Ar path
2131Specifies the path to the shell.
2132.It Ar hasErrCtl
2133Indicates whether the shell supports exit on error.
2134.It Ar check
2135The command to turn on error checking.
2136.It Ar ignore
2137The command to disable error checking.
2138.It Ar echo
2139The command to turn on echoing of commands executed.
2140.It Ar quiet
2141The command to turn off echoing of commands executed.
2142.It Ar filter
2143The output to filter after issuing the
2144.Ar quiet
2145command.
2146It is typically identical to
2147.Ar quiet .
2148.It Ar errFlag
2149The flag to pass the shell to enable error checking.
2150.It Ar echoFlag
2151The flag to pass the shell to enable command echoing.
2152.It Ar newline
2153The string literal to pass the shell that results in a single newline
2154character when used outside of any quoting characters.
2155.El
2156Example:
2157.Bd -literal
2158\&.SHELL: name=ksh path=/bin/ksh hasErrCtl=true \e
2159	check="set \-e" ignore="set +e" \e
2160	echo="set \-v" quiet="set +v" filter="set +v" \e
2161	echoFlag=v errFlag=e newline="'\en'"
2162.Ed
2163.It Ic .SILENT
2164Apply the
2165.Ic .SILENT
2166attribute to any specified sources.
2167If no sources are specified, the
2168.Ic .SILENT
2169attribute is applied to every
2170command in the file.
2171.It Ic .STALE
2172This target gets run when a dependency file contains stale entries, having
2173.Va .ALLSRC
2174set to the name of that dependency file.
2175.It Ic .SUFFIXES
2176Each source specifies a suffix to
2177.Nm .
2178If no sources are specified, any previously specified suffixes are deleted.
2179It allows the creation of suffix-transformation rules.
2180.Pp
2181Example:
2182.Bd -literal
2183\&.SUFFIXES: .o
2184\&.c.o:
2185	cc \-o ${.TARGET} \-c ${.IMPSRC}
2186.Ed
2187.El
2188.Sh ENVIRONMENT
2189.Nm
2190uses the following environment variables, if they exist:
2191.Ev MACHINE ,
2192.Ev MACHINE_ARCH ,
2193.Ev MAKE ,
2194.Ev MAKEFLAGS ,
2195.Ev MAKEOBJDIR ,
2196.Ev MAKEOBJDIRPREFIX ,
2197.Ev MAKESYSPATH ,
2198.Ev PWD ,
2199and
2200.Ev TMPDIR .
2201.Pp
2202.Ev MAKEOBJDIRPREFIX
2203and
2204.Ev MAKEOBJDIR
2205may only be set in the environment or on the command line to
2206.Nm
2207and not as makefile variables;
2208see the description of
2209.Ql Va .OBJDIR
2210for more details.
2211.Sh FILES
2212.Bl -tag -width /usr/share/mk -compact
2213.It .depend
2214list of dependencies
2215.It Makefile
2216list of dependencies
2217.It makefile
2218list of dependencies
2219.It sys.mk
2220system makefile
2221.It /usr/share/mk
2222system makefile directory
2223.El
2224.Sh COMPATIBILITY
2225The basic make syntax is compatible between different versions of make;
2226however the special variables, variable modifiers and conditionals are not.
2227.Ss Older versions
2228An incomplete list of changes in older versions of
2229.Nm :
2230.Pp
2231The way that .for loop variables are substituted changed after
2232.Nx 5.0
2233so that they still appear to be variable expansions.
2234In particular this stops them being treated as syntax, and removes some
2235obscure problems using them in .if statements.
2236.Pp
2237The way that parallel makes are scheduled changed in
2238.Nx 4.0
2239so that .ORDER and .WAIT apply recursively to the dependent nodes.
2240The algorithms used may change again in the future.
2241.Ss Other make dialects
2242Other make dialects (GNU make, SVR4 make, POSIX make, etc.) do not
2243support most of the features of
2244.Nm
2245as described in this manual.
2246Most notably:
2247.Bl -bullet -offset indent
2248.It
2249The
2250.Ic .WAIT
2251and
2252.Ic .ORDER
2253declarations and most functionality pertaining to parallelization.
2254(GNU make supports parallelization but lacks these features needed to
2255control it effectively.)
2256.It
2257Directives, including for loops and conditionals and most of the
2258forms of include files.
2259(GNU make has its own incompatible and less powerful syntax for
2260conditionals.)
2261.It
2262All built-in variables that begin with a dot.
2263.It
2264Most of the special sources and targets that begin with a dot,
2265with the notable exception of
2266.Ic .PHONY ,
2267.Ic .PRECIOUS ,
2268and
2269.Ic .SUFFIXES .
2270.It
2271Variable modifiers, except for the
2272.Dl :old=new
2273string substitution, which does not portably support globbing with
2274.Ql %
2275and historically only works on declared suffixes.
2276.It
2277The
2278.Ic $>
2279variable even in its short form; most makes support this functionality
2280but its name varies.
2281.El
2282.Pp
2283Some features are somewhat more portable, such as assignment with
2284.Ic += ,
2285.Ic ?= ,
2286and
2287.Ic != .
2288The
2289.Ic .PATH
2290functionality is based on an older feature
2291.Ic VPATH
2292found in GNU make and many versions of SVR4 make; however,
2293historically its behavior is too ill-defined (and too buggy) to rely
2294upon.
2295.Pp
2296The
2297.Ic $@
2298and
2299.Ic $<
2300variables are more or less universally portable, as is the
2301.Ic $(MAKE)
2302variable.
2303Basic use of suffix rules (for files only in the current directory,
2304not trying to chain transformations together, etc.) is also reasonably
2305portable.
2306.Sh SEE ALSO
2307.Xr mkdep 1
2308.Sh HISTORY
2309A
2310.Nm
2311command appeared in
2312.At v7 .
2313This
2314.Nm
2315implementation is based on Adam De Boor's pmake program which was written
2316for Sprite at Berkeley.
2317It was designed to be a parallel distributed make running jobs on different
2318machines using a daemon called
2319.Dq customs .
2320.Pp
2321Historically the target/dependency
2322.Dq FRC
2323has been used to FoRCe rebuilding (since the target/dependency
2324does not exist... unless someone creates an
2325.Dq FRC
2326file).
2327.Sh BUGS
2328The
2329.Nm
2330syntax is difficult to parse without actually acting of the data.
2331For instance finding the end of a variable use should involve scanning each
2332the modifiers using the correct terminator for each field.
2333In many places
2334.Nm
2335just counts {} and () in order to find the end of a variable expansion.
2336.Pp
2337There is no way of escaping a space character in a filename.
2338