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