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