mk 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  1. .TH MK 1
  2. .SH NAME
  3. mk, membername \- maintain (make) related files
  4. .SH SYNOPSIS
  5. .B mk
  6. [
  7. .B -f
  8. .I mkfile
  9. ] ...
  10. [
  11. .I option ...
  12. ]
  13. [
  14. .I target ...
  15. ]
  16. .PP
  17. .B membername
  18. .I aggregate ...
  19. .SH DESCRIPTION
  20. .I Mk
  21. uses the dependency rules specified in
  22. .I mkfile
  23. to control the update (usually by compilation) of
  24. .I targets
  25. (usually files)
  26. from the source files upon which they depend.
  27. The
  28. .I mkfile
  29. (default
  30. .LR mkfile )
  31. contains a
  32. .I rule
  33. for each target that identifies the files and other
  34. targets upon which it depends and an
  35. .IR rc (1)
  36. script, a
  37. .IR recipe ,
  38. to update the target.
  39. The script is run if the target does not exist
  40. or if it is older than any of the files it depends on.
  41. .I Mkfile
  42. may also contain
  43. .I meta-rules
  44. that define actions for updating implicit targets.
  45. If no
  46. .I target
  47. is specified, the target of the first rule (not meta-rule) in
  48. .I mkfile
  49. is updated.
  50. .PP
  51. The environment variable
  52. .B $NPROC
  53. determines how many targets may be updated simultaneously;
  54. Plan 9 sets
  55. .B $NPROC
  56. automatically to the number of CPUs on the current machine.
  57. .PP
  58. Options are:
  59. .TP \w'\fL-d[egp]\ 'u
  60. .B -a
  61. Assume all targets to be out of date.
  62. Thus, everything is updated.
  63. .PD 0
  64. .TP
  65. .BR -d [ egp ]
  66. Produce debugging output
  67. .RB ( p
  68. is for parsing,
  69. .B g
  70. for graph building,
  71. .B e
  72. for execution).
  73. .TP
  74. .B -e
  75. Explain why each target is made.
  76. .TP
  77. .B -i
  78. Force any missing intermediate targets to be made.
  79. .TP
  80. .B -k
  81. Do as much work as possible in the face of errors.
  82. .TP
  83. .B -n
  84. Print, but do not execute, the commands
  85. needed to update the targets.
  86. .TP
  87. .B -s
  88. Make the command line arguments sequentially rather than in parallel.
  89. .TP
  90. .B -t
  91. Touch (update the modified date of) file targets, without
  92. executing any recipes.
  93. .TP
  94. .BI -w target1 , target2,...
  95. Pretend the modify time for each
  96. .I target
  97. is the current time; useful in conjunction with
  98. .B -n
  99. to learn what updates would be triggered by
  100. modifying the
  101. .IR targets .
  102. .PD
  103. .PP
  104. The
  105. .IR rc (1)
  106. script
  107. .I membername
  108. extracts member names
  109. (see `Aggregates' below)
  110. from its arguments.
  111. .SS The \fLmkfile\fP
  112. A
  113. .I mkfile
  114. consists of
  115. .I assignments
  116. (described under `Environment') and
  117. .IR rules .
  118. A rule contains
  119. .I targets
  120. and a
  121. .IR tail .
  122. A target is a literal string
  123. and is normally a file name.
  124. The tail contains zero or more
  125. .I prerequisites
  126. and an optional
  127. .IR recipe ,
  128. which is an
  129. .B rc
  130. script.
  131. Each line of the recipe must begin with white space.
  132. A rule takes the form
  133. .IP
  134. .EX
  135. target: prereq1 prereq2
  136. rc \f2recipe using\fP prereq1, prereq2 \f2to build\fP target
  137. .EE
  138. .PP
  139. When the recipe is executed,
  140. the first character on every line is elided.
  141. .PP
  142. After the colon on the target line, a rule may specify
  143. .IR attributes ,
  144. described below.
  145. .PP
  146. A
  147. .I meta-rule
  148. has a target of the form
  149. .IB A % B
  150. where
  151. .I A
  152. and
  153. .I B
  154. are (possibly empty) strings.
  155. A meta-rule acts as a rule for any potential target whose
  156. name matches
  157. .IB A % B
  158. with
  159. .B %
  160. replaced by an arbitrary string, called the
  161. .IR stem .
  162. In interpreting a meta-rule,
  163. the stem is substituted for all occurrences of
  164. .B %
  165. in the prerequisite names.
  166. In the recipe of a meta-rule, the environment variable
  167. .B $stem
  168. contains the string matched by the
  169. .BR % .
  170. For example, a meta-rule to compile a C program using
  171. .IR 2c (1)
  172. might be:
  173. .IP
  174. .EX
  175. %: %.c
  176. 2c $stem.c
  177. 2l -o $stem $stem.2
  178. .EE
  179. .PP
  180. Meta-rules may contain an ampersand
  181. .B &
  182. rather than a percent sign
  183. .BR % .
  184. A
  185. .B %
  186. matches a maximal length string of any characters;
  187. an
  188. .B &
  189. matches a maximal length string of any characters except period
  190. or slash.
  191. .PP
  192. The text of the
  193. .I mkfile
  194. is processed as follows.
  195. Lines beginning with
  196. .B <
  197. followed by a file name are replaced by the contents of the named
  198. file.
  199. Lines beginning with
  200. .B "<|"
  201. followed by a file name are replaced by the output
  202. of the execution of the named
  203. file.
  204. Blank lines and comments, which run from unquoted
  205. .B #
  206. characters to the following newline, are deleted.
  207. The character sequence backslash-newline is deleted,
  208. so long lines in
  209. .I mkfile
  210. may be folded.
  211. Non-recipe lines are processed by substituting for
  212. .BI `{ command }
  213. the output of the
  214. .I command
  215. when run by
  216. .IR rc .
  217. References to variables are replaced by the variables' values.
  218. Special characters may be quoted using single quotes
  219. .BR \&''
  220. as in
  221. .IR rc (1).
  222. .PP
  223. Assignments and rules are distinguished by
  224. the first unquoted occurrence of
  225. .B :
  226. (rule)
  227. or
  228. .B =
  229. (assignment).
  230. .PP
  231. A later rule may modify or override an existing rule under the
  232. following conditions:
  233. .TP
  234. \-
  235. If the targets of the rules exactly match and one rule
  236. contains only a prerequisite clause and no recipe, the
  237. clause is added to the prerequisites of the other rule.
  238. If either or both targets are virtual, the recipe is
  239. always executed.
  240. .TP
  241. \-
  242. If the targets of the rules match exactly and the
  243. prerequisites do not match and both rules
  244. contain recipes,
  245. .I mk
  246. reports an ``ambiguous recipe'' error.
  247. .TP
  248. \-
  249. If the target and prerequisites of both rules match exactly,
  250. the second rule overrides the first.
  251. .SS Environment
  252. Rules may make use of
  253. .B rc
  254. environment variables.
  255. A legal reference of the form
  256. .B $OBJ
  257. is expanded as in
  258. .IR rc (1).
  259. A reference of the form
  260. .BI ${name: A % B = C\fL%\fID\fL}\fR,
  261. where
  262. .I A, B, C, D
  263. are (possibly empty) strings,
  264. has the value formed by expanding
  265. .B $name
  266. and substituting
  267. .I C
  268. for
  269. .I A
  270. and
  271. .I D
  272. for
  273. .I B
  274. in each word in
  275. .B $name
  276. that matches pattern
  277. .IB A % B\f1.
  278. .PP
  279. Variables can be set by
  280. assignments of the form
  281. .I
  282. var\fL=\fR[\fIattr\fL=\fR]\fIvalue\fR
  283. .br
  284. Blanks in the
  285. .I value
  286. break it into words, as in
  287. .I rc
  288. but without the surrounding parentheses.
  289. Such variables are exported
  290. to the environment of
  291. recipes as they are executed, unless
  292. .BR U ,
  293. the only legal attribute
  294. .IR attr ,
  295. is present.
  296. The initial value of a variable is
  297. taken from (in increasing order of precedence)
  298. the default values below,
  299. .I mk's
  300. environment, the
  301. .IR mkfiles ,
  302. and any command line assignment as an argument to
  303. .IR mk .
  304. A variable assignment argument overrides the first (but not any subsequent)
  305. assignment to that variable.
  306. The variable
  307. .B MKFLAGS
  308. contains all the option arguments (arguments starting with
  309. .L -
  310. or containing
  311. .LR = )
  312. and
  313. .B MKARGS
  314. contains all the targets in the call to
  315. .IR mk .
  316. .PP
  317. It is recommended that mkfiles start with
  318. .IP
  319. .EX
  320. </$objtype/mkfile
  321. .EE
  322. .PP
  323. to set
  324. .BR CC ,
  325. .BR LD ,
  326. .BR AS ,
  327. .BR O ,
  328. .BR YACC ,
  329. and
  330. .B MK
  331. to values appropriate to the target architecture (see the examples below).
  332. .SS Execution
  333. .PP
  334. During execution,
  335. .I mk
  336. determines which targets must be updated, and in what order,
  337. to build the
  338. .I names
  339. specified on the command line.
  340. It then runs the associated recipes.
  341. .PP
  342. A target is considered up to date if it has no prerequisites or
  343. if all its prerequisites are up to date and it is newer
  344. than all its prerequisites.
  345. Once the recipe for a target has executed, the target is
  346. considered up to date.
  347. .PP
  348. The date stamp
  349. used to determine if a target is up to date is computed
  350. differently for different types of targets.
  351. If a target is
  352. .I virtual
  353. (the target of a rule with the
  354. .B V
  355. attribute),
  356. its date stamp is initially zero; when the target is
  357. updated the date stamp is set to
  358. the most recent date stamp of its prerequisites.
  359. Otherwise, if a target does not exist as a file,
  360. its date stamp is set to the most recent date stamp of its prerequisites,
  361. or zero if it has no prerequisites.
  362. Otherwise, the target is the name of a file and
  363. the target's date stamp is always that file's modification date.
  364. The date stamp is computed when the target is needed in
  365. the execution of a rule; it is not a static value.
  366. .PP
  367. Nonexistent targets that have prerequisites
  368. and are themselves prerequisites are treated specially.
  369. Such a target
  370. .I t
  371. is given the date stamp of its most recent prerequisite
  372. and if this causes all the targets which have
  373. .I t
  374. as a prerequisite to be up to date,
  375. .I t
  376. is considered up to date.
  377. Otherwise,
  378. .I t
  379. is made in the normal fashion.
  380. The
  381. .B -i
  382. flag overrides this special treatment.
  383. .PP
  384. Files may be made in any order that respects
  385. the preceding restrictions.
  386. .PP
  387. A recipe is executed by supplying the recipe as standard input to
  388. the command
  389. .B
  390. /bin/rc -e -I
  391. .br
  392. (the
  393. .B -e
  394. is omitted if the
  395. .B E
  396. attribute is set).
  397. The environment is augmented by the following variables:
  398. .TP 14
  399. .B $alltarget
  400. all the targets of this rule.
  401. .TP
  402. .B $newprereq
  403. the prerequisites that caused this rule to execute.
  404. .TP
  405. .B $newmember
  406. the prerequisites that are members of an aggregate
  407. that caused this rule to execute.
  408. When the prerequisites of a rule are members of an
  409. aggregate,
  410. .B $newprereq
  411. contains the name of the aggregate and out of date
  412. members, while
  413. .B $newmember
  414. contains only the name of the members.
  415. .TP
  416. .B $nproc
  417. the process slot for this recipe.
  418. It satisfies
  419. .RB 0≤ $nproc < $NPROC .
  420. .TP
  421. .B $pid
  422. the process id for the
  423. .I mk
  424. executing the recipe.
  425. .TP
  426. .B $prereq
  427. all the prerequisites for this rule.
  428. .TP
  429. .B $stem
  430. if this is a meta-rule,
  431. .B $stem
  432. is the string that matched
  433. .B %
  434. or
  435. .BR & .
  436. Otherwise, it is empty.
  437. For regular expression meta-rules (see below), the variables
  438. .LR stem0 ", ...,"
  439. .L stem9
  440. are set to the corresponding subexpressions.
  441. .TP
  442. .B $target
  443. the targets for this rule that need to be remade.
  444. .PP
  445. These variables are available only during the execution of a recipe,
  446. not while evaluating the
  447. .IR mkfile .
  448. .PP
  449. Unless the rule has the
  450. .B Q
  451. attribute,
  452. the recipe is printed prior to execution
  453. with recognizable environment variables expanded.
  454. Commands returning nonempty status (see
  455. .IR intro (1))
  456. cause
  457. .I mk
  458. to terminate.
  459. .PP
  460. Recipes and backquoted
  461. .B rc
  462. commands in places such as assignments
  463. execute in a copy of
  464. .I mk's
  465. environment; changes they make to
  466. environment variables are not visible from
  467. .IR mk .
  468. .PP
  469. Variable substitution in a rule is done when
  470. the rule is read; variable substitution in the recipe is done
  471. when the recipe is executed. For example:
  472. .IP
  473. .EX
  474. bar=a.c
  475. foo: $bar
  476. $CC -o foo $bar
  477. bar=b.c
  478. .EE
  479. .PP
  480. will compile
  481. .B b.c
  482. into
  483. .BR foo ,
  484. if
  485. .B a.c
  486. is newer than
  487. .BR foo .
  488. .SS Aggregates
  489. Names of the form
  490. .IR a ( b )
  491. refer to member
  492. .I b
  493. of the aggregate
  494. .IR a .
  495. Currently, the only aggregates supported are
  496. .IR ar (1)
  497. archives.
  498. .SS Attributes
  499. The colon separating the target from the prerequisites
  500. may be
  501. immediately followed by
  502. .I attributes
  503. and another colon.
  504. The attributes are:
  505. .TP
  506. .B D
  507. If the recipe exits with a non-null status, the target is deleted.
  508. .TP
  509. .B E
  510. Continue execution if the recipe draws errors.
  511. .TP
  512. .B N
  513. If there is no recipe, the target has its time updated.
  514. .TP
  515. .B n
  516. The rule is a meta-rule that cannot be a target of a virtual rule.
  517. Only files match the pattern in the target.
  518. .TP
  519. .B P
  520. The characters after the
  521. .B P
  522. until the terminating
  523. .B :
  524. are taken as a program name.
  525. It will be invoked as
  526. .B "rc -c prog 'arg1' 'arg2'"
  527. and should return a null exit status
  528. if and only if arg1 is up to date with respect to arg2.
  529. Date stamps are still propagated in the normal way.
  530. .TP
  531. .B Q
  532. The recipe is not printed prior to execution.
  533. .TP
  534. .B R
  535. The rule is a meta-rule using regular expressions.
  536. In the rule,
  537. .B %
  538. has no special meaning.
  539. The target is interpreted as a regular expression as defined in
  540. .IR regexp (6).
  541. The prerequisites may contain references
  542. to subexpressions in form
  543. .BI \e n\f1,
  544. as in the substitute command of
  545. .IR sam (1).
  546. .TP
  547. .B U
  548. The targets are considered to have been updated
  549. even if the recipe did not do so.
  550. .TP
  551. .B V
  552. The targets of this rule are marked as virtual.
  553. They are distinct from files of the same name.
  554. .PD
  555. .SH EXAMPLES
  556. A simple mkfile to compile a program:
  557. .IP
  558. .EX
  559. .ta 8n +8n +8n +8n +8n +8n +8n
  560. </$objtype/mkfile
  561. prog: a.$O b.$O c.$O
  562. $LD $LDFLAGS -o $target $prereq
  563. %.$O: %.c
  564. $CC $CFLAGS $stem.c
  565. .EE
  566. .PP
  567. Override flag settings in the mkfile:
  568. .IP
  569. .EX
  570. % mk target 'CFLAGS=-S -w'
  571. .EE
  572. .PP
  573. Maintain a library:
  574. .IP
  575. .EX
  576. libc.a(%.$O):N: %.$O
  577. libc.a: libc.a(abs.$O) libc.a(access.$O) libc.a(alarm.$O) ...
  578. ar r libc.a $newmember
  579. .EE
  580. .PP
  581. String expression variables to derive names from a master list:
  582. .IP
  583. .EX
  584. NAMES=alloc arc bquote builtins expand main match mk var word
  585. OBJ=${NAMES:%=%.$O}
  586. .EE
  587. .PP
  588. Regular expression meta-rules:
  589. .IP
  590. .EX
  591. ([^/]*)/(.*)\e.$O:R: \e1/\e2.c
  592. cd $stem1; $CC $CFLAGS $stem2.c
  593. .EE
  594. .PP
  595. A correct way to deal with
  596. .IR yacc (1)
  597. grammars.
  598. The file
  599. .B lex.c
  600. includes the file
  601. .B x.tab.h
  602. rather than
  603. .B y.tab.h
  604. in order to reflect changes in content, not just modification time.
  605. .IP
  606. .EX
  607. lex.$O: x.tab.h
  608. x.tab.h: y.tab.h
  609. cmp -s x.tab.h y.tab.h || cp y.tab.h x.tab.h
  610. y.tab.c y.tab.h: gram.y
  611. $YACC -d gram.y
  612. .EE
  613. .PP
  614. The above example could also use the
  615. .B P
  616. attribute for the
  617. .B x.tab.h
  618. rule:
  619. .IP
  620. .EX
  621. x.tab.h:Pcmp -s: y.tab.h
  622. cp y.tab.h x.tab.h
  623. .EE
  624. .SH SOURCE
  625. .B /sys/src/cmd/mk
  626. .SH SEE ALSO
  627. .IR rc (1),
  628. .IR regexp (6)
  629. .br
  630. A. Hume,
  631. ``Mk: a Successor to Make''.
  632. .br
  633. Bob Flandrena,
  634. ``Plan 9 Mkfiles''.
  635. .SH BUGS
  636. Identical recipes for regular expression meta-rules only have one target.
  637. .br
  638. Seemingly appropriate input like
  639. .B CFLAGS=-DHZ=60
  640. is parsed as an erroneous attribute; correct it by inserting
  641. a space after the first
  642. .LR = .
  643. .br
  644. The recipes printed by
  645. .I mk
  646. before being passed to
  647. .I rc
  648. for execution are sometimes erroneously expanded
  649. for printing. Don't trust what's printed; rely
  650. on what
  651. .I rc
  652. does.