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