.HTML "Plan 9 Mkfiles
.TL
Plan 9 Mkfiles
.AU
Bob Flandrena
bobf@plan9.bell-labs.com
.SH
Introduction
.LP
Every Plan 9 source directory contains a file, called
.CW mkfile ,
specifying the rules for building the executable or
library that is the product of the directory.
.I Mk (1)
interprets the rules in the file, calculates
the dependencies, and executes an
.I rc (1)
script to construct the product.
If necessary components are supplied by
neighboring directories or sub-directories, the mkfiles in those
directories are first executed to build the components
before the local construction proceeds.
.LP
Most application source directories produce one of
four types of product:
a single executable, several
executables, a local library, or
a system library.
Four generic
mkfiles
define the normal rules
for building each type of product. The simplest
mkfiles need only
list the components
and include the appropriate
generic
mkfile
to do the work.
More complex
mkfiles
may supply additional rules
to augment, modify, or override the generic rules.
.SH
Using a Mkfile
.LP
To build a product, change to the directory containing
its source and invoke
.I mk
with the appropriate target as an argument.
All mkfiles provide the following standard targets:
.TS
lw(1i) lw(4.5i).
\f(CWall\fP T{
Build a local version of the product or products for the
current architecture. If the product is a single program,
the result is stored in file
.CW $O.out .
If the directory produces multiple executables, they are
stored in the files named
.CW $O.\fIprogname,\fP
where
.I progname
is the name of each executable.
A product may be built for a different architecture by
prefacing the
.CW mk
command with
\f(CWobjtype=\fP\fIarchitecture\fP,
where
.I architecture
is the name of the target architecture.
Directories producing system
libraries always operate directly on the installed version of the
library; in this case the target
.CW all
is equivalent to the target
.CW install .
T}
\f(CWinstall\fP T{
Build and install the product or products for the current
architecture.
T}
\f(CWinstallall\fP T{
Build and install the product or products for all architectures.
T}
\f(CWclean\fP T{
Rid the directory and its subdirectories of the by-products of
the build process. Intermediate files that are easily reproduced
(e.g., object files,
.CW yacc
intermediates, target executables) are always
removed. Complicated intermediates, such as local libraries, are
usually preserved.
T}
\f(CWnuke\fP T{
Remove all intermediates from the directory and any subdirectories.
This target guarantees that a subsequent build for the
architecture is performed
from scratch.
T}
.TE
.LP
If no target is specified on the
.CW mk
command line, the
.CW all
target is built by default. In a directory
producing multiple executables, there is
no default target.
.LP
In addition to the five standard targets,
additional targets may be supplied by each
generic mkfile or by the directory's mkfile.
.LP
The environment variable
.CW NPROC
is set by the system to the number of
available processors.
Setting
this variable, either in the environment or in
a mkfile, controls the amount of parallelism in
the build. For example, the command
.P1
NPROC=1 mk
.P2
restricts a build to a single thread of execution.
.SH
Creating a Mkfile
.LP
The easiest way to build a new mkfile is to copy and modify
an existing mkfile of the same type.
Failing that, it is usually possible to create a new
mkfile with minimal effort, since the appropriate
generic mkfile predefines the rules that do all the work.
In the simplest and most common cases, the new mkfile
need only define a couple of variables and include the appropriate
architecture-specific
and generic mkfiles.
.SH The Generic Mkfiles
.LP
There are four generic mkfiles containing commonly
used rules for building a product:
.CW mkone ,
.CW mkmany ,
.CW mklib ,
and
.CW mksyslib .
These rules
perform such actions as compiling C source files,
loading object files, archiving libraries, and
installing executables in the
.CW bin
directory of the appropriate architecture.
The generic mkfiles are stored in directory
.CW /sys/src/cmd .
Mkfile
.CW mkone
builds a single executable,
.CW mkmany
builds several executables from the source in a single
directory, and
.CW mklib
and
\f(CWmksyslib\fP,
maintain local and system libraries, respectively.
The rules in the generic mkfiles are driven by
the values of variables, some of which must be
set by the product mkfile and some of which are
supplied by the generic mkfile. Variables in the
latter class include:
.TS
center;
ri ci li
rw(1i) cw(0.5i) lw(2i).
Variable Default Meaning
.sp .5
\f(CWCFLAGS\fP \f(CW-FVw\fP C compiler flags
\f(CWLDFLAGS\fP Loader flags
\f(CWYFLAGS\fP \f(CW-d\fP Yacc flags
\f(CWAFLAGS\fP Assembler flags
.TE
.LP
The following variables are set by the product mkfile
and used by the generic mkfile.
Any may be empty depending on the specific product being
made.
.TS
center;
lw(1i) lw(2.5i).
\f(CWTARG\fP Name(s) of the executable(s) to be built
\f(CWLIB\fP Library name(s)
\f(CWOFILES\fP Object files
\f(CWHFILES\fP Header files included by all source files
\f(CWYFILES\fP \f(CWYacc\fP input files
\f(CWBIN\fP Directory where executables are installed
.TE
.SH
Mkfile Organization
.LP
All
mkfiles
share the following common structure:
.P1
$objtype/mkfile # \f1architecture-dependent definitions\fP
.sp
\fIvariable definitions\fP # TARG\f1, \fPOFILES\f1, \fPHFILES\f1, etc.\fP
.sp
proctab.c
$cpuobjtype.maketab: y.tab.h maketab.c
objtype=$cputype
mk maketab.$cputype
maketab.$cputype:V: y.tab.h maketab.$O
$LD -o $O.maketab maketab.$O
.P2
.SH
Mkmany
.LP
The
.CW mkmany
generic mkfile builds several
executables from the files in a
directory. It differs from the operation of
.CW mkone
in three respects:
.CW TARG
specifies the names of all executables,
there is no default command-line target,
and additional rules allow a single executable to
be built or installed.
.LP
The
.CW TARG
variable specifies the names of all
executables produced by the mkfile. The
rules assume the name of each executable is also
the name of the file containing its
.CW main
function.
.CW OFILES
specifies files containing
common subroutines loaded with all executables.
Consider the mkfile:
.P1
$objtype/mkfile
TARG=alpha beta
OFILES=common.$O
BIN=/$objtype/bin