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