]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - contrib/bmake/PSD.doc/tutorial.ms
- Copy stable/10@285827 to releng/10.2 in preparation for 10.2-RC1
[FreeBSD/releng/10.2.git] / contrib / bmake / PSD.doc / tutorial.ms
1 .\"     $NetBSD: tutorial.ms,v 1.11 2011/08/18 15:19:30 sjg Exp $
2 .\" Copyright (c) 1988, 1989, 1993
3 .\"     The Regents of the University of California.  All rights reserved.
4 .\"
5 .\" This code is derived from software contributed to Berkeley by
6 .\" Adam de Boor.
7 .\"
8 .\" Redistribution and use in source and binary forms, with or without
9 .\" modification, are permitted provided that the following conditions
10 .\" are met:
11 .\" 1. Redistributions of source code must retain the above copyright
12 .\"    notice, this list of conditions and the following disclaimer.
13 .\" 2. Redistributions in binary form must reproduce the above copyright
14 .\"    notice, this list of conditions and the following disclaimer in the
15 .\"    documentation and/or other materials provided with the distribution.
16 .\" 3. 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 .\" Copyright (c) 1988, 1989 by Adam de Boor
33 .\" Copyright (c) 1989 by Berkeley Softworks
34 .\"
35 .\" This code is derived from software contributed to Berkeley by
36 .\" Adam de Boor.
37 .\"
38 .\" Redistribution and use in source and binary forms, with or without
39 .\" modification, are permitted provided that the following conditions
40 .\" are met:
41 .\" 1. Redistributions of source code must retain the above copyright
42 .\"    notice, this list of conditions and the following disclaimer.
43 .\" 2. Redistributions in binary form must reproduce the above copyright
44 .\"    notice, this list of conditions and the following disclaimer in the
45 .\"    documentation and/or other materials provided with the distribution.
46 .\" 3. All advertising materials mentioning features or use of this software
47 .\"    must display the following acknowledgement:
48 .\"     This product includes software developed by the University of
49 .\"     California, Berkeley and its contributors.
50 .\" 4. Neither the name of the University nor the names of its contributors
51 .\"    may be used to endorse or promote products derived from this software
52 .\"    without specific prior written permission.
53 .\"
54 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 .\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64 .\" SUCH DAMAGE.
65 .\"
66 .\"     @(#)tutorial.ms 8.1 (Berkeley) 8/18/93
67 .\"
68 .EH 'PSD:12-%''PMake \*- A Tutorial'
69 .OH 'PMake \*- A Tutorial''PSD:12-%'
70 .\" xH is a macro to provide numbered headers that are automatically stuffed
71 .\" into a table-of-contents, properly indented, etc. If the first argument
72 .\" is numeric, it is taken as the depth for numbering (as for .NH), else
73 .\" the default (1) is assumed.
74 .\"
75 .\" @P The initial paragraph distance.
76 .\" @Q The piece of section number to increment (or 0 if none given)
77 .\" @R Section header.
78 .\" @S Indent for toc entry
79 .\" @T Argument to NH (can't use @Q b/c giving 0 to NH resets the counter)
80 .de xH
81 .NH \\$1
82 \\$2 \\$3 \\$4 \\$5 \\$6 \\$7 \\$8 \\$9
83 .nr PD .1v
84 .XS \\n%
85 .ta 0.6i
86 \\*(SN  \\$2 \\$3 \\$4 \\$5 \\$6 \\$7 \\$8 \\$9
87 .XE
88 .nr PD .3v
89 ..
90 .\" CW is used to place a string in fixed-width or switch to a
91 .\" fixed-width font.
92 .\" C is a typewriter font for a laserwriter. Use something else if
93 .\" you don't have one...
94 .de CW
95 .ie !\\n(.$ .ft C
96 .el \&\\$3\fC\\$1\fP\\$2
97 ..
98 .\" Anything I put in a display I want to be in fixed-width
99 .am DS
100 .CW
101 ..
102 .\" The stuff in .No produces a little stop sign in the left margin
103 .\" that says NOTE in it. Unfortunately, it does cause a break, but
104 .\" hey. Can't have everything. In case you're wondering how I came
105 .\" up with such weird commands, they came from running grn on a
106 .\" gremlin file...
107 .de No
108 .br
109 .ne 0.5i
110 .po -0.5i
111 .br
112 .mk 
113 .nr g3 \\n(.f
114 .nr g4 \\n(.s
115 .sp -1
116 .\" .st cf
117 \D't 5u'
118 .sp -1
119 \h'50u'
120 .sp -1
121 \D't 3u'
122 .sp -1
123 .sp 7u
124 \h'53u'
125 \d\D'p -0.19i 0.0i 0.0i -0.13i 0.30i 0.0i 0.0i 0.13i'
126 .sp -1
127 .ft R
128 .ps 6
129 .nr g8 \\n(.d
130 .ds g9 "NOTE
131 .sp 74u
132 \h'85u'\v'0.85n'\h\a-\w\a\\*(g9\au/2u\a\&\\*(g9
133 .sp |\\n(g8u
134 .sp 166u
135 \D't 3u'
136 .br
137 .po
138 .rt 
139 .ft \\n(g3
140 .ps \\n(g4
141 ..
142 .de Bp
143 .ie !\\n(.$ .IP \(bu 2
144 .el .IP "\&" 2
145 ..
146 .po +.3i
147 .TL
148 PMake \*- A Tutorial
149 .AU
150 Adam de Boor
151 .AI
152 Berkeley Softworks
153 2150 Shattuck Ave, Penthouse
154 Berkeley, CA 94704
155 adam@bsw.uu.net
156 \&...!uunet!bsw!adam
157 .FS
158 Permission to use, copy, modify, and distribute this software and its
159 documentation for any purpose and without fee is hereby granted,
160 provided that the above copyright notice appears in all copies.
161 The University of California, Berkeley Softworks, and Adam de Boor make no
162 representations about the suitability of this software for any
163 purpose.  It is provided "as is" without express or implied warranty.
164 .FE
165 .PP
166 .xH 1 Introduction
167 .LP
168 PMake is a program for creating other programs, or anything else you
169 can think of for it to do.  The basic idea behind PMake is that, for
170 any given system, be it a program or a document or whatever, there
171 will be some files that depend on the state of other files (on when
172 they were last modified). PMake takes these dependencies, which you
173 must specify, and uses them to build whatever it is you want it to
174 build.
175 .LP
176 PMake is almost fully-compatible with Make, with which you may already
177 be familiar. PMake's most important feature is its ability to run
178 several different jobs at once, making the creation of systems
179 considerably faster. It also has a great deal more functionality than
180 Make. Throughout the text, whenever something is mentioned that is an
181 important difference between PMake and Make (i.e. something that will
182 cause a makefile to fail if you don't do something about it), or is
183 simply important, it will be flagged with a little sign in the left
184 margin, like this:
185 .No
186 .LP
187 This tutorial is divided into three main sections corresponding to basic,
188 intermediate and advanced PMake usage. If you already know Make well,
189 you will only need to skim chapter 2 (there are some aspects of
190 PMake that I consider basic to its use that didn't exist in Make).
191 Things in chapter 3 make life much easier, while those in chapter 4
192 are strictly for those who know what they are doing. Chapter 5 has
193 definitions for the jargon I use and chapter 6 contains possible
194 solutions to the problems presented throughout the tutorial.
195 .xH 1 The Basics of PMake
196 .LP
197 PMake takes as input a file that tells a) which files depend on which
198 other files to be complete and b) what to do about files that are
199 ``out-of-date.'' This file is known as a ``makefile'' and is usually
200 .Ix 0 def makefile
201 kept in the top-most directory of the system to be built. While you
202 can call the makefile anything you want, PMake will look for
203 .CW Makefile
204 and
205 .CW makefile
206 (in that order) in the current directory if you don't tell it
207 otherwise.
208 .Ix 0 def makefile default
209 To specify a different makefile, use the
210 .B \-f
211 flag (e.g.
212 .CW "pmake -f program.mk" ''). ``
213 .Ix 0 ref flags -f
214 .Ix 0 ref makefile other
215 .LP
216 A makefile has four different types of lines in it:
217 .RS
218 .IP \(bu 2
219 File dependency specifications
220 .IP \(bu 2
221 Creation commands
222 .IP \(bu 2
223 Variable assignments
224 .IP \(bu 2
225 Comments, include statements and conditional directives
226 .RE
227 .LP
228 Any line may be continued over multiple lines by ending it with a
229 backslash.
230 .Ix 0 def "continuation line"
231 The backslash, following newline and any initial whitespace
232 on the following line are compressed into a single space before the
233 input line is examined by PMake.
234 .xH 2 Dependency Lines
235 .LP
236 As mentioned in the introduction, in any system, there are
237 dependencies between the files that make up the system.  For instance,
238 in a program made up of several C source files and one header file,
239 the C files will need to be re-compiled should the header file be
240 changed. For a document of several chapters and one macro file, the
241 chapters will need to be reprocessed if any of the macros changes.
242 .Ix 0 def "dependency"
243 These are dependencies and are specified by means of dependency lines in
244 the makefile.
245 .LP
246 .Ix 0 def "dependency line"
247 On a dependency line, there are targets and sources, separated by a
248 one- or two-character operator.
249 The targets ``depend'' on the sources and are usually created from
250 them.
251 .Ix 0 def target
252 .Ix 0 def source
253 .Ix 0 ref operator
254 Any number of targets and sources may be specified on a dependency line.
255 All the targets in the line are made to depend on all the sources.
256 Targets and sources need not be actual files, but every source must be
257 either an actual file or another target in the makefile.
258 If you run out of room, use a backslash at the end of the line to continue onto
259 the next one.
260 .LP
261 Any file may be a target and any file may be a source, but the
262 relationship between the two (or however many) is determined by the
263 ``operator'' that separates them.
264 .Ix 0 def operator
265 Three types of operators exist: one specifies that the datedness of a
266 target is determined by the state of its sources, while another
267 specifies other files (the sources) that need to be dealt with before
268 the target can be re-created. The third operator is very similar to
269 the first, with the additional condition that the target is
270 out-of-date if it has no sources. These operations are represented by
271 the colon, the exclamation point and the double-colon, respectively, and are
272 mutually exclusive. Their exact semantics are as follows:
273 .IP ":"
274 .Ix 0 def operator colon
275 .Ix 0 def :
276 If a colon is used, a target on the line is considered to be
277 ``out-of-date'' (and in need of creation) if 
278 .RS
279 .IP \(bu 2
280 any of the sources has been modified more recently than the target, or
281 .IP \(bu 2
282 the target doesn't exist.
283 .RE
284 .Ix 0 def out-of-date
285 .IP "\&"
286 Under this operation, steps will be taken to re-create the target only
287 if it is found to be out-of-date by using these two rules.
288 .IP "!"
289 .Ix 0 def operator force
290 .Ix 0 def !
291 If an exclamation point is used, the target will always be re-created,
292 but this will not happen until all of its sources have been examined
293 and re-created, if necessary.
294 .IP "::"
295 .Ix 0 def operator double-colon
296 .Ix 0 def ::
297 If a double-colon is used, a target is out-of-date if:
298 .RS
299 .IP \(bu 2
300 any of the sources has been modified more recently than the target, or
301 .IP \(bu 2
302 the target doesn't exist, or
303 .IP \(bu 2
304 the target has no sources.
305 .RE
306 .IP "\&"
307 If the target is out-of-date according to these rules, it will be re-created.
308 This operator also does something else to the targets, but I'll go
309 into that in the next section (``Shell Commands'').
310 .LP
311 Enough words, now for an example. Take that C program I mentioned
312 earlier. Say there are three C files
313 .CW a.c , (
314 .CW b.c
315 and
316 .CW  c.c )
317 each of which
318 includes the file
319 .CW defs.h .
320 The dependencies between the files could then be expressed as follows:
321 .DS
322 program         : a.o b.o c.o
323 a.o b.o c.o     : defs.h
324 a.o             : a.c
325 b.o             : b.c
326 c.o             : c.c
327 .DE
328 .LP
329 You may be wondering at this point, where
330 .CW a.o ,
331 .CW b.o
332 and
333 .CW c.o
334 came in and why
335 .I they
336 depend on
337 .CW defs.h
338 and the C files don't. The reason is quite simple:
339 .CW program
340 cannot be made by linking together .c files \*- it must be
341 made from .o files. Likewise, if you change
342 .CW defs.h ,
343 it isn't the .c files that need to be re-created, it's the .o files.
344 If you think of dependencies in these terms \*- which files (targets)
345 need to be created from which files (sources) \*- you should have no problems.
346 .LP
347 An important thing to notice about the above example, is that all the
348 \&.o files appear as targets on more than one line. This is perfectly
349 all right: the target is made to depend on all the sources mentioned
350 on all the dependency lines. E.g.
351 .CW a.o
352 depends on both
353 .CW defs.h
354 and
355 .CW a.c .
356 .Ix 0 ref dependency
357 .No
358 .LP
359 The order of the dependency lines in the makefile is
360 important: the first target on the first dependency line in the
361 makefile will be the one that gets made if you don't say otherwise.
362 That's why
363 .CW program
364 comes first in the example makefile, above.
365 .LP
366 Both targets and sources may contain the standard C-Shell wildcard
367 characters
368 .CW { , (
369 .CW } ,
370 .CW * ,
371 .CW ? ,
372 .CW [ ,
373 and
374 .CW ] ),
375 but the non-curly-brace ones may only appear in the final component
376 (the file portion) of the target or source. The characters mean the
377 following things:
378 .IP \fB{}\fP
379 These enclose a comma-separated list of options and cause the pattern
380 to be expanded once for each element of the list. Each expansion
381 contains a different element. For example, 
382 .CW src/{whiffle,beep,fish}.c
383 expands to the three words
384 .CW src/whiffle.c ,
385 .CW src/beep.c ,
386 and 
387 .CW src/fish.c .
388 These braces may be nested and, unlike the other wildcard characters,
389 the resulting words need not be actual files. All other wildcard
390 characters are expanded using the files that exist when PMake is
391 started.
392 .IP \fB*\fP
393 This matches zero or more characters of any sort. 
394 .CW src/*.c
395 will expand to the same three words as above as long as 
396 .CW src
397 contains those three files (and no other files that end in 
398 .CW .c ).
399 .IP \fB?\fP
400 Matches any single character.
401 .IP \fB[]\fP
402 This is known as a character class and contains either a list of
403 single characters, or a series of character ranges 
404 .CW a-z , (
405 for example means all characters between a and z), or both. It matches
406 any single character contained in the list. E.g.
407 .CW [A-Za-z]
408 will match all letters, while
409 .CW [0123456789]
410 will match all numbers.
411 .xH 2 Shell Commands
412 .LP
413 ``Isn't that nice,'' you say to yourself, ``but how are files
414 actually `re-created,' as he likes to spell it?''
415 The re-creation is accomplished by commands you place in the makefile.
416 These commands are passed to the Bourne shell (better known as
417 ``/bin/sh'') to be executed and are
418 .Ix 0 ref shell
419 .Ix 0 ref re-creation
420 .Ix 0 ref update
421 expected to do what's necessary to update the target file (PMake
422 doesn't actually check to see if the target was created. It just
423 assumes it's there).
424 .Ix 0 ref target
425 .LP
426 Shell commands in a makefile look a lot like shell commands you would
427 type at a terminal, with one important exception: each command in a
428 makefile
429 .I must
430 be preceded by at least one tab.
431 .LP
432 Each target has associated with it a shell script made up of
433 one or more of these shell commands. The creation script for a target
434 should immediately follow the dependency line for that target. While
435 any given target may appear on more than one dependency line, only one
436 of these dependency lines may be followed by a creation script, unless
437 the `::' operator was used on the dependency line.
438 .Ix 0 ref operator double-colon
439 .Ix 0 ref ::
440 .No
441 .LP
442 If the double-colon was used, each dependency line for the target
443 may be followed by a shell script. That script will only be executed
444 if the target on the associated dependency line is out-of-date with
445 respect to the sources on that line, according to the rules I gave
446 earlier.
447 I'll give you a good example of this later on.
448 .LP
449 To expand on the earlier makefile, you might add commands as follows:
450 .DS
451 program         : a.o b.o c.o
452         cc a.o b.o c.o \-o program
453 a.o b.o c.o     : defs.h
454 a.o             : a.c
455         cc \-c a.c
456 b.o             : b.c
457         cc \-c b.c
458 c.o             : c.c
459         cc \-c c.c
460 .DE
461 .LP
462 Something you should remember when writing a makefile is, the
463 commands will be executed if the
464 .I target
465 on the dependency line is out-of-date, not the sources.
466 .Ix 0 ref target
467 .Ix 0 ref source
468 .Ix 0 ref out-of-date
469 In this example, the command
470 .CW "cc \-c a.c" '' ``
471 will be executed if
472 .CW a.o
473 is out-of-date. Because of the `:' operator,
474 .Ix 0 ref :
475 .Ix 0 ref operator colon
476 this means that should
477 .CW a.c
478 .I or
479 .CW defs.h
480 have been modified more recently than
481 .CW a.o ,
482 the command will be executed
483 .CW a.o "\&" (
484 will be considered out-of-date).
485 .Ix 0 ref out-of-date
486 .LP
487 Remember how I said the only difference between a makefile shell
488 command and a regular shell command was the leading tab? I lied. There
489 is another way in which makefile commands differ from regular ones.
490 The first two characters after the initial whitespace are treated
491 specially.
492 If they are any combination of `@' and `\-', they cause PMake to do
493 different things.
494 .LP
495 In most cases, shell commands are printed before they're
496 actually executed. This is to keep you informed of what's going on. If
497 an `@' appears, however, this echoing is suppressed. In the case of an
498 .CW echo
499 command, say
500 .CW "echo Linking index" ,'' ``
501 it would be
502 rather silly to see
503 .DS
504 echo Linking index
505 Linking index
506 .DE
507 .LP
508 so PMake allows you to place an `@' before the command
509 .CW "@echo Linking index" '') (``
510 to prevent the command from being printed.
511 .LP
512 The other special character is the `\-'. In case you didn't know,
513 shell commands finish with a certain ``exit status.'' This status is
514 made available by the operating system to whatever program invoked the
515 command. Normally this status will be 0 if everything went ok and
516 non-zero if something went wrong. For this reason, PMake will consider
517 an error to have occurred if one of the shells it invokes returns a non-zero
518 status. When it detects an error, PMake's usual action is to abort
519 whatever it's doing and exit with a non-zero status itself (any other
520 targets that were being created will continue being made, but nothing
521 new will be started. PMake will exit after the last job finishes).
522 This behavior can be altered, however, by placing a `\-' at the front
523 of a command
524 .CW "\-mv index index.old" ''), (``
525 certain command-line arguments,
526 or doing other things, to be detailed later. In such
527 a case, the non-zero status is simply ignored and PMake keeps chugging
528 along.
529 .No
530 .LP
531 Because all the commands are given to a single shell to execute, such
532 things as setting shell variables, changing directories, etc., last
533 beyond the command in which they are found. This also allows shell
534 compound commands (like
535 .CW for
536 loops) to be entered in a natural manner.
537 Since this could cause problems for some makefiles that depend on
538 each command being executed by a single shell, PMake has a
539 .B \-B
540 .Ix 0 ref compatibility
541 .Ix 0 ref flags -B
542 flag (it stands for backwards-compatible) that forces each command to
543 be given to a separate shell. It also does several other things, all
544 of which I discourage since they are now old-fashioned.\|.\|.\|.
545 .No
546 .LP
547 A target's shell script is fed to the shell on its (the shell's) input stream.
548 This means that any commands, such as
549 .CW ci
550 that need to get input from the terminal won't work right \*- they'll
551 get the shell's input, something they probably won't find to their
552 liking. A simple way around this is to give a command like this:
553 .DS
554 ci $(SRCS) < /dev/tty
555 .DE
556 This would force the program's input to come from the terminal. If you
557 can't do this for some reason, your only other alternative is to use
558 PMake in its fullest compatibility mode. See 
559 .B Compatibility
560 in chapter 4.
561 .Ix 0 ref compatibility
562 .LP
563 .xH 2 Variables
564 .LP
565 PMake, like Make before it, has the ability to save text in variables
566 to be recalled later at your convenience. Variables in PMake are used
567 much like variables in the shell and, by tradition, consist of
568 all upper-case letters (you don't
569 .I have
570 to use all upper-case letters.
571 In fact there's nothing to stop you from calling a variable
572 .CW @^&$%$ .
573 Just tradition). Variables are assigned-to using lines of the form
574 .Ix 0 def variable assignment
575 .DS
576 VARIABLE = value
577 .DE
578 .Ix 0 def variable assignment
579 appended-to by
580 .DS
581 VARIABLE += value
582 .DE
583 .Ix 0 def variable appending
584 .Ix 0 def variable assignment appended
585 .Ix 0 def +=
586 conditionally assigned-to (if the variable isn't already defined) by
587 .DS
588 VARIABLE ?= value
589 .DE
590 .Ix 0 def variable assignment conditional
591 .Ix 0 def ?=
592 and assigned-to with expansion (i.e. the value is expanded (see below)
593 before being assigned to the variable\*-useful for placing a value at
594 the beginning of a variable, or other things) by
595 .DS
596 VARIABLE := value
597 .DE
598 .Ix 0 def variable assignment expanded
599 .Ix 0 def :=
600 .LP
601 Any whitespace before
602 .I value
603 is stripped off. When appending, a space is placed between the old
604 value and the stuff being appended.
605 .LP
606 The final way a variable may be assigned to is using
607 .DS
608 VARIABLE != shell-command
609 .DE
610 .Ix 0 def variable assignment shell-output
611 .Ix 0 def !=
612 In this case, 
613 .I shell-command
614 has all its variables expanded (see below) and is passed off to a
615 shell to execute. The output of the shell is then placed in the
616 variable. Any newlines (other than the final one) are replaced by
617 spaces before the assignment is made. This is typically used to find
618 the current directory via a line like:
619 .DS
620 CWD             != pwd
621 .DE
622 .LP
623 .B Note:
624 this is intended to be used to execute commands that produce small amounts
625 of output (e.g. ``pwd''). The implementation is less than intelligent and will
626 likely freeze if you execute something that produces thousands of
627 bytes of output (8 Kb is the limit on many UNIX systems).
628 .LP
629 The value of a variable may be retrieved by enclosing the variable
630 name in parentheses or curly braces and preceding the whole thing
631 with a dollar sign.
632 .LP
633 For example, to set the variable CFLAGS to the string
634 .CW "\-I/sprite/src/lib/libc \-O" ,'' ``
635 you would place a line
636 .DS
637 CFLAGS = \-I/sprite/src/lib/libc \-O
638 .DE
639 in the makefile and use the word
640 .CW "$(CFLAGS)"
641 wherever you would like the string
642 .CW "\-I/sprite/src/lib/libc \-O"
643 to appear. This is called variable expansion.
644 .Ix 0 def variable expansion
645 .No
646 .LP
647 Unlike Make, PMake will not expand a variable unless it knows
648 the variable exists. E.g. if you have a
649 .CW "${i}"
650 in a shell command and you have not assigned a value to the variable
651 .CW i 
652 (the empty string is considered a value, by the way), where Make would have
653 substituted the empty string, PMake will leave the
654 .CW "${i}"
655 alone.
656 To keep PMake from substituting for a variable it knows, precede the
657 dollar sign with another dollar sign.
658 (e.g. to pass
659 .CW "${HOME}"
660 to the shell, use
661 .CW "$${HOME}" ).
662 This causes PMake, in effect, to expand the
663 .CW $
664 macro, which expands to a single
665 .CW $ .
666 For compatibility, Make's style of variable expansion will be used
667 if you invoke PMake with any of the compatibility flags (\c
668 .B \-V ,
669 .B \-B
670 or
671 .B \-M .
672 The
673 .B \-V
674 flag alters just the variable expansion).
675 .Ix 0 ref flags -V
676 .Ix 0 ref flags -B
677 .Ix 0 ref flags -M
678 .Ix 0 ref compatibility
679 .LP
680 .Ix 0 ref variable expansion
681 There are two different times at which variable expansion occurs:
682 When parsing a dependency line, the expansion occurs immediately
683 upon reading the line. If any variable used on a dependency line is
684 undefined, PMake will print a message and exit.
685 Variables in shell commands are expanded when the command is
686 executed.
687 Variables used inside another variable are expanded whenever the outer
688 variable is expanded (the expansion of an inner variable has no effect
689 on the outer variable. I.e. if the outer variable is used on a dependency
690 line and in a shell command, and the inner variable changes value
691 between when the dependency line is read and the shell command is
692 executed, two different values will be substituted for the outer
693 variable).
694 .Ix 0 def variable types
695 .LP
696 Variables come in four flavors, though they are all expanded the same
697 and all look about the same. They are (in order of expanding scope):
698 .RS
699 .IP \(bu 2
700 Local variables.
701 .Ix 0 ref variable local
702 .IP \(bu 2
703 Command-line variables.
704 .Ix 0 ref variable command-line
705 .IP \(bu 2
706 Global variables.
707 .Ix 0 ref variable global
708 .IP \(bu 2
709 Environment variables.
710 .Ix 0 ref variable environment
711 .RE
712 .LP
713 The classification of variables doesn't matter much, except that the
714 classes are searched from the top (local) to the bottom (environment)
715 when looking up a variable. The first one found wins.
716 .xH 3 Local Variables
717 .LP
718 .Ix 0 def variable local
719 Each target can have as many as seven local variables. These are
720 variables that are only ``visible'' within that target's shell script
721 and contain such things as the target's name, all of its sources (from
722 all its dependency lines), those sources that were out-of-date, etc.
723 Four local variables are defined for all targets. They are:
724 .RS
725 .IP ".TARGET"
726 .Ix 0 def variable local .TARGET
727 .Ix 0 def .TARGET
728 The name of the target.
729 .IP ".OODATE"
730 .Ix 0 def variable local .OODATE
731 .Ix 0 def .OODATE
732 The list of the sources for the target that were considered out-of-date.
733 The order in the list is not guaranteed to be the same as the order in
734 which the dependencies were given.
735 .IP ".ALLSRC"
736 .Ix 0 def variable local .ALLSRC
737 .Ix 0 def .ALLSRC
738 The list of all sources for this target in the order in which they
739 were given.
740 .IP ".PREFIX"
741 .Ix 0 def variable local .PREFIX
742 .Ix 0 def .PREFIX
743 The target without its suffix and without any leading path. E.g. for
744 the target
745 .CW ../../lib/compat/fsRead.c ,
746 this variable would contain
747 .CW fsRead .
748 .RE
749 .LP
750 Three other local variables are set only for certain targets under
751 special circumstances. These are the ``.IMPSRC,''
752 .Ix 0 ref variable local .IMPSRC
753 .Ix 0 ref .IMPSRC
754 ``.ARCHIVE,''
755 .Ix 0 ref variable local .ARCHIVE
756 .Ix 0 ref .ARCHIVE
757 and ``.MEMBER''
758 .Ix 0 ref variable local .MEMBER
759 .Ix 0 ref .MEMBER
760 variables. When they are set and how they are used is described later.
761 .LP
762 Four of these variables may be used in sources as well as in shell
763 scripts.
764 .Ix 0 def "dynamic source"
765 .Ix 0 def source dynamic
766 These are ``.TARGET'', ``.PREFIX'', ``.ARCHIVE'' and ``.MEMBER''. The
767 variables in the sources are expanded once for each target on the
768 dependency line, providing what is known as a ``dynamic source,''
769 .Rd 0
770 allowing you to specify several dependency lines at once. For example,
771 .DS
772 $(OBJS)         : $(.PREFIX).c
773 .DE
774 will create a dependency between each object file and its
775 corresponding C source file.
776 .xH 3 Command-line Variables
777 .LP
778 .Ix 0 def variable command-line
779 Command-line variables are set when PMake is first invoked by giving a
780 variable assignment as one of the arguments. For example,
781 .DS
782 pmake "CFLAGS = -I/sprite/src/lib/libc -O"
783 .DE
784 would make 
785 .CW CFLAGS
786 be a command-line variable with the given value. Any assignments to
787 .CW CFLAGS
788 in the makefile will have no effect, because once it
789 is set, there is (almost) nothing you can do to change a command-line
790 variable (the search order, you see). Command-line variables may be
791 set using any of the four assignment operators, though only
792 .CW =
793 and
794 .CW ?=
795 behave as you would expect them to, mostly because assignments to
796 command-line variables are performed before the makefile is read, thus
797 the values set in the makefile are unavailable at the time.
798 .CW +=
799 .Ix 0 ref +=
800 .Ix 0 ref variable assignment appended
801 is the same as
802 .CW = ,
803 because the old value of the variable is sought only in the scope in
804 which the assignment is taking place (for reasons of efficiency that I
805 won't get into here).
806 .CW :=
807 and
808 .CW ?=
809 .Ix 0 ref :=
810 .Ix 0 ref ?=
811 .Ix 0 ref variable assignment expanded
812 .Ix 0 ref variable assignment conditional
813 will work if the only variables used are in the environment.
814 .CW !=
815 is sort of pointless to use from the command line, since the same
816 effect can no doubt be accomplished using the shell's own command
817 substitution mechanisms (backquotes and all that).
818 .xH 3 Global Variables
819 .LP
820 .Ix 0 def variable global
821 Global variables are those set or appended-to in the makefile.
822 There are two classes of global variables: those you set and those PMake sets.
823 As I said before, the ones you set can have any name you want them to have,
824 except they may not contain a colon or an exclamation point.
825 The variables PMake sets (almost) always begin with a
826 period and always contain upper-case letters, only. The variables are
827 as follows:
828 .RS
829 .IP .PMAKE
830 .Ix 0 def variable global .PMAKE
831 .Ix 0 def .PMAKE
832 .Ix 0 def variable global MAKE
833 .Ix 0 def MAKE
834 The name by which PMake was invoked is stored in this variable. For
835 compatibility, the name is also stored in the MAKE variable.
836 .IP .MAKEFLAGS
837 .Ix 0 def variable global .MAKEFLAGS
838 .Ix 0 def .MAKEFLAGS variable
839 .Ix 0 def variable global MFLAGS
840 .Ix 0 def MFLAGS
841 All the relevant flags with which PMake was invoked. This does not
842 include such things as
843 .B \-f
844 or variable assignments. Again for compatibility, this value is stored
845 in the MFLAGS variable as well.
846 .RE
847 .LP
848 Two other variables, ``.INCLUDES'' and ``.LIBS,'' are covered in the
849 section on special targets in chapter 3.
850 .Ix 0 ref variable global .INCLUDES
851 .Ix 0 ref variable global .LIBS
852 .LP
853 Global variables may be deleted using lines of the form:
854 .Ix 0 def #undef
855 .Ix 0 def variable deletion
856 .DS
857 #undef \fIvariable\fP
858 .DE
859 The
860 .CW # ' `
861 must be the first character on the line. Note that this may only be
862 done on global variables.
863 .xH 3 Environment Variables
864 .LP
865 .Ix 0 def variable environment
866 Environment variables are passed by the shell that invoked PMake and
867 are given by PMake to each shell it invokes. They are expanded like
868 any other variable, but they cannot be altered in any way.
869 .LP
870 One special environment variable,
871 .CW PMAKE ,
872 .Ix 0 def variable environment PMAKE
873 is examined by PMake for command-line flags, variable assignments,
874 etc., it should always use. This variable is examined before the
875 actual arguments to PMake are. In addition, all flags given to PMake,
876 either through the
877 .CW PMAKE
878 variable or on the command line, are placed in this environment
879 variable and exported to each shell PMake executes. Thus recursive
880 invocations of PMake automatically receive the same flags as the
881 top-most one.
882 .LP
883 Using all these variables, you can compress the sample makefile even more:
884 .DS
885 OBJS            = a.o b.o c.o
886 program         : $(OBJS)
887         cc $(.ALLSRC) \-o $(.TARGET)
888 $(OBJS)         : defs.h
889 a.o             : a.c
890         cc \-c a.c
891 b.o             : b.c
892         cc \-c b.c
893 c.o             : c.c
894         cc \-c c.c
895 .DE
896 .Ix 0 ref variable local .ALLSRC
897 .Ix 0 ref .ALLSRC
898 .Ix 0 ref variable local .TARGET
899 .Ix 0 ref .TARGET
900 .Rd 3
901 .xH 2 Comments
902 .LP
903 .Ix 0 def comments
904 Comments in a makefile start with a `#' character and extend to the
905 end of the line. They may appear
906 anywhere you want them, except in a shell command (though the shell
907 will treat it as a comment, too). If, for some reason, you need to use the `#'
908 in a variable or on a dependency line, put a backslash in front of it.
909 PMake will compress the two into a single `#' (Note: this isn't true
910 if PMake is operating in full-compatibility mode).
911 .Ix 0 ref flags -M
912 .Ix 0 ref compatibility
913 .xH 2 Parallelism
914 .No
915 .LP
916 PMake was specifically designed to re-create several targets at once,
917 when possible. You do not have to do anything special to cause this to
918 happen (unless PMake was configured to not act in parallel, in which
919 case you will have to make use of the
920 .B \-L
921 and
922 .B \-J
923 flags (see below)),
924 .Ix 0 ref flags -L
925 .Ix 0 ref flags -J
926 but you do have to be careful at times.
927 .LP
928 There are several problems you are likely to encounter. One is
929 that some makefiles (and programs) are written in such a way that it is
930 impossible for two targets to be made at once. The program
931 .CW xstr ,
932 for example,
933 always modifies the files
934 .CW strings
935 and
936 .CW x.c .
937 There is no way to change it. Thus you cannot run two of them at once
938 without something being trashed. Similarly, if you have commands
939 in the makefile that always send output to the same file, you will not
940 be able to make more than one target at once unless you change the
941 file you use. You can, for instance, add a
942 .CW $$$$
943 to the end of the file name to tack on the process ID of the shell
944 executing the command (each
945 .CW $$
946 expands to a single
947 .CW $ ,
948 thus giving you the shell variable
949 .CW $$ ).
950 Since only one shell is used for all the
951 commands, you'll get the same file name for each command in the
952 script.
953 .LP
954 The other problem comes from improperly-specified dependencies that
955 worked in Make because of its sequential, depth-first way of examining
956 them. While I don't want to go into depth on how PMake
957 works (look in chapter 4 if you're interested), I will warn you that
958 files in two different ``levels'' of the dependency tree may be
959 examined in a different order in PMake than they were in Make. For
960 example, given the makefile
961 .DS
962 a               : b c
963 b               : d
964 .DE
965 PMake will examine the targets in the order
966 .CW c ,
967 .CW d ,
968 .CW b ,
969 .CW a .
970 If the makefile's author expected PMake to abort before making
971 .CW c
972 if an error occurred while making
973 .CW b ,
974 or if
975 .CW b
976 needed to exist before
977 .CW c
978 was made,
979 s/he will be sorely disappointed. The dependencies are
980 incomplete, since in both these cases,
981 .CW c
982 would depend on
983 .CW b .
984 So watch out.
985 .LP
986 Another problem you may face is that, while PMake is set up to handle the
987 output from multiple jobs in a graceful fashion, the same is not so for input.
988 It has no way to regulate input to different jobs,
989 so if you use the redirection from
990 .CW /dev/tty
991 I mentioned earlier, you must be careful not to run two of the jobs at once.
992 .xH 2 Writing and Debugging a Makefile
993 .LP
994 Now you know most of what's in a makefile, what do you do next? There
995 are two choices: (1) use one of the uncommonly-available makefile
996 generators or (2) write your own makefile (I leave out the third choice of
997 ignoring PMake and doing everything by hand as being beyond the bounds
998 of common sense).
999 .LP
1000 When faced with the writing of a makefile, it is usually best to start
1001 from first principles: just what
1002 .I are
1003 you trying to do? What do you want the makefile finally to produce?
1004 .LP
1005 To begin with a somewhat traditional example, let's say you need to
1006 write a makefile to create a program,
1007 .CW expr ,
1008 that takes standard infix expressions and converts them to prefix form (for
1009 no readily apparent reason). You've got three source files, in C, that
1010 make up the program:
1011 .CW main.c ,
1012 .CW parse.c ,
1013 and
1014 .CW output.c .
1015 Harking back to my pithy advice about dependency lines, you write the
1016 first line of the file:
1017 .DS
1018 expr            : main.o parse.o output.o
1019 .DE
1020 because you remember
1021 .CW expr
1022 is made from
1023 .CW .o
1024 files, not
1025 .CW .c
1026 files. Similarly for the
1027 .CW .o
1028 files you produce the lines:
1029 .DS
1030 main.o          : main.c
1031 parse.o         : parse.c
1032 output.o        : output.c
1033 main.o parse.o output.o : defs.h
1034 .DE
1035 .LP
1036 Great. You've now got the dependencies specified. What you need now is
1037 commands. These commands, remember, must produce the target on the
1038 dependency line, usually by using the sources you've listed.
1039 You remember about local variables? Good, so it should come
1040 to you as no surprise when you write
1041 .DS
1042 expr            : main.o parse.o output.o
1043         cc -o $(.TARGET) $(.ALLSRC)
1044 .DE
1045 Why use the variables? If your program grows to produce postfix
1046 expressions too (which, of course, requires a name change or two), it
1047 is one fewer place you have to change the file. You cannot do this for
1048 the object files, however, because they depend on their corresponding
1049 source files
1050 .I and
1051 .CW defs.h ,
1052 thus if you said
1053 .DS
1054         cc -c $(.ALLSRC)
1055 .DE
1056 you'd get (for
1057 .CW main.o ):
1058 .DS
1059         cc -c main.c defs.h
1060 .DE
1061 which is wrong. So you round out the makefile with these lines:
1062 .DS
1063 main.o          : main.c
1064         cc -c main.c
1065 parse.o         : parse.c
1066         cc -c parse.c
1067 output.o        : output.c
1068         cc -c output.c
1069 .DE
1070 .LP
1071 The makefile is now complete and will, in fact, create the program you
1072 want it to without unnecessary compilations or excessive typing on
1073 your part. There are two things wrong with it, however (aside from it
1074 being altogether too long, something I'll address in chapter 3):
1075 .IP 1)
1076 The string
1077 .CW "main.o parse.o output.o" '' ``
1078 is repeated twice, necessitating two changes when you add postfix
1079 (you were planning on that, weren't you?). This is in direct violation
1080 of de Boor's First Rule of writing makefiles:
1081 .QP
1082 .I
1083 Anything that needs to be written more than once
1084 should be placed in a variable.
1085 .IP "\&"
1086 I cannot emphasize this enough as being very important to the
1087 maintenance of a makefile and its program.
1088 .IP 2)
1089 There is no way to alter the way compilations are performed short of
1090 editing the makefile and making the change in all places. This is evil
1091 and violates de Boor's Second Rule, which follows directly from the
1092 first:
1093 .QP
1094 .I
1095 Any flags or programs used inside a makefile should be placed in a variable so
1096 they may be changed, temporarily or permanently, with the greatest ease.
1097 .LP
1098 The makefile should more properly read:
1099 .DS
1100 OBJS            = main.o parse.o output.o
1101 expr            : $(OBJS)
1102         $(CC) $(CFLAGS) -o $(.TARGET) $(.ALLSRC)
1103 main.o          : main.c
1104         $(CC) $(CFLAGS) -c main.c
1105 parse.o         : parse.c
1106         $(CC) $(CFLAGS) -c parse.c
1107 output.o        : output.c
1108         $(CC) $(CFLAGS) -c output.c
1109 $(OBJS)         : defs.h
1110 .DE
1111 Alternatively, if you like the idea of dynamic sources mentioned in
1112 section 2.3.1,
1113 .Rm 0 2.3.1
1114 .Rd 4
1115 .Ix 0 ref "dynamic source"
1116 .Ix 0 ref source dynamic
1117 you could write it like this:
1118 .DS
1119 OBJS            = main.o parse.o output.o
1120 expr            : $(OBJS)
1121         $(CC) $(CFLAGS) -o $(.TARGET) $(.ALLSRC)
1122 $(OBJS)         : $(.PREFIX).c defs.h
1123         $(CC) $(CFLAGS) -c $(.PREFIX).c
1124 .DE
1125 These two rules and examples lead to de Boor's First Corollary:
1126 .QP
1127 .I
1128 Variables are your friends.
1129 .LP
1130 Once you've written the makefile comes the sometimes-difficult task of
1131 .Ix 0 ref debugging
1132 making sure the darn thing works. Your most helpful tool to make sure
1133 the makefile is at least syntactically correct is the
1134 .B \-n
1135 .Ix 0 ref flags -n
1136 flag, which allows you to see if PMake will choke on the makefile. The
1137 second thing the
1138 .B \-n
1139 flag lets you do is see what PMake would do without it actually doing
1140 it, thus you can make sure the right commands would be executed were
1141 you to give PMake its head.
1142 .LP
1143 When you find your makefile isn't behaving as you hoped, the first
1144 question that comes to mind (after ``What time is it, anyway?'') is
1145 ``Why not?'' In answering this, two flags will serve you well:
1146 .CW "-d m" '' ``
1147 .Ix 0 ref flags -d
1148 and
1149 .CW "-p 2" .'' ``
1150 .Ix 0 ref flags -p
1151 The first causes PMake to tell you as it examines each target in the
1152 makefile and indicate why it is deciding whatever it is deciding. You
1153 can then use the information printed for other targets to see where
1154 you went wrong. The
1155 .CW "-p 2" '' ``
1156 flag makes PMake print out its internal state when it is done,
1157 allowing you to see that you forgot to make that one chapter depend on
1158 that file of macros you just got a new version of. The output from
1159 .CW "-p 2" '' ``
1160 is intended to resemble closely a real makefile, but with additional
1161 information provided and with variables expanded in those commands
1162 PMake actually printed or executed.
1163 .LP
1164 Something to be especially careful about is circular dependencies.
1165 .Ix 0 def dependency circular
1166 E.g.
1167 .DS
1168 a               : b
1169 b               : c d
1170 d               : a
1171 .DE
1172 In this case, because of how PMake works,
1173 .CW c
1174 is the only thing PMake will examine, because
1175 .CW d
1176 and
1177 .CW a
1178 will effectively fall off the edge of the universe, making it
1179 impossible to examine
1180 .CW b
1181 (or them, for that matter).
1182 PMake will tell you (if run in its normal mode) all the targets
1183 involved in any cycle it looked at (i.e. if you have two cycles in the
1184 graph (naughty, naughty), but only try to make a target in one of
1185 them, PMake will only tell you about that one. You'll have to try to
1186 make the other to find the second cycle). When run as Make, it will
1187 only print the first target in the cycle.
1188 .xH 2 Invoking PMake
1189 .LP
1190 .Ix 0 ref flags
1191 .Ix 0 ref arguments
1192 .Ix 0 ref usage
1193 PMake comes with a wide variety of flags to choose from.
1194 They may appear in any order, interspersed with command-line variable
1195 assignments and targets to create.
1196 The flags are as follows:
1197 .IP "\fB\-d\fP \fIwhat\fP"
1198 .Ix 0 def flags -d
1199 .Ix 0 ref debugging
1200 This causes PMake to spew out debugging information that
1201 may prove useful to you. If you can't
1202 figure out why PMake is doing what it's doing, you might try using
1203 this flag. The
1204 .I what
1205 parameter is a string of single characters that tell PMake what
1206 aspects you are interested in. Most of what I describe will make
1207 little sense to you, unless you've dealt with Make before. Just
1208 remember where this table is and come back to it as you read on.
1209 The characters and the information they produce are as follows:
1210 .RS
1211 .IP a
1212 Archive searching and caching.
1213 .IP c
1214 Conditional evaluation.
1215 .IP d
1216 The searching and caching of directories.
1217 .IP j
1218 Various snippets of information related to the running of the multiple
1219 shells. Not particularly interesting.
1220 .IP m
1221 The making of each target: what target is being examined; when it was
1222 last modified; whether it is out-of-date; etc.
1223 .IP p
1224 Makefile parsing.
1225 .IP r
1226 Remote execution.
1227 .IP s
1228 The application of suffix-transformation rules. (See chapter 3)
1229 .IP t
1230 The maintenance of the list of targets.
1231 .IP v
1232 Variable assignment.
1233 .RE
1234 .IP "\&"
1235 Of these all, the
1236 .CW m
1237 and
1238 .CW s
1239 letters will be most useful to you.
1240 If the
1241 .B \-d
1242 is the final argument or the argument from which it would get these
1243 key letters (see below for a note about which argument would be used)
1244 begins with a
1245 .B \- ,
1246 all of these debugging flags will be set, resulting in massive amounts
1247 of output.
1248 .IP "\fB\-f\fP \fImakefile\fP"
1249 .Ix 0 def flags -f
1250 Specify a makefile to read different from the standard makefiles
1251 .CW Makefile "\&" (
1252 or
1253 .CW makefile ).
1254 .Ix 0 ref makefile default
1255 .Ix 0 ref makefile other
1256 If
1257 .I makefile
1258 is ``\-'', PMake uses the standard input. This is useful for making
1259 quick and dirty makefiles.\|.\|.
1260 .Ix 0 ref makefile "quick and dirty"
1261 .IP \fB\-h\fP
1262 .Ix 0 def flags -h
1263 Prints out a summary of the various flags PMake accepts. It can also
1264 be used to find out what level of concurrency was compiled into the
1265 version of PMake you are using (look at
1266 .B \-J
1267 and
1268 .B \-L )
1269 and various other information on how PMake was configured.
1270 .Ix 0 ref configuration
1271 .Ix 0 ref makefile system
1272 .IP \fB\-i\fP
1273 .Ix 0 def flags -i
1274 If you give this flag, PMake will ignore non-zero status returned
1275 by any of its shells. It's like placing a `\-' before all the commands
1276 in the makefile.
1277 .IP \fB\-k\fP
1278 .Ix 0 def flags -k
1279 This is similar to
1280 .B \-i
1281 in that it allows PMake to continue when it sees an error, but unlike
1282 .B \-i ,
1283 where PMake continues blithely as if nothing went wrong,
1284 .B \-k
1285 causes it to recognize the error and only continue work on those
1286 things that don't depend on the target, either directly or indirectly (through
1287 depending on something that depends on it), whose creation returned the error.
1288 The `k' is for ``keep going''.\|.\|.
1289 .Ix 0 ref target
1290 .IP \fB\-l\fP
1291 .Ix 0 def flags -l
1292 PMake has the ability to lock a directory against other
1293 people executing it in the same directory (by means of a file called
1294 ``LOCK.make'' that it creates and checks for in the directory). This
1295 is a Good Thing because two people doing the same thing in the same place
1296 can be disastrous for the final product (too many cooks and all that).
1297 Whether this locking is the default is up to your system
1298 administrator. If locking is on,
1299 .B \-l
1300 will turn it off, and vice versa. Note that this locking will not
1301 prevent \fIyou\fP from invoking PMake twice in the same place \*- if
1302 you own the lock file, PMake will warn you about it but continue to execute.
1303 .IP "\fB\-m\fP \fIdirectory\fP"
1304 .Ix 0 def flags -m
1305 Tells PMake another place to search for included makefiles via the <...>
1306 style.  Several
1307 .B \-m
1308 options can be given to form a search path.  If this construct is used the
1309 default system makefile search path is completely overridden.
1310 To be explained in chapter 3, section 3.2.
1311 .Rm 2 3.2
1312 .IP \fB\-n\fP
1313 .Ix 0 def flags -n
1314 This flag tells PMake not to execute the commands needed to update the
1315 out-of-date targets in the makefile. Rather, PMake will simply print
1316 the commands it would have executed and exit. This is particularly
1317 useful for checking the correctness of a makefile. If PMake doesn't do
1318 what you expect it to, it's a good chance the makefile is wrong.
1319 .IP "\fB\-p\fP \fInumber\fP"
1320 .Ix 0 def flags -p
1321 .Ix 0 ref debugging
1322 This causes PMake to print its input in a reasonable form, though
1323 not necessarily one that would make immediate sense to anyone but me. The
1324 .I number
1325 is a bitwise-or of 1 and 2 where 1 means it should print the input
1326 before doing any processing and 2 says it should print it after
1327 everything has been re-created. Thus
1328 .CW "\-p 3"
1329 would print it twice\*-once before processing and once after (you
1330 might find the difference between the two interesting). This is mostly
1331 useful to me, but you may find it informative in some bizarre circumstances.
1332 .IP \fB\-q\fP
1333 .Ix 0 def flags -q
1334 If you give PMake this flag, it will not try to re-create anything. It
1335 will just see if anything is out-of-date and exit non-zero if so.
1336 .IP \fB\-r\fP
1337 .Ix 0 def flags -r
1338 When PMake starts up, it reads a default makefile that tells it what
1339 sort of system it's on and gives it some idea of what to do if you
1340 don't tell it anything. I'll tell you about it in chapter 3. If you
1341 give this flag, PMake won't read the default makefile.
1342 .IP \fB\-s\fP
1343 .Ix 0 def flags -s
1344 This causes PMake to not print commands before they're executed. It
1345 is the equivalent of putting an `@' before every command in the
1346 makefile.
1347 .IP \fB\-t\fP
1348 .Ix 0 def flags -t
1349 Rather than try to re-create a target, PMake will simply ``touch'' it
1350 so as to make it appear up-to-date. If the target didn't exist before,
1351 it will when PMake finishes, but if the target did exist, it will
1352 appear to have been updated.
1353 .IP \fB\-v\fP
1354 .Ix 0 def flags -v
1355 This is a mixed-compatibility flag intended to mimic the System V
1356 version of Make. It is the same as giving
1357 .B \-B ,
1358 and
1359 .B \-V
1360 as well as turning off directory locking. Targets can still be created
1361 in parallel, however. This is the mode PMake will enter if it is
1362 invoked either as
1363 .CW smake '' ``
1364 or
1365 .CW vmake ''. ``
1366 .IP \fB\-x\fP
1367 .Ix 0 def flags -x
1368 This tells PMake it's ok to export jobs to other machines, if they're
1369 available. It is used when running in Make mode, as exporting in this
1370 mode tends to make things run slower than if the commands were just
1371 executed locally.
1372 .IP \fB\-B\fP
1373 .Ix 0 ref compatibility
1374 .Ix 0 def flags -B
1375 Forces PMake to be as backwards-compatible with Make as possible while
1376 still being itself.
1377 This includes:
1378 .RS
1379 .IP \(bu 2
1380 Executing one shell per shell command
1381 .IP \(bu 2
1382 Expanding anything that looks even vaguely like a variable, with the
1383 empty string replacing any variable PMake doesn't know.
1384 .IP \(bu 2
1385 Refusing to allow you to escape a `#' with a backslash.
1386 .IP \(bu 2
1387 Permitting undefined variables on dependency lines and conditionals
1388 (see below). Normally this causes PMake to abort.
1389 .RE
1390 .IP \fB\-C\fP
1391 .Ix 0 def flags -C
1392 This nullifies any and all compatibility mode flags you may have given
1393 or implied up to the time the
1394 .B \-C
1395 is encountered. It is useful mostly in a makefile that you wrote for PMake
1396 to avoid bad things happening when someone runs PMake as
1397 .CW make '' ``
1398 or has things set in the environment that tell it to be compatible.
1399 .B \-C
1400 is
1401 .I not
1402 placed in the
1403 .CW PMAKE
1404 environment variable or the
1405 .CW .MAKEFLAGS
1406 or
1407 .CW MFLAGS
1408 global variables.
1409 .Ix 0 ref variable environment PMAKE
1410 .Ix 0 ref variable global .MAKEFLAGS
1411 .Ix 0 ref variable global MFLAGS
1412 .Ix 0 ref .MAKEFLAGS variable
1413 .Ix 0 ref MFLAGS
1414 .IP "\fB\-D\fP \fIvariable\fP"
1415 .Ix 0 def flags -D
1416 Allows you to define a variable to have 
1417 .CW 1 '' ``
1418 as its value.  The variable is a global variable, not a command-line
1419 variable. This is useful mostly for people who are used to the C
1420 compiler arguments and those using conditionals, which I'll get into
1421 in section 4.3
1422 .Rm 1 4.3
1423 .IP "\fB\-I\fP \fIdirectory\fP"
1424 .Ix 0 def flags -I
1425 Tells PMake another place to search for included makefiles. Yet
1426 another thing to be explained in chapter 3 (section 3.2, to be
1427 precise).
1428 .Rm 2 3.2
1429 .IP "\fB\-J\fP \fInumber\fP"
1430 .Ix 0 def flags -J
1431 Gives the absolute maximum number of targets to create at once on both
1432 local and remote machines.
1433 .IP "\fB\-L\fP \fInumber\fP"
1434 .Ix 0 def flags -L
1435 This specifies the maximum number of targets to create on the local
1436 machine at once. This may be 0, though you should be wary of doing
1437 this, as PMake may hang until a remote machine becomes available, if
1438 one is not available when it is started.
1439 .IP \fB\-M\fP
1440 .Ix 0 ref compatibility
1441 .Ix 0 def flags -M
1442 This is the flag that provides absolute, complete, full compatibility
1443 with Make. It still allows you to use all but a few of the features of
1444 PMake, but it is non-parallel. This is the mode PMake enters if you
1445 call it
1446 .CW make .'' ``
1447 .IP \fB\-P\fP
1448 .Ix 0 def flags -P
1449 .Ix 0 ref "output control"
1450 When creating targets in parallel, several shells are executing at
1451 once, each wanting to write its own two cent's-worth to the screen.
1452 This output must be captured by PMake in some way in order to prevent
1453 the screen from being filled with garbage even more indecipherable
1454 than you usually see. PMake has two ways of doing this, one of which
1455 provides for much cleaner output and a clear separation between the
1456 output of different jobs, the other of which provides a more immediate
1457 response so one can tell what is really happening. The former is done
1458 by notifying you when the creation of a target starts, capturing the
1459 output and transferring it to the screen all at once when the job
1460 finishes. The latter is done by catching the output of the shell (and
1461 its children) and buffering it until an entire line is received, then
1462 printing that line preceded by an indication of which job produced
1463 the output. Since I prefer this second method, it is the one used by
1464 default. The first method will be used if you give the
1465 .B \-P
1466 flag to PMake.
1467 .IP \fB\-V\fP
1468 .Ix 0 def flags -V
1469 As mentioned before, the
1470 .B \-V
1471 flag tells PMake to use Make's style of expanding variables,
1472 substituting the empty string for any variable it doesn't know.
1473 .IP \fB\-W\fP
1474 .Ix 0 def flags -W
1475 There are several times when PMake will print a message at you that is
1476 only a warning, i.e. it can continue to work in spite of your having
1477 done something silly (such as forgotten a leading tab for a shell
1478 command). Sometimes you are well aware of silly things you have done
1479 and would like PMake to stop bothering you. This flag tells it to shut
1480 up about anything non-fatal.
1481 .IP \fB\-X\fP
1482 .Ix 0 def flags -X
1483 This flag causes PMake to not attempt to export any jobs to another
1484 machine.
1485 .LP
1486 Several flags may follow a single `\-'. Those flags that require
1487 arguments take them from successive parameters. E.g.
1488 .DS
1489 pmake -fDnI server.mk DEBUG /chip2/X/server/include
1490 .DE
1491 will cause PMake to read
1492 .CW server.mk
1493 as the input makefile, define the variable
1494 .CW DEBUG
1495 as a global variable and look for included makefiles in the directory
1496 .CW /chip2/X/server/include .
1497 .xH 2 Summary
1498 .LP
1499 A makefile is made of four types of lines:
1500 .RS
1501 .IP \(bu 2
1502 Dependency lines
1503 .IP \(bu 2
1504 Creation commands
1505 .IP \(bu 2
1506 Variable assignments
1507 .IP \(bu 2
1508 Comments, include statements and conditional directives
1509 .RE
1510 .LP
1511 A dependency line is a list of one or more targets, an operator
1512 .CW : ', (`
1513 .CW :: ', `
1514 or
1515 .CW ! '), `
1516 and a list of zero or more sources. Sources may contain wildcards and
1517 certain local variables.
1518 .LP
1519 A creation command is a regular shell command preceded by a tab. In
1520 addition, if the first two characters after the tab (and other
1521 whitespace) are a combination of
1522 .CW @ ' `
1523 or
1524 .CW - ', `
1525 PMake will cause the command to not be printed (if the character is
1526 .CW @ ') `
1527 or errors from it to be ignored (if
1528 .CW - '). `
1529 A blank line, dependency line or variable assignment terminates a
1530 creation script. There may be only one creation script for each target
1531 with a
1532 .CW : ' `
1533 or
1534 .CW ! ' `
1535 operator.
1536 .LP
1537 Variables are places to store text. They may be unconditionally
1538 assigned-to using the
1539 .CW = ' `
1540 .Ix 0 ref =
1541 .Ix 0 ref variable assignment
1542 operator, appended-to using the
1543 .CW += ' `
1544 .Ix 0 ref +=
1545 .Ix 0 ref variable assignment appended
1546 operator, conditionally (if the variable is undefined) assigned-to
1547 with the
1548 .CW ?= ' `
1549 .Ix 0 ref ?=
1550 .Ix 0 ref variable assignment conditional
1551 operator, and assigned-to with variable expansion with the
1552 .CW := ' `
1553 .Ix 0 ref :=
1554 .Ix 0 ref variable assignment expanded
1555 operator. The output of a shell command may be assigned to a variable
1556 using the
1557 .CW != ' `
1558 .Ix 0 ref !=
1559 .Ix 0 ref variable assignment shell-output
1560 operator.  Variables may be expanded (their value inserted) by enclosing
1561 their name in parentheses or curly braces, preceded by a dollar sign.
1562 A dollar sign may be escaped with another dollar sign. Variables are
1563 not expanded if PMake doesn't know about them. There are seven local
1564 variables:
1565 .CW .TARGET ,
1566 .CW .ALLSRC ,
1567 .CW .OODATE ,
1568 .CW .PREFIX ,
1569 .CW .IMPSRC ,
1570 .CW .ARCHIVE ,
1571 and
1572 .CW .MEMBER .
1573 Four of them
1574 .CW .TARGET , (
1575 .CW .PREFIX ,
1576 .CW .ARCHIVE ,
1577 and
1578 .CW .MEMBER )
1579 may be used to specify ``dynamic sources.''
1580 .Ix 0 ref "dynamic source"
1581 .Ix 0 ref source dynamic
1582 Variables are good. Know them. Love them. Live them.
1583 .LP
1584 Debugging of makefiles is best accomplished using the
1585 .B \-n ,
1586 .B "\-d m" ,
1587 and
1588 .B "\-p 2"
1589 flags.
1590 .xH 2 Exercises
1591 .ce
1592 \s+4\fBTBA\fP\s0
1593 .xH 1 Short-cuts and Other Nice Things
1594 .LP
1595 Based on what I've told you so far, you may have gotten the impression
1596 that PMake is just a way of storing away commands and making sure you
1597 don't forget to compile something. Good. That's just what it is.
1598 However, the ways I've described have been inelegant, at best, and
1599 painful, at worst.
1600 This chapter contains things that make the
1601 writing of makefiles easier and the makefiles themselves shorter and
1602 easier to modify (and, occasionally, simpler). In this chapter, I
1603 assume you are somewhat more
1604 familiar with Sprite (or UNIX, if that's what you're using) than I did
1605 in chapter 2, just so you're on your toes.
1606 So without further ado...
1607 .xH 2 Transformation Rules
1608 .LP
1609 As you know, a file's name consists of two parts: a base name, which
1610 gives some hint as to the contents of the file, and a suffix, which
1611 usually indicates the format of the file.
1612 Over the years, as
1613 .UX
1614 has developed,
1615 naming conventions, with regard to suffixes, have also developed that have
1616 become almost as incontrovertible as Law. E.g. a file ending in
1617 .CW .c
1618 is assumed to contain C source code; one with a
1619 .CW .o
1620 suffix is assumed to be a compiled, relocatable object file that may
1621 be linked into any program; a file with a
1622 .CW .ms
1623 suffix is usually a text file to be processed by Troff with the \-ms
1624 macro package, and so on.
1625 One of the best aspects of both Make and PMake comes from their
1626 understanding of how the suffix of a file pertains to its contents and
1627 their ability to do things with a file based solely on its suffix. This
1628 ability comes from something known as a transformation rule. A
1629 transformation rule specifies how to change a file with one suffix
1630 into a file with another suffix.
1631 .LP
1632 A transformation rule looks much like a dependency line, except the
1633 target is made of two known suffixes stuck together. Suffixes are made
1634 known to PMake by placing them as sources on a dependency line whose
1635 target is the special target
1636 .CW .SUFFIXES .
1637 E.g.
1638 .DS
1639 \&.SUFFIXES       : .o .c
1640 \&.c.o            :
1641         $(CC) $(CFLAGS) -c $(.IMPSRC)
1642 .DE
1643 The creation script attached to the target is used to transform a file with
1644 the first suffix (in this case,
1645 .CW .c )
1646 into a file with the second suffix (here,
1647 .CW .o ).
1648 In addition, the target inherits whatever attributes have been applied
1649 to the transformation rule.
1650 The simple rule given above says that to transform a C source file
1651 into an object file, you compile it using
1652 .CW cc
1653 with the
1654 .CW \-c
1655 flag.
1656 This rule is taken straight from the system makefile. Many
1657 transformation rules (and suffixes) are defined there, and I refer you
1658 to it for more examples (type
1659 .CW "pmake -h" '' ``
1660 to find out where it is).
1661 .LP
1662 There are several things to note about the transformation rule given
1663 above:
1664 .RS
1665 .IP 1)
1666 The
1667 .CW .IMPSRC 
1668 variable.
1669 .Ix 0 def variable local .IMPSRC
1670 .Ix 0 def .IMPSRC
1671 This variable is set to the ``implied source'' (the file from which
1672 the target is being created; the one with the first suffix), which, in this
1673 case, is the .c file.
1674 .IP 2)
1675 The
1676 .CW CFLAGS
1677 variable. Almost all of the transformation rules in the system
1678 makefile are set up using variables that you can alter in your
1679 makefile to tailor the rule to your needs. In this case, if you want
1680 all your C files to be compiled with the
1681 .B \-g
1682 flag, to provide information for
1683 .CW dbx ,
1684 you would set the
1685 .CW CFLAGS
1686 variable to contain
1687 .CW -g
1688 .CW "CFLAGS = -g" '') (``
1689 and PMake would take care of the rest.
1690 .RE
1691 .LP
1692 To give you a quick example, the makefile in 2.3.4 
1693 .Rm 3 2.3.4
1694 could be changed to this:
1695 .DS
1696 OBJS            = a.o b.o c.o
1697 program         : $(OBJS)
1698         $(CC) -o $(.TARGET) $(.ALLSRC)
1699 $(OBJS)         : defs.h
1700 .DE
1701 The transformation rule I gave above takes the place of the 6 lines\**
1702 .FS
1703 This is also somewhat cleaner, I think, than the dynamic source
1704 solution presented in 2.6
1705 .FE
1706 .Rm 4 2.6
1707 .DS
1708 a.o             : a.c
1709         cc -c a.c
1710 b.o             : b.c
1711         cc -c b.c
1712 c.o             : c.c
1713         cc -c c.c
1714 .DE
1715 .LP
1716 Now you may be wondering about the dependency between the
1717 .CW .o
1718 and
1719 .CW .c
1720 files \*- it's not mentioned anywhere in the new makefile. This is
1721 because it isn't needed: one of the effects of applying a
1722 transformation rule is the target comes to depend on the implied
1723 source. That's why it's called the implied
1724 .I source .
1725 .LP
1726 For a more detailed example. Say you have a makefile like this:
1727 .DS
1728 a.out           : a.o b.o
1729         $(CC) $(.ALLSRC)
1730 .DE
1731 and a directory set up like this:
1732 .DS
1733 total 4
1734 -rw-rw-r--  1 deboor         34 Sep  7 00:43 Makefile
1735 -rw-rw-r--  1 deboor        119 Oct  3 19:39 a.c
1736 -rw-rw-r--  1 deboor        201 Sep  7 00:43 a.o
1737 -rw-rw-r--  1 deboor         69 Sep  7 00:43 b.c
1738 .DE
1739 While just typing
1740 .CW pmake '' ``
1741 will do the right thing, it's much more informative to type
1742 .CW "pmake -d s" ''. ``
1743 This will show you what PMake is up to as it processes the files. In
1744 this case, PMake prints the following:
1745 .DS
1746 Suff_FindDeps (a.out)
1747         using existing source a.o
1748         applying .o -> .out to "a.o"
1749 Suff_FindDeps (a.o)
1750         trying a.c...got it
1751         applying .c -> .o to "a.c"
1752 Suff_FindDeps (b.o)
1753         trying b.c...got it
1754         applying .c -> .o to "b.c"
1755 Suff_FindDeps (a.c)
1756         trying a.y...not there
1757         trying a.l...not there
1758         trying a.c,v...not there
1759         trying a.y,v...not there
1760         trying a.l,v...not there
1761 Suff_FindDeps (b.c)
1762         trying b.y...not there
1763         trying b.l...not there
1764         trying b.c,v...not there
1765         trying b.y,v...not there
1766         trying b.l,v...not there
1767 --- a.o ---
1768 cc  -c a.c
1769 --- b.o ---
1770 cc  -c b.c
1771 --- a.out ---
1772 cc a.o b.o
1773 .DE
1774 .LP
1775 .CW Suff_FindDeps
1776 is the name of a function in PMake that is called to check for implied
1777 sources for a target using transformation rules.
1778 The transformations it tries are, naturally
1779 enough, limited to the ones that have been defined (a transformation
1780 may be defined multiple times, by the way, but only the most recent
1781 one will be used). You will notice, however, that there is a definite
1782 order to the suffixes that are tried. This order is set by the
1783 relative positions of the suffixes on the
1784 .CW .SUFFIXES
1785 line \*- the earlier a suffix appears, the earlier it is checked as
1786 the source of a transformation. Once a suffix has been defined, the
1787 only way to change its position in the pecking order is to remove all
1788 the suffixes (by having a
1789 .CW .SUFFIXES
1790 dependency line with no sources) and redefine them in the order you
1791 want. (Previously-defined transformation rules will be automatically
1792 redefined as the suffixes they involve are re-entered.)
1793 .LP
1794 Another way to affect the search order is to make the dependency
1795 explicit. In the above example,
1796 .CW a.out
1797 depends on
1798 .CW a.o
1799 and
1800 .CW b.o .
1801 Since a transformation exists from
1802 .CW .o
1803 to
1804 .CW .out ,
1805 PMake uses that, as indicated by the
1806 .CW "using existing source a.o" '' ``
1807 message.
1808 .LP
1809 The search for a transformation starts from the suffix of the target
1810 and continues through all the defined transformations, in the order
1811 dictated by the suffix ranking, until an existing file with the same
1812 base (the target name minus the suffix and any leading directories) is
1813 found. At that point, one or more transformation rules will have been
1814 found to change the one existing file into the target.
1815 .LP
1816 For example, ignoring what's in the system makefile for now, say you
1817 have a makefile like this:
1818 .DS
1819 \&.SUFFIXES       : .out .o .c .y .l
1820 \&.l.c            :
1821         lex $(.IMPSRC)
1822         mv lex.yy.c $(.TARGET)
1823 \&.y.c            :
1824         yacc $(.IMPSRC)
1825         mv y.tab.c $(.TARGET)
1826 \&.c.o            :
1827         cc -c $(.IMPSRC)
1828 \&.o.out          :
1829         cc -o $(.TARGET) $(.IMPSRC)
1830 .DE
1831 and the single file
1832 .CW jive.l .
1833 If you were to type
1834 .CW "pmake -rd ms jive.out" ,'' ``
1835 you would get the following output for
1836 .CW jive.out :
1837 .DS
1838 Suff_FindDeps (jive.out)
1839         trying jive.o...not there
1840         trying jive.c...not there
1841         trying jive.y...not there
1842         trying jive.l...got it
1843         applying .l -> .c to "jive.l"
1844         applying .c -> .o to "jive.c"
1845         applying .o -> .out to "jive.o"
1846 .DE
1847 and this is why: PMake starts with the target
1848 .CW jive.out ,
1849 figures out its suffix
1850 .CW .out ) (
1851 and looks for things it can transform to a
1852 .CW .out
1853 file. In this case, it only finds
1854 .CW .o ,
1855 so it looks for the file
1856 .CW jive.o .
1857 It fails to find it, so it looks for transformations into a
1858 .CW .o
1859 file. Again it has only one choice:
1860 .CW .c .
1861 So it looks for
1862 .CW jive.c
1863 and, as you know, fails to find it. At this point it has two choices:
1864 it can create the
1865 .CW .c
1866 file from either a
1867 .CW .y
1868 file or a
1869 .CW .l
1870 file. Since
1871 .CW .y
1872 came first on the
1873 .CW .SUFFIXES
1874 line, it checks for
1875 .CW jive.y
1876 first, but can't find it, so it looks for
1877 .CW jive.l
1878 and, lo and behold, there it is.
1879 At this point, it has defined a transformation path as follows:
1880 .CW .l
1881 \(->
1882 .CW .c
1883 \(->
1884 .CW .o
1885 \(->
1886 .CW .out
1887 and applies the transformation rules accordingly. For completeness,
1888 and to give you a better idea of what PMake actually did with this
1889 three-step transformation, this is what PMake printed for the rest of
1890 the process:
1891 .DS
1892 Suff_FindDeps (jive.o)
1893         using existing source jive.c
1894         applying .c -> .o to "jive.c"
1895 Suff_FindDeps (jive.c)
1896         using existing source jive.l
1897         applying .l -> .c to "jive.l"
1898 Suff_FindDeps (jive.l)
1899 Examining jive.l...modified 17:16:01 Oct 4, 1987...up-to-date
1900 Examining jive.c...non-existent...out-of-date
1901 --- jive.c ---
1902 lex jive.l
1903 \&.\|.\|. meaningless lex output deleted .\|.\|.
1904 mv lex.yy.c jive.c
1905 Examining jive.o...non-existent...out-of-date
1906 --- jive.o ---
1907 cc -c jive.c
1908 Examining jive.out...non-existent...out-of-date
1909 --- jive.out ---
1910 cc -o jive.out jive.o
1911 .DE
1912 .LP
1913 One final question remains: what does PMake do with targets that have
1914 no known suffix? PMake simply pretends it actually has a known suffix
1915 and searches for transformations accordingly.
1916 The suffix it chooses is the source for the
1917 .CW .NULL
1918 .Ix 0 ref .NULL
1919 target mentioned later. In the system makefile, 
1920 .CW .out
1921 is chosen as the ``null suffix''
1922 .Ix 0 def suffix null
1923 .Ix 0 def "null suffix"
1924 because most people use PMake to create programs. You are, however,
1925 free and welcome to change it to a suffix of your own choosing.
1926 The null suffix is ignored, however, when PMake is in compatibility
1927 mode (see chapter 4).
1928 .xH 2 Including Other Makefiles
1929 .Ix 0 def makefile inclusion
1930 .Rd 2
1931 .LP
1932 Just as for programs, it is often useful to extract certain parts of a
1933 makefile into another file and just include it in other makefiles
1934 somehow. Many compilers allow you say something like
1935 .DS
1936 #include "defs.h"
1937 .DE
1938 to include the contents of
1939 .CW defs.h
1940 in the source file. PMake allows you to do the same thing for
1941 makefiles, with the added ability to use variables in the filenames.
1942 An include directive in a makefile looks either like this:
1943 .DS
1944 #include <file>
1945 .DE
1946 or this
1947 .DS
1948 #include "file"
1949 .DE
1950 The difference between the two is where PMake searches for the file:
1951 the first way, PMake will look for
1952 the file only in the system makefile directory (or directories)
1953 (to find out what that directory is, give PMake the
1954 .B \-h
1955 flag).
1956 .Ix 0 ref flags -h
1957 The system makefile directory search path can be overridden via the
1958 .B \-m
1959 option.
1960 .Ix 0 ref flags -m
1961 For files in double-quotes, the search is more complex:
1962 .RS
1963 .IP 1)
1964 The directory of the makefile that's including the file.
1965 .IP 2)
1966 The current directory (the one in which you invoked PMake).
1967 .IP 3)
1968 The directories given by you using
1969 .B \-I
1970 flags, in the order in which you gave them.
1971 .IP 4)
1972 Directories given by
1973 .CW .PATH
1974 dependency lines (see chapter 4).
1975 .IP 5)
1976 The system makefile directory.
1977 .RE
1978 .LP
1979 in that order.
1980 .LP
1981 You are free to use PMake variables in the filename\*-PMake will
1982 expand them before searching for the file. You must specify the
1983 searching method with either angle brackets or double-quotes
1984 .I outside
1985 of a variable expansion. I.e. the following
1986 .DS
1987 SYSTEM  = <command.mk>
1988
1989 #include $(SYSTEM)
1990 .DE
1991 won't work.
1992 .xH 2 Saving Commands
1993 .LP
1994 .Ix 0 def ...
1995 There may come a time when you will want to save certain commands to
1996 be executed when everything else is done. For instance: you're
1997 making several different libraries at one time and you want to create the
1998 members in parallel. Problem is,
1999 .CW ranlib
2000 is another one of those programs that can't be run more than once in
2001 the same directory at the same time (each one creates a file called
2002 .CW __.SYMDEF
2003 into which it stuffs information for the linker to use. Two of them
2004 running at once will overwrite each other's file and the result will
2005 be garbage for both parties). You might want a way to save the ranlib
2006 commands til the end so they can be run one after the other, thus
2007 keeping them from trashing each other's file. PMake allows you to do
2008 this by inserting an ellipsis (``.\|.\|.'') as a command between
2009 commands to be run at once and those to be run later.
2010 .LP
2011 So for the
2012 .CW ranlib
2013 case above, you might do this:
2014 .Rd 5
2015 .DS
2016 lib1.a          : $(LIB1OBJS)
2017         rm -f $(.TARGET)
2018         ar cr $(.TARGET) $(.ALLSRC)
2019         ...
2020         ranlib $(.TARGET)
2021
2022 lib2.a          : $(LIB2OBJS)
2023         rm -f $(.TARGET)
2024         ar cr $(.TARGET) $(.ALLSRC)
2025         ...
2026         ranlib $(.TARGET)
2027 .DE
2028 .Ix 0 ref variable local .TARGET
2029 .Ix 0 ref variable local .ALLSRC
2030 This would save both
2031 .DS
2032 ranlib $(.TARGET)
2033 .DE
2034 commands until the end, when they would run one after the other
2035 (using the correct value for the
2036 .CW .TARGET
2037 variable, of course).
2038 .LP
2039 Commands saved in this manner are only executed if PMake manages to
2040 re-create everything without an error.
2041 .xH 2 Target Attributes
2042 .LP
2043 PMake allows you to give attributes to targets by means of special
2044 sources. Like everything else PMake uses, these sources begin with a
2045 period and are made up of all upper-case letters. There are various
2046 reasons for using them, and I will try to give examples for most of
2047 them. Others you'll have to find uses for yourself. Think of it as ``an
2048 exercise for the reader.'' By placing one (or more) of these as a source on a
2049 dependency line, you are ``marking the target(s) with that
2050 attribute.'' That's just the way I phrase it, so you know.
2051 .LP
2052 Any attributes given as sources for a transformation rule are applied
2053 to the target of the transformation rule when the rule is applied.
2054 .Ix 0 def attributes
2055 .Ix 0 ref source
2056 .Ix 0 ref target
2057 .nr pw 12
2058 .IP .DONTCARE \n(pw
2059 .Ix 0 def attributes .DONTCARE
2060 .Ix 0 def .DONTCARE
2061 If a target is marked with this attribute and PMake can't figure out
2062 how to create it, it will ignore this fact and assume the file isn't
2063 really needed or actually exists and PMake just can't find it. This may prove
2064 wrong, but the error will be noted later on, not when PMake tries to create
2065 the target so marked. This attribute also prevents PMake from
2066 attempting to touch the target if it is given the
2067 .B \-t
2068 flag.
2069 .Ix 0 ref flags -t
2070 .IP .EXEC \n(pw
2071 .Ix 0 def attributes .EXEC
2072 .Ix 0 def .EXEC
2073 This attribute causes its shell script to be executed while having no
2074 effect on targets that depend on it. This makes the target into a sort
2075 of subroutine.  An example. Say you have some LISP files that need to
2076 be compiled and loaded into a LISP process. To do this, you echo LISP
2077 commands into a file and execute a LISP with this file as its input
2078 when everything's done. Say also that you have to load other files
2079 from another system before you can compile your files and further,
2080 that you don't want to go through the loading and dumping unless one
2081 of
2082 .I your
2083 files has changed. Your makefile might look a little bit
2084 like this (remember, this is an educational example, and don't worry
2085 about the
2086 .CW COMPILE
2087 rule, all will soon become clear, grasshopper):
2088 .DS
2089 system          : init a.fasl b.fasl c.fasl
2090         for i in $(.ALLSRC);
2091         do
2092                 echo -n '(load "' >> input
2093                 echo -n ${i} >> input
2094                 echo '")' >> input
2095         done
2096         echo '(dump "$(.TARGET)")' >> input
2097         lisp < input
2098
2099 a.fasl          : a.l init COMPILE
2100 b.fasl          : b.l init COMPILE
2101 c.fasl          : c.l init COMPILE
2102 COMPILE         : .USE
2103         echo '(compile "$(.ALLSRC)")' >> input
2104 init            : .EXEC
2105         echo '(load-system)' > input
2106 .DE
2107 .Ix 0 ref .USE
2108 .Ix 0 ref attributes .USE
2109 .Ix 0 ref variable local .ALLSRC
2110 .IP "\&"
2111 .CW .EXEC
2112 sources, don't appear in the local variables of targets that depend on
2113 them (nor are they touched if PMake is given the
2114 .B \-t
2115 flag).
2116 .Ix 0 ref flags -t
2117 Note that all the rules, not just that for
2118 .CW system ,
2119 include
2120 .CW init
2121 as a source. This is because none of the other targets can be made
2122 until
2123 .CW init
2124 has been made, thus they depend on it.
2125 .IP .EXPORT \n(pw
2126 .Ix 0 def attributes .EXPORT
2127 .Ix 0 def .EXPORT
2128 This is used to mark those targets whose creation should be sent to
2129 another machine if at all possible. This may be used by some
2130 exportation schemes if the exportation is expensive. You should ask
2131 your system administrator if it is necessary.
2132 .IP .EXPORTSAME \n(pw
2133 .Ix 0 def attributes .EXPORTSAME
2134 .Ix 0 def .EXPORTSAME
2135 Tells the export system that the job should be exported to a machine
2136 of the same architecture as the current one. Certain operations (e.g.
2137 running text through
2138 .CW nroff )
2139 can be performed the same on any architecture (CPU and
2140 operating system type), while others (e.g. compiling a program with
2141 .CW cc )
2142 must be performed on a machine with the same architecture. Not all
2143 export systems will support this attribute.
2144 .IP .IGNORE \n(pw
2145 .Ix 0 def attributes .IGNORE
2146 .Ix 0 def .IGNORE attribute
2147 Giving a target the
2148 .CW .IGNORE
2149 attribute causes PMake to ignore errors from any of the target's commands, as
2150 if they all had `\-' before them.
2151 .IP .INVISIBLE \n(pw
2152 .Ix 0 def attributes .INVISIBLE
2153 .Ix 0 def .INVISIBLE
2154 This allows you to specify one target as a source for another without
2155 the one affecting the other's local variables. Useful if, say, you
2156 have a makefile that creates two programs, one of which is used to
2157 create the other, so it must exist before the other is created. You
2158 could say
2159 .DS
2160 prog1           : $(PROG1OBJS) prog2 MAKEINSTALL
2161 prog2           : $(PROG2OBJS) .INVISIBLE MAKEINSTALL
2162 .DE
2163 where
2164 .CW MAKEINSTALL
2165 is some complex .USE rule (see below) that depends on the
2166 .Ix 0 ref .USE
2167 .CW .ALLSRC
2168 variable containing the right things. Without the
2169 .CW .INVISIBLE
2170 attribute for
2171 .CW prog2 ,
2172 the
2173 .CW MAKEINSTALL
2174 rule couldn't be applied. This is not as useful as it should be, and
2175 the semantics may change (or the whole thing go away) in the
2176 not-too-distant future.
2177 .IP .JOIN \n(pw
2178 .Ix 0 def attributes .JOIN
2179 .Ix 0 def .JOIN
2180 This is another way to avoid performing some operations in parallel
2181 while permitting everything else to be done so. Specifically it
2182 forces the target's shell script to be executed only if one or more of the
2183 sources was out-of-date. In addition, the target's name,
2184 in both its
2185 .CW .TARGET
2186 variable and all the local variables of any target that depends on it,
2187 is replaced by the value of its
2188 .CW .ALLSRC
2189 variable.
2190 As an example, suppose you have a program that has four libraries that
2191 compile in the same directory along with, and at the same time as, the
2192 program. You again have the problem with
2193 .CW ranlib
2194 that I mentioned earlier, only this time it's more severe: you
2195 can't just put the ranlib off to the end since the program
2196 will need those libraries before it can be re-created. You can do
2197 something like this:
2198 .DS
2199 program         : $(OBJS) libraries
2200         cc -o $(.TARGET) $(.ALLSRC)
2201
2202 libraries       : lib1.a lib2.a lib3.a lib4.a .JOIN
2203         ranlib $(.OODATE)
2204 .DE
2205 .Ix 0 ref variable local .TARGET
2206 .Ix 0 ref variable local .ALLSRC
2207 .Ix 0 ref variable local .OODATE
2208 .Ix 0 ref .TARGET
2209 .Ix 0 ref .ALLSRC
2210 .Ix 0 ref .OODATE
2211 In this case, PMake will re-create the
2212 .CW $(OBJS)
2213 as necessary, along with
2214 .CW lib1.a ,
2215 .CW lib2.a ,
2216 .CW lib3.a
2217 and
2218 .CW lib4.a .
2219 It will then execute
2220 .CW ranlib
2221 on any library that was changed and set
2222 .CW program 's
2223 .CW .ALLSRC
2224 variable to contain what's in
2225 .CW $(OBJS)
2226 followed by
2227 .CW "lib1.a lib2.a lib3.a lib4.a" .'' ``
2228 In case you're wondering, it's called
2229 .CW .JOIN
2230 because it joins together different threads of the ``input graph'' at
2231 the target marked with the attribute.
2232 Another aspect of the .JOIN attribute is it keeps the target from
2233 being created if the
2234 .B \-t
2235 flag was given.
2236 .Ix 0 ref flags -t
2237 .IP .MAKE \n(pw
2238 .Ix 0 def attributes .MAKE
2239 .Ix 0 def .MAKE
2240 The
2241 .CW .MAKE
2242 attribute marks its target as being a recursive invocation of PMake.
2243 This forces PMake to execute the script associated with the target (if
2244 it's out-of-date) even if you gave the
2245 .B \-n
2246 or
2247 .B \-t
2248 flag. By doing this, you can start at the top of a system and type
2249 .DS
2250 pmake -n
2251 .DE
2252 and have it descend the directory tree (if your makefiles are set up
2253 correctly), printing what it would have executed if you hadn't
2254 included the
2255 .B \-n
2256 flag.
2257 .IP .NOEXPORT \n(pw
2258 .Ix 0 def attributes .NOEXPORT
2259 .Ix 0 def .NOEXPORT attribute
2260 If possible, PMake will attempt to export the creation of all targets to
2261 another machine (this depends on how PMake was configured). Sometimes,
2262 the creation is so simple, it is pointless to send it to another
2263 machine. If you give the target the
2264 .CW .NOEXPORT
2265 attribute, it will be run locally, even if you've given PMake the
2266 .B "\-L 0"
2267 flag.
2268 .IP .NOTMAIN \n(pw
2269 .Ix 0 def attributes .NOTMAIN
2270 .Ix 0 def .NOTMAIN
2271 Normally, if you do not specify a target to make in any other way,
2272 PMake will take the first target on the first dependency line of a
2273 makefile as the target to create. That target is known as the ``Main
2274 Target'' and is labeled as such if you print the dependencies out
2275 using the
2276 .B \-p
2277 flag.
2278 .Ix 0 ref flags -p
2279 Giving a target this attribute tells PMake that the target is
2280 definitely
2281 .I not
2282 the Main Target.
2283 This allows you to place targets in an included makefile and
2284 have PMake create something else by default.
2285 .IP .PRECIOUS \n(pw
2286 .Ix 0 def attributes .PRECIOUS
2287 .Ix 0 def .PRECIOUS attribute
2288 When PMake is interrupted (you type control-C at the keyboard), it
2289 will attempt to clean up after itself by removing any half-made
2290 targets. If a target has the
2291 .CW .PRECIOUS
2292 attribute, however, PMake will leave it alone. An additional side
2293 effect of the `::' operator is to mark the targets as
2294 .CW .PRECIOUS .
2295 .Ix 0 ref operator double-colon
2296 .Ix 0 ref ::
2297 .IP .SILENT \n(pw
2298 .Ix 0 def attributes .SILENT
2299 .Ix 0 def .SILENT attribute
2300 Marking a target with this attribute keeps its commands from being
2301 printed when they're executed, just as if they had an `@' in front of them.
2302 .IP .USE \n(pw
2303 .Ix 0 def attributes .USE
2304 .Ix 0 def .USE
2305 By giving a target this attribute, you turn it into PMake's equivalent
2306 of a macro. When the target is used as a source for another target,
2307 the other target acquires the commands, sources and attributes (except
2308 .CW .USE )
2309 of the source.
2310 If the target already has commands, the
2311 .CW .USE
2312 target's commands are added to the end. If more than one .USE-marked
2313 source is given to a target, the rules are applied sequentially.
2314 .IP "\&" \n(pw
2315 The typical .USE rule (as I call them) will use the sources of the
2316 target to which it is applied (as stored in the
2317 .CW .ALLSRC
2318 variable for the target) as its ``arguments,'' if you will.
2319 For example, you probably noticed that the commands for creating
2320 .CW lib1.a
2321 and
2322 .CW lib2.a
2323 in the example in section 3.3
2324 .Rm 5 3.3
2325 were exactly the same. You can use the
2326 .CW .USE
2327 attribute to eliminate the repetition, like so:
2328 .DS
2329 lib1.a          : $(LIB1OBJS) MAKELIB
2330 lib2.a          : $(LIB2OBJS) MAKELIB
2331
2332 MAKELIB         : .USE
2333         rm -f $(.TARGET)
2334         ar cr $(.TARGET) $(.ALLSRC)
2335         ...
2336         ranlib $(.TARGET)
2337 .DE
2338 .Ix 0 ref variable local .TARGET
2339 .Ix 0 ref variable local .ALLSRC
2340 .IP "\&" \n(pw
2341 Several system makefiles (not to be confused with The System Makefile)
2342 make use of these  .USE rules to make your
2343 life easier (they're in the default, system makefile directory...take a look).
2344 Note that the .USE rule source itself
2345 .CW MAKELIB ) (
2346 does not appear in any of the targets's local variables.
2347 There is no limit to the number of times I could use the
2348 .CW MAKELIB
2349 rule. If there were more libraries, I could continue with
2350 .CW "lib3.a : $(LIB3OBJS) MAKELIB" '' ``
2351 and so on and so forth.
2352 .xH 2 Special Targets
2353 .LP
2354 As there were in Make, so there are certain targets that have special
2355 meaning to PMake. When you use one on a dependency line, it is the
2356 only target that may appear on the left-hand-side of the operator.
2357 .Ix 0 ref target
2358 .Ix 0 ref operator
2359 As for the attributes and variables, all the special targets
2360 begin with a period and consist of upper-case letters only.
2361 I won't describe them all in detail because some of them are rather
2362 complex and I'll describe them in more detail than you'll want in
2363 chapter 4.
2364 The targets are as follows:
2365 .nr pw 10
2366 .IP .BEGIN \n(pw
2367 .Ix 0 def .BEGIN
2368 Any commands attached to this target are executed before anything else
2369 is done. You can use it for any initialization that needs doing.
2370 .IP .DEFAULT \n(pw
2371 .Ix 0 def .DEFAULT
2372 This is sort of a .USE rule for any target (that was used only as a
2373 source) that PMake can't figure out any other way to create. It's only
2374 ``sort of'' a .USE rule because only the shell script attached to the
2375 .CW .DEFAULT
2376 target is used. The
2377 .CW .IMPSRC
2378 variable of a target that inherits
2379 .CW .DEFAULT 's
2380 commands is set to the target's own name.
2381 .Ix 0 ref .IMPSRC
2382 .Ix 0 ref variable local .IMPSRC
2383 .IP .END \n(pw
2384 .Ix 0 def .END
2385 This serves a function similar to
2386 .CW .BEGIN ,
2387 in that commands attached to it are executed once everything has been
2388 re-created (so long as no errors occurred). It also serves the extra
2389 function of being a place on which PMake can hang commands you put off
2390 to the end. Thus the script for this target will be executed before
2391 any of the commands you save with the ``.\|.\|.''.
2392 .Ix 0 ref ...
2393 .IP .EXPORT \n(pw
2394 The sources for this target are passed to the exportation system compiled
2395 into PMake. Some systems will use these sources to configure
2396 themselves. You should ask your system administrator about this.
2397 .IP .IGNORE \n(pw
2398 .Ix 0 def .IGNORE target
2399 .Ix 0 ref .IGNORE attribute
2400 .Ix 0 ref attributes .IGNORE
2401 This target marks each of its sources with the
2402 .CW .IGNORE
2403 attribute. If you don't give it any sources, then it is like
2404 giving the
2405 .B \-i
2406 flag when you invoke PMake \*- errors are ignored for all commands.
2407 .Ix 0 ref flags -i
2408 .IP .INCLUDES \n(pw
2409 .Ix 0 def .INCLUDES target
2410 .Ix 0 def variable global .INCLUDES
2411 .Ix 0 def .INCLUDES variable
2412 The sources for this target are taken to be suffixes that indicate a
2413 file that can be included in a program source file.
2414 The suffix must have already been declared with
2415 .CW .SUFFIXES
2416 (see below).
2417 Any suffix so marked will have the directories on its search path
2418 (see
2419 .CW .PATH ,
2420 below) placed in the
2421 .CW .INCLUDES
2422 variable, each preceded by a
2423 .B \-I
2424 flag. This variable can then be used as an argument for the compiler
2425 in the normal fashion. The
2426 .CW .h
2427 suffix is already marked in this way in the system makefile.
2428 .Ix 0 ref makefile system
2429 E.g. if you have
2430 .DS
2431 \&.SUFFIXES       : .bitmap
2432 \&.PATH.bitmap    : /usr/local/X/lib/bitmaps
2433 \&.INCLUDES       : .bitmap
2434 .DE
2435 PMake will place
2436 .CW "-I/usr/local/X/lib/bitmaps" '' ``
2437 in the
2438 .CW .INCLUDES
2439 variable and you can then say
2440 .DS
2441 cc $(.INCLUDES) -c xprogram.c
2442 .DE
2443 (Note: the
2444 .CW .INCLUDES
2445 variable is not actually filled in until the entire makefile has been read.)
2446 .IP .INTERRUPT \n(pw
2447 .Ix 0 def .INTERRUPT
2448 When PMake is interrupted,
2449 it will execute the commands in the script for this target, if it
2450 exists.
2451 .IP .LIBS \n(pw
2452 .Ix 0 def .LIBS target
2453 .Ix 0 def .LIBS variable
2454 .Ix 0 def variable global .LIBS
2455 This does for libraries what
2456 .CW .INCLUDES
2457 does for include files, except the flag used is
2458 .B \-L ,
2459 as required by those linkers that allow you to tell them where to find
2460 libraries. The variable used is
2461 .CW .LIBS .
2462 Be forewarned that PMake may not have been compiled to do this if the
2463 linker on your system doesn't accept the
2464 .B \-L
2465 flag, though the
2466 .CW .LIBS
2467 variable will always be defined once the makefile has been read.
2468 .IP .MAIN \n(pw
2469 .Ix 0 def .MAIN
2470 If you didn't give a target (or targets) to create when you invoked
2471 PMake, it will take the sources of this target as the targets to
2472 create.
2473 .IP .MAKEFLAGS \n(pw
2474 .Ix 0 def .MAKEFLAGS target
2475 This target provides a way for you to always specify flags for PMake
2476 when the makefile is used. The flags are just as they would be typed
2477 to the shell (except you can't use shell variables unless they're in
2478 the environment),
2479 though the
2480 .B \-f
2481 and
2482 .B \-r
2483 flags have no effect.
2484 .IP .NULL \n(pw
2485 .Ix 0 def .NULL
2486 .Ix 0 ref suffix null
2487 .Ix 0 ref "null suffix"
2488 This allows you to specify what suffix PMake should pretend a file has
2489 if, in fact, it has no known suffix. Only one suffix may be so
2490 designated. The last source on the dependency line is the suffix that
2491 is used (you should, however, only give one suffix.\|.\|.).
2492 .IP .PATH \n(pw
2493 .Ix 0 def .PATH
2494 If you give sources for this target, PMake will take them as
2495 directories in which to search for files it cannot find in the current
2496 directory. If you give no sources, it will clear out any directories
2497 added to the search path before. Since the effects of this all get
2498 very complex, I'll leave it til chapter four to give you a complete
2499 explanation.
2500 .IP .PATH\fIsuffix\fP \n(pw
2501 .Ix 0 ref .PATH
2502 This does a similar thing to
2503 .CW .PATH ,
2504 but it does it only for files with the given suffix. The suffix must
2505 have been defined already. Look at
2506 .B "Search Paths"
2507 (section 4.1)
2508 .Rm 6 4.1
2509 for more information.
2510 .IP .PRECIOUS \n(pw
2511 .Ix 0 def .PRECIOUS target
2512 .Ix 0 ref .PRECIOUS attribute
2513 .Ix 0 ref attributes .PRECIOUS
2514 Similar to
2515 .CW .IGNORE ,
2516 this gives the
2517 .CW .PRECIOUS
2518 attribute to each source on the dependency line, unless there are no
2519 sources, in which case the
2520 .CW .PRECIOUS
2521 attribute is given to every target in the file.
2522 .IP .RECURSIVE \n(pw
2523 .Ix 0 def .RECURSIVE
2524 .Ix 0 ref attributes .MAKE
2525 .Ix 0 ref .MAKE
2526 This target applies the
2527 .CW .MAKE
2528 attribute to all its sources. It does nothing if you don't give it any sources.
2529 .IP .SHELL \n(pw
2530 .Ix 0 def .SHELL
2531 PMake is not constrained to only using the Bourne shell to execute
2532 the commands you put in the makefile. You can tell it some other shell
2533 to use with this target. Check out
2534 .B "A Shell is a Shell is a Shell"
2535 (section 4.4)
2536 .Rm 7 4.4
2537 for more information.
2538 .IP .SILENT \n(pw
2539 .Ix 0 def .SILENT target
2540 .Ix 0 ref .SILENT attribute
2541 .Ix 0 ref attributes .SILENT
2542 When you use
2543 .CW .SILENT
2544 as a target, it applies the
2545 .CW .SILENT
2546 attribute to each of its sources. If there are no sources on the
2547 dependency line, then it is as if you gave PMake the
2548 .B \-s
2549 flag and no commands will be echoed.
2550 .IP .SUFFIXES \n(pw
2551 .Ix 0 def .SUFFIXES
2552 This is used to give new file suffixes for PMake to handle. Each
2553 source is a suffix PMake should recognize. If you give a
2554 .CW .SUFFIXES
2555 dependency line with no sources, PMake will forget about all the
2556 suffixes it knew (this also nukes the null suffix).
2557 For those targets that need to have suffixes defined, this is how you do it.
2558 .LP
2559 In addition to these targets, a line of the form
2560 .DS
2561 \fIattribute\fP : \fIsources\fP
2562 .DE
2563 applies the
2564 .I attribute
2565 to all the targets listed as
2566 .I sources .
2567 .xH 2 Modifying Variable Expansion
2568 .LP
2569 .Ix 0 def variable expansion modified
2570 .Ix 0 ref variable expansion
2571 .Ix 0 def variable modifiers
2572 Variables need not always be expanded verbatim. PMake defines several
2573 modifiers that may be applied to a variable's value before it is
2574 expanded. You apply a modifier by placing it after the variable name
2575 with a colon between the two, like so:
2576 .DS
2577 ${\fIVARIABLE\fP:\fImodifier\fP}
2578 .DE
2579 Each modifier is a single character followed by something specific to
2580 the modifier itself.
2581 You may apply as many modifiers as you want \*- each one is applied to
2582 the result of the previous and is separated from the previous by
2583 another colon.
2584 .LP
2585 There are seven ways to modify a variable's expansion, most of which
2586 come from the C shell variable modification characters:
2587 .RS
2588 .IP "M\fIpattern\fP"
2589 .Ix 0 def :M
2590 .Ix 0 def modifier match
2591 This is used to select only those words (a word is a series of
2592 characters that are neither spaces nor tabs) that match the given
2593 .I pattern .
2594 The pattern is a wildcard pattern like that used by the shell, where
2595 .CW *
2596 means 0 or more characters of any sort;
2597 .CW ?
2598 is any single character;
2599 .CW [abcd]
2600 matches any single character that is either `a', `b', `c' or `d'
2601 (there may be any number of characters between the brackets);
2602 .CW [0-9]
2603 matches any single character that is between `0' and `9' (i.e. any
2604 digit. This form may be freely mixed with the other bracket form), and
2605 `\\' is used to escape any of the characters `*', `?', `[' or `:',
2606 leaving them as regular characters to match themselves in a word.
2607 For example, the system makefile
2608 .CW <makedepend.mk>
2609 uses
2610 .CW "$(CFLAGS:M-[ID]*)" '' ``
2611 to extract all the
2612 .CW \-I
2613 and
2614 .CW \-D
2615 flags that would be passed to the C compiler. This allows it to
2616 properly locate include files and generate the correct dependencies.
2617 .IP "N\fIpattern\fP"
2618 .Ix 0 def :N
2619 .Ix 0 def modifier nomatch
2620 This is identical to
2621 .CW :M
2622 except it substitutes all words that don't match the given pattern.
2623 .IP "S/\fIsearch-string\fP/\fIreplacement-string\fP/[g]"
2624 .Ix 0 def :S
2625 .Ix 0 def modifier substitute
2626 Causes the first occurrence of
2627 .I search-string
2628 in the variable to be replaced by
2629 .I replacement-string ,
2630 unless the
2631 .CW g
2632 flag is given at the end, in which case all occurrences of the string
2633 are replaced. The substitution is performed on each word in the
2634 variable in turn. If 
2635 .I search-string
2636 begins with a
2637 .CW ^ ,
2638 the string must match starting at the beginning of the word. If
2639 .I search-string
2640 ends with a
2641 .CW $ ,
2642 the string must match to the end of the word (these two may be
2643 combined to force an exact match). If a backslash precedes these two
2644 characters, however, they lose their special meaning. Variable
2645 expansion also occurs in the normal fashion inside both the
2646 .I search-string
2647 and the
2648 .I replacement-string ,
2649 .B except
2650 that a backslash is used to prevent the expansion of a
2651 .CW $ ,
2652 not another dollar sign, as is usual.
2653 Note that
2654 .I search-string
2655 is just a string, not a pattern, so none of the usual
2656 regular-expression/wildcard characters have any special meaning save
2657 .CW ^
2658 and
2659 .CW $ .
2660 In the replacement string,
2661 the
2662 .CW &
2663 character is replaced by the
2664 .I search-string
2665 unless it is preceded by a backslash.
2666 You are allowed to use any character except
2667 colon or exclamation point to separate the two strings. This so-called
2668 delimiter character may be placed in either string by preceding it
2669 with a backslash.
2670 .IP T
2671 .Ix 0 def :T
2672 .Ix 0 def modifier tail
2673 Replaces each word in the variable expansion by its last
2674 component (its ``tail''). For example, given
2675 .DS
2676 OBJS = ../lib/a.o b /usr/lib/libm.a
2677 TAILS = $(OBJS:T)
2678 .DE
2679 the variable
2680 .CW TAILS
2681 would expand to
2682 .CW "a.o b libm.a" .'' ``
2683 .IP H
2684 .Ix 0 def :H
2685 .Ix 0 def modifier head
2686 This is similar to
2687 .CW :T ,
2688 except that every word is replaced by everything but the tail (the
2689 ``head''). Using the same definition of
2690 .CW OBJS ,
2691 the string
2692 .CW "$(OBJS:H)" '' ``
2693 would expand to
2694 .CW "../lib /usr/lib" .'' ``
2695 Note that the final slash on the heads is removed and
2696 anything without a head is replaced by the empty string.
2697 .IP E
2698 .Ix 0 def :E
2699 .Ix 0 def modifier extension
2700 .Ix 0 def modifier suffix
2701 .Ix 0 ref suffix "variable modifier"
2702 .CW :E
2703 replaces each word by its suffix (``extension''). So
2704 .CW "$(OBJS:E)" '' ``
2705 would give you
2706 .CW ".o .a" .'' ``
2707 .IP R
2708 .Ix 0 def :R
2709 .Ix 0 def modifier root
2710 .Ix 0 def modifier base
2711 This replaces each word by everything but the suffix (the ``root'' of
2712 the word).
2713 .CW "$(OBJS:R)" '' ``
2714 expands to ``
2715 .CW "../lib/a b /usr/lib/libm" .''
2716 .RE
2717 .LP
2718 In addition, the System V style of substitution is also supported.
2719 This looks like:
2720 .DS
2721 $(\fIVARIABLE\fP:\fIsearch-string\fP=\fIreplacement\fP)
2722 .DE
2723 It must be the last modifier in the chain. The search is anchored at
2724 the end of each word, so only suffixes or whole words may be replaced.
2725 .xH 2 More on Debugging
2726 .xH 2 More Exercises
2727 .IP (3.1)
2728 You've got a set programs, each of which is created from its own
2729 assembly-language source file (suffix
2730 .CW .asm ).
2731 Each program can be assembled into two versions, one with error-checking
2732 code assembled in and one without. You could assemble them into files
2733 with different suffixes
2734 .CW .eobj \& (
2735 and
2736 .CW .obj ,
2737 for instance), but your linker only understands files that end in
2738 .CW .obj .
2739 To top it all off, the final executables
2740 .I must
2741 have the suffix
2742 .CW .exe .
2743 How can you still use transformation rules to make your life easier
2744 (Hint: assume the error-checking versions have
2745 .CW ec
2746 tacked onto their prefix)?
2747 .IP (3.2)
2748 Assume, for a moment or two, you want to perform a sort of
2749 ``indirection'' by placing the name of a variable into another one,
2750 then you want to get the value of the first by expanding the second
2751 somehow. Unfortunately, PMake doesn't allow constructs like
2752 .DS I
2753 $($(FOO))
2754 .DE
2755 What do you do? Hint: no further variable expansion is performed after
2756 modifiers are applied, thus if you cause a $ to occur in the
2757 expansion, that's what will be in the result.
2758 .xH 1 PMake for Gods
2759 .LP
2760 This chapter is devoted to those facilities in PMake that allow you to
2761 do a great deal in a makefile with very little work, as well as do
2762 some things you couldn't do in Make without a great deal of work (and
2763 perhaps the use of other programs). The problem with these features,
2764 is they must be handled with care, or you will end up with a mess.
2765 .LP
2766 Once more, I assume a greater familiarity with
2767 .UX
2768 or Sprite than I did in the previous two chapters.
2769 .xH 2 Search Paths
2770 .Rd 6
2771 .LP
2772 PMake supports the dispersal of files into multiple directories by
2773 allowing you to specify places to look for sources with
2774 .CW .PATH
2775 targets in the makefile. The directories you give as sources for these
2776 targets make up a ``search path.'' Only those files used exclusively
2777 as sources are actually sought on a search path, the assumption being
2778 that anything listed as a target in the makefile can be created by the
2779 makefile and thus should be in the current directory.
2780 .LP
2781 There are two types of search paths
2782 in PMake: one is used for all types of files (including included
2783 makefiles) and is specified with a plain
2784 .CW .PATH
2785 target (e.g.
2786 .CW ".PATH : RCS" ''), ``
2787 while the other is specific to a certain type of file, as indicated by
2788 the file's suffix. A specific search path is indicated by immediately following
2789 the
2790 .CW .PATH
2791 with the suffix of the file. For instance
2792 .DS
2793 \&.PATH.h         : /sprite/lib/include /sprite/att/lib/include
2794 .DE
2795 would tell PMake to look in the directories
2796 .CW /sprite/lib/include
2797 and
2798 .CW /sprite/att/lib/include
2799 for any files whose suffix is
2800 .CW .h .
2801 .LP
2802 The current directory is always consulted first to see if a file
2803 exists. Only if it cannot be found there are the directories in the
2804 specific search path, followed by those in the general search path,
2805 consulted.
2806 .LP
2807 A search path is also used when expanding wildcard characters. If the
2808 pattern has a recognizable suffix on it, the path for that suffix will
2809 be used for the expansion. Otherwise the default search path is employed.
2810 .LP
2811 When a file is found in some directory other than the current one, all
2812 local variables that would have contained the target's name
2813 .CW .ALLSRC , (
2814 and
2815 .CW .IMPSRC )
2816 will instead contain the path to the file, as found by PMake.
2817 Thus if you have a file
2818 .CW ../lib/mumble.c
2819 and a makefile
2820 .DS
2821 \&.PATH.c         : ../lib
2822 mumble          : mumble.c
2823         $(CC) -o $(.TARGET) $(.ALLSRC)
2824 .DE
2825 the command executed to create
2826 .CW mumble
2827 would be
2828 .CW "cc -o mumble ../lib/mumble.c" .'' ``
2829 (As an aside, the command in this case isn't strictly necessary, since
2830 it will be found using transformation rules if it isn't given. This is because
2831 .CW .out
2832 is the null suffix by default and a transformation exists from
2833 .CW .c
2834 to
2835 .CW .out .
2836 Just thought I'd throw that in.)
2837 .LP
2838 If a file exists in two directories on the same search path, the file
2839 in the first directory on the path will be the one PMake uses. So if
2840 you have a large system spread over many directories, it would behoove
2841 you to follow a naming convention that avoids such conflicts.
2842 .LP
2843 Something you should know about the way search paths are implemented
2844 is that each directory is read, and its contents cached, exactly once
2845 \&\*- when it is first encountered \*- so any changes to the
2846 directories while PMake is running will not be noted when searching
2847 for implicit sources, nor will they be found when PMake attempts to
2848 discover when the file was last modified, unless the file was created in the
2849 current directory. While people have suggested that PMake should read
2850 the directories each time, my experience suggests that the caching seldom
2851 causes problems. In addition, not caching the directories slows things
2852 down enormously because of PMake's attempts to apply transformation
2853 rules through non-existent files \*- the number of extra file-system
2854 searches is truly staggering, especially if many files without
2855 suffixes are used and the null suffix isn't changed from
2856 .CW .out .
2857 .xH 2 Archives and Libraries
2858 .LP
2859 .UX
2860 and Sprite allow you to merge files into an archive using the
2861 .CW ar
2862 command. Further, if the files are relocatable object files, you can
2863 run
2864 .CW ranlib
2865 on the archive and get yourself a library that you can link into any
2866 program you want. The main problem with archives is they double the
2867 space you need to store the archived files, since there's one copy in
2868 the archive and one copy out by itself. The problem with libraries is
2869 you usually think of them as
2870 .CW -lm
2871 rather than
2872 .CW /usr/lib/libm.a
2873 and the linker thinks they're out-of-date if you so much as look at
2874 them.
2875 .LP
2876 PMake solves the problem with archives by allowing you to tell it to
2877 examine the files in the archives (so you can remove the individual
2878 files without having to regenerate them later). To handle the problem
2879 with libraries, PMake adds an additional way of deciding if a library
2880 is out-of-date:
2881 .IP \(bu 2
2882 If the table of contents is older than the library, or is missing, the
2883 library is out-of-date.
2884 .LP
2885 A library is any target that looks like
2886 .CW \-l name'' ``
2887 or that ends in a suffix that was marked as a library using the
2888 .CW .LIBS
2889 target.
2890 .CW .a
2891 is so marked in the system makefile.
2892 .LP
2893 Members of an archive are specified as
2894 ``\fIarchive\fP(\fImember\fP[ \fImember\fP...])''.
2895 Thus
2896 .CW libdix.a(window.o) '' ``'
2897 specifies the file
2898 .CW window.o
2899 in the archive
2900 .CW libdix.a .
2901 You may also use wildcards to specify the members of the archive. Just
2902 remember that most the wildcard characters will only find 
2903 .I existing
2904 files.
2905 .LP
2906 A file that is a member of an archive is treated specially. If the
2907 file doesn't exist, but it is in the archive, the modification time
2908 recorded in the archive is used for the file when determining if the
2909 file is out-of-date. When figuring out how to make an archived member target
2910 (not the file itself, but the file in the archive \*- the
2911 \fIarchive\fP(\fImember\fP) target), special care is
2912 taken with the transformation rules, as follows:
2913 .IP \(bu 2
2914 \&\fIarchive\fP(\fImember\fP) is made to depend on \fImember\fP.
2915 .IP \(bu 2
2916 The transformation from the \fImember\fP's suffix to the
2917 \fIarchive\fP's suffix is applied to the \fIarchive\fP(\fImember\fP) target.
2918 .IP \(bu 2
2919 The \fIarchive\fP(\fImember\fP)'s
2920 .CW .TARGET
2921 variable is set to the name of the \fImember\fP if \fImember\fP is
2922 actually a target, or the path to the member file if \fImember\fP is
2923 only a source.
2924 .IP \(bu 2
2925 The
2926 .CW .ARCHIVE
2927 variable for the \fIarchive\fP(\fImember\fP) target is set to the name
2928 of the \fIarchive\fP.
2929 .Ix 0 def variable local .ARCHIVE
2930 .Ix 0 def .ARCHIVE
2931 .IP \(bu 2
2932 The
2933 .CW .MEMBER
2934 variable is set to the actual string inside the parentheses. In most
2935 cases, this will be the same as the
2936 .CW .TARGET
2937 variable.
2938 .Ix 0 def variable local .MEMBER
2939 .Ix 0 def .MEMBER
2940 .IP \(bu 2
2941 The \fIarchive\fP(\fImember\fP)'s place in the local variables of the
2942 targets that depend on it is taken by the value of its
2943 .CW .TARGET
2944 variable.
2945 .LP
2946 Thus, a program library could be created with the following makefile:
2947 .DS
2948 \&.o.a            :
2949         ...
2950         rm -f $(.TARGET:T)
2951 OBJS            = obj1.o obj2.o obj3.o
2952 libprog.a       : libprog.a($(OBJS))
2953         ar cru $(.TARGET) $(.OODATE)
2954         ranlib $(.TARGET)
2955 .DE
2956 This will cause the three object files to be compiled (if the
2957 corresponding source files were modified after the object file or, if
2958 that doesn't exist, the archived object file), the out-of-date ones
2959 archived in
2960 .CW libprog.a ,
2961 a table of contents placed in the archive and the newly-archived
2962 object files to be removed.
2963 .LP
2964 All this is used in the 
2965 .CW makelib.mk
2966 system makefile to create a single library with ease. This makefile
2967 looks like this:
2968 .DS
2969 .SM
2970 #
2971 # Rules for making libraries. The object files that make up the library
2972 # are removed once they are archived.
2973 #
2974 # To make several libraries in parallel, you should define the variable
2975 # "many_libraries". This will serialize the invocations of ranlib.
2976 #
2977 # To use, do something like this:
2978 #
2979 # OBJECTS = <files in the library>
2980 #
2981 # fish.a: fish.a($(OBJECTS)) MAKELIB
2982 #
2983 #
2984
2985 #ifndef _MAKELIB_MK
2986 _MAKELIB_MK     =
2987
2988 #include        <po.mk>
2989
2990 \&.po.a .o.a    :
2991         ...
2992         rm -f $(.MEMBER)
2993
2994 ARFLAGS         ?= crl
2995
2996 #
2997 # Re-archive the out-of-date members and recreate the library's table of
2998 # contents using ranlib. If many_libraries is defined, put the ranlib
2999 # off til the end so many libraries can be made at once.
3000 #
3001 MAKELIB         : .USE .PRECIOUS
3002         ar $(ARFLAGS) $(.TARGET) $(.OODATE)
3003 #ifndef no_ranlib
3004 # ifdef many_libraries
3005         ...
3006 # endif /* many_libraries */
3007         ranlib $(.TARGET)
3008 #endif /* no_ranlib */
3009
3010 #endif /* _MAKELIB_MK */
3011 .DE
3012 .xH 2 On the Condition...
3013 .Rd 1
3014 .LP
3015 Like the C compiler before it, PMake allows you to configure the makefile,
3016 based on the current environment, using conditional statements. A
3017 conditional looks like this:
3018 .DS
3019 #if \fIboolean expression\fP
3020 \fIlines\fP
3021 #elif \fIanother boolean expression\fP
3022 \fImore lines\fP
3023 #else
3024 \fIstill more lines\fP
3025 #endif
3026 .DE
3027 They may be nested to a maximum depth of 30 and may occur anywhere
3028 (except in a comment, of course). The
3029 .CW # '' ``
3030 must the very first character on the line.
3031 .LP
3032 Each
3033 .I "boolean expression"
3034 is made up of terms that look like function calls, the standard C
3035 boolean operators
3036 .CW && ,
3037 .CW || ,
3038 and
3039 .CW ! ,
3040 and the standard relational operators
3041 .CW == ,
3042 .CW != ,
3043 .CW > ,
3044 .CW >= ,
3045 .CW < ,
3046 and
3047 .CW <= ,
3048 with
3049 .CW ==
3050 and
3051 .CW !=
3052 being overloaded to allow string comparisons as well.
3053 .CW &&
3054 represents logical AND;
3055 .CW ||
3056 is logical OR and
3057 .CW !
3058 is logical NOT.  The arithmetic and string operators take precedence
3059 over all three of these operators, while NOT takes precedence over
3060 AND, which takes precedence over OR.  This precedence may be
3061 overridden with parentheses, and an expression may be parenthesized to
3062 your heart's content.  Each term looks like a call on one of four
3063 functions:
3064 .nr pw 9
3065 .Ix 0 def make
3066 .Ix 0 def conditional make
3067 .Ix 0 def if make
3068 .IP make \n(pw
3069 The syntax is
3070 .CW make( \fItarget\fP\c
3071 .CW )
3072 where
3073 .I target
3074 is a target in the makefile. This is true if the given target was
3075 specified on the command line, or as the source for a
3076 .CW .MAIN
3077 target (note that the sources for
3078 .CW .MAIN
3079 are only used if no targets were given on the command line).
3080 .IP defined \n(pw
3081 .Ix 0 def defined
3082 .Ix 0 def conditional defined
3083 .Ix 0 def if defined
3084 The syntax is
3085 .CW defined( \fIvariable\fP\c
3086 .CW )
3087 and is true if
3088 .I variable
3089 is defined. Certain variables are defined in the system makefile that
3090 identify the system on which PMake is being run.
3091 .IP exists \n(pw
3092 .Ix 0 def exists
3093 .Ix 0 def conditional exists
3094 .Ix 0 def if exists
3095 The syntax is
3096 .CW exists( \fIfile\fP\c
3097 .CW )
3098 and is true if the file can be found on the global search path
3099 (i.e. that defined by
3100 .CW .PATH
3101 targets, not by
3102 .CW .PATH \fIsuffix\fP
3103 targets).
3104 .IP empty \n(pw
3105 .Ix 0 def empty
3106 .Ix 0 def conditional empty
3107 .Ix 0 def if empty
3108 This syntax is much like the others, except the string inside the
3109 parentheses is of the same form as you would put between parentheses
3110 when expanding a variable, complete with modifiers and everything. The
3111 function returns true if the resulting string is empty (NOTE: an undefined
3112 variable in this context will cause at the very least a warning
3113 message about a malformed conditional, and at the worst will cause the
3114 process to stop once it has read the makefile. If you want to check
3115 for a variable being defined or empty, use the expression
3116 .CW !defined( \fIvar\fP\c ``
3117 .CW ") || empty(" \fIvar\fP\c
3118 .CW ) ''
3119 as the definition of
3120 .CW ||
3121 will prevent the
3122 .CW empty()
3123 from being evaluated and causing an error, if the variable is
3124 undefined). This can be used to see if a variable contains a given
3125 word, for example:
3126 .DS
3127 #if !empty(\fIvar\fP:M\fIword\fP)
3128 .DE
3129 .LP
3130 The arithmetic and string operators may only be used to test the value
3131 of a variable. The lefthand side must contain the variable expansion,
3132 while the righthand side contains either a string, enclosed in
3133 double-quotes, or a number. The standard C numeric conventions (except
3134 for specifying an octal number) apply to both sides. E.g.
3135 .DS
3136 #if $(OS) == 4.3
3137
3138 #if $(MACHINE) == "sun3"
3139
3140 #if $(LOAD_ADDR) < 0xc000
3141 .DE
3142 are all valid conditionals. In addition, the numeric value of a
3143 variable can be tested as a boolean as follows:
3144 .DS
3145 #if $(LOAD)
3146 .DE
3147 would see if
3148 .CW LOAD
3149 contains a non-zero value and
3150 .DS
3151 #if !$(LOAD)
3152 .DE
3153 would test if
3154 .CW LOAD
3155 contains a zero value.
3156 .LP
3157 In addition to the bare
3158 .CW #if ,'' ``
3159 there are other forms that apply one of the first two functions to each
3160 term. They are as follows:
3161 .DS
3162         ifdef   \fRdefined\fP
3163         ifndef  \fR!defined\fP
3164         ifmake  \fRmake\fP
3165         ifnmake \fR!make\fP
3166 .DE
3167 There are also the ``else if'' forms:
3168 .CW elif ,
3169 .CW elifdef ,
3170 .CW elifndef ,
3171 .CW elifmake ,
3172 and
3173 .CW elifnmake .
3174 .LP
3175 For instance, if you wish to create two versions of a program, one of which
3176 is optimized (the production version) and the other of which is for debugging
3177 (has symbols for dbx), you have two choices: you can create two
3178 makefiles, one of which uses the
3179 .CW \-g
3180 flag for the compilation, while the other uses the
3181 .CW \-O
3182 flag, or you can use another target (call it
3183 .CW debug )
3184 to create the debug version. The construct below will take care of
3185 this for you. I have also made it so defining the variable
3186 .CW DEBUG
3187 (say with
3188 .CW "pmake -D DEBUG" )
3189 will also cause the debug version to be made.
3190 .DS
3191 #if defined(DEBUG) || make(debug)
3192 CFLAGS          += -g
3193 #else
3194 CFLAGS          += -O
3195 #endif
3196 .DE
3197 There are, of course, problems with this approach. The most glaring
3198 annoyance is that if you want to go from making a debug version to
3199 making a production version, you have to remove all the object files,
3200 or you will get some optimized and some debug versions in the same
3201 program. Another annoyance is you have to be careful not to make two
3202 targets that ``conflict'' because of some conditionals in the
3203 makefile. For instance
3204 .DS
3205 #if make(print)
3206 FORMATTER       = ditroff -Plaser_printer
3207 #endif
3208 #if make(draft)
3209 FORMATTER       = nroff -Pdot_matrix_printer
3210 #endif
3211 .DE
3212 would wreak havoc if you tried
3213 .CW "pmake draft print" '' ``
3214 since you would use the same formatter for each target. As I said,
3215 this all gets somewhat complicated.
3216 .xH 2 A Shell is a Shell is a Shell
3217 .Rd 7
3218 .LP
3219 In normal operation, the Bourne Shell (better known as
3220 .CW sh '') ``
3221 is used to execute the commands to re-create targets. PMake also allows you
3222 to specify a different shell for it to use when executing these
3223 commands. There are several things PMake must know about the shell you
3224 wish to use. These things are specified as the sources for the
3225 .CW .SHELL
3226 .Ix 0 ref .SHELL
3227 .Ix 0 ref target .SHELL
3228 target by keyword, as follows:
3229 .IP "\fBpath=\fP\fIpath\fP"
3230 PMake needs to know where the shell actually resides, so it can
3231 execute it. If you specify this and nothing else, PMake will use the
3232 last component of the path and look in its table of the shells it
3233 knows and use the specification it finds, if any. Use this if you just
3234 want to use a different version of the Bourne or C Shell (yes, PMake knows
3235 how to use the C Shell too).
3236 .IP "\fBname=\fP\fIname\fP"
3237 This is the name by which the shell is to be known. It is a single
3238 word and, if no other keywords are specified (other than
3239 .B path ),
3240 it is the name by which PMake attempts to find a specification for
3241 it (as mentioned above). You can use this if you would just rather use
3242 the C Shell than the Bourne Shell
3243 .CW ".SHELL: name=csh" '' (``
3244 will do it).
3245 .IP "\fBquiet=\fP\fIecho-off command\fP"
3246 As mentioned before, PMake actually controls whether commands are
3247 printed by introducing commands into the shell's input stream. This
3248 keyword, and the next two, control what those commands are. The
3249 .B quiet
3250 keyword is the command used to turn echoing off. Once it is turned
3251 off, echoing is expected to remain off until the echo-on command is given.
3252 .IP "\fBecho=\fP\fIecho-on command\fP"
3253 The command PMake should give to turn echoing back on again.
3254 .IP "\fBfilter=\fP\fIprinted echo-off command\fP"
3255 Many shells will echo the echo-off command when it is given. This
3256 keyword tells PMake in what format the shell actually prints the
3257 echo-off command. Wherever PMake sees this string in the shell's
3258 output, it will delete it and any following whitespace, up to and
3259 including the next newline. See the example at the end of this section
3260 for more details.
3261 .IP "\fBechoFlag=\fP\fIflag to turn echoing on\fP"
3262 Unless a target has been marked
3263 .CW .SILENT ,
3264 PMake wants to start the shell running with echoing on. To do this, it
3265 passes this flag to the shell as one of its arguments. If either this
3266 or the next flag begins with a `\-', the flags will be passed to the
3267 shell as separate arguments. Otherwise, the two will be concatenated
3268 (if they are used at the same time, of course).
3269 .IP "\fBerrFlag=\fP\fIflag to turn error checking on\fP"
3270 Likewise, unless a target is marked
3271 .CW .IGNORE ,
3272 PMake wishes error-checking to be on from the very start. To this end,
3273 it will pass this flag to the shell as an argument. The same rules for
3274 an initial `\-' apply as for the
3275 .B echoFlag .
3276 .IP "\fBcheck=\fP\fIcommand to turn error checking on\fP"
3277 Just as for echo-control, error-control is achieved by inserting
3278 commands into the shell's input stream. This is the command to make
3279 the shell check for errors. It also serves another purpose if the
3280 shell doesn't have error-control as commands, but I'll get into that
3281 in a minute. Again, once error checking has been turned on, it is
3282 expected to remain on until it is turned off again.
3283 .IP "\fBignore=\fP\fIcommand to turn error checking off\fP"
3284 This is the command PMake uses to turn error checking off. It has
3285 another use if the shell doesn't do error-control, but I'll tell you
3286 about that.\|.\|.\|now.
3287 .IP "\fBhasErrCtl=\fP\fIyes or no\fP"
3288 This takes a value that is either
3289 .B yes
3290 or
3291 .B no .
3292 Now you might think that the existence of the
3293 .B check
3294 and
3295 .B ignore
3296 keywords would be enough to tell PMake if the shell can do
3297 error-control, but you'd be wrong. If
3298 .B hasErrCtl
3299 is
3300 .B yes ,
3301 PMake uses the check and ignore commands in a straight-forward manner.
3302 If this is
3303 .B no ,
3304 however, their use is rather different. In this case, the check
3305 command is used as a template, in which the string
3306 .B %s
3307 is replaced by the command that's about to be executed, to produce a
3308 command for the shell that will echo the command to be executed. The
3309 ignore command is also used as a template, again with
3310 .B %s
3311 replaced by the command to be executed, to produce a command that will
3312 execute the command to be executed and ignore any error it returns.
3313 When these strings are used as templates, you must provide newline(s)
3314 .CW \en '') (``
3315 in the appropriate place(s).
3316 .LP
3317 The strings that follow these keywords may be enclosed in single or
3318 double quotes (the quotes will be stripped off) and may contain the
3319 usual C backslash-characters (\en is newline, \er is return, \eb is
3320 backspace, \e' escapes a single-quote inside single-quotes, \e"
3321 escapes a double-quote inside double-quotes). Now for an example.
3322 .LP
3323 This is actually the contents of the
3324 .CW <shx.mk>
3325 system makefile, and causes PMake to use the Bourne Shell in such a
3326 way that each command is printed as it is executed. That is, if more
3327 than one command is given on a line, each will be printed separately.
3328 Similarly, each time the body of a loop is executed, the commands
3329 within that loop will be printed, etc. The specification runs like
3330 this:
3331 .DS
3332 #
3333 # This is a shell specification to have the Bourne shell echo
3334 # the commands just before executing them, rather than when it reads
3335 # them. Useful if you want to see how variables are being expanded, etc.
3336 #
3337 \&.SHELL        : path=/bin/sh \e
3338         quiet="set -" \e
3339         echo="set -x" \e
3340         filter="+ set - " \e
3341         echoFlag=x \e
3342         errFlag=e \e
3343         hasErrCtl=yes \e
3344         check="set -e" \e
3345         ignore="set +e"
3346 .DE
3347 .LP
3348 It tells PMake the following:
3349 .Bp
3350 The shell is located in the file
3351 .CW /bin/sh .
3352 It need not tell PMake that the name of the shell is
3353 .CW sh 
3354 as PMake can figure that out for itself (it's the last component of
3355 the path).
3356 .Bp
3357 The command to stop echoing is
3358 .CW "set -" .
3359 .Bp
3360 The command to start echoing is
3361 .CW "set -x" .
3362 .Bp
3363 When the echo off command is executed, the shell will print
3364 .CW "+ set - " 
3365 (The `+' comes from using the
3366 .CW \-x
3367 flag (rather than the
3368 .CW \-v
3369 flag PMake usually uses)). PMake will remove all occurrences of this
3370 string from the output, so you don't notice extra commands you didn't
3371 put there.
3372 .Bp
3373 The flag the Bourne Shell will take to start echoing in this way is
3374 the
3375 .CW \-x
3376 flag. The Bourne Shell will only take its flag arguments concatenated
3377 as its first argument, so neither this nor the
3378 .B errFlag
3379 specification begins with a \-.
3380 .Bp
3381 The flag to use to turn error-checking on from the start is
3382 .CW \-e .
3383 .Bp
3384 The shell can turn error-checking on and off, and the commands to do
3385 so are
3386 .CW "set +e"
3387 and
3388 .CW "set -e" ,
3389 respectively.
3390 .LP
3391 I should note that this specification is for Bourne Shells that are
3392 not part of Berkeley
3393 .UX ,
3394 as shells from Berkeley don't do error control. You can get a similar
3395 effect, however, by changing the last three lines to be:
3396 .DS
3397         hasErrCtl=no \e
3398         check="echo \e"+ %s\e"\en" \e
3399         ignore="sh -c '%s || exit 0\en"
3400 .DE
3401 .LP
3402 This will cause PMake to execute the two commands
3403 .DS
3404 echo "+ \fIcmd\fP"
3405 sh -c '\fIcmd\fP || true'
3406 .DE
3407 for each command for which errors are to be ignored. (In case you are
3408 wondering, the thing for
3409 .CW ignore
3410 tells the shell to execute another shell without error checking on and
3411 always exit 0, since the
3412 .B ||
3413 causes the
3414 .CW "exit 0"
3415 to be executed only if the first command exited non-zero, and if the
3416 first command exited zero, the shell will also exit zero, since that's
3417 the last command it executed).
3418 .xH 2 Compatibility
3419 .Ix 0 ref compatibility
3420 .LP
3421 There are three (well, 3 \(12) levels of backwards-compatibility built
3422 into PMake.  Most makefiles will need none at all. Some may need a
3423 little bit of work to operate correctly when run in parallel. Each
3424 level encompasses the previous levels (e.g.
3425 .B \-B
3426 (one shell per command) implies
3427 .B \-V )
3428 The three levels are described in the following three sections.
3429 .xH 3 DEFCON 3 \*- Variable Expansion
3430 .Ix 0 ref compatibility
3431 .LP
3432 As noted before, PMake will not expand a variable unless it knows of a
3433 value for it. This can cause problems for makefiles that expect to
3434 leave variables undefined except in special circumstances (e.g. if
3435 more flags need to be passed to the C compiler or the output from a
3436 text processor should be sent to a different printer). If the
3437 variables are enclosed in curly braces
3438 .CW ${PRINTER} ''), (``
3439 the shell will let them pass. If they are enclosed in parentheses,
3440 however, the shell will declare a syntax error and the make will come
3441 to a grinding halt.
3442 .LP
3443 You have two choices: change the makefile to define the variables
3444 (their values can be overridden on the command line, since that's
3445 where they would have been set if you used Make, anyway) or always give the
3446 .B \-V
3447 flag (this can be done with the
3448 .CW .MAKEFLAGS
3449 target, if you want).
3450 .xH 3 DEFCON 2 \*- The Number of the Beast
3451 .Ix 0 ref compatibility
3452 .LP
3453 Then there are the makefiles that expect certain commands, such as
3454 changing to a different directory, to not affect other commands in a
3455 target's creation script. You can solve this is either by going
3456 back to executing one shell per command (which is what the
3457 .B \-B
3458 flag forces PMake to do), which slows the process down a good bit and
3459 requires you to use semicolons and escaped newlines for shell constructs, or
3460 by changing the makefile to execute the offending command(s) in a subshell
3461 (by placing the line inside parentheses), like so:
3462 .DS
3463 install :: .MAKE
3464         (cd src; $(.PMAKE) install)
3465         (cd lib; $(.PMAKE) install)
3466         (cd man; $(.PMAKE) install)
3467 .DE
3468 .Ix 0 ref operator double-colon
3469 .Ix 0 ref variable global .PMAKE
3470 .Ix 0 ref .PMAKE
3471 .Ix 0 ref .MAKE
3472 .Ix 0 ref attribute .MAKE
3473 This will always execute the three makes (even if the
3474 .B \-n
3475 flag was given) because of the combination of the ``::'' operator and
3476 the
3477 .CW .MAKE
3478 attribute. Each command will change to the proper directory to perform
3479 the install, leaving the main shell in the directory in which it started.
3480 .xH 3 "DEFCON 1 \*- Imitation is the Not the Highest Form of Flattery"
3481 .Ix 0 ref compatibility
3482 .LP
3483 The final category of makefile is the one where every command requires
3484 input, the dependencies are incompletely specified, or you simply
3485 cannot create more than one target at a time, as mentioned earlier. In
3486 addition, you may not have the time or desire to upgrade the makefile
3487 to run smoothly with PMake. If you are the conservative sort, this is
3488 the compatibility mode for you. It is entered either by giving PMake
3489 the
3490 .B \-M
3491 flag (for Make), or by executing PMake as
3492 .CW make .'' ``
3493 In either case, PMake performs things exactly like Make (while still
3494 supporting most of the nice new features PMake provides). This
3495 includes:
3496 .IP \(bu 2
3497 No parallel execution.
3498 .IP \(bu 2
3499 Targets are made in the exact order specified by the makefile. The
3500 sources for each target are made in strict left-to-right order, etc.
3501 .IP \(bu 2
3502 A single Bourne shell is used to execute each command, thus the
3503 shell's
3504 .CW $$
3505 variable is useless, changing directories doesn't work across command
3506 lines, etc.
3507 .IP \(bu 2
3508 If no special characters exist in a command line, PMake will break the
3509 command into words itself and execute the command directly, without
3510 executing a shell first. The characters that cause PMake to execute a
3511 shell are:
3512 .CW # ,
3513 .CW = ,
3514 .CW | ,
3515 .CW ^ ,
3516 .CW ( ,
3517 .CW ) ,
3518 .CW { ,
3519 .CW } ,
3520 .CW ; ,
3521 .CW & ,
3522 .CW < ,
3523 .CW > ,
3524 .CW * ,
3525 .CW ? ,
3526 .CW [ ,
3527 .CW ] ,
3528 .CW : ,
3529 .CW $ ,
3530 .CW ` ,
3531 and
3532 .CW \e .
3533 You should notice that these are all the characters that are given
3534 special meaning by the shell (except
3535 .CW '
3536 and
3537 .CW " ,
3538 which PMake deals with all by its lonesome).
3539 .IP \(bu 2
3540 The use of the null suffix is turned off.
3541 .Ix 0 ref "null suffix"
3542 .Ix 0 ref suffix null
3543 .xH 2 The Way Things Work
3544 .LP
3545 When PMake reads the makefile, it parses sources and targets into
3546 nodes in a graph. The graph is directed only in the sense that PMake
3547 knows which way is up. Each node contains not only links to all its
3548 parents and children (the nodes that depend on it and those on which
3549 it depends, respectively), but also a count of the number of its
3550 children that have already been processed.
3551 .LP
3552 The most important thing to know about how PMake uses this graph is
3553 that the traversal is breadth-first and occurs in two passes.
3554 .LP
3555 After PMake has parsed the makefile, it begins with the nodes the user
3556 has told it to make (either on the command line, or via a 
3557 .CW .MAIN
3558 target, or by the target being the first in the file not labeled with
3559 the
3560 .CW .NOTMAIN
3561 attribute) placed in a queue. It continues to take the node off the
3562 front of the queue, mark it as something that needs to be made, pass
3563 the node to 
3564 .CW Suff_FindDeps
3565 (mentioned earlier) to find any implicit sources for the node, and
3566 place all the node's children that have yet to be marked at the end of
3567 the queue. If any of the children is a
3568 .CW .USE
3569 rule, its attributes are applied to the parent, then its commands are
3570 appended to the parent's list of commands and its children are linked
3571 to its parent. The parent's unmade children counter is then decremented
3572 (since the
3573 .CW .USE
3574 node has been processed). You will note that this allows a
3575 .CW .USE
3576 node to have children that are
3577 .CW .USE
3578 nodes and the rules will be applied in sequence.
3579 If the node has no children, it is placed at the end of
3580 another queue to be examined in the second pass. This process
3581 continues until the first queue is empty.
3582 .LP
3583 At this point, all the leaves of the graph are in the examination
3584 queue. PMake removes the node at the head of the queue and sees if it
3585 is out-of-date. If it is, it is passed to a function that will execute
3586 the commands for the node asynchronously. When the commands have
3587 completed, all the node's parents have their unmade children counter
3588 decremented and, if the counter is then 0, they are placed on the
3589 examination queue. Likewise, if the node is up-to-date. Only those
3590 parents that were marked on the downward pass are processed in this
3591 way. Thus PMake traverses the graph back up to the nodes the user
3592 instructed it to create. When the examination queue is empty and no
3593 shells are running to create a target, PMake is finished.
3594 .LP
3595 Once all targets have been processed, PMake executes the commands
3596 attached to the
3597 .CW .END
3598 target, either explicitly or through the use of an ellipsis in a shell
3599 script. If there were no errors during the entire process but there
3600 are still some targets unmade (PMake keeps a running count of how many
3601 targets are left to be made), there is a cycle in the graph. PMake does
3602 a depth-first traversal of the graph to find all the targets that
3603 weren't made and prints them out one by one.
3604 .xH 1 Answers to Exercises
3605 .IP (3.1)
3606 This is something of a trick question, for which I apologize. The
3607 trick comes from the UNIX definition of a suffix, which PMake doesn't
3608 necessarily share. You will have noticed that all the suffixes used in
3609 this tutorial (and in UNIX in general) begin with a period
3610 .CW .ms , (
3611 .CW .c ,
3612 etc.). Now, PMake's idea of a suffix is more like English's: it's the
3613 characters at the end of a word. With this in mind, one possible
3614 .Ix 0 def suffix
3615 solution to this problem goes as follows:
3616 .DS I
3617 \&.SUFFIXES       : ec.exe .exe ec.obj .obj .asm
3618 ec.objec.exe .obj.exe :
3619         link -o $(.TARGET) $(.IMPSRC)
3620 \&.asmec.obj      :
3621         asm -o $(.TARGET) -DDO_ERROR_CHECKING $(.IMPSRC)
3622 \&.asm.obj        :
3623         asm -o $(.TARGET) $(.IMPSRC)
3624 .DE
3625 .IP (3.2)
3626 The trick to this one lies in the ``:='' variable-assignment operator
3627 and the ``:S'' variable-expansion modifier. 
3628 .Ix 0 ref variable assignment expanded
3629 .Ix 0 ref variable expansion modified
3630 .Ix 0 ref modifier substitute
3631 .Ix 0 ref :S
3632 .Ix 0 ref :=
3633 Basically what you want is to take the pointer variable, so to speak,
3634 and transform it into an invocation of the variable at which it
3635 points. You might try something like
3636 .DS I
3637 $(PTR:S/^/\e$(/:S/$/))
3638 .DE
3639 which places
3640 .CW $( '' ``
3641 at the front of the variable name and
3642 .CW ) '' ``
3643 at the end, thus transforming
3644 .CW VAR ,'' ``
3645 for example, into
3646 .CW $(VAR) ,'' ``
3647 which is just what we want. Unfortunately (as you know if you've tried
3648 it), since, as it says in the hint, PMake does no further substitution
3649 on the result of a modified expansion, that's \fIall\fP you get. The
3650 solution is to make use of ``:='' to place that string into yet
3651 another variable, then invoke the other variable directly:
3652 .DS I
3653 *PTR            := $(PTR:S/^/\e$(/:S/$/)/)
3654 .DE
3655 You can then use
3656 .CW $(*PTR) '' ``
3657 to your heart's content.
3658 .de Gp
3659 .XP
3660 \&\fB\\$1:\fP
3661 ..
3662 .xH 1 Glossary of Jargon
3663 .Gp "attribute"
3664 A property given to a target that causes PMake to treat it differently.
3665 .Gp "command script"
3666 The lines immediately following a dependency line that specify
3667 commands to execute to create each of the targets on the dependency
3668 line. Each line in the command script must begin with a tab.
3669 .Gp "command-line variable"
3670 A variable defined in an argument when PMake is first executed.
3671 Overrides all assignments to the same variable name in the makefile.
3672 .Gp "conditional"
3673 A construct much like that used in C that allows a makefile to be
3674 configured on the fly based on the local environment, or on what is being
3675 made by that invocation of PMake.
3676 .Gp "creation script"
3677 Commands used to create a target. See ``command script.''
3678 .Gp "dependency"
3679 The relationship between a source and a target. This comes in three
3680 flavors, as indicated by the operator between the target and the
3681 source. `:' gives a straight time-wise dependency (if the target is
3682 older than the source, the target is out-of-date), while `!' provides
3683 simply an ordering and always considers the target out-of-date. `::'
3684 is much like `:', save it creates multiple instances of a target each
3685 of which depends on its own list of sources.
3686 .Gp "dynamic source"
3687 This refers to a source that has a local variable invocation in it. It
3688 allows a single dependency line to specify a different source for each
3689 target on the line.
3690 .Gp "global variable"
3691 Any variable defined in a makefile. Takes precedence over variables
3692 defined in the environment, but not over command-line or local variables.
3693 .Gp "input graph"
3694 What PMake constructs from a makefile. Consists of nodes made of the
3695 targets in the makefile, and the links between them (the
3696 dependencies). The links are directed (from source to target) and
3697 there may not be any cycles (loops) in the graph.
3698 .Gp "local variable"
3699 A variable defined by PMake visible only in a target's shell script.
3700 There are seven local variables, not all of which are defined for
3701 every target:
3702 .CW .TARGET ,
3703 .CW .ALLSRC ,
3704 .CW .OODATE ,
3705 .CW .PREFIX ,
3706 .CW .IMPSRC ,
3707 .CW .ARCHIVE ,
3708 and
3709 .CW .MEMBER .
3710 .CW .TARGET ,
3711 .CW .PREFIX ,
3712 .CW .ARCHIVE ,
3713 and 
3714 .CW .MEMBER
3715 may be used on dependency lines to create ``dynamic sources.''
3716 .Gp "makefile"
3717 A file that describes how a system is built. If you don't know what it
3718 is after reading this tutorial.\|.\|.\|.
3719 .Gp "modifier"
3720 A letter, following a colon, used to alter how a variable is expanded.
3721 It has no effect on the variable itself.
3722 .Gp "operator"
3723 What separates a source from a target (on a dependency line) and specifies
3724 the relationship between the two. There are three:
3725 .CW : ', `
3726 .CW :: ', `
3727 and
3728 .CW ! '. `
3729 .Gp "search path"
3730 A list of directories in which a file should be sought. PMake's view
3731 of the contents of directories in a search path does not change once
3732 the makefile has been read. A file is sought on a search path only if
3733 it is exclusively a source.
3734 .Gp "shell"
3735 A program to which commands are passed in order to create targets.
3736 .Gp "source"
3737 Anything to the right of an operator on a dependency line. Targets on
3738 the dependency line are usually created from the sources.
3739 .Gp "special target"
3740 A target that causes PMake to do special things when it's encountered.
3741 .Gp "suffix"
3742 The tail end of a file name. Usually begins with a period,
3743 .CW .c
3744 or
3745 .CW .ms ,
3746 e.g.
3747 .Gp "target"
3748 A word to the left of the operator on a dependency line. More
3749 generally, any file that PMake might create. A file may be (and often
3750 is) both a target and a source (what it is depends on how PMake is
3751 looking at it at the time \*- sort of like the wave/particle duality
3752 of light, you know).
3753 .Gp "transformation rule"
3754 A special construct in a makefile that specifies how to create a file
3755 of one type from a file of another, as indicated by their suffixes.
3756 .Gp "variable expansion"
3757 The process of substituting the value of a variable for a reference to
3758 it. Expansion may be altered by means of modifiers.
3759 .Gp "variable"
3760 A place in which to store text that may be retrieved later. Also used
3761 to define the local environment. Conditionals exist that test whether
3762 a variable is defined or not.
3763 .bp
3764 .\" Output table of contents last, with an entry for the index, making
3765 .\" sure to save and restore the last real page number for the index...
3766 .nr @n \n(PN+1
3767 .\" We are not generating an index
3768 .\" .XS \n(@n
3769 .\" Index
3770 .\" .XE
3771 .nr %% \n%
3772 .PX
3773 .nr % \n(%%