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