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