building.rst 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024
  1. .. SPDX-License-Identifier: CC-BY-SA-2.0-UK
  2. Building
  3. ********
  4. This section describes various build procedures, such as the steps
  5. needed for a simple build, building a target for multiple configurations,
  6. generating an image for more than one machine, and so forth.
  7. Building a Simple Image
  8. =======================
  9. In the development environment, you need to build an image whenever you
  10. change hardware support, add or change system libraries, or add or
  11. change services that have dependencies. There are several methods that allow
  12. you to build an image within the Yocto Project. This section presents
  13. the basic steps you need to build a simple image using BitBake from a
  14. build host running Linux.
  15. .. note::
  16. - For information on how to build an image using
  17. :term:`Toaster`, see the
  18. :doc:`/toaster-manual/index`.
  19. - For information on how to use ``devtool`` to build images, see the
  20. ":ref:`sdk-manual/extensible:using \`\`devtool\`\` in your sdk workflow`"
  21. section in the Yocto Project Application Development and the
  22. Extensible Software Development Kit (eSDK) manual.
  23. - For a quick example on how to build an image using the
  24. OpenEmbedded build system, see the
  25. :doc:`/brief-yoctoprojectqs/index` document.
  26. - You can also use the `Yocto Project BitBake
  27. <https://marketplace.visualstudio.com/items?itemName=yocto-project.yocto-bitbake>`__
  28. extension for Visual Studio Code to build images.
  29. The build process creates an entire Linux distribution from source and
  30. places it in your :term:`Build Directory` under ``tmp/deploy/images``. For
  31. detailed information on the build process using BitBake, see the
  32. ":ref:`overview-manual/concepts:images`" section in the Yocto Project Overview
  33. and Concepts Manual.
  34. The following figure and list overviews the build process:
  35. .. image:: figures/bitbake-build-flow.png
  36. :width: 100%
  37. #. *Set up Your Host Development System to Support Development Using the
  38. Yocto Project*: See the ":doc:`start`" section for options on how to get a
  39. build host ready to use the Yocto Project.
  40. #. *Initialize the Build Environment:* Initialize the build environment
  41. by sourcing the build environment script (i.e.
  42. :ref:`structure-core-script`)::
  43. $ source oe-init-build-env [build_dir]
  44. When you use the initialization script, the OpenEmbedded build system
  45. uses ``build`` as the default :term:`Build Directory` in your current work
  46. directory. You can use a `build_dir` argument with the script to
  47. specify a different :term:`Build Directory`.
  48. .. note::
  49. A common practice is to use a different :term:`Build Directory` for
  50. different targets; for example, ``~/build/x86`` for a ``qemux86``
  51. target, and ``~/build/arm`` for a ``qemuarm`` target. In any
  52. event, it's typically cleaner to locate the :term:`Build Directory`
  53. somewhere outside of your source directory.
  54. #. *Make Sure Your* ``local.conf`` *File is Correct*: Ensure the
  55. ``conf/local.conf`` configuration file, which is found in the
  56. :term:`Build Directory`, is set up how you want it. This file defines many
  57. aspects of the build environment including the target machine architecture
  58. through the :term:`MACHINE` variable, the packaging format used during
  59. the build (:term:`PACKAGE_CLASSES`), and a centralized tarball download
  60. directory through the :term:`DL_DIR` variable.
  61. #. *Build the Image:* Build the image using the ``bitbake`` command::
  62. $ bitbake target
  63. .. note::
  64. For information on BitBake, see the :doc:`bitbake:index`.
  65. The target is the name of the recipe you want to build. Common
  66. targets are the images in ``meta/recipes-core/images``,
  67. ``meta/recipes-sato/images``, and so forth all found in the
  68. :term:`Source Directory`. Alternatively, the target
  69. can be the name of a recipe for a specific piece of software such as
  70. BusyBox. For more details about the images the OpenEmbedded build
  71. system supports, see the
  72. ":ref:`ref-manual/images:Images`" chapter in the Yocto
  73. Project Reference Manual.
  74. As an example, the following command builds the
  75. ``core-image-minimal`` image::
  76. $ bitbake core-image-minimal
  77. Once an
  78. image has been built, it often needs to be installed. The images and
  79. kernels built by the OpenEmbedded build system are placed in the
  80. :term:`Build Directory` in ``tmp/deploy/images``. For information on how to
  81. run pre-built images such as ``qemux86`` and ``qemuarm``, see the
  82. :doc:`/sdk-manual/index` manual. For
  83. information about how to install these images, see the documentation
  84. for your particular board or machine.
  85. Building Images for Multiple Targets Using Multiple Configurations
  86. ==================================================================
  87. You can use a single ``bitbake`` command to build multiple images or
  88. packages for different targets where each image or package requires a
  89. different configuration (multiple configuration builds). The builds, in
  90. this scenario, are sometimes referred to as "multiconfigs", and this
  91. section uses that term throughout.
  92. This section describes how to set up for multiple configuration builds
  93. and how to account for cross-build dependencies between the
  94. multiconfigs.
  95. Setting Up and Running a Multiple Configuration Build
  96. -----------------------------------------------------
  97. To accomplish a multiple configuration build, you must define each
  98. target's configuration separately using a parallel configuration file in
  99. the :term:`Build Directory` or configuration directory within a layer, and you
  100. must follow a required file hierarchy. Additionally, you must enable the
  101. multiple configuration builds in your ``local.conf`` file.
  102. Follow these steps to set up and execute multiple configuration builds:
  103. - *Create Separate Configuration Files*: You need to create a single
  104. configuration file for each build target (each multiconfig).
  105. The configuration definitions are implementation dependent but often
  106. each configuration file will define the machine and the
  107. temporary directory BitBake uses for the build. Whether the same
  108. temporary directory (:term:`TMPDIR`) can be shared will depend on what is
  109. similar and what is different between the configurations. Multiple MACHINE
  110. targets can share the same (:term:`TMPDIR`) as long as the rest of the
  111. configuration is the same, multiple :term:`DISTRO` settings would need separate
  112. (:term:`TMPDIR`) directories.
  113. For example, consider a scenario with two different multiconfigs for the same
  114. :term:`MACHINE`: "qemux86" built
  115. for two distributions such as "poky" and "poky-lsb". In this case,
  116. you would need to use the different :term:`TMPDIR`.
  117. Here is an example showing the minimal statements needed in a
  118. configuration file for a "qemux86" target whose temporary build
  119. directory is ``tmpmultix86``::
  120. MACHINE = "qemux86"
  121. TMPDIR = "${TOPDIR}/tmpmultix86"
  122. The location for these multiconfig configuration files is specific.
  123. They must reside in the current :term:`Build Directory` in a sub-directory of
  124. ``conf`` named ``multiconfig`` or within a layer's ``conf`` directory
  125. under a directory named ``multiconfig``. Here is an example that defines
  126. two configuration files for the "x86" and "arm" multiconfigs:
  127. .. image:: figures/multiconfig_files.png
  128. :align: center
  129. :width: 50%
  130. The usual :term:`BBPATH` search path is used to locate multiconfig files in
  131. a similar way to other conf files.
  132. - *Add the BitBake Multi-configuration Variable to the Local
  133. Configuration File*: Use the
  134. :term:`BBMULTICONFIG`
  135. variable in your ``conf/local.conf`` configuration file to specify
  136. each multiconfig. Continuing with the example from the previous
  137. figure, the :term:`BBMULTICONFIG` variable needs to enable two
  138. multiconfigs: "x86" and "arm" by specifying each configuration file::
  139. BBMULTICONFIG = "x86 arm"
  140. .. note::
  141. A "default" configuration already exists by definition. This
  142. configuration is named: "" (i.e. empty string) and is defined by
  143. the variables coming from your ``local.conf``
  144. file. Consequently, the previous example actually adds two
  145. additional configurations to your build: "arm" and "x86" along
  146. with "".
  147. - *Launch BitBake*: Use the following BitBake command form to launch
  148. the multiple configuration build::
  149. $ bitbake [mc:multiconfigname:]target [[[mc:multiconfigname:]target] ... ]
  150. For the example in this section, the following command applies::
  151. $ bitbake mc:x86:core-image-minimal mc:arm:core-image-sato mc::core-image-base
  152. The previous BitBake command builds a ``core-image-minimal`` image
  153. that is configured through the ``x86.conf`` configuration file, a
  154. ``core-image-sato`` image that is configured through the ``arm.conf``
  155. configuration file and a ``core-image-base`` that is configured
  156. through your ``local.conf`` configuration file.
  157. .. note::
  158. Support for multiple configuration builds in the Yocto Project &DISTRO;
  159. (&DISTRO_NAME;) Release does not include Shared State (sstate)
  160. optimizations. Consequently, if a build uses the same object twice
  161. in, for example, two different :term:`TMPDIR`
  162. directories, the build either loads from an existing sstate cache for
  163. that build at the start or builds the object fresh.
  164. Enabling Multiple Configuration Build Dependencies
  165. --------------------------------------------------
  166. Sometimes dependencies can exist between targets (multiconfigs) in a
  167. multiple configuration build. For example, suppose that in order to
  168. build a ``core-image-sato`` image for an "x86" multiconfig, the root
  169. filesystem of an "arm" multiconfig must exist. This dependency is
  170. essentially that the
  171. :ref:`ref-tasks-image` task in the
  172. ``core-image-sato`` recipe depends on the completion of the
  173. :ref:`ref-tasks-rootfs` task of the
  174. ``core-image-minimal`` recipe.
  175. To enable dependencies in a multiple configuration build, you must
  176. declare the dependencies in the recipe using the following statement
  177. form::
  178. task_or_package[mcdepends] = "mc:from_multiconfig:to_multiconfig:recipe_name:task_on_which_to_depend"
  179. To better show how to use this statement, consider the example scenario
  180. from the first paragraph of this section. The following statement needs
  181. to be added to the recipe that builds the ``core-image-sato`` image::
  182. do_image[mcdepends] = "mc:x86:arm:core-image-minimal:do_rootfs"
  183. In this example, the `from_multiconfig` is "x86". The `to_multiconfig` is "arm". The
  184. task on which the :ref:`ref-tasks-image` task in the recipe depends is the
  185. :ref:`ref-tasks-rootfs` task from the ``core-image-minimal`` recipe associated
  186. with the "arm" multiconfig.
  187. Once you set up this dependency, you can build the "x86" multiconfig
  188. using a BitBake command as follows::
  189. $ bitbake mc:x86:core-image-sato
  190. This command executes all the tasks needed to create the
  191. ``core-image-sato`` image for the "x86" multiconfig. Because of the
  192. dependency, BitBake also executes through the :ref:`ref-tasks-rootfs` task for the
  193. "arm" multiconfig build.
  194. Having a recipe depend on the root filesystem of another build might not
  195. seem that useful. Consider this change to the statement in the
  196. ``core-image-sato`` recipe::
  197. do_image[mcdepends] = "mc:x86:arm:core-image-minimal:do_image"
  198. In this case, BitBake must
  199. create the ``core-image-minimal`` image for the "arm" build since the
  200. "x86" build depends on it.
  201. Because "x86" and "arm" are enabled for multiple configuration builds
  202. and have separate configuration files, BitBake places the artifacts for
  203. each build in the respective temporary build directories (i.e.
  204. :term:`TMPDIR`).
  205. Building an Initial RAM Filesystem (Initramfs) Image
  206. ====================================================
  207. An initial RAM filesystem (:term:`Initramfs`) image provides a temporary root
  208. filesystem used for early system initialization, typically providing tools and
  209. loading modules needed to locate and mount the final root filesystem.
  210. Follow these steps to create an :term:`Initramfs` image:
  211. #. *Create the Initramfs Image Recipe:* You can reference the
  212. ``core-image-minimal-initramfs.bb`` recipe found in the
  213. ``meta/recipes-core`` directory of the :term:`Source Directory`
  214. as an example from which to work. The ``core-image-minimal-initramfs`` recipe
  215. is based on the :ref:`initramfs-framework <dev-manual/building:Customizing an
  216. Initramfs using \`\`initramfs-framework\`\`>` recipe described below.
  217. #. *Decide if You Need to Bundle the Initramfs Image Into the Kernel
  218. Image:* If you want the :term:`Initramfs` image that is built to be bundled
  219. in with the kernel image, set the :term:`INITRAMFS_IMAGE_BUNDLE`
  220. variable to ``"1"`` in your ``local.conf`` configuration file and set the
  221. :term:`INITRAMFS_IMAGE` variable in the recipe that builds the kernel image.
  222. Setting the :term:`INITRAMFS_IMAGE_BUNDLE` flag causes the :term:`Initramfs`
  223. image to be unpacked into the ``${B}/usr/`` directory. The unpacked
  224. :term:`Initramfs` image is then passed to the kernel's ``Makefile`` using the
  225. :term:`CONFIG_INITRAMFS_SOURCE` variable, allowing the :term:`Initramfs`
  226. image to be built into the kernel normally.
  227. #. *Optionally Add Items to the Initramfs Image Through the Initramfs
  228. Image Recipe:* If you add items to the :term:`Initramfs` image by way of its
  229. recipe, you should use :term:`PACKAGE_INSTALL` rather than
  230. :term:`IMAGE_INSTALL`. :term:`PACKAGE_INSTALL` gives more direct control of
  231. what is added to the image as compared to the defaults you might not
  232. necessarily want that are set by the :ref:`ref-classes-image`
  233. or :ref:`ref-classes-core-image` classes.
  234. #. *Build the Kernel Image and the Initramfs Image:* Build your kernel
  235. image using BitBake. Because the :term:`Initramfs` image recipe is a
  236. dependency of the kernel image, the :term:`Initramfs` image is built as well
  237. and bundled with the kernel image if you used the
  238. :term:`INITRAMFS_IMAGE_BUNDLE` variable described earlier.
  239. Customizing an Initramfs using ``initramfs-framework``
  240. ------------------------------------------------------
  241. The ``core-image-minimal-initramfs.bb`` recipe found in
  242. :oe_git:`meta/recipes-core/images
  243. </openembedded-core/tree/meta/recipes-core/images>` uses the
  244. :oe_git:`initramfs-framework_1.0.bb
  245. </openembedded-core/tree/meta/recipes-core/initrdscripts/initramfs-framework_1.0.bb>`
  246. recipe as its base component. The goal of the ``initramfs-framework`` recipe is
  247. to provide the building blocks to build a customized :term:`Initramfs`.
  248. The ``initramfs-framework`` recipe relies on shell initialization scripts
  249. defined in :oe_git:`meta/recipes-core/initrdscripts/initramfs-framework
  250. </openembedded-core/tree/meta/recipes-core/initrdscripts/initramfs-framework>`. Since some of
  251. these scripts do not apply for all use cases, the ``initramfs-framework`` recipe
  252. defines different packages:
  253. - ``initramfs-framework-base``: this package installs the basic components of
  254. an :term:`Initramfs`, such as the ``init`` script or the ``/dev/console``
  255. character special file. As this package is a runtime dependency of all
  256. modules listed below, it is automatically pulled in when one of the modules
  257. is installed in the image.
  258. - ``initramfs-module-exec``: support for execution of applications.
  259. - ``initramfs-module-mdev``: support for `mdev
  260. <https://wiki.gentoo.org/wiki/Mdev>`__.
  261. - ``initramfs-module-udev``: support for :wikipedia:`Udev <Udev>`.
  262. - ``initramfs-module-e2fs``: support for :wikipedia:`ext4/ext3/ext2
  263. <Extended_file_system>` filesystems.
  264. - ``initramfs-module-nfsrootfs``: support for locating and mounting the root
  265. partition via :wikipedia:`NFS <Network_File_System>`.
  266. - ``initramfs-module-rootfs``: support for locating and mounting the root
  267. partition.
  268. - ``initramfs-module-debug``: dynamic debug support.
  269. - ``initramfs-module-lvm``: :wikipedia:`LVM <Logical_volume_management>` rootfs support.
  270. - ``initramfs-module-overlayroot``: support for mounting a read-write overlay
  271. on top of a read-only root filesystem.
  272. In addition to the packages defined by the ``initramfs-framework`` recipe
  273. itself, the following packages are defined by the recipes present in
  274. :oe_git:`meta/recipes-core/initrdscripts </openembedded-core/tree/meta/recipes-core/initrdscripts>`:
  275. - ``initramfs-module-install``: module to create and install a partition layout
  276. on a selected block device.
  277. - ``initramfs-module-install-efi``: module to create and install an EFI
  278. partition layout on a selected block device.
  279. - ``initramfs-module-setup-live``: module to start a shell in the
  280. :term:`Initramfs` if ``root=/dev/ram0`` in passed in the `Kernel command-line
  281. <https://www.kernel.org/doc/html/latest/admin-guide/kernel-parameters.html>`__
  282. or the ``root=`` parameter was not passed.
  283. To customize the :term:`Initramfs`, you can add or remove packages listed
  284. earlier from the :term:`PACKAGE_INSTALL` variable with a :ref:`bbappend
  285. <dev-manual/layers:Appending Other Layers Metadata With Your Layer>` on the
  286. ``core-image-minimal-initramfs`` recipe, or create a custom recipe for the
  287. :term:`Initramfs` taking ``core-image-minimal-initramfs`` as example.
  288. Custom scripts can be added to the :term:`Initramfs` by writing your own
  289. recipes. The recipes are conventionally named ``initramfs-module-<module name>``
  290. where ``<module name>`` is the name of the module. The recipe should set its
  291. :term:`RDEPENDS` package-specific variables to include
  292. ``initramfs-framework-base`` and the other packages on which the module depends
  293. at runtime.
  294. The recipe must install shell initialization scripts in :term:`${D} <D>`\
  295. ``/init.d`` and must follow the ``<number>-<script name>`` naming scheme where:
  296. - ``<number>`` is a *two-digit* number that affects the execution order of the
  297. script compared to others. For example, the script ``80-setup-live`` would be
  298. executed after ``01-udev`` because 80 is greater than 01.
  299. This number being two-digits is important here as the scripts are executed
  300. alphabetically. For example, the script ``10-script`` would be executed
  301. before the script ``8-script``, because ``1`` is inferior to ``8``.
  302. Therefore, the script should be named ``08-script``.
  303. - ``<script name>`` is the script name which you can choose freely.
  304. If two script use the same ``<number>``, they are sorted alphabetically based
  305. on ``<script name>``.
  306. Bundling an Initramfs Image From a Separate Multiconfig
  307. -------------------------------------------------------
  308. There may be a case where we want to build an :term:`Initramfs` image which does not
  309. inherit the same distro policy as our main image, for example, we may want
  310. our main image to use ``TCLIBC="glibc"``, but to use ``TCLIBC="musl"`` in our :term:`Initramfs`
  311. image to keep a smaller footprint. However, by performing the steps mentioned
  312. above the :term:`Initramfs` image will inherit ``TCLIBC="glibc"`` without allowing us
  313. to override it.
  314. To achieve this, you need to perform some additional steps:
  315. #. *Create a multiconfig for your Initramfs image:* You can perform the steps
  316. on ":ref:`dev-manual/building:building images for multiple targets using multiple configurations`" to create a separate multiconfig.
  317. For the sake of simplicity let's assume such multiconfig is called: ``initramfscfg.conf`` and
  318. contains the variables::
  319. TMPDIR="${TOPDIR}/tmp-initramfscfg"
  320. TCLIBC="musl"
  321. #. *Set additional Initramfs variables on your main configuration:*
  322. Additionally, on your main configuration (``local.conf``) you need to set the
  323. variables::
  324. INITRAMFS_MULTICONFIG = "initramfscfg"
  325. INITRAMFS_DEPLOY_DIR_IMAGE = "${TOPDIR}/tmp-initramfscfg/deploy/images/${MACHINE}"
  326. The variables :term:`INITRAMFS_MULTICONFIG` and :term:`INITRAMFS_DEPLOY_DIR_IMAGE`
  327. are used to create a multiconfig dependency from the kernel to the :term:`INITRAMFS_IMAGE`
  328. to be built coming from the ``initramfscfg`` multiconfig, and to let the
  329. buildsystem know where the :term:`INITRAMFS_IMAGE` will be located.
  330. Building a system with such configuration will build the kernel using the
  331. main configuration but the :ref:`ref-tasks-bundle_initramfs` task will grab the
  332. selected :term:`INITRAMFS_IMAGE` from :term:`INITRAMFS_DEPLOY_DIR_IMAGE`
  333. instead, resulting in a musl based :term:`Initramfs` image bundled in the kernel
  334. but a glibc based main image.
  335. The same is applicable to avoid inheriting :term:`DISTRO_FEATURES` on :term:`INITRAMFS_IMAGE`
  336. or to build a different :term:`DISTRO` for it such as ``poky-tiny``.
  337. Building a Tiny System
  338. ======================
  339. Very small distributions have some significant advantages such as
  340. requiring less on-die or in-package memory (cheaper), better performance
  341. through efficient cache usage, lower power requirements due to less
  342. memory, faster boot times, and reduced development overhead. Some
  343. real-world examples where a very small distribution gives you distinct
  344. advantages are digital cameras, medical devices, and small headless
  345. systems.
  346. This section presents information that shows you how you can trim your
  347. distribution to even smaller sizes than the ``poky-tiny`` distribution,
  348. which is around 5 Mbytes, that can be built out-of-the-box using the
  349. Yocto Project.
  350. Tiny System Overview
  351. --------------------
  352. The following list presents the overall steps you need to consider and
  353. perform to create distributions with smaller root filesystems, achieve
  354. faster boot times, maintain your critical functionality, and avoid
  355. initial RAM disks:
  356. - :ref:`Determine your goals and guiding principles
  357. <dev-manual/building:goals and guiding principles>`
  358. - :ref:`dev-manual/building:understand what contributes to your image size`
  359. - :ref:`Reduce the size of the root filesystem
  360. <dev-manual/building:trim the root filesystem>`
  361. - :ref:`Reduce the size of the kernel <dev-manual/building:trim the kernel>`
  362. - :ref:`dev-manual/building:remove package management requirements`
  363. - :ref:`dev-manual/building:look for other ways to minimize size`
  364. - :ref:`dev-manual/building:iterate on the process`
  365. Goals and Guiding Principles
  366. ----------------------------
  367. Before you can reach your destination, you need to know where you are
  368. going. Here is an example list that you can use as a guide when creating
  369. very small distributions:
  370. - Determine how much space you need (e.g. a kernel that is 1 Mbyte or
  371. less and a root filesystem that is 3 Mbytes or less).
  372. - Find the areas that are currently taking 90% of the space and
  373. concentrate on reducing those areas.
  374. - Do not create any difficult "hacks" to achieve your goals.
  375. - Leverage the device-specific options.
  376. - Work in a separate layer so that you keep changes isolated. For
  377. information on how to create layers, see the
  378. ":ref:`dev-manual/layers:understanding and creating layers`" section.
  379. Understand What Contributes to Your Image Size
  380. ----------------------------------------------
  381. It is easiest to have something to start with when creating your own
  382. distribution. You can use the Yocto Project out-of-the-box to create the
  383. ``poky-tiny`` distribution. Ultimately, you will want to make changes in
  384. your own distribution that are likely modeled after ``poky-tiny``.
  385. .. note::
  386. To use ``poky-tiny`` in your build, set the :term:`DISTRO` variable in your
  387. ``local.conf`` file to "poky-tiny" as described in the
  388. ":ref:`dev-manual/custom-distribution:creating your own distribution`"
  389. section.
  390. Understanding some memory concepts will help you reduce the system size.
  391. Memory consists of static, dynamic, and temporary memory. Static memory
  392. is the TEXT (code), DATA (initialized data in the code), and BSS
  393. (uninitialized data) sections. Dynamic memory represents memory that is
  394. allocated at runtime: stacks, hash tables, and so forth. Temporary
  395. memory is recovered after the boot process. This memory consists of
  396. memory used for decompressing the kernel and for the ``__init__``
  397. functions.
  398. To help you see where you currently are with kernel and root filesystem
  399. sizes, you can use two tools found in the :term:`Source Directory`
  400. in the
  401. ``scripts/tiny/`` directory:
  402. - ``ksize.py``: Reports component sizes for the kernel build objects.
  403. - ``dirsize.py``: Reports component sizes for the root filesystem.
  404. This next tool and command help you organize configuration fragments and
  405. view file dependencies in a human-readable form:
  406. - ``merge_config.sh``: Helps you manage configuration files and
  407. fragments within the kernel. With this tool, you can merge individual
  408. configuration fragments together. The tool allows you to make
  409. overrides and warns you of any missing configuration options. The
  410. tool is ideal for allowing you to iterate on configurations, create
  411. minimal configurations, and create configuration files for different
  412. machines without having to duplicate your process.
  413. The ``merge_config.sh`` script is part of the Linux Yocto kernel Git
  414. repositories (i.e. ``linux-yocto-3.14``, ``linux-yocto-3.10``,
  415. ``linux-yocto-3.8``, and so forth) in the ``scripts/kconfig``
  416. directory.
  417. For more information on configuration fragments, see the
  418. ":ref:`kernel-dev/common:creating configuration fragments`"
  419. section in the Yocto Project Linux Kernel Development Manual.
  420. - ``bitbake -u taskexp -g bitbake_target``: Using the BitBake command
  421. with these options brings up a Dependency Explorer from which you can
  422. view file dependencies. Understanding these dependencies allows you
  423. to make informed decisions when cutting out various pieces of the
  424. kernel and root filesystem.
  425. Trim the Root Filesystem
  426. ------------------------
  427. The root filesystem is made up of packages for booting, libraries, and
  428. applications. To change things, you can configure how the packaging
  429. happens, which changes the way you build them. You can also modify the
  430. filesystem itself or select a different filesystem.
  431. First, find out what is hogging your root filesystem by running the
  432. ``dirsize.py`` script from your root directory::
  433. $ cd root-directory-of-image
  434. $ dirsize.py 100000 > dirsize-100k.log
  435. $ cat dirsize-100k.log
  436. You can apply a filter to the script to ignore files
  437. under a certain size. The previous example filters out any files below
  438. 100 Kbytes. The sizes reported by the tool are uncompressed, and thus
  439. will be smaller by a relatively constant factor in a compressed root
  440. filesystem. When you examine your log file, you can focus on areas of
  441. the root filesystem that take up large amounts of memory.
  442. You need to be sure that what you eliminate does not cripple the
  443. functionality you need. One way to see how packages relate to each other
  444. is by using the Dependency Explorer UI with the BitBake command::
  445. $ cd image-directory
  446. $ bitbake -u taskexp -g image
  447. Use the interface to
  448. select potential packages you wish to eliminate and see their dependency
  449. relationships.
  450. When deciding how to reduce the size, get rid of packages that result in
  451. minimal impact on the feature set. For example, you might not need a VGA
  452. display. Or, you might be able to get by with ``devtmpfs`` and ``mdev``
  453. instead of ``udev``.
  454. Use your ``local.conf`` file to make changes. For example, to eliminate
  455. ``udev`` and ``glib``, set the following in the local configuration
  456. file::
  457. VIRTUAL-RUNTIME_dev_manager = ""
  458. Finally, you should consider exactly the type of root filesystem you
  459. need to meet your needs while also reducing its size. For example,
  460. consider ``cramfs``, ``squashfs``, ``ubifs``, ``ext2``, or an
  461. :term:`Initramfs` using ``initramfs``. Be aware that ``ext3`` requires a 1
  462. Mbyte journal. If you are okay with running read-only, you do not need
  463. this journal.
  464. .. note::
  465. After each round of elimination, you need to rebuild your system and
  466. then use the tools to see the effects of your reductions.
  467. Trim the Kernel
  468. ---------------
  469. The kernel is built by including policies for hardware-independent
  470. aspects. What subsystems do you enable? For what architecture are you
  471. building? Which drivers do you build by default?
  472. .. note::
  473. You can modify the kernel source if you want to help with boot time.
  474. Run the ``ksize.py`` script from the top-level Linux build directory to
  475. get an idea of what is making up the kernel::
  476. $ cd top-level-linux-build-directory
  477. $ ksize.py > ksize.log
  478. $ cat ksize.log
  479. When you examine the log, you will see how much space is taken up with
  480. the built-in ``.o`` files for drivers, networking, core kernel files,
  481. filesystem, sound, and so forth. The sizes reported by the tool are
  482. uncompressed, and thus will be smaller by a relatively constant factor
  483. in a compressed kernel image. Look to reduce the areas that are large
  484. and taking up around the "90% rule."
  485. To examine, or drill down, into any particular area, use the ``-d``
  486. option with the script::
  487. $ ksize.py -d > ksize.log
  488. Using this option
  489. breaks out the individual file information for each area of the kernel
  490. (e.g. drivers, networking, and so forth).
  491. Use your log file to see what you can eliminate from the kernel based on
  492. features you can let go. For example, if you are not going to need
  493. sound, you do not need any drivers that support sound.
  494. After figuring out what to eliminate, you need to reconfigure the kernel
  495. to reflect those changes during the next build. You could run
  496. ``menuconfig`` and make all your changes at once. However, that makes it
  497. difficult to see the effects of your individual eliminations and also
  498. makes it difficult to replicate the changes for perhaps another target
  499. device. A better method is to start with no configurations using
  500. ``allnoconfig``, create configuration fragments for individual changes,
  501. and then manage the fragments into a single configuration file using
  502. ``merge_config.sh``. The tool makes it easy for you to iterate using the
  503. configuration change and build cycle.
  504. Each time you make configuration changes, you need to rebuild the kernel
  505. and check to see what impact your changes had on the overall size.
  506. Remove Package Management Requirements
  507. --------------------------------------
  508. Packaging requirements add size to the image. One way to reduce the size
  509. of the image is to remove all the packaging requirements from the image.
  510. This reduction includes both removing the package manager and its unique
  511. dependencies as well as removing the package management data itself.
  512. To eliminate all the packaging requirements for an image, be sure that
  513. "package-management" is not part of your
  514. :term:`IMAGE_FEATURES`
  515. statement for the image. When you remove this feature, you are removing
  516. the package manager as well as its dependencies from the root
  517. filesystem.
  518. Look for Other Ways to Minimize Size
  519. ------------------------------------
  520. Depending on your particular circumstances, other areas that you can
  521. trim likely exist. The key to finding these areas is through tools and
  522. methods described here combined with experimentation and iteration. Here
  523. are a couple of areas to experiment with:
  524. - ``glibc``: In general, follow this process:
  525. #. Remove ``glibc`` features from
  526. :term:`DISTRO_FEATURES`
  527. that you think you do not need.
  528. #. Build your distribution.
  529. #. If the build fails due to missing symbols in a package, determine
  530. if you can reconfigure the package to not need those features. For
  531. example, change the configuration to not support wide character
  532. support as is done for ``ncurses``. Or, if support for those
  533. characters is needed, determine what ``glibc`` features provide
  534. the support and restore the configuration.
  535. 4. Rebuild and repeat the process.
  536. - ``busybox``: For BusyBox, use a process similar as described for
  537. ``glibc``. A difference is you will need to boot the resulting system
  538. to see if you are able to do everything you expect from the running
  539. system. You need to be sure to integrate configuration fragments into
  540. Busybox because BusyBox handles its own core features and then allows
  541. you to add configuration fragments on top.
  542. Iterate on the Process
  543. ----------------------
  544. If you have not reached your goals on system size, you need to iterate
  545. on the process. The process is the same. Use the tools and see just what
  546. is taking up 90% of the root filesystem and the kernel. Decide what you
  547. can eliminate without limiting your device beyond what you need.
  548. Depending on your system, a good place to look might be Busybox, which
  549. provides a stripped down version of Unix tools in a single, executable
  550. file. You might be able to drop virtual terminal services or perhaps
  551. ipv6.
  552. Building Images for More than One Machine
  553. =========================================
  554. A common scenario developers face is creating images for several
  555. different machines that use the same software environment. In this
  556. situation, it is tempting to set the tunings and optimization flags for
  557. each build specifically for the targeted hardware (i.e. "maxing out" the
  558. tunings). Doing so can considerably add to build times and package feed
  559. maintenance collectively for the machines. For example, selecting tunes
  560. that are extremely specific to a CPU core used in a system might enable
  561. some micro optimizations in GCC for that particular system but would
  562. otherwise not gain you much of a performance difference across the other
  563. systems as compared to using a more general tuning across all the builds
  564. (e.g. setting :term:`DEFAULTTUNE`
  565. specifically for each machine's build). Rather than "max out" each
  566. build's tunings, you can take steps that cause the OpenEmbedded build
  567. system to reuse software across the various machines where it makes
  568. sense.
  569. If build speed and package feed maintenance are considerations, you
  570. should consider the points in this section that can help you optimize
  571. your tunings to best consider build times and package feed maintenance.
  572. - *Share the :term:`Build Directory`:* If at all possible, share the
  573. :term:`TMPDIR` across builds. The Yocto Project supports switching between
  574. different :term:`MACHINE` values in the same :term:`TMPDIR`. This practice
  575. is well supported and regularly used by developers when building for
  576. multiple machines. When you use the same :term:`TMPDIR` for multiple
  577. machine builds, the OpenEmbedded build system can reuse the existing native
  578. and often cross-recipes for multiple machines. Thus, build time decreases.
  579. .. note::
  580. If :term:`DISTRO` settings change or fundamental configuration settings
  581. such as the filesystem layout, you need to work with a clean :term:`TMPDIR`.
  582. Sharing :term:`TMPDIR` under these circumstances might work but since it is
  583. not guaranteed, you should use a clean :term:`TMPDIR`.
  584. - *Enable the Appropriate Package Architecture:* By default, the
  585. OpenEmbedded build system enables three levels of package
  586. architectures: "all", "tune" or "package", and "machine". Any given
  587. recipe usually selects one of these package architectures (types) for
  588. its output. Depending for what a given recipe creates packages,
  589. making sure you enable the appropriate package architecture can
  590. directly impact the build time.
  591. A recipe that just generates scripts can enable "all" architecture
  592. because there are no binaries to build. To specifically enable "all"
  593. architecture, be sure your recipe inherits the
  594. :ref:`ref-classes-allarch` class.
  595. This class is useful for "all" architectures because it configures
  596. many variables so packages can be used across multiple architectures.
  597. If your recipe needs to generate packages that are machine-specific
  598. or when one of the build or runtime dependencies is already
  599. machine-architecture dependent, which makes your recipe also
  600. machine-architecture dependent, make sure your recipe enables the
  601. "machine" package architecture through the
  602. :term:`MACHINE_ARCH`
  603. variable::
  604. PACKAGE_ARCH = "${MACHINE_ARCH}"
  605. When you do not
  606. specifically enable a package architecture through the
  607. :term:`PACKAGE_ARCH`, The
  608. OpenEmbedded build system defaults to the
  609. :term:`TUNE_PKGARCH` setting::
  610. PACKAGE_ARCH = "${TUNE_PKGARCH}"
  611. - *Choose a Generic Tuning File if Possible:* Some tunes are more
  612. generic and can run on multiple targets (e.g. an ``armv5`` set of
  613. packages could run on ``armv6`` and ``armv7`` processors in most
  614. cases). Similarly, ``i486`` binaries could work on ``i586`` and
  615. higher processors. You should realize, however, that advances on
  616. newer processor versions would not be used.
  617. If you select the same tune for several different machines, the
  618. OpenEmbedded build system reuses software previously built, thus
  619. speeding up the overall build time. Realize that even though a new
  620. sysroot for each machine is generated, the software is not recompiled
  621. and only one package feed exists.
  622. - *Manage Granular Level Packaging:* Sometimes there are cases where
  623. injecting another level of package architecture beyond the three
  624. higher levels noted earlier can be useful. For example, consider how
  625. NXP (formerly Freescale) allows for the easy reuse of binary packages
  626. in their layer
  627. :yocto_git:`meta-freescale </meta-freescale/>`.
  628. In this example, the
  629. :yocto_git:`fsl-dynamic-packagearch </meta-freescale/tree/classes/fsl-dynamic-packagearch.bbclass>`
  630. class shares GPU packages for i.MX53 boards because all boards share
  631. the AMD GPU. The i.MX6-based boards can do the same because all
  632. boards share the Vivante GPU. This class inspects the BitBake
  633. datastore to identify if the package provides or depends on one of
  634. the sub-architecture values. If so, the class sets the
  635. :term:`PACKAGE_ARCH` value
  636. based on the ``MACHINE_SUBARCH`` value. If the package does not
  637. provide or depend on one of the sub-architecture values but it
  638. matches a value in the machine-specific filter, it sets
  639. :term:`MACHINE_ARCH`. This
  640. behavior reduces the number of packages built and saves build time by
  641. reusing binaries.
  642. - *Use Tools to Debug Issues:* Sometimes you can run into situations
  643. where software is being rebuilt when you think it should not be. For
  644. example, the OpenEmbedded build system might not be using shared
  645. state between machines when you think it should be. These types of
  646. situations are usually due to references to machine-specific
  647. variables such as :term:`MACHINE`,
  648. :term:`SERIAL_CONSOLES`,
  649. :term:`XSERVER`,
  650. :term:`MACHINE_FEATURES`,
  651. and so forth in code that is supposed to only be tune-specific or
  652. when the recipe depends
  653. (:term:`DEPENDS`,
  654. :term:`RDEPENDS`,
  655. :term:`RRECOMMENDS`,
  656. :term:`RSUGGESTS`, and so forth)
  657. on some other recipe that already has
  658. :term:`PACKAGE_ARCH` defined
  659. as "${MACHINE_ARCH}".
  660. .. note::
  661. Patches to fix any issues identified are most welcome as these
  662. issues occasionally do occur.
  663. For such cases, you can use some tools to help you sort out the
  664. situation:
  665. - ``state-diff-machines.sh``*:* You can find this tool in the
  666. ``scripts`` directory of the Source Repositories. See the comments
  667. in the script for information on how to use the tool.
  668. - *BitBake's "-S printdiff" Option:* Using this option causes
  669. BitBake to try to establish the most recent signature match
  670. (e.g. in the shared state cache) and then compare matched signatures
  671. to determine the stamps and delta where these two stamp trees diverge.
  672. Building Software from an External Source
  673. =========================================
  674. By default, the OpenEmbedded build system uses the :term:`Build Directory`
  675. when building source code. The build process involves fetching the source
  676. files, unpacking them, and then patching them if necessary before the build
  677. takes place.
  678. There are situations where you might want to build software from source
  679. files that are external to and thus outside of the OpenEmbedded build
  680. system. For example, suppose you have a project that includes a new BSP
  681. with a heavily customized kernel. And, you want to minimize exposing the
  682. build system to the development team so that they can focus on their
  683. project and maintain everyone's workflow as much as possible. In this
  684. case, you want a kernel source directory on the development machine
  685. where the development occurs. You want the recipe's
  686. :term:`SRC_URI` variable to point to
  687. the external directory and use it as is, not copy it.
  688. To build from software that comes from an external source, all you need to do
  689. is inherit the :ref:`ref-classes-externalsrc` class and then set
  690. the :term:`EXTERNALSRC` variable to point to your external source code. Here
  691. are the statements to put in your ``local.conf`` file::
  692. INHERIT += "externalsrc"
  693. EXTERNALSRC:pn-myrecipe = "path-to-your-source-tree"
  694. This next example shows how to accomplish the same thing by setting
  695. :term:`EXTERNALSRC` in the recipe itself or in the recipe's append file::
  696. EXTERNALSRC = "path"
  697. EXTERNALSRC_BUILD = "path"
  698. .. note::
  699. In order for these settings to take effect, you must globally or
  700. locally inherit the :ref:`ref-classes-externalsrc` class.
  701. By default, :ref:`ref-classes-externalsrc` builds the source code in a
  702. directory separate from the external source directory as specified by
  703. :term:`EXTERNALSRC`. If you need
  704. to have the source built in the same directory in which it resides, or
  705. some other nominated directory, you can set
  706. :term:`EXTERNALSRC_BUILD`
  707. to point to that directory::
  708. EXTERNALSRC_BUILD:pn-myrecipe = "path-to-your-source-tree"
  709. Replicating a Build Offline
  710. ===========================
  711. It can be useful to take a "snapshot" of upstream sources used in a
  712. build and then use that "snapshot" later to replicate the build offline.
  713. To do so, you need to first prepare and populate your downloads
  714. directory your "snapshot" of files. Once your downloads directory is
  715. ready, you can use it at any time and from any machine to replicate your
  716. build.
  717. Follow these steps to populate your Downloads directory:
  718. #. *Create a Clean Downloads Directory:* Start with an empty downloads
  719. directory (:term:`DL_DIR`). You
  720. start with an empty downloads directory by either removing the files
  721. in the existing directory or by setting :term:`DL_DIR` to point to either
  722. an empty location or one that does not yet exist.
  723. #. *Generate Tarballs of the Source Git Repositories:* Edit your
  724. ``local.conf`` configuration file as follows::
  725. DL_DIR = "/home/your-download-dir/"
  726. BB_GENERATE_MIRROR_TARBALLS = "1"
  727. During
  728. the fetch process in the next step, BitBake gathers the source files
  729. and creates tarballs in the directory pointed to by :term:`DL_DIR`. See
  730. the
  731. :term:`BB_GENERATE_MIRROR_TARBALLS`
  732. variable for more information.
  733. #. *Populate Your Downloads Directory Without Building:* Use BitBake to
  734. fetch your sources but inhibit the build::
  735. $ bitbake target --runonly=fetch
  736. The downloads directory (i.e. ``${DL_DIR}``) now has
  737. a "snapshot" of the source files in the form of tarballs, which can
  738. be used for the build.
  739. #. *Optionally Remove Any Git or other SCM Subdirectories From the
  740. Downloads Directory:* If you want, you can clean up your downloads
  741. directory by removing any Git or other Source Control Management
  742. (SCM) subdirectories such as ``${DL_DIR}/git2/*``. The tarballs
  743. already contain these subdirectories.
  744. Once your downloads directory has everything it needs regarding source
  745. files, you can create your "own-mirror" and build your target.
  746. Understand that you can use the files to build the target offline from
  747. any machine and at any time.
  748. Follow these steps to build your target using the files in the downloads
  749. directory:
  750. #. *Using Local Files Only:* Inside your ``local.conf`` file, add the
  751. :term:`SOURCE_MIRROR_URL` variable, inherit the
  752. :ref:`ref-classes-own-mirrors` class, and use the
  753. :term:`BB_NO_NETWORK` variable to your ``local.conf``::
  754. SOURCE_MIRROR_URL ?= "file:///home/your-download-dir/"
  755. INHERIT += "own-mirrors"
  756. BB_NO_NETWORK = "1"
  757. The :term:`SOURCE_MIRROR_URL` and :ref:`ref-classes-own-mirrors`
  758. class set up the system to use the downloads directory as your "own
  759. mirror". Using the :term:`BB_NO_NETWORK` variable makes sure that
  760. BitBake's fetching process in step 3 stays local, which means files
  761. from your "own-mirror" are used.
  762. #. *Start With a Clean Build:* You can start with a clean build by
  763. removing the ``${``\ :term:`TMPDIR`\ ``}`` directory or using a new
  764. :term:`Build Directory`.
  765. #. *Build Your Target:* Use BitBake to build your target::
  766. $ bitbake target
  767. The build completes using the known local "snapshot" of source
  768. files from your mirror. The resulting tarballs for your "snapshot" of
  769. source files are in the downloads directory.
  770. .. note::
  771. The offline build does not work if recipes attempt to find the
  772. latest version of software by setting
  773. :term:`SRCREV` to
  774. ``${``\ :term:`AUTOREV`\ ``}``::
  775. SRCREV = "${AUTOREV}"
  776. When a recipe sets :term:`SRCREV` to
  777. ``${``\ :term:`AUTOREV`\ ``}``, the build system accesses the network in an
  778. attempt to determine the latest version of software from the SCM.
  779. Typically, recipes that use :term:`AUTOREV` are custom or modified
  780. recipes. Recipes that reside in public repositories usually do not
  781. use :term:`AUTOREV`.
  782. If you do have recipes that use :term:`AUTOREV`, you can take steps to
  783. still use the recipes in an offline build. Do the following:
  784. #. Use a configuration generated by enabling :ref:`build
  785. history <dev-manual/build-quality:maintaining build output quality>`.
  786. #. Use the ``buildhistory-collect-srcrevs`` command to collect the
  787. stored :term:`SRCREV` values from the build's history. For more
  788. information on collecting these values, see the
  789. ":ref:`dev-manual/build-quality:build history package information`"
  790. section.
  791. #. Once you have the correct source revisions, you can modify
  792. those recipes to set :term:`SRCREV` to specific versions of the
  793. software.