3 mk \- maintain (make) related files
18 uses the dependency rules specified in
20 to control the update (usually by compilation) of
23 from the source files upon which they depend.
30 for each target that identifies the files and other
31 targets upon which it depends and an
36 The script is run if the target does not exist
37 or if it is older than any of the files it depends on.
41 that define actions for updating implicit targets.
44 is specified, the target of the first rule (not meta-rule) in
48 The environment variable
50 determines how many targets may be updated simultaneously;
51 Some operating systems, e.g., Plan 9, set
53 automatically to the number of CPUs on the current machine.
58 Assume all targets to be out of date.
59 Thus, everything is updated.
63 Produce debugging output
72 Explain why each target is made.
75 Force any missing intermediate targets to be made.
78 Do as much work as possible in the face of errors.
81 Print, but do not execute, the commands
82 needed to update the targets.
85 Make the command line arguments sequentially rather than in parallel.
88 Touch (update the modified date of) file targets, without
89 executing any recipes.
91 .BI -w target1 , target2,...
92 Pretend the modify time for each
94 is the current time; useful in conjunction with
96 to learn what updates would be triggered by
105 (described under `Environment') and
111 A target is a literal string
112 and is normally a file name.
113 The tail contains zero or more
120 Each line of the recipe must begin with white space.
121 A rule takes the form
124 target: prereq1 prereq2
125 \f2recipe using\fP prereq1, prereq2 \f2to build\fP target
128 When the recipe is executed,
129 the first character on every line is elided.
131 After the colon on the target line, a rule may specify
137 has a target of the form
143 are (possibly empty) strings.
144 A meta-rule acts as a rule for any potential target whose
149 replaced by an arbitrary string, called the
151 In interpreting a meta-rule,
152 the stem is substituted for all occurrences of
154 in the prerequisite names.
155 In the recipe of a meta-rule, the environment variable
157 contains the string matched by the
159 For example, a meta-rule to compile a C program
168 Meta-rules may contain an ampersand
170 rather than a percent sign
174 matches a maximal length string of any characters;
177 matches a maximal length string of any characters except period
182 is processed as follows.
185 followed by a file name are replaced by the contents of the named
189 followed by a file name are replaced by the output
190 of the execution of the named
192 Blank lines and comments, which run from unquoted
194 characters to the following newline, are deleted.
195 The character sequence backslash-newline is deleted,
199 Non-recipe lines are processed by substituting for
205 References to variables are replaced by the variables' values.
206 Special characters may be quoted using single quotes
211 Assignments and rules are distinguished by
212 the first unquoted occurrence of
219 A later rule may modify or override an existing rule under the
220 following conditions:
223 If the targets of the rules exactly match and one rule
224 contains only a prerequisite clause and no recipe, the
225 clause is added to the prerequisites of the other rule.
226 If either or both targets are virtual, the recipe is
230 If the targets of the rules match exactly and the
231 prerequisites do not match and both rules
234 reports an ``ambiguous recipe'' error.
237 If the target and prerequisites of both rules match exactly,
238 the second rule overrides the first.
240 Rules may make use of
242 environment variables.
243 A legal reference of the form
249 A reference of the form
250 .BI ${name: A % B = C\fL%\fID\fL}\fR,
253 are (possibly empty) strings,
254 has the value formed by expanding
269 Variables can be set by
270 assignments of the form
272 var\fL=\fR[\fIattr\fL=\fR]\fIvalue\fR
277 Such variables are exported
278 to the environment of
279 recipes as they are executed, unless
281 the only legal attribute
284 The initial value of a variable is
285 taken from (in increasing order of precedence)
286 the default values below,
290 and any command line assignment as an argument to
292 A variable assignment argument overrides the first (but not any subsequent)
293 assignment to that variable.
297 contains all the option arguments (arguments starting with
303 contains all the targets in the call to
308 contains the shell command line
311 If the first word of the command ends in
318 quoting rules; otherwise it uses
322 variable is consulted when the mkfile is read, not when it is executed,
323 so that different shells can be used within a single mkfile:
326 MKSHELL=$PLAN9/bin/rc
328 for(i in a b c) echo $i
332 for i in a b c; do echo $i; done
340 see their own private copy of
342 which always starts set to
345 Dynamic information may be included in the mkfile by using a line of the form
347 \fR<|\fIcommand\fR \fIargs\fR
349 This runs the command
351 with the given arguments
353 and pipes its standard output to
355 to be included as part of the mkfile. For instance, the Inferno kernels
357 to run a shell command with an awk script and a configuration
358 file as arguments in order for
361 script to process the file and output a set of variables and their values.
366 determines which targets must be updated, and in what order,
369 specified on the command line.
370 It then runs the associated recipes.
372 A target is considered up to date if it has no prerequisites or
373 if all its prerequisites are up to date and it is newer
374 than all its prerequisites.
375 Once the recipe for a target has executed, the target is
376 considered up to date.
379 used to determine if a target is up to date is computed
380 differently for different types of targets.
383 (the target of a rule with the
386 its date stamp is initially zero; when the target is
387 updated the date stamp is set to
388 the most recent date stamp of its prerequisites.
389 Otherwise, if a target does not exist as a file,
390 its date stamp is set to the most recent date stamp of its prerequisites,
391 or zero if it has no prerequisites.
392 Otherwise, the target is the name of a file and
393 the target's date stamp is always that file's modification date.
394 The date stamp is computed when the target is needed in
395 the execution of a rule; it is not a static value.
397 Nonexistent targets that have prerequisites
398 and are themselves prerequisites are treated specially.
401 is given the date stamp of its most recent prerequisite
402 and if this causes all the targets which have
404 as a prerequisite to be up to date,
406 is considered up to date.
409 is made in the normal fashion.
412 flag overrides this special treatment.
414 Files may be made in any order that respects
415 the preceding restrictions.
417 A recipe is executed by supplying the recipe as standard input to
423 feeds the entire recipe to the shell rather than running each line
424 of the recipe separately.)
425 The environment is augmented by the following variables:
428 all the targets of this rule.
431 the prerequisites that caused this rule to execute.
434 the prerequisites that are members of an aggregate
435 that caused this rule to execute.
436 When the prerequisites of a rule are members of an
439 contains the name of the aggregate and out of date
442 contains only the name of the members.
445 the process slot for this recipe.
447 .RB 0≤ $nproc < $NPROC .
450 the process id for the
452 executing the recipe.
455 all the prerequisites for this rule.
458 if this is a meta-rule,
460 is the string that matched
464 Otherwise, it is empty.
465 For regular expression meta-rules (see below), the variables
468 are set to the corresponding subexpressions.
471 the targets for this rule that need to be remade.
473 These variables are available only during the execution of a recipe,
474 not while evaluating the
477 Unless the rule has the
480 the recipe is printed prior to execution
481 with recognizable environment variables expanded.
482 Commands returning error status
487 Recipes and backquoted
489 commands in places such as assignments
492 environment; changes they make to
493 environment variables are not visible from
496 Variable substitution in a rule is done when
497 the rule is read; variable substitution in the recipe is done
498 when the recipe is executed. For example:
523 The colon separating the target from the prerequisites
525 immediately followed by
531 If the recipe exits with a non-null status, the target is deleted.
534 Continue execution if the recipe draws errors.
537 If there is no recipe, the target has its time updated.
540 The rule is a meta-rule that cannot be a target of a virtual rule.
541 Only files match the pattern in the target.
544 The characters after the
546 until the terminating
548 are taken as a program name.
549 It will be invoked as
550 .B "sh -c prog 'arg1' 'arg2'"
551 and should return a zero exit status
552 if and only if arg1 is up to date with respect to arg2.
553 Date stamps are still propagated in the normal way.
556 The recipe is not printed prior to execution.
559 The rule is a meta-rule using regular expressions.
562 has no special meaning.
563 The target is interpreted as a regular expression as defined in
565 The prerequisites may contain references
566 to subexpressions in form
568 as in the substitute command of
572 The targets are considered to have been updated
573 even if the recipe did not do so.
576 The targets of this rule are marked as virtual.
577 They are distinct from files of the same name.
580 A simple mkfile to compile a program:
583 .ta 8n +8n +8n +8n +8n +8n +8n
587 $LD $LDFLAGS -o $target $prereq
593 Override flag settings in the mkfile:
596 % mk target 'CFLAGS=-S -w'
603 libc.a: libc.a(abs.$O) libc.a(access.$O) libc.a(alarm.$O) ...
604 ar r libc.a $newmember
607 String expression variables to derive names from a master list:
610 NAMES=alloc arc bquote builtins expand main match mk var word
614 Regular expression meta-rules:
617 ([^/]*)/(.*)\e.$O:R: \e1/\e2.c
618 cd $stem1; $CC $CFLAGS $stem2.c
621 A correct way to deal with
630 in order to reflect changes in content, not just modification time.
635 cmp -s x.tab.h y.tab.h || cp y.tab.h x.tab.h
636 y.tab.c y.tab.h: gram.y
640 The above example could also use the
647 x.tab.h:Pcmp -s: y.tab.h
651 .B http://swtch.com/plan9port/unix
657 ``Mk: a Successor to Make''
658 (Tenth Edition Research Unix Manuals).
660 Andrew G. Hume and Bob Flandrena,
661 ``Maintaining Files on Plan 9 with Mk''.
662 .BR DOCPREFIX/doc/mk.pdf .
666 for Tenth Edition Research Unix.
667 It was later ported to Plan 9.
668 This software is a port of the Plan 9 version back to Unix.
670 Identical recipes for regular expression meta-rules only have one target.
672 Seemingly appropriate input like
674 is parsed as an erroneous attribute; correct it by inserting
675 a space after the first
678 The recipes printed by
680 before being passed to
682 for execution are sometimes erroneously expanded
683 for printing. Don't trust what's printed; rely