building.rst 38 KB

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