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