Blob


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