blob: 0698ef5b77d845b4ecded8cd4a27a0673aa8d91c [file] [log] [blame]
rsc058b0112005-01-03 06:40:20 +00001.TH MK 1
rsc058b0112005-01-03 06:40:20 +00002.SH NAME
rsc93aa30a2005-01-14 03:27:51 +00003mk \- maintain (make) related files
rsc058b0112005-01-03 06:40:20 +00004.SH SYNOPSIS
5.B mk
6[
7.B -f
8.I mkfile
9] ...
10[
11.I option ...
12]
13[
14.I target ...
15]
rsc058b0112005-01-03 06:40:20 +000016.SH DESCRIPTION
17.I Mk
18uses the dependency rules specified in
19.I mkfile
20to control the update (usually by compilation) of
21.I targets
22(usually files)
23from the source files upon which they depend.
24The
25.I mkfile
26(default
27.LR mkfile )
28contains a
29.I rule
30for each target that identifies the files and other
31targets upon which it depends and an
32.IR sh (1)
33script, a
34.IR recipe ,
35to update the target.
36The script is run if the target does not exist
37or if it is older than any of the files it depends on.
38.I Mkfile
39may also contain
40.I meta-rules
41that define actions for updating implicit targets.
42If no
43.I target
44is specified, the target of the first rule (not meta-rule) in
45.I mkfile
46is updated.
47.PP
48The environment variable
49.B $NPROC
50determines how many targets may be updated simultaneously;
51Some operating systems, e.g., Plan 9, set
52.B $NPROC
53automatically to the number of CPUs on the current machine.
54.PP
55Options are:
56.TP \w'\fL-d[egp]\ 'u
57.B -a
58Assume all targets to be out of date.
59Thus, everything is updated.
60.PD 0
61.TP
62.BR -d [ egp ]
63Produce debugging output
64.RB ( p
65is for parsing,
66.B g
67for graph building,
68.B e
69for execution).
70.TP
71.B -e
72Explain why each target is made.
73.TP
74.B -i
75Force any missing intermediate targets to be made.
76.TP
77.B -k
78Do as much work as possible in the face of errors.
79.TP
80.B -n
81Print, but do not execute, the commands
82needed to update the targets.
83.TP
84.B -s
85Make the command line arguments sequentially rather than in parallel.
86.TP
87.B -t
88Touch (update the modified date of) file targets, without
89executing any recipes.
90.TP
91.BI -w target1 , target2,...
92Pretend the modify time for each
93.I target
94is the current time; useful in conjunction with
95.B -n
96to learn what updates would be triggered by
97modifying the
98.IR targets .
99.PD
100.SS The \fLmkfile\fP
101A
102.I mkfile
103consists of
104.I assignments
105(described under `Environment') and
106.IR rules .
107A rule contains
108.I targets
109and a
110.IR tail .
111A target is a literal string
112and is normally a file name.
113The tail contains zero or more
114.I prerequisites
115and an optional
116.IR recipe ,
117which is an
118.B shell
119script.
120Each line of the recipe must begin with white space.
121A rule takes the form
122.IP
123.EX
124target: prereq1 prereq2
125 \f2recipe using\fP prereq1, prereq2 \f2to build\fP target
126.EE
127.PP
128When the recipe is executed,
129the first character on every line is elided.
130.PP
131After the colon on the target line, a rule may specify
132.IR attributes ,
133described below.
134.PP
135A
136.I meta-rule
137has a target of the form
138.IB A % B
139where
140.I A
141and
142.I B
143are (possibly empty) strings.
144A meta-rule acts as a rule for any potential target whose
145name matches
146.IB A % B
147with
148.B %
149replaced by an arbitrary string, called the
150.IR stem .
151In interpreting a meta-rule,
152the stem is substituted for all occurrences of
153.B %
154in the prerequisite names.
155In the recipe of a meta-rule, the environment variable
156.B $stem
157contains the string matched by the
158.BR % .
159For example, a meta-rule to compile a C program using
160.IR 9c (1)
161might be:
162.IP
163.EX
164%: %.c
165 9c -c $stem.c
166 9l -o $stem $stem.o
167.EE
168.PP
169Meta-rules may contain an ampersand
170.B &
171rather than a percent sign
172.BR % .
173A
174.B %
175matches a maximal length string of any characters;
176an
177.B &
178matches a maximal length string of any characters except period
179or slash.
180.PP
181The text of the
182.I mkfile
183is processed as follows.
184Lines beginning with
185.B <
186followed by a file name are replaced by the contents of the named
187file.
188Lines beginning with
189.B "<|"
190followed by a file name are replaced by the output
191of the execution of the named
192file.
193Blank lines and comments, which run from unquoted
194.B #
195characters to the following newline, are deleted.
196The character sequence backslash-newline is deleted,
197so long lines in
198.I mkfile
199may be folded.
200Non-recipe lines are processed by substituting for
201.BI `{ command }
202the output of the
203.I command
204when run by
205.IR sh .
206References to variables are replaced by the variables' values.
207Special characters may be quoted using single quotes
208.BR \&''
209as in
210.IR sh (1).
211.PP
212Assignments and rules are distinguished by
213the first unquoted occurrence of
214.B :
215(rule)
216or
217.B =
218(assignment).
219.PP
220A later rule may modify or override an existing rule under the
221following conditions:
222.TP
223\-
224If the targets of the rules exactly match and one rule
225contains only a prerequisite clause and no recipe, the
226clause is added to the prerequisites of the other rule.
227If either or both targets are virtual, the recipe is
228always executed.
229.TP
230\-
231If the targets of the rules match exactly and the
232prerequisites do not match and both rules
233contain recipes,
234.I mk
235reports an ``ambiguous recipe'' error.
236.TP
237\-
238If the target and prerequisites of both rules match exactly,
239the second rule overrides the first.
240.SS Environment
241Rules may make use of
242shell
243environment variables.
244A legal reference of the form
245.B $OBJ
246or
247.B ${name}
248is expanded as in
249.IR sh (1).
250A reference of the form
251.BI ${name: A % B = C\fL%\fID\fL}\fR,
252where
253.I A, B, C, D
254are (possibly empty) strings,
255has the value formed by expanding
256.B $name
257and substituting
258.I C
259for
260.I A
261and
262.I D
263for
264.I B
265in each word in
266.B $name
267that matches pattern
268.IB A % B\f1.
269.PP
270Variables can be set by
271assignments of the form
272.I
273 var\fL=\fR[\fIattr\fL=\fR]\fIvalue\fR
274.br
275Blanks in the
276.I value
277break it into words.
278Such variables are exported
279to the environment of
280recipes as they are executed, unless
281.BR U ,
282the only legal attribute
283.IR attr ,
284is present.
285The initial value of a variable is
286taken from (in increasing order of precedence)
287the default values below,
288.I mk's
289environment, the
290.IR mkfiles ,
291and any command line assignment as an argument to
292.IR mk .
293A variable assignment argument overrides the first (but not any subsequent)
294assignment to that variable.
rscc8b63422005-01-13 04:49:19 +0000295.PP
rsc058b0112005-01-03 06:40:20 +0000296The variable
297.B MKFLAGS
298contains all the option arguments (arguments starting with
299.L -
300or containing
301.LR = )
302and
303.B MKARGS
304contains all the targets in the call to
305.IR mk .
306.PP
rscc8b63422005-01-13 04:49:19 +0000307The variable
308.B MKSHELL
309contains the shell command line
310.I mk
311uses to run recipes.
312If the first word of the command ends in
313.B rc
314or
315.BR rcsh ,
316.I mk
317uses
318.IR rc (1)'s
319quoting rules; otherwise it uses
320.IR sh (1)'s.
321The
322.B MKSHELL
323variable is consulted when the mkfile is read, not when it is executed,
324so that different shells can be used within a single mkfile:
325.IP
326.EX
327MKSHELL=$PLAN9/bin/rc
328use-rc:V:
329 for(i in a b c) echo $i
330
331MKSHELL=sh
332use-sh:V:
333 for i in a b c; do echo $i; done
334.EE
335.LP
336Mkfiles included via
337.B <
338or
339.B <|
340.RI ( q.v. )
341see their own private copy of
342.BR MKSHELL ,
343which always starts set to
344.B sh .
345.PP
rsc058b0112005-01-03 06:40:20 +0000346Dynamic information may be included in the mkfile by using a line of the form
347.IP
348\fR<|\fIcommand\fR \fIargs\fR
349.LP
350This runs the command
351.I command
352with the given arguments
353.I args
354and pipes its standard output to
355.I mk
356to be included as part of the mkfile. For instance, the Inferno kernels
357use this technique
358to run a shell command with an awk script and a configuration
359file as arguments in order for
360the
361.I awk
362script to process the file and output a set of variables and their values.
363.SS Execution
364.PP
365During execution,
366.I mk
367determines which targets must be updated, and in what order,
368to build the
369.I names
370specified on the command line.
371It then runs the associated recipes.
372.PP
373A target is considered up to date if it has no prerequisites or
374if all its prerequisites are up to date and it is newer
375than all its prerequisites.
376Once the recipe for a target has executed, the target is
377considered up to date.
378.PP
379The date stamp
380used to determine if a target is up to date is computed
381differently for different types of targets.
382If a target is
383.I virtual
384(the target of a rule with the
385.B V
386attribute),
387its date stamp is initially zero; when the target is
388updated the date stamp is set to
389the most recent date stamp of its prerequisites.
390Otherwise, if a target does not exist as a file,
391its date stamp is set to the most recent date stamp of its prerequisites,
392or zero if it has no prerequisites.
393Otherwise, the target is the name of a file and
394the target's date stamp is always that file's modification date.
395The date stamp is computed when the target is needed in
396the execution of a rule; it is not a static value.
397.PP
398Nonexistent targets that have prerequisites
399and are themselves prerequisites are treated specially.
400Such a target
401.I t
402is given the date stamp of its most recent prerequisite
403and if this causes all the targets which have
404.I t
405as a prerequisite to be up to date,
406.I t
407is considered up to date.
408Otherwise,
409.I t
410is made in the normal fashion.
411The
412.B -i
413flag overrides this special treatment.
414.PP
415Files may be made in any order that respects
416the preceding restrictions.
417.PP
418A recipe is executed by supplying the recipe as standard input to
419the command
420.BR /bin/sh .
421(Note that unlike
422.IR make ,
423.I mk
424feeds the entire recipe to the shell rather than running each line
425of the recipe separately.)
426The environment is augmented by the following variables:
427.TP 14
428.B $alltarget
429all the targets of this rule.
430.TP
431.B $newprereq
432the prerequisites that caused this rule to execute.
433.TP
434.B $newmember
435the prerequisites that are members of an aggregate
436that caused this rule to execute.
437When the prerequisites of a rule are members of an
438aggregate,
439.B $newprereq
440contains the name of the aggregate and out of date
441members, while
442.B $newmember
443contains only the name of the members.
444.TP
445.B $nproc
446the process slot for this recipe.
447It satisfies
448.RB 0≤ $nproc < $NPROC .
449.TP
450.B $pid
451the process id for the
452.I mk
453executing the recipe.
454.TP
455.B $prereq
456all the prerequisites for this rule.
457.TP
458.B $stem
459if this is a meta-rule,
460.B $stem
461is the string that matched
462.B %
463or
464.BR & .
465Otherwise, it is empty.
466For regular expression meta-rules (see below), the variables
467.LR stem0 ", ...,"
468.L stem9
469are set to the corresponding subexpressions.
470.TP
471.B $target
472the targets for this rule that need to be remade.
473.PP
474These variables are available only during the execution of a recipe,
475not while evaluating the
476.IR mkfile .
477.PP
478Unless the rule has the
479.B Q
480attribute,
481the recipe is printed prior to execution
482with recognizable environment variables expanded.
483Commands returning error status
484cause
485.I mk
486to terminate.
487.PP
488Recipes and backquoted
489.B rc
490commands in places such as assignments
491execute in a copy of
492.I mk's
493environment; changes they make to
494environment variables are not visible from
495.IR mk .
496.PP
497Variable substitution in a rule is done when
498the rule is read; variable substitution in the recipe is done
499when the recipe is executed. For example:
500.IP
501.EX
502bar=a.c
503foo: $bar
504 $CC -o foo $bar
505bar=b.c
506.EE
507.PP
508will compile
509.B b.c
510into
511.BR foo ,
512if
513.B a.c
514is newer than
515.BR foo .
516.SS Aggregates
517Names of the form
518.IR a ( b )
519refer to member
520.I b
521of the aggregate
522.IR a .
523Currently, the only aggregates supported are
524.I 9ar
525(see
526.IR 9c (1))
527archives.
rsc058b0112005-01-03 06:40:20 +0000528.SS Attributes
529The colon separating the target from the prerequisites
530may be
531immediately followed by
532.I attributes
533and another colon.
534The attributes are:
535.TP
536.B D
537If the recipe exits with a non-null status, the target is deleted.
538.TP
539.B E
540Continue execution if the recipe draws errors.
541.TP
542.B N
543If there is no recipe, the target has its time updated.
544.TP
545.B n
546The rule is a meta-rule that cannot be a target of a virtual rule.
547Only files match the pattern in the target.
548.TP
549.B P
550The characters after the
551.B P
552until the terminating
553.B :
554are taken as a program name.
555It will be invoked as
556.B "sh -c prog 'arg1' 'arg2'"
557and should return a zero exit status
558if and only if arg1 is up to date with respect to arg2.
559Date stamps are still propagated in the normal way.
560.TP
561.B Q
562The recipe is not printed prior to execution.
563.TP
564.B R
565The rule is a meta-rule using regular expressions.
566In the rule,
567.B %
568has no special meaning.
569The target is interpreted as a regular expression as defined in
570.IR regexp (7).
571The prerequisites may contain references
572to subexpressions in form
573.BI \e n\f1,
574as in the substitute command of
575.IR sed (1).
576.TP
577.B U
578The targets are considered to have been updated
579even if the recipe did not do so.
580.TP
581.B V
582The targets of this rule are marked as virtual.
583They are distinct from files of the same name.
584.PD
585.SH EXAMPLES
586A simple mkfile to compile a program:
587.IP
588.EX
589.ta 8n +8n +8n +8n +8n +8n +8n
590</$objtype/mkfile
591
592prog: a.$O b.$O c.$O
593 $LD $LDFLAGS -o $target $prereq
594
595%.$O: %.c
596 $CC $CFLAGS $stem.c
597.EE
598.PP
599Override flag settings in the mkfile:
600.IP
601.EX
602% mk target 'CFLAGS=-S -w'
603.EE
604.PP
605Maintain a library:
606.IP
607.EX
608libc.a(%.$O):N: %.$O
609libc.a: libc.a(abs.$O) libc.a(access.$O) libc.a(alarm.$O) ...
610 ar r libc.a $newmember
611.EE
612.PP
613String expression variables to derive names from a master list:
614.IP
615.EX
616NAMES=alloc arc bquote builtins expand main match mk var word
617OBJ=${NAMES:%=%.$O}
618.EE
619.PP
620Regular expression meta-rules:
621.IP
622.EX
623([^/]*)/(.*)\e.$O:R: \e1/\e2.c
624 cd $stem1; $CC $CFLAGS $stem2.c
625.EE
626.PP
627A correct way to deal with
628.IR yacc (1)
629grammars.
630The file
631.B lex.c
632includes the file
633.B x.tab.h
634rather than
635.B y.tab.h
636in order to reflect changes in content, not just modification time.
637.IP
638.EX
639lex.$O: x.tab.h
640x.tab.h: y.tab.h
641 cmp -s x.tab.h y.tab.h || cp y.tab.h x.tab.h
642y.tab.c y.tab.h: gram.y
643 $YACC -d gram.y
644.EE
645.PP
646The above example could also use the
647.B P
648attribute for the
649.B x.tab.h
650rule:
651.IP
652.EX
653x.tab.h:Pcmp -s: y.tab.h
654 cp y.tab.h x.tab.h
655.EE
rsc93aa30a2005-01-14 03:27:51 +0000656.SH SOURCE
rscadc93f62005-01-14 17:37:50 +0000657.B \*9/src/cmd/mk
rsc058b0112005-01-03 06:40:20 +0000658.SH SEE ALSO
659.IR sh (1),
rscc8b63422005-01-13 04:49:19 +0000660.IR regexp (7)
rsc058b0112005-01-03 06:40:20 +0000661.PP
662A. Hume,
663``Mk: a Successor to Make''
664(Tenth Edition Research Unix Manuals).
665.PP
666Andrew G. Hume and Bob Flandrena,
667``Maintaining Files on Plan 9 with Mk''.
668DOCPREFIX/doc/mk.pdf
669.SH HISTORY
670Andrew Hume wrote
671.I mk
672for Tenth Edition Research Unix.
673It was later ported to Plan 9.
674This software is a port of the Plan 9 version back to Unix.
675.SH BUGS
676Identical recipes for regular expression meta-rules only have one target.
rscc8b63422005-01-13 04:49:19 +0000677.PP
rsc058b0112005-01-03 06:40:20 +0000678Seemingly appropriate input like
679.B CFLAGS=-DHZ=60
680is parsed as an erroneous attribute; correct it by inserting
681a space after the first
682.LR = .
rscc8b63422005-01-13 04:49:19 +0000683.PP
rsc058b0112005-01-03 06:40:20 +0000684The recipes printed by
685.I mk
686before being passed to
rscc8b63422005-01-13 04:49:19 +0000687the shell
rsc058b0112005-01-03 06:40:20 +0000688for execution are sometimes erroneously expanded
689for printing. Don't trust what's printed; rely
rscc8b63422005-01-13 04:49:19 +0000690on what the shell
rsc058b0112005-01-03 06:40:20 +0000691does.