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