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