Subversion Repositories planix.SVN

Rev

Rev 2 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 - 1
.HTML "Maintaining Files on Plan 9 with Mk
2
.TL
3
Maintaining Files on Plan 9 with Mk
4
.AU
5
Andrew G. Hume
6
andrew@research.att.com
7
Bob Flandrena
8
bobf@plan9.bell-labs.com
9
.AB
10
.PP
11
.CW Mk
12
is a tool
13
for describing and maintaining dependencies between
14
files.
15
It is similar to the
16
UNIX program
17
.CW make ,
18
but provides several extensions.
19
.CW Mk\fR'\fPs
20
flexible rule specifications, implied
21
dependency derivation, and parallel
22
execution of maintenance actions are
23
well-suited to the Plan 9 environment.
24
Almost all Plan 9 maintenance procedures
25
are automated using
26
.CW mk .
27
.AE
28
.NH 1
29
Introduction
30
.PP
31
This document describes how
32
.CW mk ,
33
a program functionally similar to
34
.CW make
35
[Feld79],
36
is used to maintain dependencies between
37
files in Plan 9.
38
.CW Mk
39
provides several extensions to the
40
capabilities of its predecessor that work
41
well in Plan 9's distributed, multi-architecture
42
environment.  It
43
exploits the power of multiprocessors by executing
44
maintenance actions in parallel and interacts with
45
the Plan 9 command interpreter
46
.CW rc
47
to provide a powerful set of maintenance tools.
48
It accepts pattern-based dependency specifications
49
that are not limited to describing
50
rules for program construction.
51
The result is a tool that is flexible enough to
52
perform many maintenance tasks including
53
database maintenance,
54
hardware design, and document production.
55
.PP
56
This document begins by discussing 
57
the syntax of the control file,
58
the pattern matching capabilities, and
59
the special rules for maintaining archives.
60
A brief description of
61
.CW mk\fR'\fPs
62
algorithm for deriving dependencies
63
is followed by a discussion
64
of the conventions used to resolve ambiguous
65
specifications.  The final sections
66
describe parallel execution
67
and special features.
68
.PP
69
An earlier paper [Hume87]
70
provides a detailed discussion of
71
.CW mk\fR'\fPs
72
design and an appendix summarizes
73
the differences between
74
.CW mk
75
and
76
.CW make .
77
.NH 1
78
The \f(CWMkfile\fP
79
.PP
80
.CW Mk
81
reads a file describing relationships among files
82
and executes commands to bring the files up to date.
83
The specification file, called a
84
.CW mkfile ,
85
contains three types of statements:
86
assignments, includes, and rules.
87
Assignment and include statements are similar
88
to those in C.
89
Rules specify dependencies between a
90
.I target
91
and its
92
.I prerequisites .
93
When the target and prerequisites are files, their
94
modification times determine if they
95
are out of date.  Rules often contain a
96
.I recipe ,
97
an
98
.I rc (1)
99
script that produces the target from
100
the prerequisites.
101
.PP
102
This simple
103
.CW mkfile
104
produces an executable
105
from a C source file:
106
.P1
107
CC=pcc
108
f1:	f1.c
109
	$CC -o f1 f1.c
110
.P2
111
The first line assigns the name of the portable ANSI/POSIX compiler
112
to the
113
.CW mk
114
variable
115
.CW CC ;
116
subsequent references of the form
117
.CW $CC
118
select this compiler.
119
The only rule specifies a dependence between the target file
120
.CW f1
121
and the prerequisite file
122
.CW f1.c .
123
If the target does not exist or if the
124
prerequisite has been modified more recently than
125
the target,
126
.CW mk
127
passes the recipe to
128
.CW rc
129
for execution.  Here,
130
.CW f1.c
131
is compiled and loaded to produce
132
.CW f1 .
133
.PP
134
The native Plan 9 environment
135
requires executables for
136
all architectures, not only the current one.
137
The Plan 9 version of the same
138
.CW mkfile
139
looks like:
140
.P1
141
</$objtype/mkfile
142
 
143
f1:	f1.$O
144
	$LD $LDFLAGS -o f1 f1.$O
145
f1.$O:	f1.c
146
	$CC $CFLAGS f1.c
147
.P2
148
The first line is an include statement
149
that replaces itself with the contents of the file
150
.CW /$objtype/mkfile .
151
The variable
152
.CW $objtype
153
is inherited from the environment and
154
contains the name of the target architecture.
155
The prototype
156
.CW mkfile
157
for that architecture defines architecture-specific variables:
158
.CW CC
159
and
160
.CW LD
161
are the names of the compiler and loader,
162
.CW O 
163
is the code character of the architecture.
164
The rules compile the source file into an object
165
file and invoke the loader to produce
166
.CW f1 .
167
Invoking
168
.CW mk
169
from the command line as follows
170
.P1
171
% objtype=mips mk
172
vc -w f1.c
173
vl $LDFLAGS -o f1 f1.k
174
%
175
.P2
176
produces the
177
.CW mips
178
executable of program
179
.CW f1
180
regardless of the current architecture type.
181
.PP
182
We can extend the
183
.CW mkfile
184
to build two programs:
185
.P1
186
</$objtype/mkfile
187
ALL=f1 f2
188
 
189
all:V:	$ALL
190
 
191
f1:	f1.$O
192
	$LD $LDFLAGS -o f1 f1.$O
193
f1.$O:	f1.c
194
	$CC $CFLAGS f1.c
195
f2:	f2.$O
196
	$LD $LDFLAGS -o f2 f2.$O
197
f2.$O:	f2.c
198
	$CC $CFLAGS f2.c
199
.P2
200
The target
201
.CW all ,
202
modified by the
203
.I attribute
204
.CW V ,
205
builds both programs.
206
The attribute identifies 
207
.CW all
208
as a dummy target that is
209
not related to a file of the same name;
210
its precise effect is explained later.
211
This example describes cascading dependencies:
212
the first target depends on another which depends on a third and
213
so on.
214
Here, individual rules build each
215
program; later we'll see how to do this with a
216
general rule.
217
.NH 1
218
Variables and the environment
219
.PP
220
.CW Mk
221
does not distinguish between its
222
internal variables and
223
.CW rc
224
variables in the environment.
225
When
226
.CW mk
227
starts, it imports each environment variable into a
228
.CW mk
229
variable of the same name.  Before executing a recipe,
230
.CW mk
231
exports all variables, including those
232
inherited from the environment,
233
to the environment in which
234
.CW rc
235
executes the recipe.
236
.PP
237
There are several ways for a
238
variable to take a value.
239
It can be set with an assignment statement,
240
inherited from the environment, or specified
241
on the command line.
242
.CW Mk
243
also maintains several special internal variables
244
that are described in
245
.I mk (1).
246
Assignments have the following decreasing order of precedence:
247
.LP
248
.in .7i
249
1)  Command line assignment
250
.br
251
2)  Assignment statement
252
.br
253
3)  Imported from the environment
254
.br
255
4)  Implicitly set by \f(CWmk\fP
256
.in 0
257
.LP
258
For example, a command line assignment overrides
259
a value imported from the environment.
260
.PP
261
All variable values are strings.  They can be
262
used for pattern matching and
263
comparison but not for arithmetic.
264
A
265
.I list
266
is a string containing several values separated by
267
white space.  Each member is
268
handled individually during pattern matching,
269
target selection, and prerequisite evaluation.
270
.PP
271
A
272
.I namelist
273
is a list produced by
274
transforming the members of an existing list.
275
The transform applies a pattern to each member,
276
replacing each matched string with a new string,
277
much as in the substitute command in
278
.I sam (1)
279
or
280
.I ed (1).
281
The syntax is
282
.P1
283
${\fIvar\fP:A%B=C%D}
284
.P2
285
where
286
.I var
287
is a variable.
288
The pattern
289
.CW A%B
290
matches a member beginning with the string
291
.I A
292
and ending with the string
293
.I B
294
with any string in between;
295
it behaves like the regular expression
296
.CW A.*B .
297
When a member of the
298
.I var
299
list
300
matches this pattern,
301
the string
302
.I C
303
replaces
304
.I A ,
305
.I D
306
replaces
307
.I B ,
308
and the matched string replaces itself.
309
Any of
310
.I A ,
311
.I B ,
312
.I C ,
313
or
314
.I D
315
may be the empty string.  In effect, a namelist is
316
generated by applying the
317
.I ed (1)
318
substitute command
319
.P1
320
	s/\fIA\fP(.*)\fIB\fP/\fIC\fP\e1\fID\fP/
321
.P2
322
to each member of a variable list.
323
.PP
324
Namelists are useful for generating
325
a list based on a predictable transformation.
326
For example,
327
.P1
328
	SRC=a.c b.c c.c
329
	OBJ=${SRC:%.c=%.v}
330
.P2
331
assigns the list \f(CW(a.v b.v c.v)\fP to
332
.CW OBJ .
333
A namelist may be used anywhere a variable is allowed
334
except in a recipe.
335
.PP
336
Command output is assigned to a variable
337
using the normal
338
.CW rc
339
syntax:
340
.P1
341
	var=`{rc command}
342
.P2
343
The command executes in an environment populated
344
with previously assigned variables, including those
345
inherited from
346
.CW mk\fR'\fPs
347
execution environment.
348
The command may
349
be arbitrarily complex; for example,
350
.P1
351
	TARG=`{ls -d *.[cy] | sed 's/..$//'}
352
.P2
353
assigns a list of the C and yacc source files in the current
354
directory, stripped of their suffix, to the variable
355
.CW TARG .
356
.NH 1
357
The include statement
358
.PP
359
The include statement
360
replaces itself with the contents of a file.
361
It is functionally similar to the C
362
.CW #include
363
statement but uses a different syntax:
364
.P1
365
	<\fIfilename\fP
366
.P2
367
The contents of the file are evaluated
368
as they are read.
369
An include statement may be used anywhere except
370
in a recipe.
371
.PP
372
Unlike
373
.CW make ,
374
.CW mk
375
has no built-in rules.  Instead,
376
the include statement allows generic rules
377
to be imported from a prototype
378
.CW mkfile ;
379
most Plan 9
380
.CW mkfiles
381
use this approach [Flan95].
382
.NH 1
383
Rules
384
.PP
385
A rule has four elements: targets,
386
prerequisites, attributes, and a recipe.
387
It has the form:
388
.P1
389
\fItargets\fP:\fIattributes\fP:\fIprerequisites\fP
390
	\fIrecipe\fP
391
.P2
392
The first line, containing the
393
targets, attributes, and prerequisites is
394
the
395
.I "rule header" ;
396
it
397
must begin at the left margin.
398
The recipe contains zero or more lines,
399
each of which begins with white space.
400
One or more targets must be specified but the
401
attributes, prerequisites, and recipe are optional.
402
A rule specifies
403
a dependency between the target(s) and its prerequisite(s),
404
the recipe brings the target(s)
405
up to date with the prerequisite(s) and
406
attributes modify
407
.CW mk\fR'\fPs
408
evaluation of the dependency.
409
.PP
410
Normally the target is a file that depends
411
on one or more prerequisite files.
412
.CW Mk
413
compares the modification times of each target
414
and each prerequisite; a target is considered out of date
415
when it does not exist or when a prerequisite has been modified
416
more recently.
417
When a target is out of date,
418
.CW mk
419
executes the
420
recipe to bring it up to date.
421
When the recipe completes,
422
the modification time of the target is checked and
423
used in later dependency evaluations.
424
If the recipe does not update the target,
425
evaluation continues with the out of date target.
426
.PP
427
A prerequisite of one rule
428
may be the target of another.  When
429
this happens, the rules cascade
430
to define a multi-step procedure.
431
For example,
432
an executable target depends on prerequisite
433
object files, each of which is a target
434
in a rule with a C source file as the prerequisite.
435
.CW Mk
436
follows a chain of dependencies until it encounters
437
a prerequisite that is not a target of another rule
438
or it finds a target that
439
is up to date.  It then
440
executes the recipes in reverse order to produce
441
the desired target.
442
.PP
443
The rule header is evaluated when the rule is read.
444
Variables are replaced by their values, namelists are
445
generated, and
446
commands are replaced by their
447
output at this time.
448
.PP
449
Most attributes modify
450
.CW mk\fR'\fPs
451
evaluation of a rule.
452
An attribute is usually a single letter but some
453
are more complicated.
454
This paper only discusses commonly used attributes;
455
see
456
.I mk (1)
457
for a complete list.
458
.PP
459
The
460
.CW V
461
attribute identifies a
462
.I virtual 
463
target;
464
that is, a target that is not a file.
465
For example,
466
.P1
467
clean:V:
468
	rm *.$O $O.out
469
.P2
470
removes executables and compiler intermediate files.
471
The target is virtual because it does not refer to a file named
472
.CW clean .
473
Without the attribute, the recipe would not be
474
executed if a file named
475
.CW clean 
476
existed.
477
The
478
.CW Q
479
attribute
480
silences the printing of a recipe before
481
execution.
482
It is useful when the output of a recipe is
483
similar to the recipe:
484
.P1
485
default:QV:
486
	echo 'No default target; use mk all or mk install'
487
.P2
488
.PP
489
The recipe is an
490
.CW rc
491
script.  It is optional but when it is
492
missing, the rule is handled specially, as described later.
493
Unlike
494
.CW make ,
495
.CW mk
496
executes recipes without interpretation.
497
After
498
stripping the first white space character from each line
499
it passes the entire recipe to
500
.CW rc 
501
on standard input.
502
Since
503
.CW mk
504
does not interpret a recipe,
505
escape conventions are exactly those of
506
.CW rc .
507
Scripts for
508
.CW awk
509
and
510
.CW sed
511
commands can be embedded exactly as they would
512
be entered from the command line.
513
.CW Mk
514
invokes
515
.CW rc
516
with the
517
.CW -e
518
flag, which causes
519
.CW rc
520
to stop if any command
521
in the recipe exits with a non-zero status; the
522
.CW E
523
attribute overrides this behavior and allows
524
.CW rc
525
to continue executing in the face of errors.
526
Before a recipe is executed, variables are exported
527
to the environment where they are available to
528
.CW rc .
529
Commands in the recipe may not read from
530
standard input because
531
.CW mk
532
uses it internally.
533
.PP
534
References to a variable can yield different
535
values depending on the location of the
536
reference in the
537
.CW mkfile .
538
.CW Mk
539
resolves variable references
540
in assignment statements and rule headers
541
when the statement is read.  Variable references
542
in recipes are evaluated by
543
.CW rc
544
when the recipe is executed; this
545
happens after the entire
546
.CW mkfile
547
has been read.  The value of a variable in a recipe
548
is the last value assigned in the file.  For example,
549
.P1
550
STRING=all
551
 
552
all:VQ:
553
	echo $STRING
554
STRING=none
555
.P2
556
produces the message
557
.CW none .
558
A variable assignment in a recipe
559
does not affect the value of the variable in the
560
.CW mkfile 
561
for two reasons.
562
First,
563
.CW mk
564
does not import values from
565
the environment when a recipe completes;
566
one recipe cannot pass a value through
567
the environment to another recipe.
568
Second, no recipe is executed until 
569
.CW mk
570
has completed its evaluation, so even if a variable
571
were changed,
572
it would not affect the dependency evaluation.
573
.NH 1
574
Metarules
575
.PP
576
A
577
.I metarule
578
is a rule based on a pattern.
579
The pattern selects a class of target(s) and 
580
identifies related prerequisites.
581
.CW Mk
582
metarules may select targets and prerequisites
583
based on any criterion that can be described by a pattern, not just
584
the suffix transformations associated with program
585
construction.
586
.PP
587
Metarule patterns are either
588
.I intrinsic
589
or regular expressions conforming to the
590
syntax of
591
.I regexp (6).
592
The intrinsic patterns are shorthand
593
for common regular expressions.
594
The intrinsic pattern
595
.CW %
596
matches one or more of anything; it is equivalent to
597
the regular expression
598
.CW `.+' .
599
The other intrinsic pattern,
600
.CW & ,
601
matches one or more of any characters except \f(CW`/'\fP
602
and \f(CW`.'\fP.
603
It matches a portion of a path and is
604
equivalent to the regular expression
605
.CW `[^./]+' .
606
An intrinsic pattern in a prerequisite references
607
the string matched by the same intrinsic pattern in the target.
608
For example, the rule
609
.P1
610
	%.v:	%.c
611
.P2
612
says that a file ending in
613
.CW .v
614
depends on a file of the same name with a
615
.CW .c
616
suffix:
617
.CW foo.v
618
depends on
619
.CW foo.c ,
620
.CW bar.v
621
depends on
622
.CW bar.c , 
623
and so on.
624
The string matched by an intrinsic pattern in the target
625
is supplied to the recipe in the variable
626
.CW $stem .
627
Thus the rule
628
.P1
629
%.$O:	%.c
630
	$CC $CFLAGS $stem.c
631
.P2
632
creates an object file for the target architecture from
633
a similarly named C source file.  If several object
634
files are out of date, the rule is applied repeatedly and
635
.CW $stem
636
refers to each file in turn.
637
Since there is only one
638
.CW stem
639
variable, there can only be one
640
.CW %
641
or
642
.CW &
643
pattern in a target;
644
the pattern
645
.CW %-%.c
646
is illegal.
647
.PP
648
Metarules simplify the
649
.CW mkfile
650
for building programs
651
.CW f1
652
and
653
.CW f2 :
654
.P1
655
</$objtype/mkfile
656
 
657
ALL=f1 f2
658
 
659
all:V:	$ALL
660
 
661
%:	%.$O
662
	$LD -o $target $prereq
663
%.$O:	%.c
664
	$CC $CFLAGS $stem.c
665
clean:V:
666
	rm -f $ALL *.[$OS]
667
.P2
668
(The variable
669
.CW $OS
670
is a list of code characters for all architectures.)
671
Here, metarules specify
672
compile and load steps for all C source files.
673
The loader rule relies on two internal variables
674
set by
675
.CW mk
676
during evaluation of the rule:
677
.CW $target
678
is the name of the target(s) and
679
.CW $prereq
680
the name of all prerequisite(s).
681
Metarules allow this
682
.CW mkfile
683
to be easily extended; a new program
684
is supported by adding its name to the third line.
685
.PP
686
A regular expression metarule must have an
687
.CW R
688
attribute.
689
Prerequisites may reference matching substrings in
690
the target using the form
691
.CW \e\fIn\fP
692
where
693
.I n
694
is a digit from 1 to 9 specifying the
695
.I n th
696
parenthesized sub-expression.  In a recipe,
697
.CW $stem\fIn\fP
698
is the equivalent reference.
699
For example, a compile rule could be
700
specified using regular expressions:
701
.P1
702
(.+)\e.$O:R:	\e1.c
703
	$CC $CFLAGS $stem1.c
704
.P2
705
Here,
706
.CW \e1
707
and
708
.CW $stem1
709
refer to the name of the target object file without the
710
suffix.  The variable
711
.CW $stem
712
associated with an intrinsic pattern is undefined
713
in a regular expression metarule.
714
.NH 1
715
Archives
716
.PP
717
.CW Mk
718
provides a special mechanism for maintaining an archive.
719
An archive member is referenced using the form
720
.CW \fIlib\fP(\fIfile\fP)
721
where
722
.I lib
723
is the name of the archive and 
724
.I file
725
is the name of the member.  Two rules define the
726
dependency between an object file and its membership
727
in an archive:
728
.P1
729
$LIB(foo.8):N:	foo.8
730
$LIB:	$LIB(foo.8)
731
	ar rv $LIB foo.8
732
.P2
733
The first rule establishes a dependency between the
734
archive member and the object file.
735
Normally,
736
.CW mk
737
detects an error when a target does not exist and the rule
738
contains no recipe; the
739
.CW N
740
attribute overrides this behavior because the subsequent rule
741
updates the member.
742
The second
743
rule establishes the dependency between the member and
744
the archive; its recipe inserts the member
745
into the archive.
746
This two-step specification allows the modification time
747
of the archive
748
to represent the state of its members.  Other rules
749
can then specify the archive as a prerequisite instead of
750
listing each member.
751
.PP
752
A metarule generalizes library maintenance:
753
.P1
754
LIB=lib.a
755
OBJS=etoa.$O atoe.$O ebcdic.$O
756
 
757
$LIB(%):N:	%
758
$LIB:	${OBJS:%=$LIB(%)}
759
	ar rv $LIB $OBJS
760
.P2
761
The namelist prerequisite of the
762
.CW $LIB
763
target generates archive member names for each object file name;
764
for example, 
765
.CW etoa.$O
766
becomes
767
.CW lib.a(etoa.$O) .
768
This formulation always updates all members.
769
This is acceptable for a small archive, but may 
770
be slow for a big one.
771
The rule
772
.P1
773
$LIB:	${OBJS:%=$LIB(%)}
774
	ar rv $LIB `{membername $newprereq}
775
.P2
776
only updates out of date object files.
777
The internal variable
778
.CW $newprereq
779
contains the names of the out of
780
date prerequisites.  The
781
.CW rc
782
script
783
.CW membername
784
transforms an archive member specification into a file name:
785
it translates
786
.CW lib.a(etoa.$O)
787
into
788
.CW etoa.$O .
789
.PP
790
The
791
.CW mkfile
792
.P1
793
</$objtype/mkfile
794
LIB=lib.a
795
OBJS=etoa.$O atoe.$O ebcdic.$O
796
 
797
prog:	main.$O $LIB
798
	$LD -o $target $prereq
799
 
800
$LIB(%):N:	%
801
$LIB:	${OBJS:%=$LIB(%)}
802
	ar rv $LIB $OBJS
803
.P2
804
builds a program by loading it with a library.
805
.NH 1
806
Evaluation algorithm
807
.PP
808
For each target of interest,
809
.CW mk
810
uses the rules in a
811
.CW mkfile
812
to build a data
813
structure called a dependency graph.  The nodes of
814
the graph represent targets and prerequisites;
815
a directed arc
816
from one node to another indicates that
817
the file associated with the first node depends
818
on the file associated with the second.
819
When the
820
.CW mkfile
821
has been completely read, the graph is analyzed.
822
In the first step, implied dependencies are resolved by
823
computing the
824
.I "transitive closure"
825
of the graph.
826
This calculation extends the graph to include all
827
targets that are potentially
828
derivable from the rules in the
829
.CW mkfile .
830
Next the graph is checked for cycles;
831
.CW make
832
accepts cyclic dependencies, but
833
.CW mk
834
does not allow them.
835
Subsequent steps
836
prune subgraphs that are irrelevant for producing the
837
desired target and verify that there is only one way
838
to build it.
839
The recipes associated with the
840
nodes on the longest path between the
841
target and an out of date prerequisite
842
are then executed in reverse order.
843
.PP
844
The transitive closure calculation is sensitive to
845
metarules; the patterns often select many potential targets
846
and cause the graph to grow rapidly.
847
Fortunately,
848
dependencies associated with the desired target
849
usually form a small part of the graph, so, after
850
pruning, analysis is tractable.
851
For example, the rules
852
.P1
853
%:	x.%
854
	recipe1
855
x.%:	%.k
856
	recipe2
857
%.k:	%.f
858
	recipe3
859
.P2
860
produce a graph with four nodes for each file in the
861
current directory.
862
If the desired target is
863
.CW foo ,
864
.CW mk
865
detects the dependency between it
866
and the original file
867
.CW foo.f
868
through intermediate dependencies on
869
.CW foo.k
870
and
871
.CW x.foo .
872
Nodes associated with other files are deleted during pruning because
873
they are irrelevant to the production of
874
.CW foo .
875
.PP
876
.CW Mk
877
avoids infinite cycles by evaluating
878
each metarule once.
879
Thus, the rule
880
.P1
881
%:	%.z
882
	cp $prereq $prereq.z
883
.P2
884
copies the prerequisite file once.
885
.NH 1
886
Conventions for evaluating rules
887
.PP
888
There must be only one
889
way to build each target.  However, during evaluation
890
metarule patterns often select potential targets that
891
conflict with the
892
targets of other rules.
893
.CW Mk
894
uses several conventions to resolve ambiguities
895
and to select the proper dependencies.
896
.PP
897
When a target selects more than one rule,
898
.CW mk
899
chooses a regular rule
900
over a metarule.
901
For example, the
902
.CW mkfile
903
.P1
904
</$objtype/mkfile
905
 
906
FILES=f1.$O f2.$O f3.$O
907
 
908
prog:	$FILES
909
	$LD -o $target $prereq
910
 
911
%.$O:	%.c
912
	$CC $CFLAGS $stem.c
913
 
914
f2.$O:	f2.c
915
	$CC f2.c
916
.P2
917
contains two rules that could build
918
.CW f2.$O .
919
.CW Mk
920
selects the last rule because its target,
921
.CW f2.$O ,
922
is explicitly specified, while the 
923
.CW %.$O
924
rule is a metarule.  In effect,
925
the explicit rule for
926
.CW f2.$O
927
overrides the general rule for building object files from
928
C source files.
929
.PP
930
When a rule has a target and prerequisites but no recipe,
931
those prerequisites are added to all other rules with
932
recipes that have the same target.
933
All prerequisites, regardless of where they were specified, are
934
exported to the recipe in variable
935
.CW $prereq .
936
For example, in
937
.P1
938
</$objtype/mkfile
939
 
940
FILES=f1.$O f2.$O f3.$O
941
 
942
prog:	$FILES
943
	$LD -o $target $prereq
944
 
945
%.$O:	hdr.h
946
 
947
%.$O:	%.c
948
	$CC $CFLAGS $stem.c
949
.P2
950
the second rule adds
951
.CW hdr.h
952
as a prerequisite of the compile metarule;
953
an object file produced from a C source file
954
depends on
955
.CW hdr.h
956
as well as the source file.  Notice that the recipe of 
957
the compile rule uses
958
.CW $stem.c
959
instead of
960
.CW $prereq
961
because the latter specification would attempt to compile
962
.CW hdr.h .
963
.PP
964
When a target is virtual and there is no other rule with
965
the same target,
966
.CW mk
967
evaluates each prerequisite.
968
For example, adding the rule
969
.P1
970
all:V:	prog
971
.P2
972
to the preceding example builds the executable
973
when either
974
.CW prog
975
or
976
.CW all
977
is the specified target.  In effect, the
978
.CW all
979
target is an alias for
980
.CW prog .
981
.PP
982
When two rules have identical rule headers and both have
983
recipes, the later rule replaces the former one.
984
For example,
985
if a file named
986
.CW mkrules
987
contains
988
.P1
989
$O.out:	$OFILES
990
	$LD $LFLAGS $OFILES
991
%.$O:	%.c
992
	$CC $CFLAGS $stem.c
993
.P2
994
the
995
.CW mkfile
996
.P1
997
OFILES=f1.$O f2.$O f3.$O
998
 
999
<mkrules
1000
 
1001
$O.out:	$OFILES
1002
	$LD $LFLAGS -l $OFILES -lbio -lc
1003
.P2
1004
overrides the general loader rule with a special
1005
rule using a non-standard library search sequence.
1006
A rule is neutralized by overriding it with a rule
1007
with a null recipe:
1008
.P1
1009
<mkrules
1010
 
1011
$O.out:Q:	$OFILES
1012
	;
1013
.P2
1014
The
1015
.CW Q
1016
attribute suppresses the printing of the semicolon.
1017
.PP
1018
When a rule has no prerequisites, the recipe is executed
1019
only when the target does not exist.  For example,
1020
.P1
1021
marker:
1022
	touch $target
1023
.P2
1024
defines a rule to manage a marker file.
1025
If the file exists, it is considered up to date
1026
regardless of its modification time.
1027
When a virtual target has no prerequisites the
1028
recipe is always executed.
1029
The
1030
.CW clean
1031
rule is of this type:
1032
.P1
1033
clean:V:
1034
	rm -f [$OS].out *.[$OS]
1035
.P2
1036
When a rule without prerequisites has multiple targets, the
1037
extra targets are aliases for the rule.
1038
For example, in
1039
.P1
1040
clean tidy nuke:V:
1041
	rm -f [$OS].out *.[$OS]
1042
.P2
1043
the
1044
rule can be invoked by any of three names.
1045
The first rule in a
1046
.CW mkfile
1047
is handled specially:
1048
when
1049
.CW mk
1050
is invoked without a command line target
1051
all targets of the first non-metarule are built.
1052
If that rule has multiple targets, the recipe
1053
is executed once for each target; normally, the recipe
1054
of a rule with multiple targets is only executed once.
1055
.PP
1056
A rule applies to a target only when its prerequisites
1057
exist or can be derived.  More than one rule may have the
1058
same target as long as only one rule with a recipe
1059
remains applicable after the dependency evaluation completes.
1060
For example, consider a program built from C
1061
and assembler source files.  Two rules produce
1062
object files:
1063
.P1
1064
%.$O:	%.c
1065
	$CC $CFLAGS $stem.c
1066
%.$O:	%.s
1067
	$AS $AFLAGS $stem.s
1068
.P2
1069
As long as there are not two source files with names like
1070
.CW \fIfoo\fP.c
1071
and
1072
.CW \fIfoo\fP.s ,
1073
.CW mk
1074
can unambiguously select the proper rule.
1075
If both files exist,
1076
the rules are ambiguous
1077
and
1078
.CW mk
1079
exits with an error message.
1080
.PP
1081
In Plan 9, many programs consist of portable code stored
1082
in one directory and architecture-specific source stored in
1083
another.
1084
For example, the
1085
.CW mkfile
1086
.P1
1087
</$objtype/mkfile
1088
 
1089
FILES=f1.$O f2.$O f3.$O f3.$O
1090
 
1091
prog:	$FILES
1092
	$LD -o $target $prereq
1093
 
1094
%.$O:	%.$c
1095
	$CC $CFLAGS $stem.c
1096
 
1097
%.$O:	../port/%.c
1098
	$CC $CFLAGS ../port/$stem.c
1099
.P2
1100
builds the program named
1101
.CW prog
1102
using portable code in directory
1103
.CW ../port
1104
and architecture-specific code in the current directory.
1105
As long as the
1106
names of the C source files in 
1107
.CW ../port
1108
do not conflict with the names of files in the current directory,
1109
.CW mk
1110
selects the appropriate rule to build the object file.
1111
If like-named files exist in both directories, the
1112
specification is ambiguous and an explicit target
1113
must be specified to resolve the ambiguity.
1114
For example,
1115
adding the rule
1116
.P1
1117
f2.$O:	f2.c
1118
	$CC $CFLAGS $f2.c
1119
.P2
1120
to the previous
1121
.CW mkfile
1122
uses the architecture-specific version of
1123
.CW f2.c
1124
instead of the portable one.
1125
Here, the explicit rule unambiguously
1126
documents which of the
1127
like-named source files is used to build the program.
1128
.PP
1129
.CW Mk\fR'\fP s
1130
heuristics can produce unintended results
1131
when rules are not carefully specified.
1132
For example, the rules that build
1133
object files from C or assembler source files
1134
.P1
1135
%.$O:	%.c
1136
	$CC $CFLAGS $stem.c
1137
%.$O:	%.s
1138
	$AS $AFLAGS $stem.s
1139
.P2
1140
illustrate a subtle pratfall.
1141
Adding a header file dependency to the compile rule
1142
.P1
1143
%.$O:	%.c hdr.h
1144
	$CC $CFLAGS $stem.c
1145
.P2
1146
produces the error message
1147
.P1
1148
.CW "don't know how to make '\fIfile\fP.c'"
1149
.P2
1150
when \fIfile\fP.s is an assembler
1151
source file.
1152
This occurs because 
1153
.CW \fIfile\fP.s
1154
satisfies the assemble rule and
1155
.CW hdr.h
1156
satisfies the compile rule, so
1157
either rule can potentially produce the target.
1158
When a prerequisite exists or can be
1159
derived,
1160
all other prerequisites in that
1161
rule header must exist or be derivable; here,
1162
the existence of
1163
.CW hdr.h
1164
forces the evaluation of a C source file.
1165
Specifying the dependencies in different
1166
rules avoids this interpretation:
1167
.P1
1168
%.$O:	hdr.h
1169
%.$O:	%.c
1170
	$CC $CFLAGS $stem.c
1171
.P2
1172
Although
1173
.CW hdr.h
1174
is an additional prerequisite of the compile rule,
1175
the two rules are evaluated independently and
1176
the existence of the C source file is not linked
1177
to the existence of the header file.
1178
However, this specification describes a different
1179
dependency.  Originally, only object
1180
files derived from C files depended on
1181
.CW hdr.h ;
1182
now all object files, including those built
1183
from assembler source, depend on the header file.
1184
.PP
1185
Metarule patterns should be as restrictive as possible to
1186
prevent conflicts with other rules.
1187
Consider the
1188
.CW mkfile
1189
.P1
1190
</$objtype/mkfile
1191
BIN=/$objtype/bin
1192
PROG=foo
1193
 
1194
install:V:	$BIN/$PROG
1195
 
1196
%:	%.c
1197
	$CC $stem.c
1198
	$LD -o $target $stem.$O
1199
 
1200
$BIN/%:	%
1201
	mv $stem $target
1202
.P2
1203
The first target builds an executable
1204
in the local directory; the second
1205
installs it in the directory
1206
of executables for the architecture.
1207
Invoking
1208
.CW mk
1209
with the
1210
.CW install
1211
target produces:
1212
.P1 0
1213
mk: ambiguous recipes for /mips/bin/foo:
1214
/mips/bin/foo <-(mkfile:8)- /mips/bin/foo.c <-(mkfile:12)- foo.c
1215
/mips/bin/foo <-(mkfile:12)- foo <-(mkfile:8)- foo.c
1216
.P2
1217
The prerequisite of the
1218
.CW install
1219
rule,
1220
.CW $BIN/$PROG ,
1221
matches both metarules because the
1222
.CW %
1223
pattern matches everything.
1224
The
1225
.CW &
1226
pattern restricts the compile rule to files in the
1227
current directory and avoids the conflict:
1228
.P1
1229
&:	&.c
1230
	$CC $stem.c
1231
	$LD -o $target $stem.$O
1232
.P2
1233
.NH 1
1234
Missing intermediates
1235
.PP
1236
.CW Mk
1237
does not build a missing intermediate file if a target
1238
is up to date with the prerequisites of the intermediate.
1239
For example,
1240
when an executable is up to date with its source file,
1241
.CW mk
1242
does not compile the source to create a missing object file.
1243
The evaluation only applies
1244
when a target is considered up to date by pretending that the
1245
intermediate exists.  Thus, it does not apply
1246
when the intermediate is a command line target
1247
or when it has no prerequisites.
1248
.PP
1249
This capability is useful for
1250
maintaining archives.  We can modify the archive
1251
update recipe to remove object files after
1252
they are archived:
1253
.P1
1254
$LIB(%):N:	%
1255
$LIB:	${OBJS:%=$LIB(%)}
1256
	names=`{membername $newprereq}
1257
	ar rv $LIB $names
1258
	rm -f $names
1259
.P2
1260
A subsequent
1261
.CW mk
1262
does not remake the object files as long as the members
1263
of the archive remain up to date with the source files.
1264
The
1265
.CW -i
1266
command line option overrides this behavior
1267
and causes all intermediates to be built.
1268
.NH 1
1269
Alternative out-of-date determination
1270
.PP
1271
Sometimes the modification time is not useful
1272
for deciding when a target and prerequisite are out of date.
1273
The
1274
.CW P
1275
attribute replaces the default mechanism with the result of
1276
a command.  The command immediately follows the attribute
1277
and is repeatedly executed with each
1278
target and each prerequisite as its arguments;
1279
if its exit status is non-zero, they are considered out of date
1280
and the recipe is executed.  Consider the
1281
.CW mkfile
1282
.P1
1283
foo.ref:Pcmp -s:	foo
1284
	cp $prereq $target
1285
.P2
1286
The command
1287
.P1
1288
cmp -s foo.ref foo
1289
.P2
1290
is executed and if 
1291
.CW foo.ref
1292
differs from
1293
.CW foo ,
1294
the latter file is copied to the former.
1295
.NH 1
1296
Parallel processing
1297
.PP
1298
When possible,
1299
.CW mk
1300
executes recipes in parallel.
1301
The variable
1302
.CW $NPROC
1303
specifies the maximum number of simultaneously executing
1304
recipes.
1305
Normally it is imported from the environment,
1306
where the system has set it to the number of available processors.
1307
It can be decreased by assigning a new
1308
value and can be set to 1 to force single-threaded recipe execution.
1309
This is necessary when several targets access
1310
a common resource such as
1311
a status file or data base.
1312
When there is no dependency between targets,
1313
.CW mk
1314
assumes the
1315
recipes can be
1316
executed concurrently.
1317
Normally, this allows
1318
multiple prerequisites to be built simultaneously;
1319
for example, the object file prerequisites of
1320
a load rule can be produced by compiling the source files in parallel.
1321
.CW Mk
1322
does not define the order of execution of independent recipes.
1323
When the prerequisites of a rule are not independent,
1324
the dependencies between them should be specified in a rule or the
1325
.CW mkfile
1326
should be single-threaded.
1327
For example, the archive update rules
1328
.P1
1329
$LIB(%):N:	%
1330
$LIB:	${OBJS:%=$LIB(%)}
1331
	ar rv $LIB `{membername $newprereq}
1332
.P2
1333
compile source files in parallel but update
1334
all members of the archive at once.
1335
It is a mistake to merge the two rules
1336
.P1
1337
$LIB(%):	%
1338
	ar rv $LIB $stem
1339
.P2
1340
because an
1341
.CW ar
1342
command is executed for every
1343
member of the library.  Not only is this
1344
inefficient, but the archive is updated
1345
in parallel, making interference likely.
1346
.PP
1347
The
1348
.CW $nproc
1349
environment variable contains a number associated
1350
with the processor executing a recipe.
1351
It can be used to create unique
1352
names when the
1353
recipe may be executing simultaneously on several processors.
1354
Other maintenance tools provide mechanisms to control recipe
1355
scheduling explicitly [Cmel86], but
1356
.CW mk\fR'\fPs
1357
general rules are sufficient for all but the most unusual cases.
1358
.NH 1
1359
Deleting target files on errors
1360
.PP
1361
The
1362
.CW D
1363
attribute
1364
causes
1365
.CW mk
1366
to remove the target file when a
1367
recipe terminates prematurely.
1368
The error message describing the
1369
termination condition warns
1370
of the deletion.
1371
A partially built file is doubly dangerous:
1372
it is not only wrong, but is also
1373
considered to be up to date so
1374
a subsequent
1375
.CW mk
1376
will not rebuild it.  For example,
1377
.P1
1378
pic.out:D:	mk.ms
1379
		pic $prereq | tbl | troff -ms > $target
1380
.P2
1381
produces the message
1382
.P1
1383
.CW "mk: pic mk.ms | ...  : exit status=rc 685: deleting 'pic.out'"
1384
.P2
1385
if any program in the recipe exits with an error status.
1386
.NH 1
1387
Unspecified dependencies
1388
.PP
1389
The
1390
.CW -w
1391
command line flag forces the
1392
files following the flag to be treated
1393
as if they were just modified.
1394
We can use this flag with a command that selects files
1395
to force a build based on the selection criterion.
1396
For example, if the declaration of
1397
a global variable named
1398
.I var
1399
is changed in a header file,
1400
all source files that reference
1401
it can be rebuilt with the command
1402
.P1
1403
$ mk -w`{grep -l \fIvar\fP *.[cyl]}
1404
.P2
1405
.NH 1
1406
Conclusion
1407
.PP
1408
There are many programs related to
1409
.CW make ,
1410
each choosing a different balance between
1411
specialization and generality.
1412
.CW Mk
1413
emphasizes generality but allows
1414
customization through its pattern specifications and
1415
include facilities.
1416
.PP
1417
Plan 9 presents a difficult maintenance environment
1418
with its heterogeneous
1419
architectures and languages.
1420
.CW Mk\fR'\fPs
1421
flexible specification language and simple
1422
interaction with
1423
.CW rc
1424
work well in this environment.
1425
As a result,
1426
Plan 9 relies on
1427
.CW mk
1428
to automate almost all maintenance.
1429
Tasks as diverse as updating the
1430
network data base, producing the manual,
1431
or building a release are expressed as
1432
.CW mk
1433
procedures.
1434
.NH 1
1435
References
1436
.LP
1437
[Cmel86] R. F. Cmelik,
1438
``Concurrent Make: A Distributed Program in Concurrent C'',
1439
AT&T Bell Laboratories Technical Report, 1986.
1440
.LP
1441
[Feld79] S. I. Feldman,
1442
``Make \(em a program for maintaining computer programs'',
1443
.I
1444
Software Practice & Experience ,
1445
.R
1446
1979
1447
Vol 9 #4,
1448
pp. 255-266.
1449
.LP
1450
[Flan95] Bob Flandrena,
1451
``Plan 9 Mkfiles'',
1452
this volume.
1453
.LP
1454
[Hume87] A. G. Hume,
1455
``Mk: A Successor to Make'',
1456
.I
1457
USENIX Summer Conf. Proc.,
1458
.R
1459
Phoenix, Az.
1460
.NH 1
1461
Appendix: Differences between
1462
.CW make
1463
and
1464
.CW mk
1465
.PP
1466
The differences between
1467
.CW mk
1468
and
1469
.CW make
1470
are:
1471
.IP \(bu 3n
1472
.CW Make
1473
builds targets when it needs them, allowing systematic use of side effects.
1474
.CW Mk
1475
constructs the entire dependency graph before building any target.
1476
.IP \(bu
1477
.CW Make
1478
supports suffix rules and
1479
.CW %
1480
metarules.
1481
.CW Mk
1482
supports
1483
.CW %
1484
and regular expression metarules.
1485
(Older versions of
1486
.CW make
1487
support only suffix rules.)
1488
.IP \(bu
1489
.CW Mk
1490
performs transitive closure on metarules,
1491
.CW make
1492
does not.
1493
.IP \(bu
1494
.CW Make
1495
supports cyclic dependencies,
1496
.CW mk
1497
does not.
1498
.IP \(bu
1499
.CW Make
1500
evaluates recipes one line at a time, replacing variables by their values and
1501
executing some commands internally.
1502
.CW Mk
1503
passes the entire recipe to the shell without
1504
interpretation or internal execution.
1505
.IP \(bu
1506
.CW Make
1507
supports parallel execution of single-line recipes when building
1508
the prerequisites for specified targets.
1509
.CW Mk
1510
supports parallel execution of all recipes.
1511
(Older versions of
1512
.CW make
1513
did not support parallel execution.)
1514
.IP \(bu
1515
.CW Make
1516
uses special targets (beginning with a period)
1517
to indicate special processing.
1518
.CW Mk
1519
uses attributes to modify rule evaluation.
1520
.IP \(bu
1521
.CW Mk
1522
supports virtual
1523
targets that are independent of the file system.
1524
.IP \(bu
1525
.CW Mk
1526
allows non-standard out-of-date determination,
1527
.CW make
1528
does not.
1529
.PP
1530
It is usually easy to convert a
1531
.CW makefile
1532
to or from an equivalent
1533
.CW mkfile .