building.rst 40 KB

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