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