recipe-style-guide.rst 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. .. SPDX-License-Identifier: CC-BY-SA-2.0-UK
  2. Recipe Style Guide
  3. ******************
  4. Recipe Naming Conventions
  5. =========================
  6. In general, most recipes should follow the naming convention
  7. ``recipes-category/recipename/recipename_version.bb``. Recipes for related
  8. projects may share the same recipe directory. ``recipename`` and ``category``
  9. may contain hyphens, but hyphens are not allowed in ``version``.
  10. If the recipe is tracking a Git revision that does not correspond to a released
  11. version of the software, ``version`` may be ``git`` (e.g. ``recipename_git.bb``)
  12. and the recipe would set :term:`PV`.
  13. Version Policy
  14. ==============
  15. Our versions follow the form ``<epoch>:<version>-<revision>``
  16. or in BitBake variable terms ${:term:`PE`}:${:term:`PV`}-${:term:`PR`}. We
  17. generally follow the `Debian <https://www.debian.org/doc/debian-policy/ch-controlfields.html#version>`__
  18. version policy which defines these terms.
  19. In most cases the version :term:`PV` will be set automatically from the recipe
  20. file name. It is recommended to use released versions of software as these are
  21. revisions that upstream are expecting people to use.
  22. Recipe versions should always compare and sort correctly so that upgrades work
  23. as expected. With conventional versions such as ``1.4`` upgrading ``to 1.5``
  24. this happens naturally, but some versions don't sort. For example,
  25. ``1.5 Release Candidate 2`` could be written as ``1.5rc2`` but this sorts after
  26. ``1.5``, so upgrades from feeds won't happen correctly.
  27. Instead the tilde (``~``) operator can be used, which sorts before the empty
  28. string so ``1.5~rc2`` comes before ``1.5``. There is a historical syntax which
  29. may be found where :term:`PV` is set as a combination of the prior version
  30. ``+`` the pre-release version, for example ``PV=1.4+1.5rc2``. This is a valid
  31. syntax but the tilde form is preferred.
  32. For version comparisons, the ``opkg-compare-versions`` program from
  33. ``opkg-utils`` can be useful when attempting to determine how two version
  34. numbers compare to each other. Our definitive version comparison algorithm is
  35. the one within bitbake which aims to match those of the package managers and
  36. Debian policy closely.
  37. When a recipe references a git revision that does not correspond to a released
  38. version of software (e.g. is not a tagged version), the :term:`PV` variable
  39. should include the sign ``+``, so :term:`bitbake` automatically includes package
  40. version information during the packaging phase::
  41. PV = "<version>+git"
  42. In this case, ``<version>`` should be the most recently released version of the
  43. software from the current source revision (``git describe`` can be useful for
  44. determining this). Whilst not recommended for published layers, this format is
  45. also useful when using :term:`AUTOREV` to set the recipe to increment source
  46. control revisions automatically, which can be useful during local development.
  47. Version Number Changes
  48. ======================
  49. The :term:`PR` variable is used to indicate different revisions of a recipe
  50. that reference the same upstream source version. It can be used to force a
  51. new version of a recipe to be installed onto a device from a package feed.
  52. These once had to be set manually but in most cases these can now be set and
  53. incremented automatically by a PR Server connected with a package feed.
  54. When :term:`PV` increases, any existing :term:`PR` value can and should be
  55. removed.
  56. If :term:`PV` changes in such a way that it does not increase with respect to
  57. the previous value, you need to increase :term:`PE` to ensure package managers
  58. will upgrade it correctly. If unset you should set :term:`PE` to "1" since
  59. the default of empty is easily confused with "0" depending on the package
  60. manager. :term:`PE` can only have an integer value.
  61. Recipe formatting
  62. =================
  63. Variable Formatting
  64. -------------------
  65. - Variable assignment should a space around each side of the operator, e.g.
  66. ``FOO = "bar"``, not ``FOO="bar"``.
  67. - Double quotes should be used on the right-hand side of the assignment,
  68. e.g. ``FOO = "bar"`` not ``FOO = 'bar'``
  69. - Spaces should be used for indenting variables, with 4 spaces per tab
  70. - Long variables should be split over multiple lines when possible by using
  71. the continuation character (``\``)
  72. - When splitting a long variable over multiple lines, all continuation lines
  73. should be indented (with spaces) to align with the start of the quote on the
  74. first line::
  75. FOO = "this line is \
  76. long \
  77. "
  78. Instead of::
  79. FOO = "this line is \
  80. long \
  81. "
  82. Python Function formatting
  83. --------------------------
  84. - Spaces must be used for indenting Python code, with 4 spaces per tab
  85. Shell Function formatting
  86. -------------------------
  87. - The formatting of shell functions should be consistent within layers.
  88. Some use tabs, some use spaces.
  89. Recipe metadata
  90. ===============
  91. Required Variables
  92. ------------------
  93. The following variables should be included in all recipes:
  94. - :term:`SUMMARY`: a one line description of the upstream project
  95. - :term:`DESCRIPTION`: an extended description of the upstream project,
  96. possibly with multiple lines. If no reasonable description can be written,
  97. this may be omitted as it defaults to :term:`SUMMARY`.
  98. - :term:`HOMEPAGE`: the URL to the upstream projects homepage.
  99. - :term:`BUGTRACKER`: the URL upstream projects bug tracking website,
  100. if applicable.
  101. Recipe Ordering
  102. ---------------
  103. When a variable is defined in recipes and classes, variables should follow the
  104. general order when possible:
  105. - :term:`SUMMARY`
  106. - :term:`DESCRIPTION`
  107. - :term:`HOMEPAGE`
  108. - :term:`BUGTRACKER`
  109. - :term:`SECTION`
  110. - :term:`LICENSE`
  111. - :term:`LIC_FILES_CHKSUM`
  112. - :term:`DEPENDS`
  113. - :term:`PROVIDES`
  114. - :term:`PV`
  115. - :term:`SRC_URI`
  116. - :term:`SRCREV`
  117. - :term:`S`
  118. - ``inherit ...``
  119. - :term:`PACKAGECONFIG`
  120. - Build class specific variables such as ``EXTRA_QMAKEVARS_POST`` and :term:`EXTRA_OECONF`
  121. - Tasks such as :ref:`ref-tasks-configure`
  122. - :term:`PACKAGE_ARCH`
  123. - :term:`PACKAGES`
  124. - :term:`FILES`
  125. - :term:`RDEPENDS`
  126. - :term:`RRECOMMENDS`
  127. - :term:`RSUGGESTS`
  128. - :term:`RPROVIDES`
  129. - :term:`RCONFLICTS`
  130. - :term:`BBCLASSEXTEND`
  131. There are some cases where ordering is important and these cases would override
  132. this default order. Examples include:
  133. - :term:`PACKAGE_ARCH` needing to be set before ``inherit packagegroup``
  134. Tasks should be ordered based on the order they generally execute. For commonly
  135. used tasks this would be:
  136. - :ref:`ref-tasks-fetch`
  137. - :ref:`ref-tasks-unpack`
  138. - :ref:`ref-tasks-patch`
  139. - :ref:`ref-tasks-prepare_recipe_sysroot`
  140. - :ref:`ref-tasks-configure`
  141. - :ref:`ref-tasks-compile`
  142. - :ref:`ref-tasks-install`
  143. - :ref:`ref-tasks-populate_sysroot`
  144. - :ref:`ref-tasks-package`
  145. Custom tasks should be sorted similarly.
  146. Package specific variables are typically grouped together, e.g.::
  147. RDEPENDS:${PN} = “foo”
  148. RDEPENDS:${PN}-libs = “bar”
  149. RRECOMMENDS:${PN} = “one”
  150. RRECOMMENDS:${PN}-libs = “two”
  151. Recipe License Fields
  152. ---------------------
  153. Recipes need to define both the :term:`LICENSE` and
  154. :term:`LIC_FILES_CHKSUM` variables:
  155. - :term:`LICENSE`: This variable specifies the license for the software.
  156. If you do not know the license under which the software you are
  157. building is distributed, you should go to the source code and look
  158. for that information. Typical files containing this information
  159. include ``COPYING``, :term:`LICENSE`, and ``README`` files. You could
  160. also find the information near the top of a source file. For example,
  161. given a piece of software licensed under the GNU General Public
  162. License version 2, you would set :term:`LICENSE` as follows::
  163. LICENSE = "GPL-2.0-only"
  164. The licenses you specify within :term:`LICENSE` can have any name as long
  165. as you do not use spaces, since spaces are used as separators between
  166. license names. For standard licenses, use the names of the files in
  167. ``meta/files/common-licenses/`` or the :term:`SPDXLICENSEMAP` flag names
  168. defined in ``meta/conf/licenses.conf``.
  169. - :term:`LIC_FILES_CHKSUM`: The OpenEmbedded build system uses this
  170. variable to make sure the license text has not changed. If it has,
  171. the build produces an error and it affords you the chance to figure
  172. it out and correct the problem.
  173. You need to specify all applicable licensing files for the software.
  174. At the end of the configuration step, the build process will compare
  175. the checksums of the files to be sure the text has not changed. Any
  176. differences result in an error with the message containing the
  177. current checksum. For more explanation and examples of how to set the
  178. :term:`LIC_FILES_CHKSUM` variable, see the
  179. ":ref:`dev-manual/licenses:tracking license changes`" section.
  180. To determine the correct checksum string, you can list the
  181. appropriate files in the :term:`LIC_FILES_CHKSUM` variable with incorrect
  182. md5 strings, attempt to build the software, and then note the
  183. resulting error messages that will report the correct md5 strings.
  184. See the ":ref:`dev-manual/new-recipe:fetching code`" section for
  185. additional information.
  186. Here is an example that assumes the software has a ``COPYING`` file::
  187. LIC_FILES_CHKSUM = "file://COPYING;md5=xxx"
  188. When you try to build the
  189. software, the build system will produce an error and give you the
  190. correct string that you can substitute into the recipe file for a
  191. subsequent build.
  192. License Updates
  193. ~~~~~~~~~~~~~~~
  194. When you change the :term:`LICENSE` or :term:`LIC_FILES_CHKSUM` in the recipe
  195. you need to briefly explain the reason for the change via a ``License-Update:``
  196. tag. Often it's quite trivial, such as::
  197. License-Update: copyright years refreshed
  198. Less often, the actual licensing terms themselves will have changed. If so, do
  199. try to link to upstream making/justifying that decision.
  200. Tips and Guidelines for Writing Recipes
  201. ---------------------------------------
  202. - Use :term:`BBCLASSEXTEND` instead of creating separate recipes such as ``-native``
  203. and ``-nativesdk`` ones, whenever possible. This avoids having to maintain multiple
  204. recipe files at the same time.
  205. - Recipes should have tasks which are idempotent, i.e. that executing a given task
  206. multiple times shouldn't change the end result. The build environment is built upon
  207. this assumption and breaking it can cause obscure build failures.
  208. - For idempotence when modifying files in tasks, it is usually best to:
  209. - copy a file ``X`` to ``X.orig`` (only if it doesn't exist already)
  210. - then, copy ``X.orig`` back to ``X``,
  211. - and, finally, modify ``X``.
  212. This ensures if rerun the task always has the same end result and the
  213. original file can be preserved to reuse. It also guards against an
  214. interrupted build corrupting the file.
  215. Patch Upstream Status
  216. =====================
  217. In order to keep track of patches applied by recipes and ultimately reduce the
  218. number of patches that need maintaining, the OpenEmbedded build system
  219. requires information about the upstream status of each patch.
  220. In its description, each patch should provide detailed information about the
  221. bug that it addresses, such as the URL in a bug tracking system and links
  222. to relevant mailing list archives.
  223. Then, you should also add an ``Upstream-Status:`` tag containing one of the
  224. following status strings:
  225. ``Pending``
  226. No determination has been made yet, or patch has not yet been submitted to
  227. upstream.
  228. Keep in mind that every patch submitted upstream reduces the maintainance
  229. burden in OpenEmbedded and Yocto Project in the long run, so this patch
  230. status should only be used in exceptional cases if there are genuine
  231. obstacles to submitting a patch upstream; the reason for that should be
  232. included in the patch.
  233. ``Submitted [where]``
  234. Submitted to upstream, waiting for approval. Optionally include where
  235. it was submitted, such as the author, mailing list, etc.
  236. ``Backport [version]``
  237. Accepted upstream and included in the next release, or backported from newer
  238. upstream version, because we are at a fixed version.
  239. Include upstream version info (e.g. commit ID or next expected version).
  240. ``Denied``
  241. Not accepted by upstream, include reason in patch.
  242. ``Inactive-Upstream [lastcommit: when (and/or) lastrelease: when]``
  243. The upstream is no longer available. This typically means a defunct project
  244. where no activity has happened for a long time --- measured in years. To make
  245. that judgement, it is recommended to look at not only when the last release
  246. happened, but also when the last commit happened, and whether newly made bug
  247. reports and merge requests since that time receive no reaction. It is also
  248. recommended to add to the patch description any relevant links where the
  249. inactivity can be clearly seen.
  250. ``Inappropriate [reason]``
  251. The patch is not appropriate for upstream, include a brief reason on the
  252. same line enclosed with ``[]``. In the past, there were several different
  253. reasons not to submit patches upstream, but we have to consider that every
  254. non-upstreamed patch means a maintainance burden for recipe maintainers.
  255. Currently, the only reasons to mark patches as inappropriate for upstream
  256. submission are:
  257. - ``oe specific``: the issue is specific to how OpenEmbedded performs builds
  258. or sets things up at runtime, and can be resolved only with a patch that
  259. is not however relevant or appropriate for general upstream submission.
  260. - ``upstream ticket <link>``: the issue is not specific to Open-Embedded
  261. and should be fixed upstream, but the patch in its current form is not
  262. suitable for merging upstream, and the author lacks sufficient expertise
  263. to develop a proper patch. Instead the issue is handled via a bug report
  264. (include link).
  265. Of course, if another person later takes care of submitting this patch upstream,
  266. the status should be changed to ``Submitted [where]``, and an additional
  267. ``Signed-off-by:`` line should be added to the patch by the person claiming
  268. responsibility for upstreaming.
  269. Examples
  270. --------
  271. Here's an example of a patch that has been submitted upstream::
  272. rpm: Adjusted the foo setting in bar
  273. [RPM Ticket #65] -- http://rpm5.org/cvs/tktview?tn=65,5
  274. The foo setting in bar was decreased from X to X-50% in order to
  275. ensure we don't exhaust all system memory with foobar threads.
  276. Upstream-Status: Submitted [rpm5-devel@rpm5.org]
  277. Signed-off-by: Joe Developer <joe.developer@example.com>
  278. A future update can change the value to ``Backport`` or ``Denied`` as
  279. appropriate.
  280. Another example of a patch that is specific to OpenEmbedded::
  281. Do not treat warnings as errors
  282. There are additional warnings found with musl which are
  283. treated as errors and fails the build, we have more combinations
  284. than upstream supports to handle.
  285. Upstream-Status: Inappropriate [oe specific]
  286. Here's a patch that has been backported from an upstream commit::
  287. include missing sys/file.h for LOCK_EX
  288. Upstream-Status: Backport [https://github.com/systemd/systemd/commit/ac8db36cbc26694ee94beecc8dca208ec4b5fd45]
  289. CVE patches
  290. ===========
  291. In order to have a better control of vulnerabilities, patches that fix CVEs must
  292. contain a ``CVE:`` tag. This tag list all CVEs fixed by the patch. If more than
  293. one CVE is fixed, separate them using spaces.
  294. CVE Examples
  295. ------------
  296. This should be the header of patch that fixes :cve_nist:`2015-8370` in GRUB2::
  297. grub2: Fix CVE-2015-8370
  298. [No upstream tracking] -- https://bugzilla.redhat.com/show_bug.cgi?id=1286966
  299. Back to 28; Grub2 Authentication
  300. Two functions suffer from integer underflow fault; the grub_username_get() and grub_password_get()located in
  301. grub-core/normal/auth.c and lib/crypto.c respectively. This can be exploited to obtain a Grub rescue shell.
  302. Upstream-Status: Backport [http://git.savannah.gnu.org/cgit/grub.git/commit/?id=451d80e52d851432e109771bb8febafca7a5f1f2]
  303. CVE: CVE-2015-8370
  304. Signed-off-by: Joe Developer <joe.developer@example.com>
  305. Patch format
  306. ============
  307. By default, patches created with ``git format-patch`` have a `Git` version signature at the end.
  308. To avoid having a `Git` signature at the end of generated or updated patches,
  309. you can use `Git` configuration settings::
  310. git config --global format.signature ""
  311. .. note::
  312. Patches generated or updated by ``devtool`` are created with no signature.