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