xref: /freebsd/contrib/bmake/bmake.1 (revision 1e248b8346cbf585990a4da0199c6cc3b9cc2340)
1.\"	$NetBSD: make.1,v 1.254 2016/02/20 01:43:28 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 February 19, 2016
33.Dt MAKE 1
34.Os
35.Sh NAME
36.Nm bmake
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 @ .
688.El
689.Pp
690The shorter forms
691.Ql ( Va \*[Gt] ,
692.Ql Va \&! ,
693.Ql Va \*[Lt] ,
694.Ql Va % ,
695.Ql Va \&? ,
696.Ql Va * ,
697and
698.Ql Va @ )
699are permitted for backward
700compatibility with historical makefiles and legacy POSIX make and are
701not recommended.
702.Pp
703Variants of these variables with the punctuation followed immediately by
704.Ql D
705or
706.Ql F ,
707e.g.
708.Ql Va $(@D) ,
709are legacy forms equivalent to using the
710.Ql :H
711and
712.Ql :T
713modifiers.
714These forms are accepted for compatibility with
715.At V
716makefiles and POSIX but are not recommended.
717.Pp
718Four of the local variables may be used in sources on dependency lines
719because they expand to the proper value for each target on the line.
720These variables are
721.Ql Va .TARGET ,
722.Ql Va .PREFIX ,
723.Ql Va .ARCHIVE ,
724and
725.Ql Va .MEMBER .
726.Ss Additional built-in variables
727In addition,
728.Nm
729sets or knows about the following variables:
730.Bl -tag -width .MAKEOVERRIDES
731.It Va \&$
732A single dollar sign
733.Ql \&$ ,
734i.e.
735.Ql \&$$
736expands to a single dollar
737sign.
738.It Va .ALLTARGETS
739The list of all targets encountered in the Makefile.
740If evaluated during
741Makefile parsing, lists only those targets encountered thus far.
742.It Va .CURDIR
743A path to the directory where
744.Nm
745was executed.
746Refer to the description of
747.Ql Ev PWD
748for more details.
749.It Va .INCLUDEDFROMDIR
750The directory of the file this Makefile was included from.
751.It Va .INCLUDEDFROMFILE
752The filename of the file this Makefile was included from.
753.It Ev MAKE
754The name that
755.Nm
756was executed with
757.Pq Va argv[0] .
758For compatibility
759.Nm
760also sets
761.Va .MAKE
762with the same value.
763The preferred variable to use is the environment variable
764.Ev MAKE
765because it is more compatible with other versions of
766.Nm
767and cannot be confused with the special target with the same name.
768.It Va .MAKE.DEPENDFILE
769Names the makefile (default
770.Ql Pa .depend )
771from which generated dependencies are read.
772.It Va .MAKE.EXPAND_VARIABLES
773A boolean that controls the default behavior of the
774.Fl V
775option.
776.It Va .MAKE.EXPORTED
777The list of variables exported by
778.Nm .
779.It Va .MAKE.JOBS
780The argument to the
781.Fl j
782option.
783.It Va .MAKE.JOB.PREFIX
784If
785.Nm
786is run with
787.Ar j
788then output for each target is prefixed with a token
789.Ql --- target ---
790the first part of which can be controlled via
791.Va .MAKE.JOB.PREFIX .
792If
793.Va .MAKE.JOB.PREFIX
794is empty, no token is printed.
795.br
796For example:
797.Li .MAKE.JOB.PREFIX=${.newline}---${.MAKE:T}[${.MAKE.PID}]
798would produce tokens like
799.Ql ---make[1234] target ---
800making it easier to track the degree of parallelism being achieved.
801.It Ev MAKEFLAGS
802The environment variable
803.Ql Ev MAKEFLAGS
804may contain anything that
805may be specified on
806.Nm Ns 's
807command line.
808Anything specified on
809.Nm Ns 's
810command line is appended to the
811.Ql Ev MAKEFLAGS
812variable which is then
813entered into the environment for all programs which
814.Nm
815executes.
816.It Va .MAKE.LEVEL
817The recursion depth of
818.Nm .
819The initial instance of
820.Nm
821will be 0, and an incremented value is put into the environment
822to be seen by the next generation.
823This allows tests like:
824.Li .if ${.MAKE.LEVEL} == 0
825to protect things which should only be evaluated in the initial instance of
826.Nm .
827.It Va .MAKE.MAKEFILE_PREFERENCE
828The ordered list of makefile names
829(default
830.Ql Pa makefile ,
831.Ql Pa Makefile )
832that
833.Nm
834will look for.
835.It Va .MAKE.MAKEFILES
836The list of makefiles read by
837.Nm ,
838which is useful for tracking dependencies.
839Each makefile is recorded only once, regardless of the number of times read.
840.It Va .MAKE.MODE
841Processed after reading all makefiles.
842Can affect the mode that
843.Nm
844runs in.
845It can contain a number of keywords:
846.Bl -hang -width ignore-cmd
847.It Pa compat
848Like
849.Fl B ,
850puts
851.Nm
852into "compat" mode.
853.It Pa meta
854Puts
855.Nm
856into "meta" mode, where meta files are created for each target
857to capture the command run, the output generated and if
858.Xr filemon 4
859is available, the system calls which are of interest to
860.Nm .
861The captured output can be very useful when diagnosing errors.
862.It Pa curdirOk= Ar bf
863Normally
864.Nm
865will not create .meta files in
866.Ql Va .CURDIR .
867This can be overridden by setting
868.Va bf
869to a value which represents True.
870.It Pa env
871For debugging, it can be useful to include the environment
872in the .meta file.
873.It Pa verbose
874If in "meta" mode, print a clue about the target being built.
875This is useful if the build is otherwise running silently.
876The message printed the value of:
877.Va .MAKE.META.PREFIX .
878.It Pa ignore-cmd
879Some makefiles have commands which are simply not stable.
880This keyword causes them to be ignored for
881determining whether a target is out of date in "meta" mode.
882See also
883.Ic .NOMETA_CMP .
884.It Pa silent= Ar bf
885If
886.Va bf
887is True, when a .meta file is created, mark the target
888.Ic .SILENT .
889.El
890.It Va .MAKE.META.BAILIWICK
891In "meta" mode, provides a list of prefixes which
892match the directories controlled by
893.Nm .
894If a file that was generated outside of
895.Va .OBJDIR
896but within said bailiwick is missing,
897the current target is considered out-of-date.
898.It Va .MAKE.META.CREATED
899In "meta" mode, this variable contains a list of all the meta files
900updated.
901If not empty, it can be used to trigger processing of
902.Va .MAKE.META.FILES .
903.It Va .MAKE.META.FILES
904In "meta" mode, this variable contains a list of all the meta files
905used (updated or not).
906This list can be used to process the meta files to extract dependency
907information.
908.It Va .MAKE.META.IGNORE_PATHS
909Provides a list of path prefixes that should be ignored;
910because the contents are expected to change over time.
911The default list includes:
912.Ql Pa /dev /etc /proc /tmp /var/run /var/tmp
913.It Va .MAKE.META.PREFIX
914Defines the message printed for each meta file updated in "meta verbose" mode.
915The default value is:
916.Dl Building ${.TARGET:H:tA}/${.TARGET:T}
917.It Va .MAKEOVERRIDES
918This variable is used to record the names of variables assigned to
919on the command line, so that they may be exported as part of
920.Ql Ev MAKEFLAGS .
921This behavior can be disabled by assigning an empty value to
922.Ql Va .MAKEOVERRIDES
923within a makefile.
924Extra variables can be exported from a makefile
925by appending their names to
926.Ql Va .MAKEOVERRIDES .
927.Ql Ev MAKEFLAGS
928is re-exported whenever
929.Ql Va .MAKEOVERRIDES
930is modified.
931.It Va .MAKE.PATH_FILEMON
932If
933.Nm
934was built with
935.Xr filemon 4
936support, this is set to the path of the device node.
937This allows makefiles to test for this support.
938.It Va .MAKE.PID
939The process-id of
940.Nm .
941.It Va .MAKE.PPID
942The parent process-id of
943.Nm .
944.It Va .MAKE.SAVE_DOLLARS
945value should be a boolean that controls whether
946.Ql $$
947are preserved when doing
948.Ql :=
949assignments.
950The default is false, for backwards compatibility.
951Set to true for compatability with other makes.
952If set to false,
953.Ql $$
954becomes
955.Ql $
956per normal evaluation rules.
957.It Va MAKE_PRINT_VAR_ON_ERROR
958When
959.Nm
960stops due to an error, it prints its name and the value of
961.Ql Va .CURDIR
962as well as the value of any variables named in
963.Ql Va MAKE_PRINT_VAR_ON_ERROR .
964.It Va .newline
965This variable is simply assigned a newline character as its value.
966This allows expansions using the
967.Cm \&:@
968modifier to put a newline between
969iterations of the loop rather than a space.
970For example, the printing of
971.Ql Va MAKE_PRINT_VAR_ON_ERROR
972could be done as ${MAKE_PRINT_VAR_ON_ERROR:@v@$v='${$v}'${.newline}@}.
973.It Va .OBJDIR
974A path to the directory where the targets are built.
975Its value is determined by trying to
976.Xr chdir 2
977to the following directories in order and using the first match:
978.Bl -enum
979.It
980.Ev ${MAKEOBJDIRPREFIX}${.CURDIR}
981.Pp
982(Only if
983.Ql Ev MAKEOBJDIRPREFIX
984is set in the environment or on the command line.)
985.It
986.Ev ${MAKEOBJDIR}
987.Pp
988(Only if
989.Ql Ev MAKEOBJDIR
990is set in the environment or on the command line.)
991.It
992.Ev ${.CURDIR} Ns Pa /obj. Ns Ev ${MACHINE}
993.It
994.Ev ${.CURDIR} Ns Pa /obj
995.It
996.Pa /usr/obj/ Ns Ev ${.CURDIR}
997.It
998.Ev ${.CURDIR}
999.El
1000.Pp
1001Variable expansion is performed on the value before it's used,
1002so expressions such as
1003.Dl ${.CURDIR:S,^/usr/src,/var/obj,}
1004may be used.
1005This is especially useful with
1006.Ql Ev MAKEOBJDIR .
1007.Pp
1008.Ql Va .OBJDIR
1009may be modified in the makefile via the special target
1010.Ql Ic .OBJDIR .
1011In all cases,
1012.Nm
1013will
1014.Xr chdir 2
1015to the specified directory if it exists, and set
1016.Ql Va .OBJDIR
1017and
1018.Ql Ev PWD
1019to that directory before executing any targets.
1020.
1021.It Va .PARSEDIR
1022A path to the directory of the current
1023.Ql Pa Makefile
1024being parsed.
1025.It Va .PARSEFILE
1026The basename of the current
1027.Ql Pa Makefile
1028being parsed.
1029This variable and
1030.Ql Va .PARSEDIR
1031are both set only while the
1032.Ql Pa Makefiles
1033are being parsed.
1034If you want to retain their current values, assign them to a variable
1035using assignment with expansion:
1036.Pq Ql Cm \&:= .
1037.It Va .PATH
1038A variable that represents the list of directories that
1039.Nm
1040will search for files.
1041The search list should be updated using the target
1042.Ql Va .PATH
1043rather than the variable.
1044.It Ev PWD
1045Alternate path to the current directory.
1046.Nm
1047normally sets
1048.Ql Va .CURDIR
1049to the canonical path given by
1050.Xr getcwd 3 .
1051However, if the environment variable
1052.Ql Ev PWD
1053is set and gives a path to the current directory, then
1054.Nm
1055sets
1056.Ql Va .CURDIR
1057to the value of
1058.Ql Ev PWD
1059instead.
1060This behavior is disabled if
1061.Ql Ev MAKEOBJDIRPREFIX
1062is set or
1063.Ql Ev MAKEOBJDIR
1064contains a variable transform.
1065.Ql Ev PWD
1066is set to the value of
1067.Ql Va .OBJDIR
1068for all programs which
1069.Nm
1070executes.
1071.It Ev .TARGETS
1072The list of targets explicitly specified on the command line, if any.
1073.It Ev VPATH
1074Colon-separated
1075.Pq Dq \&:
1076lists of directories that
1077.Nm
1078will search for files.
1079The variable is supported for compatibility with old make programs only,
1080use
1081.Ql Va .PATH
1082instead.
1083.El
1084.Ss Variable modifiers
1085Variable expansion may be modified to select or modify each word of the
1086variable (where a
1087.Dq word
1088is white-space delimited sequence of characters).
1089The general format of a variable expansion is as follows:
1090.Pp
1091.Dl ${variable[:modifier[:...]]}
1092.Pp
1093Each modifier begins with a colon,
1094which may be escaped with a backslash
1095.Pq Ql \e .
1096.Pp
1097A set of modifiers can be specified via a variable, as follows:
1098.Pp
1099.Dl modifier_variable=modifier[:...]
1100.Dl ${variable:${modifier_variable}[:...]}
1101.Pp
1102In this case the first modifier in the modifier_variable does not
1103start with a colon, since that must appear in the referencing
1104variable.
1105If any of the modifiers in the modifier_variable contain a dollar sign
1106.Pq Ql $ ,
1107these must be doubled to avoid early expansion.
1108.Pp
1109The supported modifiers are:
1110.Bl -tag -width EEE
1111.It Cm \&:E
1112Replaces each word in the variable with its suffix.
1113.It Cm \&:H
1114Replaces each word in the variable with everything but the last component.
1115.It Cm \&:M Ns Ar pattern
1116Select only those words that match
1117.Ar pattern .
1118The standard shell wildcard characters
1119.Pf ( Ql * ,
1120.Ql \&? ,
1121and
1122.Ql Oo Oc )
1123may
1124be used.
1125The wildcard characters may be escaped with a backslash
1126.Pq Ql \e .
1127As a consequence of the way values are split into words, matched,
1128and then joined, a construct like
1129.Dl ${VAR:M*}
1130will normalize the inter-word spacing, removing all leading and
1131trailing space, and converting multiple consecutive spaces
1132to single spaces.
1133.
1134.It Cm \&:N Ns Ar pattern
1135This is identical to
1136.Ql Cm \&:M ,
1137but selects all words which do not match
1138.Ar pattern .
1139.It Cm \&:O
1140Order every word in variable alphabetically.
1141To sort words in
1142reverse order use the
1143.Ql Cm \&:O:[-1..1]
1144combination of modifiers.
1145.It Cm \&:Ox
1146Randomize words in variable.
1147The results will be different each time you are referring to the
1148modified variable; use the assignment with expansion
1149.Pq Ql Cm \&:=
1150to prevent such behavior.
1151For example,
1152.Bd -literal -offset indent
1153LIST=			uno due tre quattro
1154RANDOM_LIST=		${LIST:Ox}
1155STATIC_RANDOM_LIST:=	${LIST:Ox}
1156
1157all:
1158	@echo "${RANDOM_LIST}"
1159	@echo "${RANDOM_LIST}"
1160	@echo "${STATIC_RANDOM_LIST}"
1161	@echo "${STATIC_RANDOM_LIST}"
1162.Ed
1163may produce output similar to:
1164.Bd -literal -offset indent
1165quattro due tre uno
1166tre due quattro uno
1167due uno quattro tre
1168due uno quattro tre
1169.Ed
1170.It Cm \&:Q
1171Quotes every shell meta-character in the variable, so that it can be passed
1172safely through recursive invocations of
1173.Nm .
1174.It Cm \&:R
1175Replaces each word in the variable with everything but its suffix.
1176.It Cm \&:gmtime
1177The value is a format string for
1178.Xr strftime 3 ,
1179using the current
1180.Xr gmtime 3 .
1181.It Cm \&:hash
1182Compute a 32-bit hash of the value and encode it as hex digits.
1183.It Cm \&:localtime
1184The value is a format string for
1185.Xr strftime 3 ,
1186using the current
1187.Xr localtime 3 .
1188.It Cm \&:tA
1189Attempt to convert variable to an absolute path using
1190.Xr realpath 3 ,
1191if that fails, the value is unchanged.
1192.It Cm \&:tl
1193Converts variable to lower-case letters.
1194.It Cm \&:ts Ns Ar c
1195Words in the variable are normally separated by a space on expansion.
1196This modifier sets the separator to the character
1197.Ar c .
1198If
1199.Ar c
1200is omitted, then no separator is used.
1201The common escapes (including octal numeric codes), work as expected.
1202.It Cm \&:tu
1203Converts variable to upper-case letters.
1204.It Cm \&:tW
1205Causes the value to be treated as a single word
1206(possibly containing embedded white space).
1207See also
1208.Ql Cm \&:[*] .
1209.It Cm \&:tw
1210Causes the value to be treated as a sequence of
1211words delimited by white space.
1212See also
1213.Ql Cm \&:[@] .
1214.Sm off
1215.It Cm \&:S No \&/ Ar old_string No \&/ Ar new_string No \&/ Op Cm 1gW
1216.Sm on
1217Modify the first occurrence of
1218.Ar old_string
1219in the variable's value, replacing it with
1220.Ar new_string .
1221If a
1222.Ql g
1223is appended to the last slash of the pattern, all occurrences
1224in each word are replaced.
1225If a
1226.Ql 1
1227is appended to the last slash of the pattern, only the first word
1228is affected.
1229If a
1230.Ql W
1231is appended to the last slash of the pattern,
1232then the value is treated as a single word
1233(possibly containing embedded white space).
1234If
1235.Ar old_string
1236begins with a caret
1237.Pq Ql ^ ,
1238.Ar old_string
1239is anchored at the beginning of each word.
1240If
1241.Ar old_string
1242ends with a dollar sign
1243.Pq Ql \&$ ,
1244it is anchored at the end of each word.
1245Inside
1246.Ar new_string ,
1247an ampersand
1248.Pq Ql \*[Am]
1249is replaced by
1250.Ar old_string
1251(without any
1252.Ql ^
1253or
1254.Ql \&$ ) .
1255Any character may be used as a delimiter for the parts of the modifier
1256string.
1257The anchoring, ampersand and delimiter characters may be escaped with a
1258backslash
1259.Pq Ql \e .
1260.Pp
1261Variable expansion occurs in the normal fashion inside both
1262.Ar old_string
1263and
1264.Ar new_string
1265with the single exception that a backslash is used to prevent the expansion
1266of a dollar sign
1267.Pq Ql \&$ ,
1268not a preceding dollar sign as is usual.
1269.Sm off
1270.It Cm \&:C No \&/ Ar pattern No \&/ Ar replacement No \&/ Op Cm 1gW
1271.Sm on
1272The
1273.Cm \&:C
1274modifier is just like the
1275.Cm \&:S
1276modifier except that the old and new strings, instead of being
1277simple strings, are an extended regular expression (see
1278.Xr regex 3 )
1279string
1280.Ar pattern
1281and an
1282.Xr ed 1 Ns \-style
1283string
1284.Ar replacement .
1285Normally, the first occurrence of the pattern
1286.Ar pattern
1287in each word of the value is substituted with
1288.Ar replacement .
1289The
1290.Ql 1
1291modifier causes the substitution to apply to at most one word; the
1292.Ql g
1293modifier causes the substitution to apply to as many instances of the
1294search pattern
1295.Ar pattern
1296as occur in the word or words it is found in; the
1297.Ql W
1298modifier causes the value to be treated as a single word
1299(possibly containing embedded white space).
1300Note that
1301.Ql 1
1302and
1303.Ql g
1304are orthogonal; the former specifies whether multiple words are
1305potentially affected, the latter whether multiple substitutions can
1306potentially occur within each affected word.
1307.Pp
1308As for the
1309.Cm \&:S
1310modifier, the
1311.Ar pattern
1312and
1313.Ar replacement
1314are subjected to variable expansion before being parsed as
1315regular expressions.
1316.It Cm \&:T
1317Replaces each word in the variable with its last component.
1318.It Cm \&:u
1319Remove adjacent duplicate words (like
1320.Xr uniq 1 ) .
1321.Sm off
1322.It Cm \&:\&? Ar true_string Cm \&: Ar false_string
1323.Sm on
1324If the variable name (not its value), when parsed as a .if conditional
1325expression, evaluates to true, return as its value the
1326.Ar true_string ,
1327otherwise return the
1328.Ar false_string .
1329Since the variable name is used as the expression, \&:\&? must be the
1330first modifier after the variable name itself - which will, of course,
1331usually contain variable expansions.
1332A common error is trying to use expressions like
1333.Dl ${NUMBERS:M42:?match:no}
1334which actually tests defined(NUMBERS),
1335to determine is any words match "42" you need to use something like:
1336.Dl ${"${NUMBERS:M42}" != \&"\&":?match:no} .
1337.It Ar :old_string=new_string
1338This is the
1339.At V
1340style variable substitution.
1341It must be the last modifier specified.
1342If
1343.Ar old_string
1344or
1345.Ar new_string
1346do not contain the pattern matching character
1347.Ar %
1348then it is assumed that they are
1349anchored at the end of each word, so only suffixes or entire
1350words may be replaced.
1351Otherwise
1352.Ar %
1353is the substring of
1354.Ar old_string
1355to be replaced in
1356.Ar new_string .
1357.Pp
1358Variable expansion occurs in the normal fashion inside both
1359.Ar old_string
1360and
1361.Ar new_string
1362with the single exception that a backslash is used to prevent the
1363expansion of a dollar sign
1364.Pq Ql \&$ ,
1365not a preceding dollar sign as is usual.
1366.Sm off
1367.It Cm \&:@ Ar temp Cm @ Ar string Cm @
1368.Sm on
1369This is the loop expansion mechanism from the OSF Development
1370Environment (ODE) make.
1371Unlike
1372.Cm \&.for
1373loops expansion occurs at the time of
1374reference.
1375Assign
1376.Ar temp
1377to each word in the variable and evaluate
1378.Ar string .
1379The ODE convention is that
1380.Ar temp
1381should start and end with a period.
1382For example.
1383.Dl ${LINKS:@.LINK.@${LN} ${TARGET} ${.LINK.}@}
1384.Pp
1385However a single character variable is often more readable:
1386.Dl ${MAKE_PRINT_VAR_ON_ERROR:@v@$v='${$v}'${.newline}@}
1387.It Cm \&:U Ns Ar newval
1388If the variable is undefined
1389.Ar newval
1390is the value.
1391If the variable is defined, the existing value is returned.
1392This is another ODE make feature.
1393It is handy for setting per-target CFLAGS for instance:
1394.Dl ${_${.TARGET:T}_CFLAGS:U${DEF_CFLAGS}}
1395If a value is only required if the variable is undefined, use:
1396.Dl ${VAR:D:Unewval}
1397.It Cm \&:D Ns Ar newval
1398If the variable is defined
1399.Ar newval
1400is the value.
1401.It Cm \&:L
1402The name of the variable is the value.
1403.It Cm \&:P
1404The path of the node which has the same name as the variable
1405is the value.
1406If no such node exists or its path is null, then the
1407name of the variable is used.
1408In order for this modifier to work, the name (node) must at least have
1409appeared on the rhs of a dependency.
1410.Sm off
1411.It Cm \&:\&! Ar cmd Cm \&!
1412.Sm on
1413The output of running
1414.Ar cmd
1415is the value.
1416.It Cm \&:sh
1417If the variable is non-empty it is run as a command and the output
1418becomes the new value.
1419.It Cm \&::= Ns Ar str
1420The variable is assigned the value
1421.Ar str
1422after substitution.
1423This modifier and its variations are useful in
1424obscure situations such as wanting to set a variable when shell commands
1425are being parsed.
1426These assignment modifiers always expand to
1427nothing, so if appearing in a rule line by themselves should be
1428preceded with something to keep
1429.Nm
1430happy.
1431.Pp
1432The
1433.Ql Cm \&::
1434helps avoid false matches with the
1435.At V
1436style
1437.Cm \&:=
1438modifier and since substitution always occurs the
1439.Cm \&::=
1440form is vaguely appropriate.
1441.It Cm \&::?= Ns Ar str
1442As for
1443.Cm \&::=
1444but only if the variable does not already have a value.
1445.It Cm \&::+= Ns Ar str
1446Append
1447.Ar str
1448to the variable.
1449.It Cm \&::!= Ns Ar cmd
1450Assign the output of
1451.Ar cmd
1452to the variable.
1453.It Cm \&:\&[ Ns Ar range Ns Cm \&]
1454Selects one or more words from the value,
1455or performs other operations related to the way in which the
1456value is divided into words.
1457.Pp
1458Ordinarily, a value is treated as a sequence of words
1459delimited by white space.
1460Some modifiers suppress this behavior,
1461causing a value to be treated as a single word
1462(possibly containing embedded white space).
1463An empty value, or a value that consists entirely of white-space,
1464is treated as a single word.
1465For the purposes of the
1466.Ql Cm \&:[]
1467modifier, the words are indexed both forwards using positive integers
1468(where index 1 represents the first word),
1469and backwards using negative integers
1470(where index \-1 represents the last word).
1471.Pp
1472The
1473.Ar range
1474is subjected to variable expansion, and the expanded result is
1475then interpreted as follows:
1476.Bl -tag -width index
1477.\" :[n]
1478.It Ar index
1479Selects a single word from the value.
1480.\" :[start..end]
1481.It Ar start Ns Cm \&.. Ns Ar end
1482Selects all words from
1483.Ar start
1484to
1485.Ar end ,
1486inclusive.
1487For example,
1488.Ql Cm \&:[2..-1]
1489selects all words from the second word to the last word.
1490If
1491.Ar start
1492is greater than
1493.Ar end ,
1494then the words are output in reverse order.
1495For example,
1496.Ql Cm \&:[-1..1]
1497selects all the words from last to first.
1498.\" :[*]
1499.It Cm \&*
1500Causes subsequent modifiers to treat the value as a single word
1501(possibly containing embedded white space).
1502Analogous to the effect of
1503\&"$*\&"
1504in Bourne shell.
1505.\" :[0]
1506.It 0
1507Means the same as
1508.Ql Cm \&:[*] .
1509.\" :[*]
1510.It Cm \&@
1511Causes subsequent modifiers to treat the value as a sequence of words
1512delimited by white space.
1513Analogous to the effect of
1514\&"$@\&"
1515in Bourne shell.
1516.\" :[#]
1517.It Cm \&#
1518Returns the number of words in the value.
1519.El \" :[range]
1520.El
1521.Sh INCLUDE STATEMENTS, CONDITIONALS AND FOR LOOPS
1522Makefile inclusion, conditional structures and for loops  reminiscent
1523of the C programming language are provided in
1524.Nm .
1525All such structures are identified by a line beginning with a single
1526dot
1527.Pq Ql \&.
1528character.
1529Files are included with either
1530.Cm \&.include Aq Ar file
1531or
1532.Cm \&.include Pf \*q Ar file Ns \*q .
1533Variables between the angle brackets or double quotes are expanded
1534to form the file name.
1535If angle brackets are used, the included makefile is expected to be in
1536the system makefile directory.
1537If double quotes are used, the including makefile's directory and any
1538directories specified using the
1539.Fl I
1540option are searched before the system
1541makefile directory.
1542For compatibility with other versions of
1543.Nm
1544.Ql include file ...
1545is also accepted.
1546.Pp
1547If the include statement is written as
1548.Cm .-include
1549or as
1550.Cm .sinclude
1551then errors locating and/or opening include files are ignored.
1552.Pp
1553If the include statement is written as
1554.Cm .dinclude
1555not only are errors locating and/or opening include files ignored,
1556but stale dependencies within the included file will be ignored
1557just like
1558.Va .MAKE.DEPENDFILE .
1559.Pp
1560Conditional expressions are also preceded by a single dot as the first
1561character of a line.
1562The possible conditionals are as follows:
1563.Bl -tag -width Ds
1564.It Ic .error Ar message
1565The message is printed along with the name of the makefile and line number,
1566then
1567.Nm
1568will exit.
1569.It Ic .export Ar variable ...
1570Export the specified global variable.
1571If no variable list is provided, all globals are exported
1572except for internal variables (those that start with
1573.Ql \&. ) .
1574This is not affected by the
1575.Fl X
1576flag, so should be used with caution.
1577For compatibility with other
1578.Nm
1579programs
1580.Ql export variable=value
1581is also accepted.
1582.Pp
1583Appending a variable name to
1584.Va .MAKE.EXPORTED
1585is equivalent to exporting a variable.
1586.It Ic .export-env Ar variable ...
1587The same as
1588.Ql .export ,
1589except that the variable is not appended to
1590.Va .MAKE.EXPORTED .
1591This allows exporting a value to the environment which is different from that
1592used by
1593.Nm
1594internally.
1595.It Ic .export-literal Ar variable ...
1596The same as
1597.Ql .export-env ,
1598except that variables in the value are not expanded.
1599.It Ic .info Ar message
1600The message is printed along with the name of the makefile and line number.
1601.It Ic .undef Ar variable
1602Un-define the specified global variable.
1603Only global variables may be un-defined.
1604.It Ic .unexport Ar variable ...
1605The opposite of
1606.Ql .export .
1607The specified global
1608.Va variable
1609will be removed from
1610.Va .MAKE.EXPORTED .
1611If no variable list is provided, all globals are unexported,
1612and
1613.Va .MAKE.EXPORTED
1614deleted.
1615.It Ic .unexport-env
1616Unexport all globals previously exported and
1617clear the environment inherited from the parent.
1618This operation will cause a memory leak of the original environment,
1619so should be used sparingly.
1620Testing for
1621.Va .MAKE.LEVEL
1622being 0, would make sense.
1623Also note that any variables which originated in the parent environment
1624should be explicitly preserved if desired.
1625For example:
1626.Bd -literal -offset indent
1627.Li .if ${.MAKE.LEVEL} == 0
1628PATH := ${PATH}
1629.Li .unexport-env
1630.Li .export PATH
1631.Li .endif
1632.Pp
1633.Ed
1634Would result in an environment containing only
1635.Ql Ev PATH ,
1636which is the minimal useful environment.
1637Actually
1638.Ql Ev .MAKE.LEVEL
1639will also be pushed into the new environment.
1640.It Ic .warning Ar message
1641The message prefixed by
1642.Ql Pa warning:
1643is printed along with the name of the makefile and line number.
1644.It Ic \&.if Oo \&! Oc Ns Ar expression Op Ar operator expression ...
1645Test the value of an expression.
1646.It Ic .ifdef Oo \&! Oc Ns Ar variable Op Ar operator variable ...
1647Test the value of a variable.
1648.It Ic .ifndef Oo \&! Oc Ns Ar variable Op Ar operator variable ...
1649Test the value of a variable.
1650.It Ic .ifmake Oo \&! Oc Ns Ar target Op Ar operator target ...
1651Test the target being built.
1652.It Ic .ifnmake Oo \&! Ns Oc Ar target Op Ar operator target ...
1653Test the target being built.
1654.It Ic .else
1655Reverse the sense of the last conditional.
1656.It Ic .elif Oo \&! Ns Oc Ar expression Op Ar operator expression ...
1657A combination of
1658.Ql Ic .else
1659followed by
1660.Ql Ic .if .
1661.It Ic .elifdef Oo \&! Oc Ns Ar variable Op Ar operator variable ...
1662A combination of
1663.Ql Ic .else
1664followed by
1665.Ql Ic .ifdef .
1666.It Ic .elifndef Oo \&! Oc Ns Ar variable Op Ar operator variable ...
1667A combination of
1668.Ql Ic .else
1669followed by
1670.Ql Ic .ifndef .
1671.It Ic .elifmake Oo \&! Oc Ns Ar target Op Ar operator target ...
1672A combination of
1673.Ql Ic .else
1674followed by
1675.Ql Ic .ifmake .
1676.It Ic .elifnmake Oo \&! Oc Ns Ar target Op Ar operator target ...
1677A combination of
1678.Ql Ic .else
1679followed by
1680.Ql Ic .ifnmake .
1681.It Ic .endif
1682End the body of the conditional.
1683.El
1684.Pp
1685The
1686.Ar operator
1687may be any one of the following:
1688.Bl -tag -width "Cm XX"
1689.It Cm \&|\&|
1690Logical OR.
1691.It Cm \&\*[Am]\*[Am]
1692Logical
1693.Tn AND ;
1694of higher precedence than
1695.Dq \&|\&| .
1696.El
1697.Pp
1698As in C,
1699.Nm
1700will only evaluate a conditional as far as is necessary to determine
1701its value.
1702Parentheses may be used to change the order of evaluation.
1703The boolean operator
1704.Ql Ic \&!
1705may be used to logically negate an entire
1706conditional.
1707It is of higher precedence than
1708.Ql Ic \&\*[Am]\*[Am] .
1709.Pp
1710The value of
1711.Ar expression
1712may be any of the following:
1713.Bl -tag -width defined
1714.It Ic defined
1715Takes a variable name as an argument and evaluates to true if the variable
1716has been defined.
1717.It Ic make
1718Takes a target name as an argument and evaluates to true if the target
1719was specified as part of
1720.Nm Ns 's
1721command line or was declared the default target (either implicitly or
1722explicitly, see
1723.Va .MAIN )
1724before the line containing the conditional.
1725.It Ic empty
1726Takes a variable, with possible modifiers, and evaluates to true if
1727the expansion of the variable would result in an empty string.
1728.It Ic exists
1729Takes a file name as an argument and evaluates to true if the file exists.
1730The file is searched for on the system search path (see
1731.Va .PATH ) .
1732.It Ic target
1733Takes a target name as an argument and evaluates to true if the target
1734has been defined.
1735.It Ic commands
1736Takes a target name as an argument and evaluates to true if the target
1737has been defined and has commands associated with it.
1738.El
1739.Pp
1740.Ar Expression
1741may also be an arithmetic or string comparison.
1742Variable expansion is
1743performed on both sides of the comparison, after which the integral
1744values are compared.
1745A value is interpreted as hexadecimal if it is
1746preceded by 0x, otherwise it is decimal; octal numbers are not supported.
1747The standard C relational operators are all supported.
1748If after
1749variable expansion, either the left or right hand side of a
1750.Ql Ic ==
1751or
1752.Ql Ic "!="
1753operator is not an integral value, then
1754string comparison is performed between the expanded
1755variables.
1756If no relational operator is given, it is assumed that the expanded
1757variable is being compared against 0 or an empty string in the case
1758of a string comparison.
1759.Pp
1760When
1761.Nm
1762is evaluating one of these conditional expressions, and it encounters
1763a (white-space separated) word it doesn't recognize, either the
1764.Dq make
1765or
1766.Dq defined
1767expression is applied to it, depending on the form of the conditional.
1768If the form is
1769.Ql Ic .ifdef ,
1770.Ql Ic .ifndef ,
1771or
1772.Ql Ic .if
1773the
1774.Dq defined
1775expression is applied.
1776Similarly, if the form is
1777.Ql Ic .ifmake
1778or
1779.Ql Ic .ifnmake , the
1780.Dq make
1781expression is applied.
1782.Pp
1783If the conditional evaluates to true the parsing of the makefile continues
1784as before.
1785If it evaluates to false, the following lines are skipped.
1786In both cases this continues until a
1787.Ql Ic .else
1788or
1789.Ql Ic .endif
1790is found.
1791.Pp
1792For loops are typically used to apply a set of rules to a list of files.
1793The syntax of a for loop is:
1794.Pp
1795.Bl -tag -compact -width Ds
1796.It Ic \&.for Ar variable Oo Ar variable ... Oc Ic in Ar expression
1797.It Aq make-rules
1798.It Ic \&.endfor
1799.El
1800.Pp
1801After the for
1802.Ic expression
1803is evaluated, it is split into words.
1804On each iteration of the loop, one word is taken and assigned to each
1805.Ic variable ,
1806in order, and these
1807.Ic variables
1808are substituted into the
1809.Ic make-rules
1810inside the body of the for loop.
1811The number of words must come out even; that is, if there are three
1812iteration variables, the number of words provided must be a multiple
1813of three.
1814.Sh COMMENTS
1815Comments begin with a hash
1816.Pq Ql \&#
1817character, anywhere but in a shell
1818command line, and continue to the end of an unescaped new line.
1819.Sh SPECIAL SOURCES (ATTRIBUTES)
1820.Bl -tag -width .IGNOREx
1821.It Ic .EXEC
1822Target is never out of date, but always execute commands anyway.
1823.It Ic .IGNORE
1824Ignore any errors from the commands associated with this target, exactly
1825as if they all were preceded by a dash
1826.Pq Ql \- .
1827.\" .It Ic .INVISIBLE
1828.\" XXX
1829.\" .It Ic .JOIN
1830.\" XXX
1831.It Ic .MADE
1832Mark all sources of this target as being up-to-date.
1833.It Ic .MAKE
1834Execute the commands associated with this target even if the
1835.Fl n
1836or
1837.Fl t
1838options were specified.
1839Normally used to mark recursive
1840.Nm Ns s .
1841.It Ic .META
1842Create a meta file for the target, even if it is flagged as
1843.Ic .PHONY ,
1844.Ic .MAKE ,
1845or
1846.Ic .SPECIAL .
1847Usage in conjunction with
1848.Ic .MAKE
1849is the most likely case.
1850In "meta" mode, the target is out-of-date if the meta file is missing.
1851.It Ic .NOMETA
1852Do not create a meta file for the target.
1853Meta files are also not created for
1854.Ic .PHONY ,
1855.Ic .MAKE ,
1856or
1857.Ic .SPECIAL
1858targets.
1859.It Ic .NOMETA_CMP
1860Ignore differences in commands when deciding if target is out of date.
1861This is useful if the command contains a value which always changes.
1862If the number of commands change, though, the target will still be out of date.
1863The same effect applies to any command line that uses the variable
1864.Va .OODATE ,
1865which can be used for that purpose even when not otherwise needed or desired:
1866.Bd -literal -offset indent
1867
1868skip-compare-for-some:
1869	@echo this will be compared
1870	@echo this will not ${.OODATE:M.NOMETA_CMP}
1871	@echo this will also be compared
1872
1873.Ed
1874The
1875.Cm \&:M
1876pattern suppresses any expansion of the unwanted variable.
1877.It Ic .NOPATH
1878Do not search for the target in the directories specified by
1879.Ic .PATH .
1880.It Ic .NOTMAIN
1881Normally
1882.Nm
1883selects the first target it encounters as the default target to be built
1884if no target was specified.
1885This source prevents this target from being selected.
1886.It Ic .OPTIONAL
1887If a target is marked with this attribute and
1888.Nm
1889can't figure out how to create it, it will ignore this fact and assume
1890the file isn't needed or already exists.
1891.It Ic .PHONY
1892The target does not
1893correspond to an actual file; it is always considered to be out of date,
1894and will not be created with the
1895.Fl t
1896option.
1897Suffix-transformation rules are not applied to
1898.Ic .PHONY
1899targets.
1900.It Ic .PRECIOUS
1901When
1902.Nm
1903is interrupted, it normally removes any partially made targets.
1904This source prevents the target from being removed.
1905.It Ic .RECURSIVE
1906Synonym for
1907.Ic .MAKE .
1908.It Ic .SILENT
1909Do not echo any of the commands associated with this target, exactly
1910as if they all were preceded by an at sign
1911.Pq Ql @ .
1912.It Ic .USE
1913Turn the target into
1914.Nm Ns 's
1915version of a macro.
1916When the target is used as a source for another target, the other target
1917acquires the commands, sources, and attributes (except for
1918.Ic .USE )
1919of the
1920source.
1921If the target already has commands, the
1922.Ic .USE
1923target's commands are appended
1924to them.
1925.It Ic .USEBEFORE
1926Exactly like
1927.Ic .USE ,
1928but prepend the
1929.Ic .USEBEFORE
1930target commands to the target.
1931.It Ic .WAIT
1932If
1933.Ic .WAIT
1934appears in a dependency line, the sources that precede it are
1935made before the sources that succeed it in the line.
1936Since the dependents of files are not made until the file itself
1937could be made, this also stops the dependents being built unless they
1938are needed for another branch of the dependency tree.
1939So given:
1940.Bd -literal
1941x: a .WAIT b
1942	echo x
1943a:
1944	echo a
1945b: b1
1946	echo b
1947b1:
1948	echo b1
1949
1950.Ed
1951the output is always
1952.Ql a ,
1953.Ql b1 ,
1954.Ql b ,
1955.Ql x .
1956.br
1957The ordering imposed by
1958.Ic .WAIT
1959is only relevant for parallel makes.
1960.El
1961.Sh SPECIAL TARGETS
1962Special targets may not be included with other targets, i.e. they must be
1963the only target specified.
1964.Bl -tag -width .BEGINx
1965.It Ic .BEGIN
1966Any command lines attached to this target are executed before anything
1967else is done.
1968.It Ic .DEFAULT
1969This is sort of a
1970.Ic .USE
1971rule for any target (that was used only as a
1972source) that
1973.Nm
1974can't figure out any other way to create.
1975Only the shell script is used.
1976The
1977.Ic .IMPSRC
1978variable of a target that inherits
1979.Ic .DEFAULT Ns 's
1980commands is set
1981to the target's own name.
1982.It Ic .END
1983Any command lines attached to this target are executed after everything
1984else is done.
1985.It Ic .ERROR
1986Any command lines attached to this target are executed when another target fails.
1987The
1988.Ic .ERROR_TARGET
1989variable is set to the target that failed.
1990See also
1991.Ic MAKE_PRINT_VAR_ON_ERROR .
1992.It Ic .IGNORE
1993Mark each of the sources with the
1994.Ic .IGNORE
1995attribute.
1996If no sources are specified, this is the equivalent of specifying the
1997.Fl i
1998option.
1999.It Ic .INTERRUPT
2000If
2001.Nm
2002is interrupted, the commands for this target will be executed.
2003.It Ic .MAIN
2004If no target is specified when
2005.Nm
2006is invoked, this target will be built.
2007.It Ic .MAKEFLAGS
2008This target provides a way to specify flags for
2009.Nm
2010when the makefile is used.
2011The flags are as if typed to the shell, though the
2012.Fl f
2013option will have
2014no effect.
2015.\" XXX: NOT YET!!!!
2016.\" .It Ic .NOTPARALLEL
2017.\" The named targets are executed in non parallel mode.
2018.\" If no targets are
2019.\" specified, then all targets are executed in non parallel mode.
2020.It Ic .NOPATH
2021Apply the
2022.Ic .NOPATH
2023attribute to any specified sources.
2024.It Ic .NOTPARALLEL
2025Disable parallel mode.
2026.It Ic .NO_PARALLEL
2027Synonym for
2028.Ic .NOTPARALLEL ,
2029for compatibility with other pmake variants.
2030.It Ic .OBJDIR
2031The source is a new value for
2032.Ql Va .OBJDIR .
2033If it exists,
2034.Nm
2035will
2036.Xr chdir 2
2037to it and update the value of
2038.Ql Va .OBJDIR .
2039.It Ic .ORDER
2040The named targets are made in sequence.
2041This ordering does not add targets to the list of targets to be made.
2042Since the dependents of a target do not get built until the target itself
2043could be built, unless
2044.Ql a
2045is built by another part of the dependency graph,
2046the following is a dependency loop:
2047.Bd -literal
2048\&.ORDER: b a
2049b: a
2050.Ed
2051.Pp
2052The ordering imposed by
2053.Ic .ORDER
2054is only relevant for parallel makes.
2055.\" XXX: NOT YET!!!!
2056.\" .It Ic .PARALLEL
2057.\" The named targets are executed in parallel mode.
2058.\" If no targets are
2059.\" specified, then all targets are executed in parallel mode.
2060.It Ic .PATH
2061The sources are directories which are to be searched for files not
2062found in the current directory.
2063If no sources are specified, any previously specified directories are
2064deleted.
2065If the source is the special
2066.Ic .DOTLAST
2067target, then the current working
2068directory is searched last.
2069.It Ic .PATH. Ns Va suffix
2070Like
2071.Ic .PATH
2072but applies only to files with a particular suffix.
2073The suffix must have been previously declared with
2074.Ic .SUFFIXES .
2075.It Ic .PHONY
2076Apply the
2077.Ic .PHONY
2078attribute to any specified sources.
2079.It Ic .PRECIOUS
2080Apply the
2081.Ic .PRECIOUS
2082attribute to any specified sources.
2083If no sources are specified, the
2084.Ic .PRECIOUS
2085attribute is applied to every
2086target in the file.
2087.It Ic .SHELL
2088Sets the shell that
2089.Nm
2090will use to execute commands.
2091The sources are a set of
2092.Ar field=value
2093pairs.
2094.Bl -tag -width hasErrCtls
2095.It Ar name
2096This is the minimal specification, used to select one of the built-in
2097shell specs;
2098.Ar sh ,
2099.Ar ksh ,
2100and
2101.Ar csh .
2102.It Ar path
2103Specifies the path to the shell.
2104.It Ar hasErrCtl
2105Indicates whether the shell supports exit on error.
2106.It Ar check
2107The command to turn on error checking.
2108.It Ar ignore
2109The command to disable error checking.
2110.It Ar echo
2111The command to turn on echoing of commands executed.
2112.It Ar quiet
2113The command to turn off echoing of commands executed.
2114.It Ar filter
2115The output to filter after issuing the
2116.Ar quiet
2117command.
2118It is typically identical to
2119.Ar quiet .
2120.It Ar errFlag
2121The flag to pass the shell to enable error checking.
2122.It Ar echoFlag
2123The flag to pass the shell to enable command echoing.
2124.It Ar newline
2125The string literal to pass the shell that results in a single newline
2126character when used outside of any quoting characters.
2127.El
2128Example:
2129.Bd -literal
2130\&.SHELL: name=ksh path=/bin/ksh hasErrCtl=true \e
2131	check="set \-e" ignore="set +e" \e
2132	echo="set \-v" quiet="set +v" filter="set +v" \e
2133	echoFlag=v errFlag=e newline="'\en'"
2134.Ed
2135.It Ic .SILENT
2136Apply the
2137.Ic .SILENT
2138attribute to any specified sources.
2139If no sources are specified, the
2140.Ic .SILENT
2141attribute is applied to every
2142command in the file.
2143.It Ic .STALE
2144This target gets run when a dependency file contains stale entries, having
2145.Va .ALLSRC
2146set to the name of that dependency file.
2147.It Ic .SUFFIXES
2148Each source specifies a suffix to
2149.Nm .
2150If no sources are specified, any previously specified suffixes are deleted.
2151It allows the creation of suffix-transformation rules.
2152.Pp
2153Example:
2154.Bd -literal
2155\&.SUFFIXES: .o
2156\&.c.o:
2157	cc \-o ${.TARGET} \-c ${.IMPSRC}
2158.Ed
2159.El
2160.Sh ENVIRONMENT
2161.Nm
2162uses the following environment variables, if they exist:
2163.Ev MACHINE ,
2164.Ev MACHINE_ARCH ,
2165.Ev MAKE ,
2166.Ev MAKEFLAGS ,
2167.Ev MAKEOBJDIR ,
2168.Ev MAKEOBJDIRPREFIX ,
2169.Ev MAKESYSPATH ,
2170.Ev PWD ,
2171and
2172.Ev TMPDIR .
2173.Pp
2174.Ev MAKEOBJDIRPREFIX
2175and
2176.Ev MAKEOBJDIR
2177may only be set in the environment or on the command line to
2178.Nm
2179and not as makefile variables;
2180see the description of
2181.Ql Va .OBJDIR
2182for more details.
2183.Sh FILES
2184.Bl -tag -width /usr/share/mk -compact
2185.It .depend
2186list of dependencies
2187.It Makefile
2188list of dependencies
2189.It makefile
2190list of dependencies
2191.It sys.mk
2192system makefile
2193.It /usr/share/mk
2194system makefile directory
2195.El
2196.Sh COMPATIBILITY
2197The basic make syntax is compatible between different versions of make;
2198however the special variables, variable modifiers and conditionals are not.
2199.Ss Older versions
2200An incomplete list of changes in older versions of
2201.Nm :
2202.Pp
2203The way that .for loop variables are substituted changed after
2204NetBSD 5.0
2205so that they still appear to be variable expansions.
2206In particular this stops them being treated as syntax, and removes some
2207obscure problems using them in .if statements.
2208.Pp
2209The way that parallel makes are scheduled changed in
2210NetBSD 4.0
2211so that .ORDER and .WAIT apply recursively to the dependent nodes.
2212The algorithms used may change again in the future.
2213.Ss Other make dialects
2214Other make dialects (GNU make, SVR4 make, POSIX make, etc.) do not
2215support most of the features of
2216.Nm
2217as described in this manual.
2218Most notably:
2219.Bl -bullet -offset indent
2220.It
2221The
2222.Ic .WAIT
2223and
2224.Ic .ORDER
2225declarations and most functionality pertaining to parallelization.
2226(GNU make supports parallelization but lacks these features needed to
2227control it effectively.)
2228.It
2229Directives, including for loops and conditionals and most of the
2230forms of include files.
2231(GNU make has its own incompatible and less powerful syntax for
2232conditionals.)
2233.It
2234All built-in variables that begin with a dot.
2235.It
2236Most of the special sources and targets that begin with a dot,
2237with the notable exception of
2238.Ic .PHONY ,
2239.Ic .PRECIOUS ,
2240and
2241.Ic .SUFFIXES .
2242.It
2243Variable modifiers, except for the
2244.Dl :old=new
2245string substitution, which does not portably support globbing with
2246.Ql %
2247and historically only works on declared suffixes.
2248.It
2249The
2250.Ic $>
2251variable even in its short form; most makes support this functionality
2252but its name varies.
2253.El
2254.Pp
2255Some features are somewhat more portable, such as assignment with
2256.Ic += ,
2257.Ic ?= ,
2258and
2259.Ic != .
2260The
2261.Ic .PATH
2262functionality is based on an older feature
2263.Ic VPATH
2264found in GNU make and many versions of SVR4 make; however,
2265historically its behavior is too ill-defined (and too buggy) to rely
2266upon.
2267.Pp
2268The
2269.Ic $@
2270and
2271.Ic $<
2272variables are more or less universally portable, as is the
2273.Ic $(MAKE)
2274variable.
2275Basic use of suffix rules (for files only in the current directory,
2276not trying to chain transformations together, etc.) is also reasonably
2277portable.
2278.Sh SEE ALSO
2279.Xr mkdep 1
2280.Sh HISTORY
2281.Nm
2282is derived from NetBSD
2283.Xr make 1 .
2284It uses autoconf to facilitate portability to other platforms.
2285.Pp
2286A
2287make
2288command appeared in
2289.At v7 .
2290This
2291make
2292implementation is based on Adam De Boor's pmake program which was written
2293for Sprite at Berkeley.
2294It was designed to be a parallel distributed make running jobs on different
2295machines using a daemon called
2296.Dq customs .
2297.Pp
2298Historically the target/dependency
2299.Dq FRC
2300has been used to FoRCe rebuilding (since the target/dependency
2301does not exist... unless someone creates an
2302.Dq FRC
2303file).
2304.Sh BUGS
2305The
2306make
2307syntax is difficult to parse without actually acting of the data.
2308For instance finding the end of a variable use should involve scanning each
2309the modifiers using the correct terminator for each field.
2310In many places
2311make
2312just counts {} and () in order to find the end of a variable expansion.
2313.Pp
2314There is no way of escaping a space character in a filename.
2315