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