mk, membername – maintain (make) related files
uses the dependency rules specified in
to control the update (usually by compilation) of
from the source files upon which they depend.
for each target that identifies the files and other
targets upon which it depends and an
to update the target.
The script is run if the target does not exist
or if it is older than any of the files it depends on.
may also contain
that define actions for updating implicit targets.
is specified, the target of the first rule (not meta-rule) in
The environment variable
determines how many targets may be updated simultaneously;
Plan 9 sets
automatically to the number of CPUs on the current machine.
Assume all targets to be out of date.
Thus, everything is updated.
Produce debugging output
is for parsing,
for graph building,
Explain why each target is made.
Force any missing intermediate targets to be made.
Do as much work as possible in the face of errors.
Print, but do not execute, the commands
needed to update the targets.
Make the command line arguments sequentially rather than in parallel.
Touch (update the modified date of) file targets, without
executing any recipes.
Pretend the modify time for each
is the current time; useful in conjunction with
to learn what updates would be triggered by
extracts member names
(see ‘Aggregates’ below)
from its arguments.
(described under ‘Environment’) and
A rule contains
A target is a literal string
and is normally a file name.
The tail contains zero or more
and an optional
which is an
Each line of the recipe must begin with white space.
A rule takes the form
target: prereq1 prereq2
rc recipe using prereq1, prereq2 to build target
When the recipe is executed,
the first character on every line is elided.
After the colon on the target line, a rule may specify
has a target of the form
are (possibly empty) strings.
A meta-rule acts as a rule for any potential target whose
replaced by an arbitrary string, called the
In interpreting a meta-rule,
the stem is substituted for all occurrences of
in the prerequisite names.
In the recipe of a meta-rule, the environment variable
contains the string matched by the
For example, a meta-rule to compile a C program using
2l -o $stem $stem.2
Meta-rules may contain an ampersand
rather than a percent sign
matches a maximal length string of any characters;
matches a maximal length string of any characters except period
The text of the
is processed as follows.
Lines beginning with
followed by a file name are replaced by the contents of the named
Lines beginning with
followed by a file name are replaced by the output
of the execution of the named
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
may be folded.
Non-recipe lines are processed by substituting for
the output of the
when run by
References to variables are replaced by the variables’ values.
Special characters may be quoted using single quotes
Assignments and rules are distinguished by
the first unquoted occurrence of
A later rule may modify or override an existing rule under the
If the targets of the rules exactly match and one rule
contains only a prerequisite clause and no recipe, the
clause is added to the prerequisites of the other rule.
If either or both targets are virtual, the recipe is
If the targets of the rules match exactly and the
prerequisites do not match and both rules
reports an “ambiguous recipe” error.
If the target and prerequisites of both rules match exactly,
the second rule overrides the first.
Rules may make use of
A legal reference of the form
is expanded as in
A reference of the form
A, B, C, D
are (possibly empty) strings,
has the value formed by expanding
in each word in
that matches pattern
Variables can be set by
assignments of the form
Blanks in the
break it into words, as in
but without the surrounding parentheses.
Such variables are exported
to the environment of
recipes as they are executed, unless
the only legal attribute
The initial value of a variable is
taken from (in increasing order of precedence)
the default values below,
and any command line assignment as an argument to
A variable assignment argument overrides the first (but not any subsequent)
assignment to that variable.
contains all the option arguments (arguments starting with
contains all the targets in the call to
It is recommended that mkfiles start with
to values appropriate to the target architecture (see the examples below).
determines which targets must be updated, and in what order,
to build the
specified on the command line.
It then 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, the target is
considered up to date.
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
(the target of a rule with the
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.
Nonexistent targets that have prerequisites
and are themselves prerequisites are treated specially.
Such a target
is given the date stamp of its most recent prerequisite
and if this causes all the targets which have
as a prerequisite to be up to date,
is considered up to date.
is made in the normal fashion.
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
/bin/rc -e -I
is omitted if the
attribute is set).
The environment is augmented by the following variables:
all the targets of this rule.
the prerequisites that caused this rule to execute.
the prerequisites that are members of an aggregate
that caused this rule to execute.
When the prerequisites of a rule are members of an
contains the name of the aggregate and out of date
contains only the name of the members.
the process slot for this recipe.
the process id for the
executing the recipe.
all the prerequisites for this rule.
if this is a meta-rule,
is the string that matched
Otherwise, it is empty.
For regular expression meta-rules (see below), the variables
are set to the corresponding subexpressions.
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
Unless the rule has the
the recipe is printed prior to execution
with recognizable environment variables expanded.
Commands returning nonempty status (see
Recipes and backquoted
commands in places such as assignments
execute in a copy of
environment; changes they make to
environment variables are not visible from
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:
$CC -o foo $bar
is newer than
Names of the form
refer to member
of the aggregate
Currently, the only aggregates supported are
The colon separating the target from the prerequisites
immediately followed by
and another colon.
The attributes are:
If the recipe exits with a non-null status, the target is deleted.
Continue execution if the recipe draws errors.
If there is no recipe, the target has its time updated.
The rule is a meta-rule that cannot be a target of a virtual rule.
Only files match the pattern in the target.
The characters after the
until the terminating
are taken as a program name.
It will be invoked as
rc -c prog ’arg1’ ’arg2’
and should return a null exit status
if and only if arg1 is up to date with respect to arg2.
Date stamps are still propagated in the normal way.
The recipe is not printed prior to execution.
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
The prerequisites may contain references
to subexpressions in form
as in the substitute command of
The targets are considered to have been updated
even if the recipe did not do so.
The targets of this rule are marked as virtual.
They are distinct from files of the same name.
A simple mkfile to compile a program:
prog: a.$O b.$O c.$O
$LD $LDFLAGS -o $target $prereq
$CC $CFLAGS $stem.c
Override flag settings in the mkfile:
% mk target 'CFLAGS=-S -w'
Maintain a library:
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
Regular expression meta-rules:
cd $stem1; $CC $CFLAGS $stem2.c
A correct way to deal with
includes the file
in order to reflect changes in content, not just modification time.
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
attribute for the
x.tab.h:Pcmp -s: y.tab.h
cp y.tab.h x.tab.h
“Mk: a Successor to Make”.
Andrew G. Hume and Bob Flandrena,
“Maintaining Files on Plan 9 with Mk”.
Identical recipes for regular expression meta-rules only have one target.
Seemingly appropriate input like
is parsed as an erroneous attribute; correct it by inserting
a space after the first
The recipes printed by
before being passed to
for execution are sometimes erroneously expanded
for printing. Don’t trust what’s printed; rely