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