Warning: Attempt to read property "date" on null in /usr/local/www/websvn.planix.org/blame.php on line 247

Warning: Attempt to read property "msg" on null in /usr/local/www/websvn.planix.org/blame.php on line 247
WebSVN – planix.SVN – Blame – /os/branches/feature_unix/sys/doc/mkfiles.ms – Rev 2

Subversion Repositories planix.SVN

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 - 1
.HTML "Plan 9 Mkfiles
2
.TL
3
Plan 9 Mkfiles
4
.AU
5
Bob Flandrena
6
bobf@plan9.bell-labs.com
7
.SH
8
Introduction
9
.LP
10
Every Plan 9 source directory contains a file, called
11
.CW mkfile ,
12
specifying the rules for building the executable or
13
library that is the product of the directory.
14
.I Mk (1)
15
interprets the rules in the file, calculates
16
the dependencies, and executes an
17
.I rc (1)
18
script to construct the product.
19
If necessary components are supplied by
20
neighboring directories or sub-directories, the mkfiles in those
21
directories are first executed to build the components
22
before the local construction proceeds.
23
.LP
24
Most application source directories produce one of
25
four types of product:
26
a single executable, several
27
executables, a local library, or
28
a system library.
29
Four generic
30
mkfiles
31
define the normal rules
32
for building each type of product.  The simplest
33
mkfiles need only
34
list the components
35
and include the appropriate
36
generic
37
mkfile 
38
to do the work.
39
More complex 
40
mkfiles
41
may supply additional rules
42
to augment, modify, or override the generic rules.
43
.SH
44
Using a Mkfile
45
.LP
46
To build a product, change to the directory containing
47
its source and invoke
48
.I mk
49
with the appropriate target as an argument.
50
All mkfiles provide the following standard targets:
51
.TS
52
lw(1i) lw(4.5i).
53
\f(CWall\fP	T{
54
Build a local version of the product or products for the
55
current architecture.  If the product is a single program,
56
the result is stored in file
57
.CW $O.out .
58
If the directory produces multiple executables, they are
59
stored in the files named
60
.CW $O.\fIprogname,\fP
61
where
62
.I progname
63
is the name of each executable.
64
A product may be built for a different architecture by
65
prefacing the
66
.CW mk
67
command with
68
\f(CWobjtype=\fP\fIarchitecture\fP,
69
where
70
.I architecture
71
is the name of the target architecture.
72
Directories producing system
73
libraries always operate directly on the installed version of the
74
library; in this case the target
75
.CW all
76
is equivalent to the target
77
.CW install .
78
T}
79
\f(CWinstall\fP	T{
80
Build and install the product or products for the current
81
architecture.
82
T}
83
\f(CWinstallall\fP	T{
84
Build and install the product or products for all architectures.
85
T}
86
\f(CWclean\fP	T{
87
Rid the directory and its subdirectories of the by-products of
88
the build process.  Intermediate files that are easily reproduced
89
(e.g., object files,
90
.CW yacc
91
intermediates, target executables) are always
92
removed.  Complicated intermediates, such as local libraries, are
93
usually preserved.
94
T}
95
\f(CWnuke\fP	T{
96
Remove all intermediates from the directory and any subdirectories.
97
This target guarantees that a subsequent build for the
98
architecture is performed
99
from scratch.
100
T}
101
.TE
102
.LP
103
If no target is specified on the
104
.CW mk
105
command line, the
106
.CW all
107
target is built by default.  In a directory
108
producing multiple executables, there is
109
no default target.
110
.LP
111
In addition to the five standard targets,
112
additional targets may be supplied by each
113
generic mkfile or by the directory's mkfile.
114
.LP
115
The environment variable
116
.CW NPROC
117
is set by the system to the number of
118
available processors.
119
Setting
120
this variable, either in the environment or in
121
a mkfile, controls the amount of parallelism in
122
the build.  For example, the command
123
.P1
124
	NPROC=1 mk
125
.P2
126
restricts a build to a single thread of execution.
127
.SH
128
Creating a Mkfile
129
.LP
130
The easiest way to build a new mkfile is to copy and modify
131
an existing mkfile of the same type.
132
Failing that, it is usually possible to create a new
133
mkfile with minimal effort, since the appropriate
134
generic mkfile predefines the rules that do all the work.
135
In the simplest and most common cases, the new mkfile
136
need only define a couple of variables and include the appropriate
137
architecture-specific
138
and generic mkfiles.
139
.SH The Generic Mkfiles
140
.LP
141
There are four generic mkfiles containing commonly
142
used rules for building a product:
143
.CW mkone ,
144
.CW mkmany ,
145
.CW mklib ,
146
and
147
.CW mksyslib .
148
These rules
149
perform such actions as compiling C source files,
150
loading object files, archiving libraries, and
151
installing executables in the
152
.CW bin
153
directory of the appropriate architecture.
154
The generic mkfiles are stored in directory
155
.CW /sys/src/cmd .
156
Mkfile
157
.CW mkone
158
builds a single executable,
159
.CW mkmany
160
builds several executables from the source in a single
161
directory, and
162
.CW mklib
163
and
164
\f(CWmksyslib\fP,
165
maintain local and system libraries, respectively.
166
The rules in the generic mkfiles are driven by
167
the values of variables, some of which must be
168
set by the product mkfile and some of which are
169
supplied by the generic mkfile.  Variables in the
170
latter class include:
171
.TS
172
center;
173
ri ci li
174
rw(1i) cw(0.5i) lw(2i).
175
Variable	Default	Meaning
176
.sp .5
177
\f(CWCFLAGS\fP	\f(CW-FVw\fP	C compiler flags
178
\f(CWLDFLAGS\fP		Loader flags
179
\f(CWYFLAGS\fP	\f(CW-d\fP	Yacc flags
180
\f(CWAFLAGS\fP		Assembler flags
181
.TE
182
.LP
183
The following variables are set by the product mkfile
184
and used by the generic mkfile.
185
Any may be empty depending on the specific product being
186
made.
187
.TS
188
center;
189
lw(1i) lw(2.5i).
190
\f(CWTARG\fP	Name(s) of the executable(s) to be built
191
\f(CWLIB\fP	Library name(s)
192
\f(CWOFILES\fP	Object files
193
\f(CWHFILES\fP	Header files included by all source files
194
\f(CWYFILES\fP	\f(CWYacc\fP input files
195
\f(CWBIN\fP	Directory where executables are installed
196
.TE
197
.SH
198
Mkfile Organization
199
.LP
200
All
201
mkfiles
202
share the following common structure:
203
.P1
204
</$objtype/mkfile	# \f1architecture-dependent definitions\fP
205
.sp
206
\fIvariable definitions\fP		# TARG\f1, \fPOFILES\f1, \fPHFILES\f1, etc.\fP
207
.sp
208
</sys/src/cmd/\fIgeneric\fP	# mkone\f1, \fPmkmany\f1, \fPmklib\f1, or \fPmksyslib
209
.sp
210
\fIvariable overrides\fP		# CFLAGS\f1, \fPobjtype\f1, etc.\fP
211
.sp
212
\fIextra rules\fP			# \f1overrides, augmented rules, additional targets\fP
213
.P2
214
Note that the architecture-dependent mkfiles include file
215
.CW /sys/src/mkfile.proto
216
for system-wide variables that are common to all architectures.
217
.LP
218
The variables driving the expansion of the generic mkfile
219
may be specified in any order as long as they are defined
220
before the inclusion of the generic mkfile.  The value
221
of a variable may be changed by assigning a new value
222
following the inclusion of the generic mkfile, but the
223
effects are sometimes counter-intuitive.
224
Such variable assignments do not apply to the target and
225
prerequisite portions of any previously defined rules;
226
the new values only apply to the recipes of rules preceding
227
the assignment statement and
228
to all parts of any rules following it.
229
.LP
230
The rules supplied by the generic mkfile may
231
be overridden or augmented.  The new rules must
232
be specified after the inclusion of the generic
233
mkfile.  If the target and prerequisite portion
234
of the rule exactly match the target and prerequisite
235
portion of a previously defined rule and the new rule contains
236
a recipe, the new rule replaces the old one.
237
If the target of a new rule exactly matches the
238
target of a previous rule and one or more new
239
prerequisites are specified and the new rule contains
240
no recipe, the new prerequisites are added to the prerequisites
241
of the old rule.
242
.LP
243
Following sections discuss
244
each generic mkfile in detail.
245
.SH
246
Mkone
247
.LP
248
The
249
.CW mkone
250
generic mkfile contains rules for building
251
a single executable from one or more files
252
in a directory.
253
The variable
254
.CW TARG
255
specifies the name of the executable and
256
variables
257
.CW OFILES
258
and
259
.CW YFILES
260
specify the object files and
261
.CW yacc
262
source files used to build it.
263
.CW HFILES
264
contains the names of the local header files
265
included in all source files.
266
.CW BIN
267
is the name of the directory where the executable
268
is installed.
269
.CW LIB
270
contains the names of local libraries used by the
271
linker.  This variable is rarely needed
272
as libraries referenced by a
273
.CW #pragma
274
directive in an associated header file, including
275
all system libraries, are automatically
276
searched by the loader.
277
.LP
278
If
279
.CW mk
280
is executed without a target, the
281
.CW all
282
target is built; it
283
produces an executable in
284
.CW $O.out .
285
Variable
286
.CW HFILES
287
identifies the header files that
288
are included in all or most or
289
the C source files.  Occasionally,
290
a program has other header files
291
that are only used in some
292
source files.  A
293
header can be added to the prerequisites for
294
those object files by adding a rule of
295
the following form following the inclusion of generic mkfile
296
.CW mkone :
297
.P1
298
file.$O:	header.h
299
.P2
300
.LP
301
The mkfile for a directory producing a single
302
executable using the normal set of rules is
303
trivial: a list of some files followed by the
304
inclusion of
305
.I mkone.
306
For example, 
307
.CW /sys/src/cmd/diff/mkfile
308
contains:
309
.P1
310
< /$objtype/mkfile
311
 
312
TARG=diff
313
OFILES=\e
314
	diffdir.$O\e
315
	diffio.$O\e
316
	diffreg.$O\e
317
	main.$O\e
318
 
319
HFILES=diff.h
320
 
321
BIN=/$objtype/bin
322
</sys/src/cmd/mkone
323
.P2
324
The more complex mkfile in
325
.CW /sys/src/cmd/awk
326
overrides compiler and loader variables to
327
select the ANSI/POSIX Computing Environment with appropriately
328
defined command line variables.  It also overrides
329
the default
330
.CW yacc
331
rule to place the output soure in file
332
.CW awkgram.c
333
and the
334
.CW clean
335
and
336
.CW nuke
337
rules, so it can remove the non-standard intermediate
338
files.  Finally, the last three rules build a version of
339
.CW maketab
340
appropriate for the architecture where the
341
.CW mk
342
is being
343
run and then executes it to create source file
344
.CW proctab.c :
345
.P1
346
</$objtype/mkfile
347
 
348
TARG=awk
349
OFILES=re.$O\e
350
	lex.$O\e
351
	main.$O\e
352
	parse.$O\e
353
	proctab.$O\e
354
	tran.$O\e
355
	lib.$O\e
356
	run.$O\e
357
	awkgram.$O\e
358
 
359
HFILES=awk.h\e
360
	y.tab.h\e
361
	proto.h\e
362
 
363
YFILES=awkgram.y
364
 
365
BIN=/$objtype/bin
366
</sys/src/cmd/mkone
367
CFLAGS=-c -D_REGEXP_EXTENSION -D_RESEARCH_SOURCE \e
368
	-D_BSD_EXTENSION -DUTF
369
YFLAGS=-S -d -v
370
CC=pcc
371
LD=pcc
372
cpuobjtype=`{sed -n 's/^O=//p' /$cputype/mkfile}
373
 
374
y.tab.h awkgram.c:	$YFILES
375
	$YACC -o awkgram.c $YFLAGS $prereq
376
 
377
clean:V:
378
	rm -f *.[$OS] [$OS].out [$OS].maketab y.tab.? y.debug\e
379
		 y.output $TARG
380
 
381
nuke:V:
382
	rm -f *.[$OS] [$OS].out [$OS].maketab y.tab.? y.debug\e
383
		 y.output awkgram.c $TARG
384
 
385
proctab.c:	$cpuobjtype.maketab
386
	./$cpuobjtype.maketab >proctab.c
387
 
388
$cpuobjtype.maketab:	y.tab.h maketab.c
389
	objtype=$cputype
390
	mk maketab.$cputype
391
 
392
maketab.$cputype:V:	y.tab.h maketab.$O
393
	$LD -o $O.maketab maketab.$O
394
.P2
395
.SH
396
Mkmany
397
.LP
398
The
399
.CW mkmany
400
generic mkfile builds several
401
executables from the files in a
402
directory.  It differs from the operation of
403
.CW mkone
404
in three respects:
405
.CW TARG
406
specifies the names of all executables,
407
there is no default command-line target,
408
and additional rules allow a single executable to
409
be built or installed.
410
.LP
411
The
412
.CW TARG
413
variable specifies the names of all
414
executables produced by the mkfile.  The
415
rules assume the name of each executable is also
416
the name of the file containing its
417
.CW main
418
function.
419
.CW OFILES
420
specifies files containing
421
common subroutines loaded with all executables.
422
Consider the mkfile:
423
.P1
424
</$objtype/mkfile
425
 
426
TARG=alpha beta
427
OFILES=common.$O
428
BIN=/$objtype/bin
429
</sys/src/cmd/mkmany
430
.P2
431
It assumes the main functions for executables
432
.CW alpha
433
and
434
.CW beta
435
are in files
436
.CW alpha.$O
437
and
438
.CW beta.$O
439
and that both programs use the subroutines
440
in file
441
.CW common.$O .
442
The
443
.CW all
444
target builds all executables, leaving each in
445
a file with a name of the form
446
.CW $O.\fIprogname\fP
447
where
448
.I progname
449
is the name of the executable.  In this
450
example the
451
.CW all
452
target produces executables
453
.CW $O.alpha
454
and 
455
.CW $O.beta .
456
.LP
457
The
458
.CW mkmany
459
rules provide additional
460
targets for building a single
461
executable:
462
.TS
463
lw(1i) lw(3.8i).
464
\f(CW$O.progname\fP	T{
465
Builds executable 
466
\f(CW$O.\fP\fIprogname\fP
467
in the current directory.  When the target
468
architecture is not the current architecture
469
the
470
.CW mk
471
command
472
must be prefixed with the customary
473
.CW objtype=\fIarchitecture\fP
474
assignment to select the proper compilers and loaders.
475
T}
476
\f(CWprogname.install\fP	T{
477
Installs executable
478
.I progname
479
for the target architecture.
480
T}
481
\f(CWprogname.installall\fP	T{
482
Installs executable
483
.I progname
484
for all architectures.
485
T}
486
.TE
487
.SH
488
Mklib
489
.LP
490
The
491
.CW mklib
492
generic mkfile builds a local library.
493
Since this form of mkfile constructs no
494
executable, the
495
.CW TARG 
496
and
497
.CW BIN
498
variables are not needed.  Instead, the
499
.CW LIB
500
variable specifies the library
501
to be built or updated.  Variable
502
.CW OFILES
503
contains the names of the object files to be archived
504
in the library.  The use of variables
505
.CW YFILES
506
and
507
.CW HFILES
508
does not change.  When possible, only the
509
out-of-date members of the library are updated.
510
.LP
511
The variable
512
.CW LIBDIR
513
contains the name of the directory where the
514
library is installed; by default it selects
515
the current directory.  It can be overridden
516
by assigning the new directory name after the
517
point where
518
.CW mklib
519
is included.
520
.LP
521
The
522
.CW clean
523
target removes object files and
524
.CW yacc
525
intermediate files but does not touch the
526
library.  The
527
.CW nuke
528
target removes the library as well as the
529
files removed by the
530
.CW clean
531
target.  The command
532
.P1
533
mk -s clean all
534
.P2
535
causes the existing library to be updated, or
536
created if it doesn't already exist.  The command
537
.P1
538
mk -s nuke all
539
.P2
540
forces the library to be rebuilt from scratch.
541
.LP
542
The mkfile from
543
.CW /sys/src/cmd/upas/libString
544
contains the following specifications to
545
build the local library
546
.CW libString.a$O
547
for the object architecture referenced by
548
.CW $O\fR\:\fP
549
.P1
550
</$objtype/mkfile
551
 
552
LIB=libString.a$O
553
OFILES=	s_alloc.$O\e
554
	s_append.$O\e
555
	s_array.$O\e
556
	s_copy.$O\e
557
	s_getline.$O\e
558
	s_grow.$O\e
559
	s_nappend.$O\e
560
	s_parse.$O\e
561
	s_read.$O\e
562
	s_read_line.$O\e
563
	s_tolower.$O\e
564
 
565
</sys/src/cmd/mklib
566
 
567
nuke:V:
568
	mk clean
569
	rm -f libString.a[$OS]
570
.P2
571
The override of the rule for target
572
.CW nuke
573
removes the libraries for all architectures as
574
opposed to the default recipe for this target
575
which removes the library for the current architecture.
576
.SH
577
Mksyslib
578
.LP
579
The
580
.CW mksyslib
581
generic mkfile is similar to the
582
.CW mklib
583
mkfile except that it operates on a system library
584
instead of a local library.
585
The
586
.CW install
587
and
588
.CW all
589
targets are the same; since there is no local copy of
590
the library, all updates are performed on the
591
installed library.
592
The rule for the
593
.CW nuke
594
target is identical to that of the
595
.CW clean
596
target; unlike the
597
.CW nuke
598
target for local libraries,
599
the library is never removed.
600
.LP
601
No attempt is made to determine if individual library
602
members are up-to-date; all members of a
603
library are always updated.
604
Special targets support manipulation of a single
605
object file; the target
606
.CW objfile
607
updates file
608
.CW objfile\f(CW.$O\fP
609
in the library of the current architecture and the target
610
.CW objfile.all
611
updates
612
.CW objfile\f(CW.$O\fP
613
in the libraries of all architectures.
614
.SH
615
Overrides
616
.LP
617
The rules provided by a generic mkfile or
618
the variables used to control the evaluation
619
of those rules may be overridden in most
620
circumstances.  Overrides
621
must be specified in the product mkfile
622
after the point where the generic
623
mkfile is included; in general, variable
624
and rule overrides occupy the end of a
625
product mkfile.
626
.LP
627
The value of a variable is overridden by
628
assigning a new value to the variable.
629
Most variable overrides modify the
630
values of flags or the names of commands executed
631
in recipes.  For example, the default value of
632
.CW CFLAGS
633
is often overridden or augmented and
634
the ANSI/POSIX Computing Environment is selected by
635
setting the
636
.CW CC
637
and
638
.CW LD
639
variables to
640
.CW pcc.
641
.LP
642
Modifying rules is trickier than modifying
643
variables.  Additional constraints can be added
644
to a rule by specifying the target and
645
the new prerequisite.  For example,
646
.P1
647
%.$O:	header.h
648
.P2
649
adds file
650
.CW header.h
651
the set of prerequisites for all object files.
652
There is no mechanism for adding additional
653
commands to an existing recipe; if a
654
recipe is unsatisfactory, the rule and its recipe
655
must be completely overridden.
656
A rule is overridden only when the replacement rule
657
matches the target and prerequisite portions
658
of the original rule exactly.  The recipe
659
associated with the new rule
660
then replaces the recipe of the original rule.
661
For example,
662
.CW /sys/src/cmd/lex/mkfile
663
overrides the default
664
.CW installall
665
rule to perform the normal loop on all
666
architectures and then copy a prototype file
667
to the system library directory.
668
.P1
669
</$objtype/mkfile
670
 
671
TARG=lex
672
OFILES=lmain.$O\e
673
	y.tab.$O\e
674
	sub1.$O\e
675
	sub2.$O\e
676
	header.$O\e
677
 
678
HFILES=ldefs.h\e
679
 
680
YFILES=parser.y\e
681
 
682
BIN=/$objtype/bin
683
</sys/src/cmd/mkone
684
 
685
installall:V:
686
	for(objtype in $CPUS)
687
		mk install
688
	cp ncform /sys/lib/lex
689
.P2
690
Another way to perform the same override is to
691
add a dependency to the default
692
.CW installall
693
rule that executes an additional rule to
694
install the prototype file:
695
.P1
696
installall:V:	ncform.install
697
 
698
ncform.install:V:
699
	cp ncform /sys/lib/lex
700
.P2
701
.SH
702
Special Tricks
703
.LP
704
Two special cases
705
require extra deviousness.
706
.LP
707
In the first, a file needed to build an
708
executable is generated by a program that,
709
in turn, is built from a source file that
710
is not part of the product.  In this case,
711
the
712
executable must be built for the
713
target architecture, but the intermediate
714
executable must be built for the architecture
715
.CW mk
716
is executing on.  The intermediate executable
717
is built by recursively invoking
718
.CW  mk
719
with the appropriate target and the
720
executing architecture as the target
721
architecture.  When that
722
.CW mk
723
completes, the intermediate is
724
executed to generate the source file to
725
complete the build for the target architecture.
726
The earlier example of
727
.CW /sys/src/cmd/awk/mkfile
728
illustrates this technique.
729
.LP
730
Another awkward situation
731
occurs when a directory contains
732
source to build an executable as
733
well as source for auxiliary executables
734
that are not to be installed.  In this case
735
the
736
.CW mkmany
737
generic rules are inappropriate, because
738
all executables would be built and installed.
739
Instead, use the
740
.CW mkone
741
generic file to build the primary executable
742
and provide extra targets to
743
build the auxiliary files.  This
744
approach is also useful when the auxiliary
745
files are not executables;
746
.CW /sys/src/cmd/spell/mkfile
747
augments the default rules to build and install the
748
.CW spell
749
executable with
750
elaborate rules to generate
751
and maintain the auxiliary spelling lists.