recipe-style-guide.rst 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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/package/packagename_version.bb``. Recipes for related
  8. projects may share the same package directory. ``packagename``, ``category``,
  9. and ``package`` 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. ``packagename_git.bb``)
  12. Version Policy
  13. ==============
  14. Our versions follow the form ``<package epoch>:<package version>-<package revision>``
  15. or in BitBake variable terms ${:term:`PE`}:${:term:`PV`}-${:term:`PR`}. We
  16. generally follow the `Debian <https://www.debian.org/doc/debian-policy/ch-controlfields.html#version>`__
  17. version policy which defines these terms.
  18. In most cases the version :term:`PV` will be set automatically from the recipe
  19. file name. It is recommended to use released versions of software as these are
  20. revisions that upstream are expecting people to use.
  21. Package versions should always compare and sort correctly so that upgrades work
  22. as expected. With conventional versions such as ``1.4`` upgrading ``to 1.5``
  23. this happens naturally, but some versions don't sort. For example,
  24. ``1.5 Release Candidate 2`` could be written as ``1.5rc2`` but this sorts after
  25. ``1.5``, so upgrades from feeds won't happen correctly.
  26. Instead the tilde (``~``) operator can be used, which sorts before the empty
  27. string so ``1.5~rc2`` comes before ``1.5``. There is a historical syntax which
  28. may be found where :term:`PV` is set as a combination of the prior version
  29. ``+`` the pre-release version, for example ``PV=1.4+1.5rc2``. This is a valid
  30. syntax but the tilde form is preferred.
  31. For version comparisons, the ``opkg-compare-versions`` program from
  32. ``opkg-utils`` can be useful when attempting to determine how two version
  33. numbers compare to each other. Our definitive version comparison algorithm is
  34. the one within bitbake which aims to match those of the package managers and
  35. Debian policy closely.
  36. When a recipe references a git revision that does not correspond to a released
  37. version of software (e.g. is not a tagged version), the :term:`PV` variable
  38. should include the Git revision using the following to make the
  39. version clear::
  40. PV = "<version>+git${SRCPV}"
  41. In this case, ``<version>`` should be the most recently released version of the
  42. software from the current source revision (``git describe`` can be useful for
  43. determining this). Whilst not recommended for published layers, this format is
  44. also useful when using :term:`AUTOREV` to set the recipe to increment source
  45. control revisions automatically, which can be useful during local development.
  46. Version Number Changes
  47. ======================
  48. The :term:`PR` variable is used to indicate different revisions of a recipe
  49. that reference the same upstream source version. It can be used to force a
  50. new version of a package to be installed onto a device from a package feed.
  51. These once had to be set manually but in most cases these can now be set and
  52. incremented automatically by a PR Server connected with a package feed.
  53. When :term:`PV` increases, any existing :term:`PR` value can and should be
  54. removed.
  55. If :term:`PV` changes in such a way that it does not increase with respect to
  56. the previous value, you need to increase :term:`PE` to ensure package managers
  57. will upgrade it correctly. If unset you should set :term:`PE` to "1" since
  58. the default of empty is easily confused with "0" depending on the package
  59. manager. :term:`PE` can only have an integer value.
  60. Recipe formatting
  61. =================
  62. Variable Formatting
  63. -------------------
  64. - Variable assignment should a space around each side of the operator, e.g.
  65. ``FOO = "bar"``, not ``FOO="bar"``.
  66. - Double quotes should be used on the right-hand side of the assignment,
  67. e.g. ``FOO = "bar"`` not ``FOO = 'bar'``
  68. - Spaces should be used for indenting variables, with 4 spaces per tab
  69. - Long variables should be split over multiple lines when possible by using
  70. the continuation character (``\``)
  71. - When splitting a long variable over multiple lines, all continuation lines
  72. should be indented (with spaces) to align with the start of the quote on the
  73. first line::
  74. FOO = "this line is \
  75. long \
  76. "
  77. Instead of::
  78. FOO = "this line is \
  79. long \
  80. "
  81. Python Function formatting
  82. --------------------------
  83. - Spaces must be used for indenting Python code, with 4 spaces per tab
  84. Shell Function formatting
  85. -------------------------
  86. - The formatting of shell functions should be consistent within layers.
  87. Some use tabs, some use spaces.
  88. Recipe metadata
  89. ===============
  90. Required Variables
  91. ------------------
  92. The following variables should be included in all recipes:
  93. - :term:`SUMMARY`: a one line description of the upstream project
  94. - :term:`DESCRIPTION`: an extended description of the upstream project,
  95. possibly with multiple lines. If no reasonable description can be written,
  96. this may be omitted as it defaults to :term:`SUMMARY`.
  97. - :term:`HOMEPAGE`: the URL to the upstream projects homepage.
  98. - :term:`BUGTRACKER`: the URL upstream projects bug tracking website,
  99. if applicable.
  100. Recipe Ordering
  101. ---------------
  102. When a variable is defined in recipes and classes, variables should follow the
  103. general order when possible:
  104. - :term:`SUMMARY`
  105. - :term:`DESCRIPTION`
  106. - :term:`HOMEPAGE`
  107. - :term:`BUGTRACKER`
  108. - :term:`SECTION`
  109. - :term:`LICENSE`
  110. - :term:`LIC_FILES_CHKSUM`
  111. - :term:`DEPENDS`
  112. - :term:`PROVIDES`
  113. - :term:`PV`
  114. - :term:`SRC_URI`
  115. - :term:`SRCREV`
  116. - :term:`S`
  117. - ``inherit ...``
  118. - :term:`PACKAGECONFIG`
  119. - Build class specific variables such as ``EXTRA_QMAKEVARS_POST`` and :term:`EXTRA_OECONF`
  120. - Tasks such as :ref:`ref-tasks-configure`
  121. - :term:`PACKAGE_ARCH`
  122. - :term:`PACKAGES`
  123. - :term:`FILES`
  124. - :term:`RDEPENDS`
  125. - :term:`RRECOMMENDS`
  126. - :term:`RSUGGESTS`
  127. - :term:`RPROVIDES`
  128. - :term:`RCONFLICTS`
  129. - :term:`BBCLASSEXTEND`
  130. There are some cases where ordering is important and these cases would override
  131. this default order. Examples include:
  132. - :term:`PACKAGE_ARCH` needing to be set before ``inherit packagegroup``
  133. Tasks should be ordered based on the order they generally execute. For commonly
  134. used tasks this would be:
  135. - :ref:`ref-tasks-fetch`
  136. - :ref:`ref-tasks-unpack`
  137. - :ref:`ref-tasks-patch`
  138. - :ref:`ref-tasks-prepare_recipe_sysroot`
  139. - :ref:`ref-tasks-configure`
  140. - :ref:`ref-tasks-compile`
  141. - :ref:`ref-tasks-install`
  142. - :ref:`ref-tasks-populate_sysroot`
  143. - :ref:`ref-tasks-package`
  144. Custom tasks should be sorted similarly.
  145. Package specific variables are typically grouped together, e.g.::
  146. RDEPENDS:${PN} = “foo”
  147. RDEPENDS:${PN}-libs = “bar”
  148. RRECOMMENDS:${PN} = “one”
  149. RRECOMMENDS:${PN}-libs = “two”
  150. Recipe License Fields
  151. ---------------------
  152. Recipes need to define both the :term:`LICENSE` and
  153. :term:`LIC_FILES_CHKSUM` variables:
  154. - :term:`LICENSE`: This variable specifies the license for the software.
  155. If you do not know the license under which the software you are
  156. building is distributed, you should go to the source code and look
  157. for that information. Typical files containing this information
  158. include ``COPYING``, :term:`LICENSE`, and ``README`` files. You could
  159. also find the information near the top of a source file. For example,
  160. given a piece of software licensed under the GNU General Public
  161. License version 2, you would set :term:`LICENSE` as follows::
  162. LICENSE = "GPL-2.0-only"
  163. The licenses you specify within :term:`LICENSE` can have any name as long
  164. as you do not use spaces, since spaces are used as separators between
  165. license names. For standard licenses, use the names of the files in
  166. ``meta/files/common-licenses/`` or the :term:`SPDXLICENSEMAP` flag names
  167. defined in ``meta/conf/licenses.conf``.
  168. - :term:`LIC_FILES_CHKSUM`: The OpenEmbedded build system uses this
  169. variable to make sure the license text has not changed. If it has,
  170. the build produces an error and it affords you the chance to figure
  171. it out and correct the problem.
  172. You need to specify all applicable licensing files for the software.
  173. At the end of the configuration step, the build process will compare
  174. the checksums of the files to be sure the text has not changed. Any
  175. differences result in an error with the message containing the
  176. current checksum. For more explanation and examples of how to set the
  177. :term:`LIC_FILES_CHKSUM` variable, see the
  178. ":ref:`dev-manual/licenses:tracking license changes`" section.
  179. To determine the correct checksum string, you can list the
  180. appropriate files in the :term:`LIC_FILES_CHKSUM` variable with incorrect
  181. md5 strings, attempt to build the software, and then note the
  182. resulting error messages that will report the correct md5 strings.
  183. See the ":ref:`dev-manual/new-recipe:fetching code`" section for
  184. additional information.
  185. Here is an example that assumes the software has a ``COPYING`` file::
  186. LIC_FILES_CHKSUM = "file://COPYING;md5=xxx"
  187. When you try to build the
  188. software, the build system will produce an error and give you the
  189. correct string that you can substitute into the recipe file for a
  190. subsequent build.
  191. Tips and Guidelines for Writing Recipes
  192. ---------------------------------------
  193. - Use :term:`BBCLASSEXTEND` instead of creating separate recipes such as ``-native``
  194. and ``-nativesdk`` ones, whenever possible. This avoids having to maintain multiple
  195. recipe files at the same time.
  196. - Avoid manually mangling ``pkg-config`` ``.pc`` files.
  197. Recipes using ``pkg-config`` should use variables to ensure they are correctly
  198. relocatable and not need manual path correction in the recipe.