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