MK(1plan9)

πŸ‘‘ mk(1plan9) πŸ‘‘

okay bestie, gather round, because this is mk β€” the Plan 9 section 1 manual page for the build tool of the century, and honestly? she ate. If you've ever met make, mk is her cooler older cousin. Let's get into it πŸ’…


✨ NAME ✨

mk β€” maintain (make) related files

πŸ¦‹ SYNOPSIS πŸ¦‹

mk [ -f mkfile ] ...  [ option ...  ] [ target ...  ]

🌸 DESCRIPTION 🌸

okay so mk uses the dependency rules written in mkfile to control the update (usually by compilation, iykyk) of targets (usually files) from the source files they depend on. The mkfile (default) contains a rule for each target that identifies the files and other targets it depends on, plus an sh(1) script β€” a recipe β€” to update that target. The script runs if the target doesn't exist, or if it's older than any of the files it depends on. A mkfile can also hold meta-rules that define actions for updating implicit targets. And if you don't name a target? Then the target of the first rule (not meta-rule) in mkfile gets updated. periodt πŸ’…

The environment variable $NPROC decides how many targets can be updated simultaneously. Some operating systems β€” e.g., Plan 9 β€” set $NPROC automatically to the number of CPUs on the current machine. slay.

✨ The Options ✨

  • -a β€” Assume all targets are out of date. So yeah, everything gets updated. everything. 😭
  • -d[egp] β€” Produce debugging output (p is for parsing, g is for graph building, e is for execution). pick your fighter.
  • -e β€” Explain why each target is made. she's giving ✨transparency✨.
  • -i β€” Force any missing intermediate targets to be made.
  • -k β€” Do as much work as possible in the face of errors. resilient queen πŸ‘‘
  • -n β€” Print, but do not execute, the commands needed to update the targets. window shopping fr.
  • -s β€” Make the command line arguments sequentially rather than in parallel.
  • -t β€” Touch (update the modified date of) file targets, without executing any recipes.
  • -wtarget1,target2,... β€” Pretend the modify time for each target is the current time. super useful with -n to learn what updates would be triggered by modifying those targets. πŸ‘€

πŸ“œ The mkfile πŸ“œ

A mkfile is made of assignments (covered down in Environment) and rules. A rule contains targets and a tail. A target is a literal string, and it's normally a file name. The tail holds zero or more prerequisites plus an optional recipe, which is a shell script. Every single line of the recipe has to begin with white space. A rule looks like this:

target: prereq1 prereq2
        recipe using prereq1, prereq2 to build target

When the recipe runs, the first character on every line gets elided. bye πŸ‘‹

After the colon on the target line, a rule can specify attributes β€” those are described below.

A meta-rule has a target of the form A%B where A and B are (possibly empty) strings. A meta-rule acts as a rule for any potential target whose name matches A%B with % replaced by an arbitrary string β€” that string is called the stem. When a meta-rule is interpreted, the stem gets substituted for all occurrences of % in the prerequisite names. In the recipe of a meta-rule, the environment variable $stem contains the string matched by the %. For example, a meta-rule to compile a C program using 9c(1) might be:

%:    %.c
        9c -c $stem.c
        9l -o $stem $stem.o

Meta-rules may contain an ampersand & rather than a percent sign %. A % matches a maximal length string of any characters; an & matches a maximal length string of any characters except period or slash. iconic behavior.

