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