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