mkfiles.html 17 KB

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