mkfiles.ms 17 KB

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