The text of the mkfile is processed like this, bestie, follow along:

  • Lines beginning with < followed by a file name are replaced by the contents of the named file.
  • Lines beginning with <| followed by a file name are replaced by the output of the execution of the named file.
  • Blank lines and comments β€” which run from unquoted # characters to the following newline β€” are deleted.
  • The character sequence backslash-newline is deleted, so long lines in mkfile may be folded. 🫢
  • Non-recipe lines are processed by substituting for `{command} the output of the command when run by sh.
  • References to variables are replaced by the variables' values.
  • Special characters may be quoted using single quotes '' as in sh(1).

Assignments and rules are told apart by the first unquoted occurrence of : (rule) or = (assignment).

A later rule may modify or override an existing rule under these conditions:

  • If the targets of the rules exactly match and one rule contains only a prerequisite clause and no recipe, that clause is added to the prerequisites of the other rule. If either or both targets are virtual, the recipe is always executed.
  • If the targets of the rules match exactly and the prerequisites do not match, and both rules contain recipes, mk reports an ``ambiguous recipe'' error. πŸ’€
  • If the target and prerequisites of both rules match exactly, the second rule overrides the first.

🌈 Environment 🌈

Rules may make use of shell environment variables. A legal reference of the form $OBJ or ${name} is expanded as in sh(1). A reference of the form ${name:A%B=C%D}, where A, B, C, D are (possibly empty) strings, has the value formed by expanding $name and substituting C for A and D for B in each word in $name that matches pattern A%B. fancy fr.

Variables can be set by assignments of the form

var=[attr=]value

Blanks in the value break it into words. Such variables are exported to the environment of recipes as they're executed β€” unless U is present, and U is the only legal attribute attr. The initial value of a variable is taken from (in increasing order of precedence): the default values below, mk's environment, the mkfiles, and any command line assignment as an argument to mk. A variable assignment argument overrides the first (but not any subsequent) assignment to that variable.

The variable MKFLAGS contains all the option arguments (arguments starting with or containing and MKARGS contains all the targets in the call to mk.

The variable MKSHELL contains the shell command line mk uses to run recipes. If the first word of the command ends in rc or rcsh, mk uses rc(1)'s quoting rules; otherwise it uses sh(1)'s. The MKSHELL variable is consulted when the mkfile is read, not when it's executed, so different shells can be used within a single mkfile:

MKSHELL=$PLAN9/bin/rc
use-rc:V:
     for(i in a b c) echo $i

MKSHELL=sh
use-sh:V:
     for i in a b c; do echo $i; done

Mkfiles included via < or <| (q.v.) see their own private copy of MKSHELL, which always starts set to sh .

Dynamic information may be included in the mkfile by using a line of the form

<|command args

This runs the command command with the given arguments args and pipes its standard output to mk to be included as part of the mkfile. For instance, the Inferno kernels use this technique to run a shell command with an awk script and a configuration file as arguments, so that the awk script processes the file and outputs a set of variables and their values. chef's kiss πŸ§‘β€πŸ³

πŸš€ Execution πŸš€

During execution, mk figures out which targets must be updated β€” and in what order β€” to build the names specified on the command line. Then it runs the associated recipes.

A target is considered up to date if it has no prerequisites, or if all its prerequisites are up to date and it is newer than all its prerequisites. Once the recipe for a target has executed, that target is considered up to date. simple as.

The date stamp used to determine if a target is up to date is computed differently for different types of targets:

  • If a target is virtual (the target of a rule with the V attribute), its date stamp is initially zero; when the target is updated, the date stamp is set to the most recent date stamp of its prerequisites.
  • Otherwise, if a target does not exist as a file, its date stamp is set to the most recent date stamp of its prerequisites, or zero if it has no prerequisites.
  • Otherwise, the target is the name of a file, and the target's date stamp is always that file's modification date.

The date stamp is computed when the target is needed in the execution of a rule; it is not a static value. she's dynamic, she's fluid, she's that girl πŸ’…

Nonexistent targets that have prerequisites and are themselves prerequisites are treated specially. Such a target t is given the date stamp of its most recent prerequisite, and if this causes all the targets which have t as a prerequisite to be up to date, t is considered up to date. Otherwise, t is made in the normal fashion. The -i flag overrides this special treatment.

Files may be made in any order that respects the preceding restrictions.

A recipe is executed by supplying the recipe as standard input to the command /bin/sh. (Note that unlike make, mk feeds the entire recipe to the shell rather than running each line of the recipe separately.) The environment is augmented by the following variables:

  • $alltarget β€” all the targets of this rule.
  • $newprereq β€” the prerequisites that caused this rule to execute.
  • $newmember β€” the prerequisites that are members of an aggregate that caused this rule to execute. When the prerequisites of a rule are members of an aggregate, $newprereq contains the name of the aggregate and out of date members, while $newmember contains only the name of the members.
  • $nproc β€” the process slot for this recipe. It satisfies 0≀$nproc<$NPROC.
  • $pid β€” the process id for the mk executing the recipe.
  • $prereq β€” all the prerequisites for this rule.
  • $stem β€” if this is a meta-rule, $stem is the string that matched % or &. Otherwise, it is empty. For regular expression meta-rules (see below), the variables are set to the corresponding subexpressions.
  • $target β€” the targets for this rule that need to be remade.

These variables are available only during the execution of a recipe, not while evaluating the mkfile.

Unless the rule has the Q attribute, the recipe is printed prior to execution with recognizable environment variables expanded. Commands returning error status cause mk to terminate. πŸ’€

Recipes and backquoted rc commands in places such as assignments execute in a copy of mk's environment; changes they make to environment variables are not visible from mk. sorry bestie, what happens in the subshell stays in the subshell.

Variable substitution in a rule is done when the rule is read; variable substitution in the recipe is done when the recipe is executed. For example:

bar=a.c
foo: $bar
        $CC -o foo $bar
bar=b.c

will compile b.c into foo, if a.c is newer than foo. the way that order matters… 😭

πŸŽ€ Aggregates πŸŽ€

Names of the form a(b) refer to member b of the aggregate a. Currently, the only aggregates supported are 9ar (see 9c(1)) archives.

πŸ‘‘ Attributes πŸ‘‘

The colon separating the target from the prerequisites may be immediately followed by attributes and another colon. The attributes are:


and the attributes themselves? oh bestie, that's the next part πŸ’– stay tuned ✨

  • D β€” if the recipe exits with a non-null status, bestie, the target is deleted. no cap. πŸ’€
  • E β€” continue execution if the recipe draws errors. we don't stop for the drama. ✨
  • N β€” if there is no recipe, the target has its time updated. lowkey just a timestamp glow-up. πŸ•°οΈ
  • n β€” the rule is a meta-rule that cannot be a target of a virtual rule. only files match the pattern in the target. iykyk. 🧚
  • P β€” the characters after the P until the terminating : are taken as a program name. it will be invoked as sh -c prog 'arg1' 'arg2' and should return a zero exit status if and only if arg1 is up to date with respect to arg2. date stamps are still propagated in the normal way. periodt. πŸ“…
  • Q β€” the recipe is not printed prior to execution. stealth mode activated. 🀫
  • R β€” the rule is a meta-rule using regular expressions. in the rule, % has no special meaning. the target is interpreted as a regular expression as defined in regexp(7). the prerequisites may contain references to subexpressions in form \n, as in the substitute command of sed(1). regex girlies rise. πŸ”₯
  • U β€” the targets are considered to have been updated even if the recipe did not do so. fake it till you make it. πŸ’…
  • V β€” the targets of this rule are marked as virtual. they are distinct from files of the same name. main character energy, not a file. πŸ‘‘

✨ EXAMPLES ✨

A simple mkfile to compile a program:

</$objtype/mkfile

prog:   a.$O b.$O c.$O
        $LD $LDFLAGS -o $target $prereq

%.$O:   %.c
        $CC $CFLAGS $stem.c

Override flag settings in the mkfile:

% mk target 'CFLAGS=-S -w'

Maintain a library:

libc.a(%.$O):N: %.$O
libc.a: libc.a(abs.$O) libc.a(access.$O) libc.a(alarm.$O) ...
        ar r libc.a $newmember

String expression variables to derive names from a master list:

NAMES=alloc arc bquote builtins expand main match mk var word
OBJ=${NAMES:%=%.$O}

Regular expression meta-rules:

([^/]*)/(.*)\.$O:R:  \1/\2.c
        cd $stem1; $CC $CFLAGS $stem2.c

A correct way to deal with yacc(1) grammars. The file lex.c includes the file x.tab.h rather than y.tab.h in order to reflect changes in content, not just modification time.

lex.$O: x.tab.h
x.tab.h:        y.tab.h
        cmp -s x.tab.h y.tab.h || cp y.tab.h x.tab.h
y.tab.c y.tab.h:        gram.y
        $YACC -d gram.y

The above example could also use the P attribute for the x.tab.h rule:

x.tab.h:Pcmp -s:        y.tab.h
        cp y.tab.h x.tab.h

πŸ“¦ SOURCE πŸ“¦

/src/cmd/mk


πŸ‘€ SEE ALSO πŸ‘€

  • sh(1)
  • regexp(7)
  • A. Hume, ``Mk: a Successor to Make'' (Tenth Edition Research Unix Manuals).
  • Andrew G. Hume and Bob Flandrena, ``Maintaining Files on Plan 9 with Mk''. DOCPREFIX/doc/mk.pdf

πŸ•°οΈ HISTORY πŸ•°οΈ

Andrew Hume wrote mk for Tenth Edition Research Unix. It was later ported to Plan 9. This software is a port of the Plan 9 version back to Unix.


πŸ’€ BUGS πŸ’€

Identical recipes for regular expression meta-rules only have one target.

Seemingly appropriate input like CFLAGS=-DHZ=60 is parsed as an erroneous attribute; correct it by inserting a space after the first

The recipes printed by mk before being passed to the shell for execution are sometimes erroneously expanded for printing. Don't trust what's printed; rely on what the shell does.


This page is MK(1plan9) β€” that's the source/version β€” and the date is... well, the footer didn't show it, but we move. stay slaying, bestie. πŸ’–βœ¨

πŸ“¦ where this page comes from