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