new-recipe.rst 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639
  1. .. SPDX-License-Identifier: CC-BY-SA-2.0-UK
  2. Writing a New Recipe
  3. ********************
  4. Recipes (``.bb`` files) are fundamental components in the Yocto Project
  5. environment. Each software component built by the OpenEmbedded build
  6. system requires a recipe to define the component. This section describes
  7. how to create, write, and test a new recipe.
  8. .. note::
  9. For information on variables that are useful for recipes and for
  10. information about recipe naming issues, see the
  11. ":ref:`ref-manual/varlocality:recipes`" section of the Yocto Project
  12. Reference Manual.
  13. Overview
  14. ========
  15. The following figure shows the basic process for creating a new recipe.
  16. The remainder of the section provides details for the steps.
  17. .. image:: figures/recipe-workflow.png
  18. :align: center
  19. :width: 50%
  20. Locate or Automatically Create a Base Recipe
  21. ============================================
  22. You can always write a recipe from scratch. However, there are three choices
  23. that can help you quickly get started with a new recipe:
  24. - ``devtool add``: A command that assists in creating a recipe and an
  25. environment conducive to development.
  26. - ``recipetool create``: A command provided by the Yocto Project that
  27. automates creation of a base recipe based on the source files.
  28. - *Existing Recipes:* Location and modification of an existing recipe
  29. that is similar in function to the recipe you need.
  30. .. note::
  31. For information on recipe syntax, see the
  32. ":ref:`dev-manual/new-recipe:recipe syntax`" section.
  33. Creating the Base Recipe Using ``devtool add``
  34. ----------------------------------------------
  35. The ``devtool add`` command uses the same logic for auto-creating the
  36. recipe as ``recipetool create``, which is listed below. Additionally,
  37. however, ``devtool add`` sets up an environment that makes it easy for
  38. you to patch the source and to make changes to the recipe as is often
  39. necessary when adding a recipe to build a new piece of software to be
  40. included in a build.
  41. You can find a complete description of the ``devtool add`` command in
  42. the ":ref:`dev-manual/devtool:a closer look at \`\`devtool add\`\``" section
  43. in the Yocto Project Application Development and the Extensible Software
  44. Development Kit (eSDK) manual.
  45. Creating the Base Recipe Using ``recipetool create``
  46. ----------------------------------------------------
  47. ``recipetool create`` automates creation of a base recipe given a set of
  48. source code files. As long as you can extract or point to the source
  49. files, the tool will construct a recipe and automatically configure all
  50. pre-build information into the recipe. For example, suppose you have an
  51. application that builds using Autotools. Creating the base recipe using
  52. ``recipetool`` results in a recipe that has the pre-build dependencies,
  53. license requirements, and checksums configured.
  54. To run the tool, you just need to be in your :term:`Build Directory` and
  55. have sourced the build environment setup script (i.e.
  56. :ref:`structure-core-script`). To get help on the tool, use the following
  57. command::
  58. $ recipetool -h
  59. NOTE: Starting bitbake server...
  60. usage: recipetool [-d] [-q] [--color COLOR] [-h] <subcommand> ...
  61. OpenEmbedded recipe tool
  62. options:
  63. -d, --debug Enable debug output
  64. -q, --quiet Print only errors
  65. --color COLOR Colorize output (where COLOR is auto, always, never)
  66. -h, --help show this help message and exit
  67. subcommands:
  68. create Create a new recipe
  69. newappend Create a bbappend for the specified target in the specified
  70. layer
  71. setvar Set a variable within a recipe
  72. appendfile Create/update a bbappend to replace a target file
  73. appendsrcfiles Create/update a bbappend to add or replace source files
  74. appendsrcfile Create/update a bbappend to add or replace a source file
  75. Use recipetool <subcommand> --help to get help on a specific command
  76. Running ``recipetool create -o OUTFILE`` creates the base recipe and
  77. locates it properly in the layer that contains your source files.
  78. Here are some syntax examples:
  79. - Use this syntax to generate a recipe based on source. Once generated,
  80. the recipe resides in the existing source code layer::
  81. recipetool create -o OUTFILE source
  82. - Use this syntax to generate a recipe using code that
  83. you extract from source. The extracted code is placed in its own layer
  84. defined by :term:`EXTERNALSRC`::
  85. recipetool create -o OUTFILE -x EXTERNALSRC source
  86. - Use this syntax to generate a recipe based on source. The options
  87. direct ``recipetool`` to generate debugging information. Once generated,
  88. the recipe resides in the existing source code layer::
  89. recipetool create -d -o OUTFILE source
  90. Locating and Using a Similar Recipe
  91. -----------------------------------
  92. Before writing a recipe from scratch, it is often useful to discover
  93. whether someone else has already written one that meets (or comes close
  94. to meeting) your needs. The Yocto Project and OpenEmbedded communities
  95. maintain many recipes that might be candidates for what you are doing.
  96. You can find a good central index of these recipes in the
  97. :oe_layerindex:`OpenEmbedded Layer Index <>`.
  98. Working from an existing recipe or a skeleton recipe is the best way to
  99. get started. Here are some points on both methods:
  100. - *Locate and modify a recipe that is close to what you want to do:*
  101. This method works when you are familiar with the current recipe
  102. space. The method does not work so well for those new to the Yocto
  103. Project or writing recipes.
  104. Some risks associated with this method are using a recipe that has
  105. areas totally unrelated to what you are trying to accomplish with
  106. your recipe, not recognizing areas of the recipe that you might have
  107. to add from scratch, and so forth. All these risks stem from
  108. unfamiliarity with the existing recipe space.
  109. - *Use and modify the following skeleton recipe:* If for some reason
  110. you do not want to use ``recipetool`` and you cannot find an existing
  111. recipe that is close to meeting your needs, you can use the following
  112. structure to provide the fundamental areas of a new recipe::
  113. DESCRIPTION = ""
  114. HOMEPAGE = ""
  115. LICENSE = ""
  116. SECTION = ""
  117. DEPENDS = ""
  118. LIC_FILES_CHKSUM = ""
  119. SRC_URI = ""
  120. Storing and Naming the Recipe
  121. =============================
  122. Once you have your base recipe, you should put it in your own layer and
  123. name it appropriately. Locating it correctly ensures that the
  124. OpenEmbedded build system can find it when you use BitBake to process
  125. the recipe.
  126. - *Storing Your Recipe:* The OpenEmbedded build system locates your
  127. recipe through the layer's ``conf/layer.conf`` file and the
  128. :term:`BBFILES` variable. This
  129. variable sets up a path from which the build system can locate
  130. recipes. Here is the typical use::
  131. BBFILES += "${LAYERDIR}/recipes-*/*/*.bb \
  132. ${LAYERDIR}/recipes-*/*/*.bbappend"
  133. Consequently, you need to be sure you locate your new recipe inside
  134. your layer such that it can be found.
  135. You can find more information on how layers are structured in the
  136. ":ref:`dev-manual/layers:understanding and creating layers`" section.
  137. - *Naming Your Recipe:* When you name your recipe, you need to follow
  138. this naming convention::
  139. basename_version.bb
  140. Use lower-cased characters and do not include the reserved suffixes
  141. ``-native``, ``-cross``, ``-initial``, or ``-dev`` casually (i.e. do not use
  142. them as part of your recipe name unless the string applies). Here are some
  143. examples:
  144. .. code-block:: none
  145. cups_1.7.0.bb
  146. gawk_4.0.2.bb
  147. irssi_0.8.16-rc1.bb
  148. Running a Build on the Recipe
  149. =============================
  150. Creating a new recipe is usually an iterative process that requires
  151. using BitBake to process the recipe multiple times in order to
  152. progressively discover and add information to the recipe file.
  153. Assuming you have sourced the build environment setup script (i.e.
  154. :ref:`structure-core-script`) and you are in the :term:`Build Directory`, use
  155. BitBake to process your recipe. All you need to provide is the
  156. ``basename`` of the recipe as described in the previous section::
  157. $ bitbake basename
  158. During the build, the OpenEmbedded build system creates a temporary work
  159. directory for each recipe
  160. (``${``\ :term:`WORKDIR`\ ``}``)
  161. where it keeps extracted source files, log files, intermediate
  162. compilation and packaging files, and so forth.
  163. The path to the per-recipe temporary work directory depends on the
  164. context in which it is being built. The quickest way to find this path
  165. is to have BitBake return it by running the following::
  166. $ bitbake -e basename | grep ^WORKDIR=
  167. As an example, assume a Source Directory
  168. top-level folder named ``poky``, a default :term:`Build Directory` at
  169. ``poky/build``, and a ``qemux86-poky-linux`` machine target system.
  170. Furthermore, suppose your recipe is named ``foo_1.3.0.bb``. In this
  171. case, the work directory the build system uses to build the package
  172. would be as follows::
  173. poky/build/tmp/work/qemux86-poky-linux/foo/1.3.0-r0
  174. Inside this directory you can find sub-directories such as ``image``,
  175. ``packages-split``, and ``temp``. After the build, you can examine these
  176. to determine how well the build went.
  177. .. note::
  178. You can find log files for each task in the recipe's ``temp``
  179. directory (e.g. ``poky/build/tmp/work/qemux86-poky-linux/foo/1.3.0-r0/temp``).
  180. Log files are named ``log.taskname`` (e.g. ``log.do_configure``,
  181. ``log.do_fetch``, and ``log.do_compile``).
  182. You can find more information about the build process in
  183. ":doc:`/overview-manual/development-environment`"
  184. chapter of the Yocto Project Overview and Concepts Manual.
  185. Fetching Code
  186. =============
  187. The first thing your recipe must do is specify how to fetch the source
  188. files. Fetching is controlled mainly through the
  189. :term:`SRC_URI` variable. Your recipe
  190. must have a :term:`SRC_URI` variable that points to where the source is
  191. located. For a graphical representation of source locations, see the
  192. ":ref:`overview-manual/concepts:sources`" section in
  193. the Yocto Project Overview and Concepts Manual.
  194. The :ref:`ref-tasks-fetch` task uses the prefix of each entry in the
  195. :term:`SRC_URI` variable value to determine which
  196. :ref:`fetcher <bitbake-user-manual/bitbake-user-manual-fetching:fetchers>`
  197. to use to get your source files. It is the :term:`SRC_URI` variable that triggers
  198. the fetcher. The :ref:`ref-tasks-patch` task uses the variable after source is
  199. fetched to apply patches. The OpenEmbedded build system uses
  200. :term:`FILESOVERRIDES` for scanning directory locations for local files in
  201. :term:`SRC_URI`.
  202. The :term:`SRC_URI` variable in your recipe must define each unique location
  203. for your source files. It is good practice to not hard-code version
  204. numbers in a URL used in :term:`SRC_URI`. Rather than hard-code these
  205. values, use ``${``\ :term:`PV`\ ``}``,
  206. which causes the fetch process to use the version specified in the
  207. recipe filename. Specifying the version in this manner means that
  208. upgrading the recipe to a future version is as simple as renaming the
  209. recipe to match the new version.
  210. Here is a simple example from the
  211. ``meta/recipes-devtools/strace/strace_5.5.bb`` recipe where the source
  212. comes from a single tarball. Notice the use of the
  213. :term:`PV` variable::
  214. SRC_URI = "https://strace.io/files/${PV}/strace-${PV}.tar.xz \
  215. Files mentioned in :term:`SRC_URI` whose names end in a typical archive
  216. extension (e.g. ``.tar``, ``.tar.gz``, ``.tar.bz2``, ``.zip``, and so
  217. forth), are automatically extracted during the
  218. :ref:`ref-tasks-unpack` task. For
  219. another example that specifies these types of files, see the
  220. ":ref:`dev-manual/new-recipe:building an autotooled package`" section.
  221. Another way of specifying source is from an SCM. For Git repositories,
  222. you must specify :term:`SRCREV` and you should specify :term:`PV` to include
  223. a ``+`` sign in its definition. Here is an example from the recipe
  224. :oe_git:`meta/recipes-sato/l3afpad/l3afpad_git.bb </openembedded-core/tree/meta/recipes-sato/l3afpad/l3afpad_git.bb>`::
  225. SRC_URI = "git://github.com/stevenhoneyman/l3afpad.git;branch=master;protocol=https"
  226. PV = "0.8.18.1.11+git"
  227. SRCREV ="3cdccdc9505643e50f8208171d9eee5de11a42ff"
  228. If your :term:`SRC_URI` statement includes URLs pointing to individual files
  229. fetched from a remote server other than a version control system,
  230. BitBake attempts to verify the files against checksums defined in your
  231. recipe to ensure they have not been tampered with or otherwise modified
  232. since the recipe was written. Multiple checksums are supported:
  233. ``SRC_URI[md5sum]``, ``SRC_URI[sha1sum]``, ``SRC_URI[sha256sum]``.
  234. ``SRC_URI[sha384sum]`` and ``SRC_URI[sha512sum]``, but only
  235. ``SRC_URI[sha256sum]`` is commonly used.
  236. .. note::
  237. ``SRC_URI[md5sum]`` used to also be commonly used, but it is deprecated
  238. and should be replaced by ``SRC_URI[sha256sum]`` when updating existing
  239. recipes.
  240. If your :term:`SRC_URI` variable points to more than a single URL (excluding
  241. SCM URLs), you need to provide the ``sha256`` checksum for each URL. For these
  242. cases, you provide a name for each URL as part of the :term:`SRC_URI` and then
  243. reference that name in the subsequent checksum statements. Here is an example
  244. combining lines from the files ``git.inc`` and ``git_2.24.1.bb``::
  245. SRC_URI = "${KERNELORG_MIRROR}/software/scm/git/git-${PV}.tar.gz;name=tarball \
  246. ${KERNELORG_MIRROR}/software/scm/git/git-manpages-${PV}.tar.gz;name=manpages"
  247. SRC_URI[tarball.sha256sum] = "ad5334956301c86841eb1e5b1bb20884a6bad89a10a6762c958220c7cf64da02"
  248. SRC_URI[manpages.sha256sum] = "9a7ae3a093bea39770eb96ca3e5b40bff7af0b9f6123f089d7821d0e5b8e1230"
  249. The proper value for the ``sha256`` checksum might be available together
  250. with other signatures on the download page for the upstream source (e.g.
  251. ``md5``, ``sha1``, ``sha256``, ``GPG``, and so forth). Because the
  252. OpenEmbedded build system typically only deals with ``sha256sum``,
  253. you should verify all the signatures you find by hand.
  254. If no :term:`SRC_URI` checksums are specified when you attempt to build the
  255. recipe, or you provide an incorrect checksum, the build will produce an
  256. error for each missing or incorrect checksum. As part of the error
  257. message, the build system provides the checksum string corresponding to
  258. the fetched file. Once you have the correct checksums, you can copy and
  259. paste them into your recipe and then run the build again to continue.
  260. .. note::
  261. As mentioned, if the upstream source provides signatures for
  262. verifying the downloaded source code, you should verify those
  263. manually before setting the checksum values in the recipe and
  264. continuing with the build.
  265. This final example is a bit more complicated and is from the
  266. ``meta/recipes-sato/rxvt-unicode/rxvt-unicode_9.20.bb`` recipe. The
  267. example's :term:`SRC_URI` statement identifies multiple files as the source
  268. files for the recipe: a tarball, a patch file, a desktop file, and an icon::
  269. SRC_URI = "http://dist.schmorp.de/rxvt-unicode/Attic/rxvt-unicode-${PV}.tar.bz2 \
  270. file://xwc.patch \
  271. file://rxvt.desktop \
  272. file://rxvt.png"
  273. When you specify local files using the ``file://`` URI protocol, the
  274. build system fetches files from the local machine. The path is relative
  275. to the :term:`FILESPATH` variable
  276. and searches specific directories in a certain order:
  277. ``${``\ :term:`BP`\ ``}``,
  278. ``${``\ :term:`BPN`\ ``}``, and
  279. ``files``. The directories are assumed to be subdirectories of the
  280. directory in which the recipe or append file resides. For another
  281. example that specifies these types of files, see the
  282. "`building a single .c file package`_" section.
  283. The previous example also specifies a patch file. Patch files are files
  284. whose names usually end in ``.patch`` or ``.diff`` but can end with
  285. compressed suffixes such as ``diff.gz`` and ``patch.bz2``, for example.
  286. The build system automatically applies patches as described in the
  287. ":ref:`dev-manual/new-recipe:patching code`" section.
  288. Fetching Code Through Firewalls
  289. -------------------------------
  290. Some users are behind firewalls and need to fetch code through a proxy.
  291. See the ":doc:`/ref-manual/faq`" chapter for advice.
  292. Limiting the Number of Parallel Connections
  293. -------------------------------------------
  294. Some users are behind firewalls or use servers where the number of parallel
  295. connections is limited. In such cases, you can limit the number of fetch
  296. tasks being run in parallel by adding the following to your ``local.conf``
  297. file::
  298. do_fetch[number_threads] = "4"
  299. Unpacking Code
  300. ==============
  301. During the build, the
  302. :ref:`ref-tasks-unpack` task unpacks
  303. the source with ``${``\ :term:`S`\ ``}``
  304. pointing to where it is unpacked.
  305. If you are fetching your source files from an upstream source archived
  306. tarball and the tarball's internal structure matches the common
  307. convention of a top-level subdirectory named
  308. ``${``\ :term:`BPN`\ ``}-${``\ :term:`PV`\ ``}``,
  309. then you do not need to set :term:`S`. However, if :term:`SRC_URI` specifies to
  310. fetch source from an archive that does not use this convention, or from
  311. an SCM like Git or Subversion, your recipe needs to define :term:`S`.
  312. If processing your recipe using BitBake successfully unpacks the source
  313. files, you need to be sure that the directory pointed to by ``${S}``
  314. matches the structure of the source.
  315. Patching Code
  316. =============
  317. Sometimes it is necessary to patch code after it has been fetched. Any
  318. files mentioned in :term:`SRC_URI` whose names end in ``.patch`` or
  319. ``.diff`` or compressed versions of these suffixes (e.g. ``diff.gz``,
  320. ``patch.bz2``, etc.) are treated as patches. The
  321. :ref:`ref-tasks-patch` task
  322. automatically applies these patches.
  323. The build system should be able to apply patches with the "-p1" option
  324. (i.e. one directory level in the path will be stripped off). If your
  325. patch needs to have more directory levels stripped off, specify the
  326. number of levels using the "striplevel" option in the :term:`SRC_URI` entry
  327. for the patch. Alternatively, if your patch needs to be applied in a
  328. specific subdirectory that is not specified in the patch file, use the
  329. "patchdir" option in the entry.
  330. As with all local files referenced in
  331. :term:`SRC_URI` using ``file://``,
  332. you should place patch files in a directory next to the recipe either
  333. named the same as the base name of the recipe
  334. (:term:`BP` and
  335. :term:`BPN`) or "files".
  336. Licensing
  337. =========
  338. Your recipe needs to define variables related to the license
  339. under whith the software is distributed. See the
  340. :ref:`contributor-guide/recipe-style-guide:recipe license fields`
  341. section in the Contributor Guide for details.
  342. Dependencies
  343. ============
  344. Most software packages have a short list of other packages that they
  345. require, which are called dependencies. These dependencies fall into two
  346. main categories: build-time dependencies, which are required when the
  347. software is built; and runtime dependencies, which are required to be
  348. installed on the target in order for the software to run.
  349. Within a recipe, you specify build-time dependencies using the
  350. :term:`DEPENDS` variable. Although there are nuances,
  351. items specified in :term:`DEPENDS` should be names of other
  352. recipes. It is important that you specify all build-time dependencies
  353. explicitly.
  354. Another consideration is that configure scripts might automatically
  355. check for optional dependencies and enable corresponding functionality
  356. if those dependencies are found. If you wish to make a recipe that is
  357. more generally useful (e.g. publish the recipe in a layer for others to
  358. use), instead of hard-disabling the functionality, you can use the
  359. :term:`PACKAGECONFIG` variable to allow functionality and the
  360. corresponding dependencies to be enabled and disabled easily by other
  361. users of the recipe.
  362. Similar to build-time dependencies, you specify runtime dependencies
  363. through a variable -
  364. :term:`RDEPENDS`, which is
  365. package-specific. All variables that are package-specific need to have
  366. the name of the package added to the end as an override. Since the main
  367. package for a recipe has the same name as the recipe, and the recipe's
  368. name can be found through the
  369. ``${``\ :term:`PN`\ ``}`` variable, then
  370. you specify the dependencies for the main package by setting
  371. ``RDEPENDS:${PN}``. If the package were named ``${PN}-tools``, then you
  372. would set ``RDEPENDS:${PN}-tools``, and so forth.
  373. Some runtime dependencies will be set automatically at packaging time.
  374. These dependencies include any shared library dependencies (i.e. if a
  375. package "example" contains "libexample" and another package "mypackage"
  376. contains a binary that links to "libexample" then the OpenEmbedded build
  377. system will automatically add a runtime dependency to "mypackage" on
  378. "example"). See the
  379. ":ref:`overview-manual/concepts:automatically added runtime dependencies`"
  380. section in the Yocto Project Overview and Concepts Manual for further
  381. details.
  382. Configuring the Recipe
  383. ======================
  384. Most software provides some means of setting build-time configuration
  385. options before compilation. Typically, setting these options is
  386. accomplished by running a configure script with options, or by modifying
  387. a build configuration file.
  388. .. note::
  389. As of Yocto Project Release 1.7, some of the core recipes that
  390. package binary configuration scripts now disable the scripts due to
  391. the scripts previously requiring error-prone path substitution. The
  392. OpenEmbedded build system uses ``pkg-config`` now, which is much more
  393. robust. You can find a list of the ``*-config`` scripts that are disabled
  394. in the ":ref:`migration-1.7-binary-configuration-scripts-disabled`" section
  395. in the Yocto Project Reference Manual.
  396. A major part of build-time configuration is about checking for
  397. build-time dependencies and possibly enabling optional functionality as
  398. a result. You need to specify any build-time dependencies for the
  399. software you are building in your recipe's
  400. :term:`DEPENDS` value, in terms of
  401. other recipes that satisfy those dependencies. You can often find
  402. build-time or runtime dependencies described in the software's
  403. documentation.
  404. The following list provides configuration items of note based on how
  405. your software is built:
  406. - *Autotools:* If your source files have a ``configure.ac`` file, then
  407. your software is built using Autotools. If this is the case, you just
  408. need to modify the configuration.
  409. When using Autotools, your recipe needs to inherit the
  410. :ref:`ref-classes-autotools` class and it does not have to
  411. contain a :ref:`ref-tasks-configure` task. However, you might still want to
  412. make some adjustments. For example, you can set :term:`EXTRA_OECONF` or
  413. :term:`PACKAGECONFIG_CONFARGS` to pass any needed configure options that
  414. are specific to the recipe.
  415. - *CMake:* If your source files have a ``CMakeLists.txt`` file, then
  416. your software is built using CMake. If this is the case, you just
  417. need to modify the configuration.
  418. When you use CMake, your recipe needs to inherit the
  419. :ref:`ref-classes-cmake` class and it does not have to contain a
  420. :ref:`ref-tasks-configure` task. You can make some adjustments by setting
  421. :term:`EXTRA_OECMAKE` to pass any needed configure options that are
  422. specific to the recipe.
  423. .. note::
  424. If you need to install one or more custom CMake toolchain files
  425. that are supplied by the application you are building, install the
  426. files to ``${D}${datadir}/cmake/Modules`` during :ref:`ref-tasks-install`.
  427. - *Other:* If your source files do not have a ``configure.ac`` or
  428. ``CMakeLists.txt`` file, then your software is built using some
  429. method other than Autotools or CMake. If this is the case, you
  430. normally need to provide a
  431. :ref:`ref-tasks-configure` task
  432. in your recipe unless, of course, there is nothing to configure.
  433. Even if your software is not being built by Autotools or CMake, you
  434. still might not need to deal with any configuration issues. You need
  435. to determine if configuration is even a required step. You might need
  436. to modify a Makefile or some configuration file used for the build to
  437. specify necessary build options. Or, perhaps you might need to run a
  438. provided, custom configure script with the appropriate options.
  439. For the case involving a custom configure script, you would run
  440. ``./configure --help`` and look for the options you need to set.
  441. Once configuration succeeds, it is always good practice to look at the
  442. ``log.do_configure`` file to ensure that the appropriate options have
  443. been enabled and no additional build-time dependencies need to be added
  444. to :term:`DEPENDS`. For example, if the configure script reports that it
  445. found something not mentioned in :term:`DEPENDS`, or that it did not find
  446. something that it needed for some desired optional functionality, then
  447. you would need to add those to :term:`DEPENDS`. Looking at the log might
  448. also reveal items being checked for, enabled, or both that you do not
  449. want, or items not being found that are in :term:`DEPENDS`, in which case
  450. you would need to look at passing extra options to the configure script
  451. as needed. For reference information on configure options specific to
  452. the software you are building, you can consult the output of the
  453. ``./configure --help`` command within ``${S}`` or consult the software's
  454. upstream documentation.
  455. Using Headers to Interface with Devices
  456. =======================================
  457. If your recipe builds an application that needs to communicate with some
  458. device or needs an API into a custom kernel, you will need to provide
  459. appropriate header files. Under no circumstances should you ever modify
  460. the existing
  461. ``meta/recipes-kernel/linux-libc-headers/linux-libc-headers.inc`` file.
  462. These headers are used to build ``libc`` and must not be compromised
  463. with custom or machine-specific header information. If you customize
  464. ``libc`` through modified headers all other applications that use
  465. ``libc`` thus become affected.
  466. .. note::
  467. Never copy and customize the ``libc`` header file (i.e.
  468. ``meta/recipes-kernel/linux-libc-headers/linux-libc-headers.inc``).
  469. The correct way to interface to a device or custom kernel is to use a
  470. separate package that provides the additional headers for the driver or
  471. other unique interfaces. When doing so, your application also becomes
  472. responsible for creating a dependency on that specific provider.
  473. Consider the following:
  474. - Never modify ``linux-libc-headers.inc``. Consider that file to be
  475. part of the ``libc`` system, and not something you use to access the
  476. kernel directly. You should access ``libc`` through specific ``libc``
  477. calls.
  478. - Applications that must talk directly to devices should either provide
  479. necessary headers themselves, or establish a dependency on a special
  480. headers package that is specific to that driver.
  481. For example, suppose you want to modify an existing header that adds I/O
  482. control or network support. If the modifications are used by a small
  483. number programs, providing a unique version of a header is easy and has
  484. little impact. When doing so, bear in mind the guidelines in the
  485. previous list.
  486. .. note::
  487. If for some reason your changes need to modify the behavior of the ``libc``,
  488. and subsequently all other applications on the system, use a ``.bbappend``
  489. to modify the ``linux-kernel-headers.inc`` file. However, take care to not
  490. make the changes machine specific.
  491. Consider a case where your kernel is older and you need an older
  492. ``libc`` ABI. The headers installed by your recipe should still be a
  493. standard mainline kernel, not your own custom one.
  494. When you use custom kernel headers you need to get them from
  495. :term:`STAGING_KERNEL_DIR`,
  496. which is the directory with kernel headers that are required to build
  497. out-of-tree modules. Your recipe will also need the following::
  498. do_configure[depends] += "virtual/kernel:do_shared_workdir"
  499. Compilation
  500. ===========
  501. During a build, the :ref:`ref-tasks-compile` task happens after source is fetched,
  502. unpacked, and configured. If the recipe passes through :ref:`ref-tasks-compile`
  503. successfully, nothing needs to be done.
  504. However, if the compile step fails, you need to diagnose the failure.
  505. Here are some common issues that cause failures.
  506. .. note::
  507. For cases where improper paths are detected for configuration files
  508. or for when libraries/headers cannot be found, be sure you are using
  509. the more robust ``pkg-config``. See the note in section
  510. ":ref:`dev-manual/new-recipe:Configuring the Recipe`" for additional information.
  511. - *Parallel build failures:* These failures manifest themselves as
  512. intermittent errors, or errors reporting that a file or directory
  513. that should be created by some other part of the build process could
  514. not be found. This type of failure can occur even if, upon
  515. inspection, the file or directory does exist after the build has
  516. failed, because that part of the build process happened in the wrong
  517. order.
  518. To fix the problem, you need to either satisfy the missing dependency
  519. in the Makefile or whatever script produced the Makefile, or (as a
  520. workaround) set :term:`PARALLEL_MAKE` to an empty string::
  521. PARALLEL_MAKE = ""
  522. For information on parallel Makefile issues, see the
  523. ":ref:`dev-manual/debugging:debugging parallel make races`" section.
  524. - *Improper host path usage:* This failure applies to recipes building
  525. for the target or ":ref:`ref-classes-nativesdk`" only. The
  526. failure occurs when the compilation process uses improper headers,
  527. libraries, or other files from the host system when cross-compiling for
  528. the target.
  529. To fix the problem, examine the ``log.do_compile`` file to identify
  530. the host paths being used (e.g. ``/usr/include``, ``/usr/lib``, and
  531. so forth) and then either add configure options, apply a patch, or do
  532. both.
  533. - *Failure to find required libraries/headers:* If a build-time
  534. dependency is missing because it has not been declared in
  535. :term:`DEPENDS`, or because the
  536. dependency exists but the path used by the build process to find the
  537. file is incorrect and the configure step did not detect it, the
  538. compilation process could fail. For either of these failures, the
  539. compilation process notes that files could not be found. In these
  540. cases, you need to go back and add additional options to the
  541. configure script as well as possibly add additional build-time
  542. dependencies to :term:`DEPENDS`.
  543. Occasionally, it is necessary to apply a patch to the source to
  544. ensure the correct paths are used. If you need to specify paths to
  545. find files staged into the sysroot from other recipes, use the
  546. variables that the OpenEmbedded build system provides (e.g.
  547. :term:`STAGING_BINDIR`, :term:`STAGING_INCDIR`, :term:`STAGING_DATADIR`, and so
  548. forth).
  549. Installing
  550. ==========
  551. During :ref:`ref-tasks-install`, the task copies the built files along with their
  552. hierarchy to locations that would mirror their locations on the target
  553. device. The installation process copies files from the
  554. ``${``\ :term:`S`\ ``}``,
  555. ``${``\ :term:`B`\ ``}``, and
  556. ``${``\ :term:`WORKDIR`\ ``}``
  557. directories to the ``${``\ :term:`D`\ ``}``
  558. directory to create the structure as it should appear on the target
  559. system.
  560. How your software is built affects what you must do to be sure your
  561. software is installed correctly. The following list describes what you
  562. must do for installation depending on the type of build system used by
  563. the software being built:
  564. - *Autotools and CMake:* If the software your recipe is building uses
  565. Autotools or CMake, the OpenEmbedded build system understands how to
  566. install the software. Consequently, you do not have to have a
  567. :ref:`ref-tasks-install` task as part of your recipe. You just need to make
  568. sure the install portion of the build completes with no issues.
  569. However, if you wish to install additional files not already being
  570. installed by ``make install``, you should do this using a
  571. ``do_install:append`` function using the install command as described
  572. in the "Manual" bulleted item later in this list.
  573. - *Other (using* ``make install``\ *)*: You need to define a :ref:`ref-tasks-install`
  574. function in your recipe. The function should call
  575. ``oe_runmake install`` and will likely need to pass in the
  576. destination directory as well. How you pass that path is dependent on
  577. how the ``Makefile`` being run is written (e.g. ``DESTDIR=${D}``,
  578. ``PREFIX=${D}``, ``INSTALLROOT=${D}``, and so forth).
  579. For an example recipe using ``make install``, see the
  580. ":ref:`dev-manual/new-recipe:building a makefile-based package`" section.
  581. - *Manual:* You need to define a :ref:`ref-tasks-install` function in your
  582. recipe. The function must first use ``install -d`` to create the
  583. directories under
  584. ``${``\ :term:`D`\ ``}``. Once the
  585. directories exist, your function can use ``install`` to manually
  586. install the built software into the directories.
  587. You can find more information on ``install`` at
  588. https://www.gnu.org/software/coreutils/manual/html_node/install-invocation.html.
  589. For the scenarios that do not use Autotools or CMake, you need to track
  590. the installation and diagnose and fix any issues until everything
  591. installs correctly. You need to look in the default location of
  592. ``${D}``, which is ``${WORKDIR}/image``, to be sure your files have been
  593. installed correctly.
  594. .. note::
  595. - During the installation process, you might need to modify some of
  596. the installed files to suit the target layout. For example, you
  597. might need to replace hard-coded paths in an initscript with
  598. values of variables provided by the build system, such as
  599. replacing ``/usr/bin/`` with ``${bindir}``. If you do perform such
  600. modifications during :ref:`ref-tasks-install`, be sure to modify the
  601. destination file after copying rather than before copying.
  602. Modifying after copying ensures that the build system can
  603. re-execute :ref:`ref-tasks-install` if needed.
  604. - ``oe_runmake install``, which can be run directly or can be run
  605. indirectly by the :ref:`ref-classes-autotools` and
  606. :ref:`ref-classes-cmake` classes, runs ``make install`` in parallel.
  607. Sometimes, a Makefile can have missing dependencies between targets that
  608. can result in race conditions. If you experience intermittent failures
  609. during :ref:`ref-tasks-install`, you might be able to work around them by
  610. disabling parallel Makefile installs by adding the following to the
  611. recipe::
  612. PARALLEL_MAKEINST = ""
  613. See :term:`PARALLEL_MAKEINST` for additional information.
  614. - If you need to install one or more custom CMake toolchain files
  615. that are supplied by the application you are building, install the
  616. files to ``${D}${datadir}/cmake/Modules`` during
  617. :ref:`ref-tasks-install`.
  618. Enabling System Services
  619. ========================
  620. If you want to install a service, which is a process that usually starts
  621. on boot and runs in the background, then you must include some
  622. additional definitions in your recipe.
  623. If you are adding services and the service initialization script or the
  624. service file itself is not installed, you must provide for that
  625. installation in your recipe using a ``do_install:append`` function. If
  626. your recipe already has a :ref:`ref-tasks-install` function, update the function
  627. near its end rather than adding an additional ``do_install:append``
  628. function.
  629. When you create the installation for your services, you need to
  630. accomplish what is normally done by ``make install``. In other words,
  631. make sure your installation arranges the output similar to how it is
  632. arranged on the target system.
  633. The OpenEmbedded build system provides support for starting services two
  634. different ways:
  635. - *SysVinit:* SysVinit is a system and service manager that manages the
  636. init system used to control the very basic functions of your system.
  637. The init program is the first program started by the Linux kernel
  638. when the system boots. Init then controls the startup, running and
  639. shutdown of all other programs.
  640. To enable a service using SysVinit, your recipe needs to inherit the
  641. :ref:`ref-classes-update-rc.d` class. The class helps
  642. facilitate safely installing the package on the target.
  643. You will need to set the
  644. :term:`INITSCRIPT_PACKAGES`,
  645. :term:`INITSCRIPT_NAME`,
  646. and
  647. :term:`INITSCRIPT_PARAMS`
  648. variables within your recipe.
  649. - *systemd:* System Management Daemon (systemd) was designed to replace
  650. SysVinit and to provide enhanced management of services. For more
  651. information on systemd, see the systemd homepage at
  652. https://freedesktop.org/wiki/Software/systemd/.
  653. To enable a service using systemd, your recipe needs to inherit the
  654. :ref:`ref-classes-systemd` class. See the ``systemd.bbclass`` file
  655. located in your :term:`Source Directory` section for more information.
  656. Packaging
  657. =========
  658. Successful packaging is a combination of automated processes performed
  659. by the OpenEmbedded build system and some specific steps you need to
  660. take. The following list describes the process:
  661. - *Splitting Files*: The :ref:`ref-tasks-package` task splits the files produced
  662. by the recipe into logical components. Even software that produces a
  663. single binary might still have debug symbols, documentation, and
  664. other logical components that should be split out. The :ref:`ref-tasks-package`
  665. task ensures that files are split up and packaged correctly.
  666. - *Running QA Checks*: The :ref:`ref-classes-insane` class adds a
  667. step to the package generation process so that output quality
  668. assurance checks are generated by the OpenEmbedded build system. This
  669. step performs a range of checks to be sure the build's output is free
  670. of common problems that show up during runtime. For information on
  671. these checks, see the :ref:`ref-classes-insane` class and
  672. the ":ref:`ref-manual/qa-checks:qa error and warning messages`"
  673. chapter in the Yocto Project Reference Manual.
  674. - *Hand-Checking Your Packages*: After you build your software, you
  675. need to be sure your packages are correct. Examine the
  676. ``${``\ :term:`WORKDIR`\ ``}/packages-split``
  677. directory and make sure files are where you expect them to be. If you
  678. discover problems, you can set
  679. :term:`PACKAGES`,
  680. :term:`FILES`,
  681. ``do_install(:append)``, and so forth as needed.
  682. - *Splitting an Application into Multiple Packages*: If you need to
  683. split an application into several packages, see the
  684. ":ref:`dev-manual/new-recipe:splitting an application into multiple packages`"
  685. section for an example.
  686. - *Installing a Post-Installation Script*: For an example showing how
  687. to install a post-installation script, see the
  688. ":ref:`dev-manual/new-recipe:post-installation scripts`" section.
  689. - *Marking Package Architecture*: Depending on what your recipe is
  690. building and how it is configured, it might be important to mark the
  691. packages produced as being specific to a particular machine, or to
  692. mark them as not being specific to a particular machine or
  693. architecture at all.
  694. By default, packages apply to any machine with the same architecture
  695. as the target machine. When a recipe produces packages that are
  696. machine-specific (e.g. the
  697. :term:`MACHINE` value is passed
  698. into the configure script or a patch is applied only for a particular
  699. machine), you should mark them as such by adding the following to the
  700. recipe::
  701. PACKAGE_ARCH = "${MACHINE_ARCH}"
  702. On the other hand, if the recipe produces packages that do not
  703. contain anything specific to the target machine or architecture at
  704. all (e.g. recipes that simply package script files or configuration
  705. files), you should use the :ref:`ref-classes-allarch` class to
  706. do this for you by adding this to your recipe::
  707. inherit allarch
  708. Ensuring that the package architecture is correct is not critical
  709. while you are doing the first few builds of your recipe. However, it
  710. is important in order to ensure that your recipe rebuilds (or does
  711. not rebuild) appropriately in response to changes in configuration,
  712. and to ensure that you get the appropriate packages installed on the
  713. target machine, particularly if you run separate builds for more than
  714. one target machine.
  715. Sharing Files Between Recipes
  716. =============================
  717. Recipes often need to use files provided by other recipes on the build
  718. host. For example, an application linking to a common library needs
  719. access to the library itself and its associated headers. The way this
  720. access is accomplished is by populating a sysroot with files. Each
  721. recipe has two sysroots in its work directory, one for target files
  722. (``recipe-sysroot``) and one for files that are native to the build host
  723. (``recipe-sysroot-native``).
  724. .. note::
  725. You could find the term "staging" used within the Yocto project
  726. regarding files populating sysroots (e.g. the :term:`STAGING_DIR`
  727. variable).
  728. Recipes should never populate the sysroot directly (i.e. write files
  729. into sysroot). Instead, files should be installed into standard
  730. locations during the
  731. :ref:`ref-tasks-install` task within
  732. the ``${``\ :term:`D`\ ``}`` directory. The
  733. reason for this limitation is that almost all files that populate the
  734. sysroot are cataloged in manifests in order to ensure the files can be
  735. removed later when a recipe is either modified or removed. Thus, the
  736. sysroot is able to remain free from stale files.
  737. A subset of the files installed by the :ref:`ref-tasks-install` task are
  738. used by the :ref:`ref-tasks-populate_sysroot` task as defined by the
  739. :term:`SYSROOT_DIRS` variable to automatically populate the sysroot. It
  740. is possible to modify the list of directories that populate the sysroot.
  741. The following example shows how you could add the ``/opt`` directory to
  742. the list of directories within a recipe::
  743. SYSROOT_DIRS += "/opt"
  744. .. note::
  745. The `/sysroot-only` is to be used by recipes that generate artifacts
  746. that are not included in the target filesystem, allowing them to share
  747. these artifacts without needing to use the :term:`DEPLOY_DIR`.
  748. For a more complete description of the :ref:`ref-tasks-populate_sysroot`
  749. task and its associated functions, see the
  750. :ref:`staging <ref-classes-staging>` class.
  751. Using Virtual Providers
  752. =======================
  753. Prior to a build, if you know that several different recipes provide the
  754. same functionality, you can use a virtual provider (i.e. ``virtual/*``)
  755. as a placeholder for the actual provider. The actual provider is
  756. determined at build-time.
  757. A common scenario where a virtual provider is used would be for the kernel
  758. recipe. Suppose you have three kernel recipes whose :term:`PN` values map to
  759. ``kernel-big``, ``kernel-mid``, and ``kernel-small``. Furthermore, each of
  760. these recipes in some way uses a :term:`PROVIDES` statement that essentially
  761. identifies itself as being able to provide ``virtual/kernel``. Here is one way
  762. through the :ref:`ref-classes-kernel` class::
  763. PROVIDES += "virtual/kernel"
  764. Any recipe that inherits the :ref:`ref-classes-kernel` class is
  765. going to utilize a :term:`PROVIDES` statement that identifies that recipe as
  766. being able to provide the ``virtual/kernel`` item.
  767. Now comes the time to actually build an image and you need a kernel
  768. recipe, but which one? You can configure your build to call out the
  769. kernel recipe you want by using the :term:`PREFERRED_PROVIDER` variable. As
  770. an example, consider the :yocto_git:`x86-base.inc
  771. </poky/tree/meta/conf/machine/include/x86/x86-base.inc>` include file, which is a
  772. machine (i.e. :term:`MACHINE`) configuration file. This include file is the
  773. reason all x86-based machines use the ``linux-yocto`` kernel. Here are the
  774. relevant lines from the include file::
  775. PREFERRED_PROVIDER_virtual/kernel ??= "linux-yocto"
  776. PREFERRED_VERSION_linux-yocto ??= "4.15%"
  777. When you use a virtual provider, you do not have to "hard code" a recipe
  778. name as a build dependency. You can use the
  779. :term:`DEPENDS` variable to state the
  780. build is dependent on ``virtual/kernel`` for example::
  781. DEPENDS = "virtual/kernel"
  782. During the build, the OpenEmbedded build system picks
  783. the correct recipe needed for the ``virtual/kernel`` dependency based on
  784. the :term:`PREFERRED_PROVIDER` variable. If you want to use the small kernel
  785. mentioned at the beginning of this section, configure your build as
  786. follows::
  787. PREFERRED_PROVIDER_virtual/kernel ??= "kernel-small"
  788. .. note::
  789. Any recipe that :term:`PROVIDES` a ``virtual/*`` item that is ultimately not
  790. selected through :term:`PREFERRED_PROVIDER` does not get built. Preventing these
  791. recipes from building is usually the desired behavior since this mechanism's
  792. purpose is to select between mutually exclusive alternative providers.
  793. The following lists specific examples of virtual providers:
  794. - ``virtual/kernel``: Provides the name of the kernel recipe to use
  795. when building a kernel image.
  796. - ``virtual/bootloader``: Provides the name of the bootloader to use
  797. when building an image.
  798. - ``virtual/libgbm``: Provides ``gbm.pc``.
  799. - ``virtual/egl``: Provides ``egl.pc`` and possibly ``wayland-egl.pc``.
  800. - ``virtual/libgl``: Provides ``gl.pc`` (i.e. libGL).
  801. - ``virtual/libgles1``: Provides ``glesv1_cm.pc`` (i.e. libGLESv1_CM).
  802. - ``virtual/libgles2``: Provides ``glesv2.pc`` (i.e. libGLESv2).
  803. .. note::
  804. Virtual providers only apply to build time dependencies specified with
  805. :term:`PROVIDES` and :term:`DEPENDS`. They do not apply to runtime
  806. dependencies specified with :term:`RPROVIDES` and :term:`RDEPENDS`.
  807. Properly Versioning Pre-Release Recipes
  808. =======================================
  809. Sometimes the name of a recipe can lead to versioning problems when the
  810. recipe is upgraded to a final release. For example, consider the
  811. ``irssi_0.8.16-rc1.bb`` recipe file in the list of example recipes in
  812. the ":ref:`dev-manual/new-recipe:storing and naming the recipe`" section.
  813. This recipe is at a release candidate stage (i.e. "rc1"). When the recipe is
  814. released, the recipe filename becomes ``irssi_0.8.16.bb``. The version
  815. change from ``0.8.16-rc1`` to ``0.8.16`` is seen as a decrease by the
  816. build system and package managers, so the resulting packages will not
  817. correctly trigger an upgrade.
  818. In order to ensure the versions compare properly, the recommended
  819. convention is to use a tilde (``~``) character as follows::
  820. PV = 0.8.16~rc1
  821. This way ``0.8.16~rc1`` sorts before ``0.8.16``. See the
  822. ":ref:`contributor-guide/recipe-style-guide:version policy`" section in the
  823. Yocto Project and OpenEmbedded Contributor Guide for more details about
  824. versioning code corresponding to a pre-release or to a specific Git commit.
  825. Post-Installation Scripts
  826. =========================
  827. Post-installation scripts run immediately after installing a package on
  828. the target or during image creation when a package is included in an
  829. image. To add a post-installation script to a package, add a
  830. ``pkg_postinst:``\ `PACKAGENAME`\ ``()`` function to the recipe file
  831. (``.bb``) and replace `PACKAGENAME` with the name of the package you want
  832. to attach to the ``postinst`` script. To apply the post-installation
  833. script to the main package for the recipe, which is usually what is
  834. required, specify
  835. ``${``\ :term:`PN`\ ``}`` in place of
  836. PACKAGENAME.
  837. A post-installation function has the following structure::
  838. pkg_postinst:PACKAGENAME() {
  839. # Commands to carry out
  840. }
  841. The script defined in the post-installation function is called when the
  842. root filesystem is created. If the script succeeds, the package is
  843. marked as installed.
  844. .. note::
  845. Any RPM post-installation script that runs on the target should
  846. return a 0 exit code. RPM does not allow non-zero exit codes for
  847. these scripts, and the RPM package manager will cause the package to
  848. fail installation on the target.
  849. Sometimes it is necessary for the execution of a post-installation
  850. script to be delayed until the first boot. For example, the script might
  851. need to be executed on the device itself. To delay script execution
  852. until boot time, you must explicitly mark post installs to defer to the
  853. target. You can use ``pkg_postinst_ontarget()`` or call
  854. ``postinst_intercept delay_to_first_boot`` from ``pkg_postinst()``. Any
  855. failure of a ``pkg_postinst()`` script (including exit 1) triggers an
  856. error during the
  857. :ref:`ref-tasks-rootfs` task.
  858. If you have recipes that use ``pkg_postinst`` function and they require
  859. the use of non-standard native tools that have dependencies during
  860. root filesystem construction, you need to use the
  861. :term:`PACKAGE_WRITE_DEPS`
  862. variable in your recipe to list these tools. If you do not use this
  863. variable, the tools might be missing and execution of the
  864. post-installation script is deferred until first boot. Deferring the
  865. script to the first boot is undesirable and impossible for read-only
  866. root filesystems.
  867. .. note::
  868. There is equivalent support for pre-install, pre-uninstall, and post-uninstall
  869. scripts by way of ``pkg_preinst``, ``pkg_prerm``, and ``pkg_postrm``,
  870. respectively. These scrips work in exactly the same way as does
  871. ``pkg_postinst`` with the exception that they run at different times. Also,
  872. because of when they run, they are not applicable to being run at image
  873. creation time like ``pkg_postinst``.
  874. Testing
  875. =======
  876. The final step for completing your recipe is to be sure that the
  877. software you built runs correctly. To accomplish runtime testing, add
  878. the build's output packages to your image and test them on the target.
  879. For information on how to customize your image by adding specific
  880. packages, see ":ref:`dev-manual/customizing-images:customizing images`" section.
  881. Examples
  882. ========
  883. To help summarize how to write a recipe, this section provides some
  884. recipe examples given various scenarios:
  885. - `Building a single .c file package`_
  886. - `Building a Makefile-based package`_
  887. - `Building an Autotooled package`_
  888. - `Building a Meson package`_
  889. - `Splitting an application into multiple packages`_
  890. - `Packaging externally produced binaries`_
  891. Building a Single .c File Package
  892. ---------------------------------
  893. Building an application from a single file that is stored locally (e.g. under
  894. ``files``) requires a recipe that has the file listed in the :term:`SRC_URI`
  895. variable. Additionally, you need to manually write the :ref:`ref-tasks-compile`
  896. and :ref:`ref-tasks-install` tasks. The :term:`S` variable defines the
  897. directory containing the source code, which is set to :term:`WORKDIR` in this
  898. case --- the directory BitBake uses for the build::
  899. SUMMARY = "Simple helloworld application"
  900. SECTION = "examples"
  901. LICENSE = "MIT"
  902. LIC_FILES_CHKSUM = "file://${COMMON_LICENSE_DIR}/MIT;md5=0835ade698e0bcf8506ecda2f7b4f302"
  903. SRC_URI = "file://helloworld.c"
  904. S = "${WORKDIR}"
  905. do_compile() {
  906. ${CC} ${LDFLAGS} helloworld.c -o helloworld
  907. }
  908. do_install() {
  909. install -d ${D}${bindir}
  910. install -m 0755 helloworld ${D}${bindir}
  911. }
  912. By default, the ``helloworld``, ``helloworld-dbg``, and ``helloworld-dev`` packages
  913. are built. For information on how to customize the packaging process, see the
  914. ":ref:`dev-manual/new-recipe:splitting an application into multiple packages`"
  915. section.
  916. Building a Makefile-Based Package
  917. ---------------------------------
  918. Applications built with GNU ``make`` require a recipe that has the source archive
  919. listed in :term:`SRC_URI`. You do not need to add a :ref:`ref-tasks-compile`
  920. step since by default BitBake starts the ``make`` command to compile the
  921. application. If you need additional ``make`` options, you should store them in
  922. the :term:`EXTRA_OEMAKE` or :term:`PACKAGECONFIG_CONFARGS` variables. BitBake
  923. passes these options into the GNU ``make`` invocation. Note that a
  924. :ref:`ref-tasks-install` task is still required. Otherwise, BitBake runs an
  925. empty :ref:`ref-tasks-install` task by default.
  926. Some applications might require extra parameters to be passed to the
  927. compiler. For example, the application might need an additional header
  928. path. You can accomplish this by adding to the :term:`CFLAGS` variable. The
  929. following example shows this::
  930. CFLAGS:prepend = "-I ${S}/include "
  931. In the following example, ``lz4`` is a makefile-based package::
  932. SUMMARY = "Extremely Fast Compression algorithm"
  933. DESCRIPTION = "LZ4 is a very fast lossless compression algorithm, providing compression speed at 400 MB/s per core, scalable with multi-cores CPU. It also features an extremely fast decoder, with speed in multiple GB/s per core, typically reaching RAM speed limits on multi-core systems."
  934. HOMEPAGE = "https://github.com/lz4/lz4"
  935. LICENSE = "BSD-2-Clause | GPL-2.0-only"
  936. LIC_FILES_CHKSUM = "file://lib/LICENSE;md5=ebc2ea4814a64de7708f1571904b32cc \
  937. file://programs/COPYING;md5=b234ee4d69f5fce4486a80fdaf4a4263 \
  938. file://LICENSE;md5=d57c0d21cb917fb4e0af2454aa48b956 \
  939. "
  940. PE = "1"
  941. SRCREV = "d44371841a2f1728a3f36839fd4b7e872d0927d3"
  942. SRC_URI = "git://github.com/lz4/lz4.git;branch=release;protocol=https \
  943. file://CVE-2021-3520.patch \
  944. "
  945. UPSTREAM_CHECK_GITTAGREGEX = "v(?P<pver>.*)"
  946. S = "${WORKDIR}/git"
  947. CVE_STATUS[CVE-2014-4715] = "fixed-version: Fixed in r118, which is larger than the current version"
  948. EXTRA_OEMAKE = "PREFIX=${prefix} CC='${CC}' CFLAGS='${CFLAGS}' DESTDIR=${D} LIBDIR=${libdir} INCLUDEDIR=${includedir} BUILD_STATIC=no"
  949. do_install() {
  950. oe_runmake install
  951. }
  952. BBCLASSEXTEND = "native nativesdk"
  953. Building an Autotooled Package
  954. ------------------------------
  955. Applications built with the Autotools such as ``autoconf`` and ``automake``
  956. require a recipe that has a source archive listed in :term:`SRC_URI` and also
  957. inherit the :ref:`ref-classes-autotools` class, which contains the definitions
  958. of all the steps needed to build an Autotool-based application. The result of
  959. the build is automatically packaged. And, if the application uses NLS for
  960. localization, packages with local information are generated (one package per
  961. language). Here is one example: (``hello_2.3.bb``)::
  962. SUMMARY = "GNU Helloworld application"
  963. SECTION = "examples"
  964. LICENSE = "GPL-2.0-or-later"
  965. LIC_FILES_CHKSUM = "file://COPYING;md5=751419260aa954499f7abaabaa882bbe"
  966. SRC_URI = "${GNU_MIRROR}/hello/hello-${PV}.tar.gz"
  967. inherit autotools gettext
  968. The variable :term:`LIC_FILES_CHKSUM` is used to track source license changes
  969. as described in the ":ref:`dev-manual/licenses:tracking license changes`"
  970. section in the Yocto Project Overview and Concepts Manual. You can quickly
  971. create Autotool-based recipes in a manner similar to the previous example.
  972. .. _ref-building-meson-package:
  973. Building a Meson Package
  974. ------------------------
  975. Applications built with the `Meson build system <https://mesonbuild.com/>`__
  976. just need a recipe that has sources described in :term:`SRC_URI` and inherits
  977. the :ref:`ref-classes-meson` class.
  978. The :oe_git:`ipcalc recipe </meta-openembedded/tree/meta-networking/recipes-support/ipcalc>`
  979. is a simple example of an application without dependencies::
  980. SUMMARY = "Tool to assist in network address calculations for IPv4 and IPv6."
  981. HOMEPAGE = "https://gitlab.com/ipcalc/ipcalc"
  982. SECTION = "net"
  983. LICENSE = "GPL-2.0-only"
  984. LIC_FILES_CHKSUM = "file://COPYING;md5=b234ee4d69f5fce4486a80fdaf4a4263"
  985. SRC_URI = "git://gitlab.com/ipcalc/ipcalc.git;protocol=https;branch=master"
  986. SRCREV = "4c4261a47f355946ee74013d4f5d0494487cc2d6"
  987. S = "${WORKDIR}/git"
  988. inherit meson
  989. Applications with dependencies are likely to inherit the
  990. :ref:`ref-classes-pkgconfig` class, as ``pkg-config`` is the default method
  991. used by Meson to find dependencies and compile applications against them.
  992. Splitting an Application into Multiple Packages
  993. -----------------------------------------------
  994. You can use the variables :term:`PACKAGES` and :term:`FILES` to split an
  995. application into multiple packages.
  996. Here is an example that uses the ``libxpm`` recipe. By default,
  997. this recipe generates a single package that contains the library along
  998. with a few binaries. You can modify the recipe to split the binaries
  999. into separate packages::
  1000. require xorg-lib-common.inc
  1001. SUMMARY = "Xpm: X Pixmap extension library"
  1002. LICENSE = "MIT"
  1003. LIC_FILES_CHKSUM = "file://COPYING;md5=51f4270b012ecd4ab1a164f5f4ed6cf7"
  1004. DEPENDS += "libxext libsm libxt"
  1005. PE = "1"
  1006. XORG_PN = "libXpm"
  1007. PACKAGES =+ "sxpm cxpm"
  1008. FILES:cxpm = "${bindir}/cxpm"
  1009. FILES:sxpm = "${bindir}/sxpm"
  1010. In the previous example, we want to ship the ``sxpm`` and ``cxpm``
  1011. binaries in separate packages. Since ``bindir`` would be packaged into
  1012. the main :term:`PN` package by default, we prepend the :term:`PACKAGES` variable
  1013. so additional package names are added to the start of list. This results
  1014. in the extra ``FILES:*`` variables then containing information that
  1015. define which files and directories go into which packages. Files
  1016. included by earlier packages are skipped by latter packages. Thus, the
  1017. main :term:`PN` package does not include the above listed files.
  1018. Packaging Externally Produced Binaries
  1019. --------------------------------------
  1020. Sometimes, you need to add pre-compiled binaries to an image. For
  1021. example, suppose that there are binaries for proprietary code,
  1022. created by a particular division of a company. Your part of the company
  1023. needs to use those binaries as part of an image that you are building
  1024. using the OpenEmbedded build system. Since you only have the binaries
  1025. and not the source code, you cannot use a typical recipe that expects to
  1026. fetch the source specified in
  1027. :term:`SRC_URI` and then compile it.
  1028. One method is to package the binaries and then install them as part of
  1029. the image. Generally, it is not a good idea to package binaries since,
  1030. among other things, it can hinder the ability to reproduce builds and
  1031. could lead to compatibility problems with ABI in the future. However,
  1032. sometimes you have no choice.
  1033. The easiest solution is to create a recipe that uses the
  1034. :ref:`ref-classes-bin-package` class and to be sure that you are using default
  1035. locations for build artifacts. In most cases, the
  1036. :ref:`ref-classes-bin-package` class handles "skipping" the configure and
  1037. compile steps as well as sets things up to grab packages from the appropriate
  1038. area. In particular, this class sets ``noexec`` on both the
  1039. :ref:`ref-tasks-configure` and :ref:`ref-tasks-compile` tasks, sets
  1040. ``FILES:${PN}`` to "/" so that it picks up all files, and sets up a
  1041. :ref:`ref-tasks-install` task, which effectively copies all files from ``${S}``
  1042. to ``${D}``. The :ref:`ref-classes-bin-package` class works well when the files
  1043. extracted into ``${S}`` are already laid out in the way they should be laid out
  1044. on the target. For more information on these variables, see the :term:`FILES`,
  1045. :term:`PN`, :term:`S`, and :term:`D` variables in the Yocto Project Reference
  1046. Manual's variable glossary.
  1047. .. note::
  1048. - Using :term:`DEPENDS` is a good
  1049. idea even for components distributed in binary form, and is often
  1050. necessary for shared libraries. For a shared library, listing the
  1051. library dependencies in :term:`DEPENDS` makes sure that the libraries
  1052. are available in the staging sysroot when other recipes link
  1053. against the library, which might be necessary for successful
  1054. linking.
  1055. - Using :term:`DEPENDS` also allows runtime dependencies between
  1056. packages to be added automatically. See the
  1057. ":ref:`overview-manual/concepts:automatically added runtime dependencies`"
  1058. section in the Yocto Project Overview and Concepts Manual for more
  1059. information.
  1060. If you cannot use the :ref:`ref-classes-bin-package` class, you need to be sure you are
  1061. doing the following:
  1062. - Create a recipe where the
  1063. :ref:`ref-tasks-configure` and
  1064. :ref:`ref-tasks-compile` tasks do
  1065. nothing: It is usually sufficient to just not define these tasks in
  1066. the recipe, because the default implementations do nothing unless a
  1067. Makefile is found in
  1068. ``${``\ :term:`S`\ ``}``.
  1069. If ``${S}`` might contain a Makefile, or if you inherit some class
  1070. that replaces :ref:`ref-tasks-configure` and :ref:`ref-tasks-compile` with custom
  1071. versions, then you can use the
  1072. ``[``\ :ref:`noexec <bitbake-user-manual/bitbake-user-manual-metadata:variable flags>`\ ``]``
  1073. flag to turn the tasks into no-ops, as follows::
  1074. do_configure[noexec] = "1"
  1075. do_compile[noexec] = "1"
  1076. Unlike :ref:`bitbake-user-manual/bitbake-user-manual-metadata:deleting a task`,
  1077. using the flag preserves the dependency chain from the :ref:`ref-tasks-fetch`,
  1078. :ref:`ref-tasks-unpack`, and :ref:`ref-tasks-patch` tasks to the
  1079. :ref:`ref-tasks-install` task.
  1080. - Make sure your :ref:`ref-tasks-install` task installs the binaries
  1081. appropriately.
  1082. - Ensure that you set up :term:`FILES`
  1083. (usually
  1084. ``FILES:${``\ :term:`PN`\ ``}``) to
  1085. point to the files you have installed, which of course depends on
  1086. where you have installed them and whether those files are in
  1087. different locations than the defaults.
  1088. Following Recipe Style Guidelines
  1089. =================================
  1090. When writing recipes, it is good to conform to existing style guidelines.
  1091. See the ":doc:`../contributor-guide/recipe-style-guide`" in the Yocto Project
  1092. and OpenEmbedded Contributor Guide for reference.
  1093. It is common for existing recipes to deviate a bit from this style.
  1094. However, aiming for at least a consistent style is a good idea. Some
  1095. practices, such as omitting spaces around ``=`` operators in assignments
  1096. or ordering recipe components in an erratic way, are widely seen as poor
  1097. style.
  1098. Recipe Syntax
  1099. =============
  1100. Understanding recipe file syntax is important for writing recipes. The
  1101. following list overviews the basic items that make up a BitBake recipe
  1102. file. For more complete BitBake syntax descriptions, see the
  1103. ":doc:`bitbake:bitbake-user-manual/bitbake-user-manual-metadata`"
  1104. chapter of the BitBake User Manual.
  1105. - *Variable Assignments and Manipulations:* Variable assignments allow
  1106. a value to be assigned to a variable. The assignment can be static
  1107. text or might include the contents of other variables. In addition to
  1108. the assignment, appending and prepending operations are also
  1109. supported.
  1110. The following example shows some of the ways you can use variables in
  1111. recipes::
  1112. S = "${WORKDIR}/postfix-${PV}"
  1113. CFLAGS += "-DNO_ASM"
  1114. CFLAGS:append = " --enable-important-feature"
  1115. - *Functions:* Functions provide a series of actions to be performed.
  1116. You usually use functions to override the default implementation of a
  1117. task function or to complement a default function (i.e. append or
  1118. prepend to an existing function). Standard functions use ``sh`` shell
  1119. syntax, although access to OpenEmbedded variables and internal
  1120. methods are also available.
  1121. Here is an example function from the ``sed`` recipe::
  1122. do_install () {
  1123. autotools_do_install
  1124. install -d ${D}${base_bindir}
  1125. mv ${D}${bindir}/sed ${D}${base_bindir}/sed
  1126. rmdir ${D}${bindir}/
  1127. }
  1128. It is
  1129. also possible to implement new functions that are called between
  1130. existing tasks as long as the new functions are not replacing or
  1131. complementing the default functions. You can implement functions in
  1132. Python instead of shell. Both of these options are not seen in the
  1133. majority of recipes.
  1134. - *Keywords:* BitBake recipes use only a few keywords. You use keywords
  1135. to include common functions (``inherit``), load parts of a recipe
  1136. from other files (``include`` and ``require``) and export variables
  1137. to the environment (``export``).
  1138. The following example shows the use of some of these keywords::
  1139. export POSTCONF = "${STAGING_BINDIR}/postconf"
  1140. inherit autoconf
  1141. require otherfile.inc
  1142. - *Comments (#):* Any lines that begin with the hash character (``#``)
  1143. are treated as comment lines and are ignored::
  1144. # This is a comment
  1145. This next list summarizes the most important and most commonly used
  1146. parts of the recipe syntax. For more information on these parts of the
  1147. syntax, you can reference the
  1148. ":doc:`bitbake:bitbake-user-manual/bitbake-user-manual-metadata`" chapter
  1149. in the BitBake User Manual.
  1150. - *Line Continuation (\\):* Use the backward slash (``\``) character to
  1151. split a statement over multiple lines. Place the slash character at
  1152. the end of the line that is to be continued on the next line::
  1153. VAR = "A really long \
  1154. line"
  1155. .. note::
  1156. You cannot have any characters including spaces or tabs after the
  1157. slash character.
  1158. - *Using Variables (${VARNAME}):* Use the ``${VARNAME}`` syntax to
  1159. access the contents of a variable::
  1160. SRC_URI = "${SOURCEFORGE_MIRROR}/libpng/zlib-${PV}.tar.gz"
  1161. .. note::
  1162. It is important to understand that the value of a variable
  1163. expressed in this form does not get substituted automatically. The
  1164. expansion of these expressions happens on-demand later (e.g.
  1165. usually when a function that makes reference to the variable
  1166. executes). This behavior ensures that the values are most
  1167. appropriate for the context in which they are finally used. On the
  1168. rare occasion that you do need the variable expression to be
  1169. expanded immediately, you can use the
  1170. :=
  1171. operator instead of
  1172. =
  1173. when you make the assignment, but this is not generally needed.
  1174. - *Quote All Assignments ("value"):* Use double quotes around values in
  1175. all variable assignments (e.g. ``"value"``). Here is an example::
  1176. VAR1 = "${OTHERVAR}"
  1177. VAR2 = "The version is ${PV}"
  1178. - *Conditional Assignment (?=):* Conditional assignment is used to
  1179. assign a value to a variable, but only when the variable is currently
  1180. unset. Use the question mark followed by the equal sign (``?=``) to
  1181. make a "soft" assignment used for conditional assignment. Typically,
  1182. "soft" assignments are used in the ``local.conf`` file for variables
  1183. that are allowed to come through from the external environment.
  1184. Here is an example where ``VAR1`` is set to "New value" if it is
  1185. currently empty. However, if ``VAR1`` has already been set, it
  1186. remains unchanged::
  1187. VAR1 ?= "New value"
  1188. In this next example, ``VAR1`` is left with the value "Original value"::
  1189. VAR1 = "Original value"
  1190. VAR1 ?= "New value"
  1191. - *Appending (+=):* Use the plus character followed by the equals sign
  1192. (``+=``) to append values to existing variables.
  1193. .. note::
  1194. This operator adds a space between the existing content of the
  1195. variable and the new content.
  1196. Here is an example::
  1197. SRC_URI += "file://fix-makefile.patch"
  1198. - *Prepending (=+):* Use the equals sign followed by the plus character
  1199. (``=+``) to prepend values to existing variables.
  1200. .. note::
  1201. This operator adds a space between the new content and the
  1202. existing content of the variable.
  1203. Here is an example::
  1204. VAR =+ "Starts"
  1205. - *Appending (:append):* Use the ``:append`` operator to append values
  1206. to existing variables. This operator does not add any additional
  1207. space. Also, the operator is applied after all the ``+=``, and ``=+``
  1208. operators have been applied and after all ``=`` assignments have
  1209. occurred. This means that if ``:append`` is used in a recipe, it can
  1210. only be overridden by another layer using the special ``:remove``
  1211. operator, which in turn will prevent further layers from adding it back.
  1212. The following example shows the space being explicitly added to the
  1213. start to ensure the appended value is not merged with the existing
  1214. value::
  1215. CFLAGS:append = " --enable-important-feature"
  1216. You can also use
  1217. the ``:append`` operator with overrides, which results in the actions
  1218. only being performed for the specified target or machine::
  1219. CFLAGS:append:sh4 = " --enable-important-sh4-specific-feature"
  1220. - *Prepending (:prepend):* Use the ``:prepend`` operator to prepend
  1221. values to existing variables. This operator does not add any
  1222. additional space. Also, the operator is applied after all the ``+=``,
  1223. and ``=+`` operators have been applied and after all ``=``
  1224. assignments have occurred.
  1225. The following example shows the space being explicitly added to the
  1226. end to ensure the prepended value is not merged with the existing
  1227. value::
  1228. CFLAGS:prepend = "-I${S}/myincludes "
  1229. You can also use the
  1230. ``:prepend`` operator with overrides, which results in the actions
  1231. only being performed for the specified target or machine::
  1232. CFLAGS:prepend:sh4 = "-I${S}/myincludes "
  1233. - *Overrides:* You can use overrides to set a value conditionally,
  1234. typically based on how the recipe is being built. For example, to set
  1235. the :term:`KBRANCH` variable's
  1236. value to "standard/base" for any target
  1237. :term:`MACHINE`, except for
  1238. qemuarm where it should be set to "standard/arm-versatile-926ejs",
  1239. you would do the following::
  1240. KBRANCH = "standard/base"
  1241. KBRANCH:qemuarm = "standard/arm-versatile-926ejs"
  1242. Overrides are also used to separate
  1243. alternate values of a variable in other situations. For example, when
  1244. setting variables such as
  1245. :term:`FILES` and
  1246. :term:`RDEPENDS` that are
  1247. specific to individual packages produced by a recipe, you should
  1248. always use an override that specifies the name of the package.
  1249. - *Indentation:* Use spaces for indentation rather than tabs. For
  1250. shell functions, both currently work. However, it is a policy
  1251. decision of the Yocto Project to use tabs in shell functions. Realize
  1252. that some layers have a policy to use spaces for all indentation.
  1253. - *Using Python for Complex Operations:* For more advanced processing,
  1254. it is possible to use Python code during variable assignments (e.g.
  1255. search and replacement on a variable).
  1256. You indicate Python code using the ``${@python_code}`` syntax for the
  1257. variable assignment::
  1258. SRC_URI = "ftp://ftp.info-zip.org/pub/infozip/src/zip${@d.getVar('PV',1).replace('.', '')}.tgz
  1259. - *Shell Function Syntax:* Write shell functions as if you were writing
  1260. a shell script when you describe a list of actions to take. You
  1261. should ensure that your script works with a generic ``sh`` and that
  1262. it does not require any ``bash`` or other shell-specific
  1263. functionality. The same considerations apply to various system
  1264. utilities (e.g. ``sed``, ``grep``, ``awk``, and so forth) that you
  1265. might wish to use. If in doubt, you should check with multiple
  1266. implementations --- including those from BusyBox.