README.internals 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. $OpenBSD: README.internals,v 1.14 2016/04/26 17:35:35 naddy Exp $
  2. Copyright (C) 2011-2012 Marc Espie <espie@openbsd.org>
  3. Permission to use, copy, modify, and distribute this software for any
  4. purpose with or without fee is hereby granted, provided that the above
  5. copyright notice and this permission notice appear in all copies.
  6. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  7. WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  8. MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  9. ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  10. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  11. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  12. OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  13. This file is *not* user documentation of the *mk files. The full user
  14. documentation is available as manpages such as bsd.port.mk(5)
  15. There are comments in bsd.port.mk but this file is already too long,
  16. so I finally decided to put the design notes in a separate file.
  17. Most of this is not for the faint of heart.
  18. "Imagination could conceive almost anything in connexion with this place."
  19. (H.P. Lovecraft, At the Mountains of Madness)
  20. Some design principles
  21. ----------------------
  22. - all variables and targets prefixed with _ are for internal use.
  23. It may be that some other tools reuse them, but that's generally following
  24. the same guidelines: no user serviceable parts inside (and you should always
  25. ask ME about it, since I'm liable to change these with very little notice).
  26. This is an attempt to avoid exposing too many knobs, and a clear distinction
  27. between "supported" stuff, and "you're on your own, if you abuse this, bad
  28. things will happen".
  29. - most variables will always be defined, even with an empty value. This avoids
  30. stupid .if defined(TESTS).
  31. - bsd.port.mk has a strict separation between variable definitions (at top)
  32. and target definitions (right after the
  33. ###
  34. ### end of variable setup. Only targets now
  35. ###
  36. comment). This is because of make's "lazy" way to evaluate things:
  37. constructs in shell-lines get evaluated very very late. But all
  38. targets (such as ${_WRKDIR_COOKIE}) get evaluated when they're
  39. encountered. There was some significant effort in the early days
  40. when I took bsd.port.mk over to achieve that separation. Be very
  41. careful with Makefile.inc and modules, since those get included at
  42. a very specific location in the Makefile. They should mostly define
  43. variables. Makefile.inc is included very early so that it can
  44. override about anything it should be able to (but then, nothing is
  45. already defined except for read-only variables / global user-tweakable
  46. defaults). Modules is included a little bit later...
  47. - a lot of the code is done through shell fragments. All of these are
  48. internal and prefixed with _. This is done for speed and compactness (forking
  49. an external shell would be very slow, and a total nightmare with respect
  50. to parameter passing). These often communicate through internal shell
  51. variables with well-defined names.
  52. - the infrastructure tries very hard to have real files (as far as possible)
  53. attached to most targets: "make package" points to the actual pkgfile(s)
  54. being generated. There's a whole section of the makefile devoted to
  55. targets generated from the distfiles devoted to fetch and fetch-all
  56. (see the section around _EVERYTHING for variable definition, and the
  57. # Separate target for each file fetch-all will retrieve
  58. for the actual targets). Apart from that, once a port starts building,
  59. targets "without" an actual file will generate a cookie under WRKDIR or
  60. some subdir of it (see all the _COOKIE* definitions). Of particular interest
  61. are dependencies, where the cookie file is generated depending on the
  62. name of the dependency: that's the infamous
  63. .for _i in ${_DEPLIST}
  64. ${WRKDIR}/.dep-${_i:C,>=,ge-,g:C,<=,le-,g:C,<,lt-,g:C,>,gt-,g:C,\*,ANY,g:C,[|:/=],-,g}: ${_WRKDIR_COOKIE}
  65. loop. The reason for that complexity is that this avoids rechecking
  66. dependencies while working on a port, since only the new stuff will need
  67. evaluation...
  68. In general, make works really bad when you write
  69. phony-target1: phony-target2
  70. @do_some_shit
  71. as phony targets are always out-of-date, and thus you will always do_some_shit.
  72. - a lot of computations are very expensive, so there are shortcuts all over
  73. the place whenever a variable is empty, especially for dependency computation.
  74. Introspection
  75. -------------
  76. There's a lot of stuff in the infrastructure which is there only for
  77. introspection purposes: show, show-all, print-plist-*, dump-vars,
  78. *-dir-depends, print-update-signature.
  79. They allow external tools to interrogate the ports tree and get most
  80. information from it. dpb and sqlports rely very heavily on it.
  81. Significant work has been done to achieve better MI. The MULTI_PACKAGES
  82. code now assumes you will define MULTI_PACKAGES *independently of the
  83. architecture*, annotate subpackage with ONLY_FOR_ARCHS-sub if appropriate,
  84. then .include <bsd.port.arch.mk> (which was split off the main file
  85. specifically for that purpose), and rely on BUILD_PACKAGES for building.
  86. Shell tricks and constructs
  87. ---------------------------
  88. - IFS is your friend: echo "a:b:c"| while IFS=: read a b c
  89. will split things along any character without requiring external commands
  90. such as cut.
  91. - don't run test for checking variable values. Rather:
  92. case "$$subdir" in *,*) ... ;; *) ... ;; esac
  93. is entirely internal
  94. - the shell has booleans. Set variables to true/false to achieve boolean
  95. conditions. Note that those are usually built-ins, and hence even faster
  96. than you would think.
  97. - () forks a subshell. If you need to syntactally keep a list of commands
  98. together, "{ cmd1; cmd2; }" is the way to do it.
  99. - use trap to clean up at the end of a command. That's mostly used for
  100. caching stuff (depfile and cache), but also for the locking mechanism.
  101. - exec is often used for tail recursion: it replaces the shell with the
  102. executed command. BUT beware of pending traps, as you will lose them.
  103. - all of make commands are executed under -e. Thus, it's deadly to fail
  104. without a test around it. In the end, you might end up with:
  105. if ( eval $$toset exec ${MAKE} subupdate ); then
  106. this forks a subshell to avoid the unpleasantness of -e, then execs the
  107. resulting command to avoid piling up an extra process.
  108. Make vs. Shell
  109. --------------
  110. There are at least *4* kind of variables in those files:
  111. - internal shell variables
  112. - environment variables
  113. - make variables set on the command line
  114. - make variables set within a makefile
  115. Note that make variables set on the command line ARE set in stone: it's very
  116. difficult to change them from within the Makefile, and they will override
  117. mostly anything you can do. They also appear in the environment.
  118. Thus, a lot of make thingies, such as FLAVOR and SUBPACKAGE must be set in
  119. the environment because make will run into subdirs and requires them to
  120. be changeable.
  121. Also, note that stuff set within Makefiles is not exported to the environment,
  122. you have to be explicit and set them when you run a command.
  123. When things get ambiguous, This document uses $$var for a shell variable,
  124. ${VAR} for a variable set in make, and VAR for an environment variables.
  125. pkgpath.mk
  126. ----------
  127. Named that way because it mostly deals with pkgpath parsing, but in reality,
  128. it's mostly a lift-up from the common parts between bsd.port.mk and
  129. bsd.port.subdir.mk (no reason for a name change so late in the game)
  130. fragments and common shell variables
  131. ------------------------------------
  132. _pflavor_fragment (in pkgpath.mk)
  133. takes $$subdir as a pkgpath parameter.
  134. will parse it into a $$dir and $$toset variables, perform a cd $$dir
  135. so that eval $$toset ${MAKE} gets you into that
  136. dependency (this will set PKGPATH, FLAVOR, SUBPACKAGE accordingly)
  137. depending on $$_fullpath (true/false), will consider the absence
  138. of flavor to be an empty flavor (e.g. $$toset contains FLAVOR='' )
  139. or to be the default flavor (e.g. unset FLAVOR)
  140. takes ${PORTSDIR_PATH} into account
  141. succeeds if the dependency is correct and the directory is found.
  142. Otherwise will emit an error message that ends with $$extra_msg.
  143. _flavor_fragment (in pkgpath.mk)
  144. calls ${_pflavor_fragment} after imposing $$_fullpath=false. e.g., for
  145. actual dependencies.
  146. _depfile_fragment (in pkgpath.mk)
  147. sets _DEPENDS_FILE to a temporary file in the environment if it's
  148. not already set. Set a trap to remove it on end.
  149. _DEPENDS_FILE is used by the *dir-depends and show-run-depends targets
  150. to not go thru the same directory twice. Beware of setting it
  151. "too globally", since it prevents directory walking from happening
  152. twice.
  153. _cache_fragment (in pkgpath.mk)
  154. sets _DEPENDS_CACHE to a temporary directory in the environment
  155. if it's not already set. Set a trap to remove it on end.
  156. _DEPENDS_CACHE is used to store library lists in _libs2cache.
  157. pkg_create(1) also knows about it and use it to store plists.
  158. the ${SUDO} part in the trap is because some of the caching happens
  159. during ${SUDO} pkg_create.
  160. XXX also sets PKGPATH, as it reduces drastically the number of
  161. PKGPATH=... needed in bsd.port.mk
  162. _read_spec/_parse_spec (in bsd.port.mk)
  163. normally used in a pattern like:
  164. ${_emit_run_depends}|while ${_read_spec}; do
  165. ${_parse_spec};
  166. ...
  167. done
  168. takes specs, one per line, sets $$pkg $$subdir $$target,
  169. and calls ${_flavor_fragment} to cd to the correct dir
  170. and set $$toset. Note that this takes "processed" specs:
  171. transformation of pkgpath>=0.0 -> STEM->=0.0:pkgpath must have
  172. already happened upfront (which happens in place for all
  173. *DEPENDS variables).
  174. This sets $$d to the spec being processed, which is probably a
  175. bad variable name...
  176. _emit_run_depends/_emit_lib_depends (in bsd.port.mk)
  177. writes out lists of specs suitable for ${_parse_spec}/${_read_spec}.
  178. _compute_default (in bsd.port.mk)
  179. comes after ${_pflavor_fragment}, and obtains information from
  180. the dependent port: sets $$default to the default pkgname,
  181. $$pkgspec to the default pkgspec (see PKGSPEC), and $$pkgpath
  182. to the actual pkgpath, when default flavors and subpackages are
  183. taken into account (hence suitable for FULLPATH=Yes/$$_fullpath=true
  184. Used alone when we only need $$default, as ${_complete_pkgspec} is
  185. slightly more expensive.
  186. _complete_pkgspec (in bsd.port.mk)
  187. comes after ${_pflavor_fragment}, obtains information from
  188. the dependent port through ${_compute_default}, then make sure
  189. $$pkg has the correct value.
  190. _libs2cache (in bsd.port.mk)
  191. requires properly set dependency information obtained through
  192. ${_flavor_fragment} (often through ${_parse_spec}).
  193. assumes ${_depcache_fragment} has been called, either directly,
  194. or from an upper level target.
  195. If not already available, secure the library list from the
  196. dependency and store it in a file under _DEPENDS_CACHE.
  197. Sets $$cached_libs to the file name.
  198. Locking
  199. -------
  200. Done thru _DO_LOCK and _SIMPLE_LOCK mostly. Most user-visible targets
  201. redirect through
  202. ${_DO_LOCK}; make _internal-$@
  203. as soon as locking is used (which is the default these days).
  204. The locking mechanism carefully maintains the _LOCK_HELD environment variable
  205. so that dependencies can relock themselves (yeah, it looks strange, but this
  206. happens !).
  207. _SIMPLE_LOCK only exists to provide separate locking targets for fetch: it
  208. assumes $$lock has been set to an appropriate lock file name.
  209. Internal targets are:
  210. _internal-clean _internal-package-only _internal-run-depends
  211. _internal-runlib-depends _internal-runwantlib-depends _internal-package
  212. _internal-build-depends _internal-buildlib-depends
  213. _internal-buildwantlib-depends _internal-test-depends
  214. _internal-prepare _internal-depends _internal-fetch-all
  215. _internal-fetch _internal-all _internal-build _internal-checksum
  216. _internal-configure _internal-deinstall _internal-extract _internal-fake
  217. _internal-patch _internal-plist _internal-test _internal-subpackage
  218. _internal-subupdate _internal-uninstall _internal-update
  219. _internal-update-or-install _internal-update-or-install-all
  220. _internal-update-plist _internal-distpatch
  221. (yeah, that's a lot).
  222. So, if you type "make", it will do
  223. make build -> lock && make _internal-all
  224. then make _internal-all will normally run thru
  225. _internal-fetch _internal-extract _internal-patch _internal-build
  226. without ever releasing the lock, thus preventing anything else to sneak
  227. in and break the build.
  228. The difference between _internal-package and _internal-package-only is only
  229. due to BULK=Yes, which has to happen exactly once after a user-level target
  230. that involves packaging is finished.
  231. Misc
  232. ----
  233. - Modules inclusion is done through a separate modules.port.mk to handle
  234. recursion: modules may trigger the inclusion of other modules, thus
  235. modules.port.mk will re-include itself until the whole list is done.
  236. - MULTI_PACKAGES and SUBPACKAGE always get set to a non-empty value:
  237. if MULTI_PACKAGES is not set, it ends up as MULTI_PACKAGES=- and
  238. SUBPACKAGE=-. This does make loops over all subpackages simpler, e.g.,
  239. .for _s in ${MULTI_PACKAGES} since make doesn't do anything if the list
  240. is empty...
  241. As a result, most subpackage-dependent variables end up being used as their
  242. "true" VAR- form in the case of a port without multi-packages.
  243. Exception: FULLPKGNAME needs some specific code for the !multi-packages case.
  244. There's also a special case for the PKGDIR stuff, which isn't suffixed with '-'
  245. in the non multi-package case.
  246. Also, dump-vars distinguishes the !multi case.
  247. Note that there are a few checks for empty SUBPACKAGE, these allow the whole
  248. bsd.port.mk file to parse, so that we end up with an actual user error
  249. message instead of some weird internal error.
  250. Dependency variables
  251. --------------------
  252. Old legacy dependency styles are gone, so the compat code went away.
  253. The code has to deal with BUILD_DEPENDS, LIB_DEPENDS, RUN_DEPENDS,
  254. TEST_DEPENDS, WANTLIB and RUN_DEPENDS-* LIB_DEPENDS-*, WANTLIB-*
  255. There is obviously completely different treatment for WANTLIB* and
  256. *DEPENDS*. Also, BUILD* stuff gets to inherit from LIB_DEPENDS* stripped
  257. of inter-dependencies, and RUN-s stuff must have LIB_DEPENDS* from
  258. inter-dependencies.
  259. First, the framework substitutes in place constructs like
  260. pkgpath>=version into STEM->=version:pkgpath
  261. Then it accumulates dependency into build-time dependencies:
  262. - collect LIB_DEPENDS and LIB_DEPENDS-* as _BUILDLIB_DEPENDS
  263. - collect WANTLIB and WANTLIB-* as _BUILDWANTLIB
  264. (strip any interdependencies)
  265. Symetrically:
  266. - define _LIB4-* as the interdependencies in LIB_DEPENDS-*
  267. - define _LIB4 as the full list of all _LIB4-*
  268. - define _BUILD_DEPLIST, _RUN_DEPLIST, _TEST_DEPLIST, _BUILDLIB_DEPLIST,
  269. _RUNLIB_DEPLIST as the dependency lists relevant to the SUBPACKAGE being
  270. built (not set if NO_DEPENDS is Yes !)
  271. This accumulates as _DEPLIST, which is the full list of all specs in every
  272. dependency.
  273. *2 and *3 have the :configure/:patch/:build modifiers stripped:
  274. _BUILD_DEP2: BUILD_DEPENDS + _BUILDLIB_DEPENDS
  275. _BUILD_DEP3: BUILD_DEPENDS
  276. _BUILD_DEP3-*: _BUILD_DEP3
  277. _RUN_DEP2: RUN_DEPENDS (+LIB_DEPENDS-s if shared)
  278. _RUN_DEP3: RUN_DEPEND
  279. _RUN_DEP3-*: RUN_DEPENDS-* (+LIB_DEPENDS-* if shared)
  280. _LIB_DEP3: (LIB_DEPENDS-s if shared)
  281. _LIB_DEP3-*: (LIB_DEPENDS-* if shared)
  282. _TEST_DEP2: TEST_DEPENDS
  283. _TEST_DEP3: TEST_DEPENDS
  284. _DEPBUILDLIBS: _BUILDWANTLIB
  285. _DEPRUNLIBS: WANTLIB-s
  286. _BUILD_DEP is the _BUILD_DEP2 paths, without the specs
  287. _RUN_DEP is the _RUN_DEP2 paths, without the specs
  288. _TEST_DEP is the _TEST_DEP2 paths, without the specs
  289. e.g., *2 is "all the shit relevant to the port at hand", *3 is
  290. "only the shit directly relevant to the port with subpackages,
  291. or everything pertinent to THAT given subpackage".
  292. This gets converted into cookies relevant to each dependency
  293. _DEPBUILDLIBS -> _DEPBUILDLIBSPECS_COOKIES as .spec-$i
  294. _DEPRUNLIBS -> _DEPRUNLIBSPECS_COOKIES as .spec-$i
  295. _DEPBUILDWANTLIB_COOKIE as .buildwantlibs
  296. _DEPRUNWANTLIB_COOKIE as .runwantlibs-s
  297. Then there are rules for each dependency: a loop over _DEPLIST
  298. for each spec, and code to build both _DEP*WANTLIB_COOKIEs
  299. There's also some dependency in handling in
  300. *wantlib-args, lib-depends-args and run-depends-args.
  301. _DEPENDS_TARGET: used to be DEPENDS_TARGET, and visible, but not documented
  302. for years, and it can be set internally. Ports normally install their
  303. dependencies, with the exception of "make reinstall", and "make update".
  304. _FETCH_MAKEFILE: where to put the output of "fetch-makefile", by default
  305. stdout, but overridden from the main Makefile.
  306. _SHSCRIPT: prepend to a shell script in the infrastructure, e.g.,
  307. ${_SHSCRIPT}/update-patches
  308. _PERLSCRIPT: likewise for perl scripts.
  309. _ALL_VARIABLES _ALL_VARIABLES_INDEXED _ALL_VARIABLES_PER_ARCH:
  310. stuff to dump in dump-vars. First is "simple" variable, second one
  311. will depend on the subpackage, and for last one, must iterate
  312. over arches to get all relevant information.
  313. _BAD_LICENSING:
  314. note that the PERMIT_* variables are defined for subpackages as soon as
  315. the main variables are defined, so it's enough to check the normal variables.
  316. _lt_libs:
  317. derived from SHARED_LIBRARIES explicitly for libtool
  318. _FLAVOR_EXT2: FLAVOR_EXT with pseudo-flavors included
  319. _PKG_ARGS: list of stuff to append to base PKG_ARGS-sub based on a lot of
  320. internal choices.
  321. _README_DIR: location for the pkg-readmes, no user serviceable parts inside.
  322. _MASTER: dependencies may be confusing, so set _MASTER to know where we come
  323. from when patching/configuring only.
  324. _DEPENDENCY_STACK: pkg_add -a to avoid manually installed packages.
  325. _CHECK_DEPENDS: save all depends prior to tweaking them, just in case.
  326. _PERL_FIX_SHAR make visible ?
  327. TODO document:
  328. MAKE_JOBS
  329. PARALLEL_BUILD
  330. PARALLEL_INSTALL
  331. XAUTHORITY for interactive tests
  332. fishy: FLAVORS tests ?
  333. fishy: TEST_STATUS_IGNORE
  334. fishy:
  335. GZIP
  336. GZIP_CMD
  337. M4
  338. STRIP
  339. uninstall ? deinstall
  340. Todo: kill PORT_LD_LIBRARY_PATH
  341. Document: all recursive targets