layers.rst 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050
  1. .. SPDX-License-Identifier: CC-BY-SA-2.0-UK
  2. Understanding and Creating Layers
  3. *********************************
  4. The OpenEmbedded build system supports organizing
  5. :term:`Metadata` into multiple layers.
  6. Layers allow you to isolate different types of customizations from each
  7. other. For introductory information on the Yocto Project Layer Model,
  8. see the
  9. ":ref:`overview-manual/yp-intro:the yocto project layer model`"
  10. section in the Yocto Project Overview and Concepts Manual.
  11. Creating Your Own Layer
  12. =======================
  13. .. note::
  14. It is very easy to create your own layers to use with the OpenEmbedded
  15. build system, as the Yocto Project ships with tools that speed up creating
  16. layers. This section describes the steps you perform by hand to create
  17. layers so that you can better understand them. For information about the
  18. layer-creation tools, see the
  19. ":ref:`bsp-guide/bsp:creating a new bsp layer using the \`\`bitbake-layers\`\` script`"
  20. section in the Yocto Project Board Support Package (BSP) Developer's
  21. Guide and the ":ref:`dev-manual/layers:creating a general layer using the \`\`bitbake-layers\`\` script`"
  22. section further down in this manual.
  23. Follow these general steps to create your layer without using tools:
  24. #. *Check Existing Layers:* Before creating a new layer, you should be
  25. sure someone has not already created a layer containing the Metadata
  26. you need. You can see the :oe_layerindex:`OpenEmbedded Metadata Index <>`
  27. for a list of layers from the OpenEmbedded community that can be used in
  28. the Yocto Project. You could find a layer that is identical or close
  29. to what you need.
  30. #. *Create a Directory:* Create the directory for your layer. When you
  31. create the layer, be sure to create the directory in an area not
  32. associated with the Yocto Project :term:`Source Directory`
  33. (e.g. the cloned ``poky`` repository).
  34. While not strictly required, prepend the name of the directory with
  35. the string "meta-". For example::
  36. meta-mylayer
  37. meta-GUI_xyz
  38. meta-mymachine
  39. With rare exceptions, a layer's name follows this form::
  40. meta-root_name
  41. Following this layer naming convention can save
  42. you trouble later when tools, components, or variables "assume" your
  43. layer name begins with "meta-". A notable example is in configuration
  44. files as shown in the following step where layer names without the
  45. "meta-" string are appended to several variables used in the
  46. configuration.
  47. #. *Create a Layer Configuration File:* Inside your new layer folder,
  48. you need to create a ``conf/layer.conf`` file. It is easiest to take
  49. an existing layer configuration file and copy that to your layer's
  50. ``conf`` directory and then modify the file as needed.
  51. The ``meta-yocto-bsp/conf/layer.conf`` file in the Yocto Project
  52. :yocto_git:`Source Repositories </poky/tree/meta-yocto-bsp/conf>`
  53. demonstrates the required syntax. For your layer, you need to replace
  54. "yoctobsp" with a unique identifier for your layer (e.g. "machinexyz"
  55. for a layer named "meta-machinexyz")::
  56. # We have a conf and classes directory, add to BBPATH
  57. BBPATH .= ":${LAYERDIR}"
  58. # We have recipes-* directories, add to BBFILES
  59. BBFILES += "${LAYERDIR}/recipes-*/*/*.bb \
  60. ${LAYERDIR}/recipes-*/*/*.bbappend"
  61. BBFILE_COLLECTIONS += "yoctobsp"
  62. BBFILE_PATTERN_yoctobsp = "^${LAYERDIR}/"
  63. BBFILE_PRIORITY_yoctobsp = "5"
  64. LAYERVERSION_yoctobsp = "4"
  65. LAYERSERIES_COMPAT_yoctobsp = "walnascar"
  66. Here is an explanation of the layer configuration file:
  67. - :term:`BBPATH`: Adds the layer's
  68. root directory to BitBake's search path. Through the use of the
  69. :term:`BBPATH` variable, BitBake locates class files (``.bbclass``),
  70. configuration files, and files that are included with ``include``
  71. and ``require`` statements. For these cases, BitBake uses the
  72. first file that matches the name found in :term:`BBPATH`. This is
  73. similar to the way the ``PATH`` variable is used for binaries. It
  74. is recommended, therefore, that you use unique class and
  75. configuration filenames in your custom layer.
  76. - :term:`BBFILES`: Defines the
  77. location for all recipes in the layer.
  78. - :term:`BBFILE_COLLECTIONS`:
  79. Establishes the current layer through a unique identifier that is
  80. used throughout the OpenEmbedded build system to refer to the
  81. layer. In this example, the identifier "yoctobsp" is the
  82. representation for the container layer named "meta-yocto-bsp".
  83. - :term:`BBFILE_PATTERN`:
  84. Expands immediately during parsing to provide the directory of the
  85. layer.
  86. - :term:`BBFILE_PRIORITY`:
  87. Establishes a priority to use for recipes in the layer when the
  88. OpenEmbedded build finds recipes of the same name in different
  89. layers.
  90. - :term:`LAYERVERSION`:
  91. Establishes a version number for the layer. You can use this
  92. version number to specify this exact version of the layer as a
  93. dependency when using the
  94. :term:`LAYERDEPENDS`
  95. variable.
  96. - :term:`LAYERDEPENDS`:
  97. Lists all layers on which this layer depends (if any).
  98. - :term:`LAYERSERIES_COMPAT`:
  99. Lists the :yocto_wiki:`Yocto Project </Releases>`
  100. releases for which the current version is compatible. This
  101. variable is a good way to indicate if your particular layer is
  102. current.
  103. .. note::
  104. A layer does not have to contain only recipes ``.bb`` or append files
  105. ``.bbappend``. Generally, developers create layers using
  106. ``bitbake-layers create-layer``.
  107. See ":ref:`dev-manual/layers:creating a general layer using the \`\`bitbake-layers\`\` script`",
  108. explaining how the ``layer.conf`` file is created from a template located in
  109. ``meta/lib/bblayers/templates/layer.conf``.
  110. In fact, none of the variables set in ``layer.conf`` are mandatory,
  111. except when :term:`BBFILE_COLLECTIONS` is present. In this case
  112. :term:`LAYERSERIES_COMPAT` and :term:`BBFILE_PATTERN` have to be
  113. defined too.
  114. #. *Add Content:* Depending on the type of layer, add the content. If
  115. the layer adds support for a machine, add the machine configuration
  116. in a ``conf/machine/`` file within the layer. If the layer adds
  117. distro policy, add the distro configuration in a ``conf/distro/``
  118. file within the layer. If the layer introduces new recipes, put the
  119. recipes you need in ``recipes-*`` subdirectories within the layer.
  120. .. note::
  121. For an explanation of layer hierarchy that is compliant with the
  122. Yocto Project, see the ":ref:`bsp-guide/bsp:example filesystem layout`"
  123. section in the Yocto Project Board Support Package (BSP) Developer's Guide.
  124. #. *Optionally Test for Compatibility:* If you want permission to use
  125. the Yocto Project Compatibility logo with your layer or application
  126. that uses your layer, perform the steps to apply for compatibility.
  127. See the
  128. ":ref:`dev-manual/layers:making sure your layer is compatible with yocto project`"
  129. section for more information.
  130. Following Best Practices When Creating Layers
  131. =============================================
  132. To create layers that are easier to maintain and that will not impact
  133. builds for other machines, you should consider the information in the
  134. following list:
  135. - *Avoid "Overlaying" Entire Recipes from Other Layers in Your
  136. Configuration:* In other words, do not copy an entire recipe into
  137. your layer and then modify it. Rather, use an append file
  138. (``.bbappend``) to override only those parts of the original recipe
  139. you need to modify.
  140. - *Avoid Duplicating Include Files:* Use append files (``.bbappend``)
  141. for each recipe that uses an include file. Or, if you are introducing
  142. a new recipe that requires the included file, use the path relative
  143. to the original layer directory to refer to the file. For example,
  144. use ``require recipes-core/``\ `package`\ ``/``\ `file`\ ``.inc`` instead
  145. of ``require`` `file`\ ``.inc``. If you're finding you have to overlay
  146. the include file, it could indicate a deficiency in the include file
  147. in the layer to which it originally belongs. If this is the case, you
  148. should try to address that deficiency instead of overlaying the
  149. include file. For example, you could address this by getting the
  150. maintainer of the include file to add a variable or variables to make
  151. it easy to override the parts needing to be overridden.
  152. - *Structure Your Layers:* Proper use of overrides within append files
  153. and placement of machine-specific files within your layer can ensure
  154. that a build is not using the wrong Metadata and negatively impacting
  155. a build for a different machine. Here are some examples:
  156. - *Modify Variables to Support a Different Machine:* Suppose you
  157. have a layer named ``meta-one`` that adds support for building
  158. machine "one". To do so, you use an append file named
  159. ``base-files.bbappend`` and create a dependency on "foo" by
  160. altering the :term:`DEPENDS`
  161. variable::
  162. DEPENDS = "foo"
  163. The dependency is created during any
  164. build that includes the layer ``meta-one``. However, you might not
  165. want this dependency for all machines. For example, suppose you
  166. are building for machine "two" but your ``bblayers.conf`` file has
  167. the ``meta-one`` layer included. During the build, the
  168. ``base-files`` for machine "two" will also have the dependency on
  169. ``foo``.
  170. To make sure your changes apply only when building machine "one",
  171. use a machine override with the :term:`DEPENDS` statement::
  172. DEPENDS:one = "foo"
  173. You should follow the same strategy when using ``:append``
  174. and ``:prepend`` operations::
  175. DEPENDS:append:one = " foo"
  176. DEPENDS:prepend:one = "foo "
  177. As an actual example, here's a
  178. snippet from the generic kernel include file ``linux-yocto.inc``,
  179. wherein the kernel compile and link options are adjusted in the
  180. case of a subset of the supported architectures::
  181. DEPENDS:append:aarch64 = " libgcc"
  182. KERNEL_CC:append:aarch64 = " ${TOOLCHAIN_OPTIONS}"
  183. KERNEL_LD:append:aarch64 = " ${TOOLCHAIN_OPTIONS}"
  184. DEPENDS:append:nios2 = " libgcc"
  185. KERNEL_CC:append:nios2 = " ${TOOLCHAIN_OPTIONS}"
  186. KERNEL_LD:append:nios2 = " ${TOOLCHAIN_OPTIONS}"
  187. DEPENDS:append:arc = " libgcc"
  188. KERNEL_CC:append:arc = " ${TOOLCHAIN_OPTIONS}"
  189. KERNEL_LD:append:arc = " ${TOOLCHAIN_OPTIONS}"
  190. KERNEL_FEATURES:append:qemuall=" features/debug/printk.scc"
  191. - *Place Machine-Specific Files in Machine-Specific Locations:* When
  192. you have a base recipe, such as ``base-files.bb``, that contains a
  193. :term:`SRC_URI` statement to a
  194. file, you can use an append file to cause the build to use your
  195. own version of the file. For example, an append file in your layer
  196. at ``meta-one/recipes-core/base-files/base-files.bbappend`` could
  197. extend :term:`FILESPATH` using :term:`FILESEXTRAPATHS` as follows::
  198. FILESEXTRAPATHS:prepend := "${THISDIR}/${BPN}:"
  199. The build for machine "one" will pick up your machine-specific file as
  200. long as you have the file in
  201. ``meta-one/recipes-core/base-files/base-files/``. However, if you
  202. are building for a different machine and the ``bblayers.conf``
  203. file includes the ``meta-one`` layer and the location of your
  204. machine-specific file is the first location where that file is
  205. found according to :term:`FILESPATH`, builds for all machines will
  206. also use that machine-specific file.
  207. You can make sure that a machine-specific file is used for a
  208. particular machine by putting the file in a subdirectory specific
  209. to the machine. For example, rather than placing the file in
  210. ``meta-one/recipes-core/base-files/base-files/`` as shown above,
  211. put it in ``meta-one/recipes-core/base-files/base-files/one/``.
  212. Not only does this make sure the file is used only when building
  213. for machine "one", but the build process locates the file more
  214. quickly.
  215. In summary, you need to place all files referenced from
  216. :term:`SRC_URI` in a machine-specific subdirectory within the layer in
  217. order to restrict those files to machine-specific builds.
  218. - *Perform Steps to Apply for Yocto Project Compatibility:* If you want
  219. permission to use the Yocto Project Compatibility logo with your
  220. layer or application that uses your layer, perform the steps to apply
  221. for compatibility. See the
  222. ":ref:`dev-manual/layers:making sure your layer is compatible with yocto project`"
  223. section for more information.
  224. - *Follow the Layer Naming Convention:* Store custom layers in a Git
  225. repository that use the ``meta-layer_name`` format.
  226. - *Group Your Layers Locally:* Clone your repository alongside other
  227. cloned ``meta`` directories from the :term:`Source Directory`.
  228. Making Sure Your Layer is Compatible With Yocto Project
  229. =======================================================
  230. When you create a layer used with the Yocto Project, it is advantageous
  231. to make sure that the layer interacts well with existing Yocto Project
  232. layers (i.e. the layer is compatible with the Yocto Project). Ensuring
  233. compatibility makes the layer easy to be consumed by others in the Yocto
  234. Project community and could allow you permission to use the Yocto
  235. Project Compatible Logo.
  236. .. note::
  237. Only Yocto Project member organizations are permitted to use the
  238. Yocto Project Compatible Logo. The logo is not available for general
  239. use. For information on how to become a Yocto Project member
  240. organization, see the :yocto_home:`Yocto Project Website <>`.
  241. The Yocto Project Compatibility Program consists of a layer application
  242. process that requests permission to use the Yocto Project Compatibility
  243. Logo for your layer and application. The process consists of two parts:
  244. #. Successfully passing a script (``yocto-check-layer``) that, when run
  245. against your layer, tests it against constraints based on experiences
  246. of how layers have worked in the real world and where pitfalls have
  247. been found. Getting a "PASS" result from the script is required for
  248. successful compatibility registration.
  249. #. Completion of an application acceptance form, which you can find at
  250. :yocto_home:`/compatible-registration/`.
  251. To be granted permission to use the logo, you need to satisfy the
  252. following:
  253. - Be able to check the box indicating that you got a "PASS" when
  254. running the script against your layer.
  255. - Answer "Yes" to the questions on the form or have an acceptable
  256. explanation for any questions answered "No".
  257. - Be a Yocto Project Member Organization.
  258. The remainder of this section presents information on the registration
  259. form and on the ``yocto-check-layer`` script.
  260. Yocto Project Compatible Program Application
  261. --------------------------------------------
  262. Use the form to apply for your layer's approval. Upon successful
  263. application, you can use the Yocto Project Compatibility Logo with your
  264. layer and the application that uses your layer.
  265. To access the form, use this link:
  266. :yocto_home:`/compatible-registration`.
  267. Follow the instructions on the form to complete your application.
  268. The application consists of the following sections:
  269. - *Contact Information:* Provide your contact information as the fields
  270. require. Along with your information, provide the released versions
  271. of the Yocto Project for which your layer is compatible.
  272. - *Acceptance Criteria:* Provide "Yes" or "No" answers for each of the
  273. items in the checklist. There is space at the bottom of the form for
  274. any explanations for items for which you answered "No".
  275. - *Recommendations:* Provide answers for the questions regarding Linux
  276. kernel use and build success.
  277. ``yocto-check-layer`` Script
  278. ----------------------------
  279. The ``yocto-check-layer`` script provides you a way to assess how
  280. compatible your layer is with the Yocto Project. You should run this
  281. script prior to using the form to apply for compatibility as described
  282. in the previous section. You need to achieve a "PASS" result in order to
  283. have your application form successfully processed.
  284. The script divides tests into three areas: COMMON, BSP, and DISTRO. For
  285. example, given a distribution layer (DISTRO), the layer must pass both
  286. the COMMON and DISTRO related tests. Furthermore, if your layer is a BSP
  287. layer, the layer must pass the COMMON and BSP set of tests.
  288. To execute the script, enter the following commands from your build
  289. directory::
  290. $ source oe-init-build-env
  291. $ yocto-check-layer your_layer_directory
  292. Be sure to provide the actual directory for your
  293. layer as part of the command.
  294. You should pass the dependencies of your layer using the ``--dependency``
  295. argument::
  296. $ source oe-init-build-env
  297. $ yocto-check-layer --dependency <layer1_directory> <layer2_directory> ... -- your_layer_directory
  298. For :term:`BSP <Board Support Package (BSP)>` layers, the ``--machines``
  299. argument should be passed as part of the command::
  300. $ source oe-init-build-env
  301. $ yocto-check-layer --machines <machine1> <machine2> ... -- your_layer_directory
  302. These machines are the ones present in your BSP layer, in the ``conf/machine/``
  303. directory.
  304. Entering the command causes the script to determine the type of layer
  305. and then to execute a set of specific tests against the layer.
  306. The following list overviews the test:
  307. - ``common.test_readme``: Tests if a ``README`` file exists in the
  308. layer and the file is not empty.
  309. - ``common.test_security``: Tests that the layer has a ``SECURITY.md``
  310. (or similar) file, either in the layer itself or at the top of the containing
  311. git repository.
  312. - ``common.test_parse``: Tests to make sure that BitBake can parse the
  313. files without error (i.e. ``bitbake -p``).
  314. - ``common.test_show_environment``: Tests that the global or per-recipe
  315. environment is in order without errors (i.e. ``bitbake -e``).
  316. - ``common.test_world``: Verifies that ``bitbake world`` works.
  317. - ``common.test_world_inherit_class``: Verifies that ``bitbake world`` works
  318. when the :ref:`ref-classes-yocto-check-layer` class is inherited.
  319. - ``common.test_patches_upstream_status``: Verifies that all the patch files
  320. included in the layer contain a
  321. :ref:`contributor-guide/recipe-style-guide:Patch Upstream Status`.
  322. - ``common.test_signatures``: Tests to be sure that BSP and DISTRO
  323. layers do not come with recipes that change signatures.
  324. - ``common.test_layerseries_compat``: Verifies layer compatibility is
  325. set properly.
  326. - ``bsp.test_bsp_defines_machines``: Tests if a BSP layer has machine
  327. configurations.
  328. - ``bsp.test_bsp_no_set_machine``: Tests to ensure a BSP layer does not
  329. set the machine when the layer is added.
  330. - ``bsp.test_machine_world``: Verifies that ``bitbake world`` works
  331. regardless of which machine is selected.
  332. - ``bsp.test_machine_signatures``: Verifies that building for a
  333. particular machine affects only the signature of tasks specific to
  334. that machine.
  335. - ``distro.test_distro_defines_distros``: Tests if a DISTRO layer has
  336. distro configurations.
  337. - ``distro.test_distro_no_set_distros``: Tests to ensure a DISTRO layer
  338. does not set the distribution when the layer is added.
  339. For a complete list of tests, see the :oe_git:`scripts/lib/checklayer/cases
  340. </openembedded-core/tree/scripts/lib/checklayer/cases>` directory in
  341. :term:`OpenEmbedded-Core (OE-Core)`.
  342. Enabling Your Layer
  343. ===================
  344. Before the OpenEmbedded build system can use your new layer, you need to
  345. enable it. To enable your layer, simply add your layer's path to the
  346. :term:`BBLAYERS` variable in your ``conf/bblayers.conf`` file, which is
  347. found in the :term:`Build Directory`. The following example shows how to
  348. enable your new ``meta-mylayer`` layer (note how your new layer exists
  349. outside of the official ``poky`` repository which you would have checked
  350. out earlier)::
  351. # POKY_BBLAYERS_CONF_VERSION is increased each time build/conf/bblayers.conf
  352. # changes incompatibly
  353. POKY_BBLAYERS_CONF_VERSION = "2"
  354. BBPATH = "${TOPDIR}"
  355. BBFILES ?= ""
  356. BBLAYERS ?= " \
  357. /home/user/poky/meta \
  358. /home/user/poky/meta-poky \
  359. /home/user/poky/meta-yocto-bsp \
  360. /home/user/mystuff/meta-mylayer \
  361. "
  362. BitBake parses each ``conf/layer.conf`` file from the top down as
  363. specified in the :term:`BBLAYERS` variable within the ``conf/bblayers.conf``
  364. file. During the processing of each ``conf/layer.conf`` file, BitBake
  365. adds the recipes, classes and configurations contained within the
  366. particular layer to the source directory.
  367. Appending Other Layers Metadata With Your Layer
  368. ===============================================
  369. A recipe that appends Metadata to another recipe is called a BitBake
  370. append file. A BitBake append file uses the ``.bbappend`` file type
  371. suffix, while the corresponding recipe to which Metadata is being
  372. appended uses the ``.bb`` file type suffix.
  373. You can use a ``.bbappend`` file in your layer to make additions or
  374. changes to the content of another layer's recipe without having to copy
  375. the other layer's recipe into your layer. Your ``.bbappend`` file
  376. resides in your layer, while the main ``.bb`` recipe file to which you
  377. are appending Metadata resides in a different layer.
  378. Being able to append information to an existing recipe not only avoids
  379. duplication, but also automatically applies recipe changes from a
  380. different layer into your layer. If you were copying recipes, you would
  381. have to manually merge changes as they occur.
  382. When you create an append file, you must use the same root name as the
  383. corresponding recipe file. For example, the append file
  384. ``someapp_3.1.bbappend`` must apply to ``someapp_3.1.bb``. This
  385. means the original recipe and append filenames are version
  386. number-specific. If the corresponding recipe is renamed to update to a
  387. newer version, you must also rename and possibly update the
  388. corresponding ``.bbappend`` as well.
  389. During the build process, BitBake displays an error on startup if it detects a
  390. ``.bbappend`` file that does not have a corresponding recipe with a matching
  391. name. To handle these errors, the best practice is to rename the ``.bbappend``
  392. to match the original recipe version. This also gives you the opportunity to see
  393. if the ``.bbappend`` is still relevant for the new version of the recipe.
  394. Another method is to use the character ``%`` in the ``.bbappend`` filename. For
  395. example, to append information to every ``6.*`` minor versions of the recipe
  396. ``someapp``, the ``someapp_6.%.bbappend`` file can be created. This way, an
  397. error will only be triggered if the ``someapp`` recipe has a major version
  398. update.
  399. Finally, another method to deal with these errors is to use the variable
  400. :term:`BBMASK`, especially in cases where modifying the ``.bbappend`` is not
  401. possible.
  402. Overlaying a File Using Your Layer
  403. ----------------------------------
  404. As an example, consider the main formfactor recipe and a corresponding
  405. formfactor append file both from the :term:`Source Directory`.
  406. Here is the main
  407. formfactor recipe, which is named ``formfactor_0.0.bb`` and located in
  408. the "meta" layer at ``meta/recipes-bsp/formfactor``::
  409. SUMMARY = "Device formfactor information"
  410. DESCRIPTION = "A formfactor configuration file provides information about the \
  411. target hardware for which the image is being built and information that the \
  412. build system cannot obtain from other sources such as the kernel."
  413. SECTION = "base"
  414. LICENSE = "MIT"
  415. LIC_FILES_CHKSUM = "file://${COREBASE}/meta/COPYING.MIT;md5=3da9cfbcb788c80a0384361b4de20420"
  416. SRC_URI = "file://config file://machconfig"
  417. S = "${UNPACKDIR}"
  418. PACKAGE_ARCH = "${MACHINE_ARCH}"
  419. INHIBIT_DEFAULT_DEPS = "1"
  420. do_install() {
  421. # Install file only if it has contents
  422. install -d ${D}${sysconfdir}/formfactor/
  423. install -m 0644 ${S}/config ${D}${sysconfdir}/formfactor/
  424. if [ -s "${S}/machconfig" ]; then
  425. install -m 0644 ${S}/machconfig ${D}${sysconfdir}/formfactor/
  426. fi
  427. }
  428. In the main recipe, note the :term:`SRC_URI`
  429. variable, which tells the OpenEmbedded build system where to find files
  430. during the build.
  431. Here is the append file, which is named ``formfactor_0.0.bbappend``
  432. and is from the Raspberry Pi BSP Layer named ``meta-raspberrypi``. The
  433. file is in the layer at ``recipes-bsp/formfactor``::
  434. FILESEXTRAPATHS:prepend := "${THISDIR}/${PN}:"
  435. By default, the build system uses the
  436. :term:`FILESPATH` variable to
  437. locate files. This append file extends the locations by setting the
  438. :term:`FILESEXTRAPATHS`
  439. variable. Setting this variable in the ``.bbappend`` file is the most
  440. reliable and recommended method for adding directories to the search
  441. path used by the build system to find files.
  442. The statement in this example extends the directories to include
  443. ``${``\ :term:`THISDIR`\ ``}/${``\ :term:`PN`\ ``}``,
  444. which resolves to a directory named ``formfactor`` in the same directory
  445. in which the append file resides (i.e.
  446. ``meta-raspberrypi/recipes-bsp/formfactor``. This implies that you must
  447. have the supporting directory structure set up that will contain any
  448. files or patches you will be including from the layer.
  449. Using the immediate expansion assignment operator ``:=`` is important
  450. because of the reference to :term:`THISDIR`. The trailing colon character is
  451. important as it ensures that items in the list remain colon-separated.
  452. .. note::
  453. BitBake automatically defines the :term:`THISDIR` variable. You should
  454. never set this variable yourself. Using ":prepend" as part of the
  455. :term:`FILESEXTRAPATHS` ensures your path will be searched prior to other
  456. paths in the final list.
  457. Also, not all append files add extra files. Many append files simply
  458. allow to add build options (e.g. ``systemd``). For these cases, your
  459. append file would not even use the :term:`FILESEXTRAPATHS` statement.
  460. The end result of this ``.bbappend`` file is that on a Raspberry Pi, where
  461. ``rpi`` will exist in the list of :term:`OVERRIDES`, the file
  462. ``meta-raspberrypi/recipes-bsp/formfactor/formfactor/rpi/machconfig`` will be
  463. used during :ref:`ref-tasks-fetch` and the test for a non-zero file size in
  464. :ref:`ref-tasks-install` will return true, and the file will be installed.
  465. Installing Additional Files Using Your Layer
  466. --------------------------------------------
  467. As another example, consider the main ``xserver-xf86-config`` recipe and a
  468. corresponding ``xserver-xf86-config`` append file both from the :term:`Source
  469. Directory`. Here is the main ``xserver-xf86-config`` recipe, which is named
  470. ``xserver-xf86-config_0.1.bb`` and located in the "meta" layer at
  471. ``meta/recipes-graphics/xorg-xserver``::
  472. SUMMARY = "X.Org X server configuration file"
  473. HOMEPAGE = "http://www.x.org"
  474. SECTION = "x11/base"
  475. LICENSE = "MIT"
  476. LIC_FILES_CHKSUM = "file://${COREBASE}/meta/COPYING.MIT;md5=3da9cfbcb788c80a0384361b4de20420"
  477. SRC_URI = "file://xorg.conf"
  478. S = "${UNPACKDIR}"
  479. CONFFILES:${PN} = "${sysconfdir}/X11/xorg.conf"
  480. PACKAGE_ARCH = "${MACHINE_ARCH}"
  481. ALLOW_EMPTY:${PN} = "1"
  482. do_install () {
  483. if test -s ${UNPACKDIR}/xorg.conf; then
  484. install -d ${D}/${sysconfdir}/X11
  485. install -m 0644 ${UNPACKDIR}/xorg.conf ${D}/${sysconfdir}/X11/
  486. fi
  487. }
  488. Here is the append file, which is named ``xserver-xf86-config_%.bbappend``
  489. and is from the Raspberry Pi BSP Layer named ``meta-raspberrypi``. The
  490. file is in the layer at ``recipes-graphics/xorg-xserver``::
  491. FILESEXTRAPATHS:prepend := "${THISDIR}/${PN}:"
  492. SRC_URI:append:rpi = " \
  493. file://xorg.conf.d/98-pitft.conf \
  494. file://xorg.conf.d/99-calibration.conf \
  495. "
  496. do_install:append:rpi () {
  497. PITFT = "${@bb.utils.contains("MACHINE_FEATURES", "pitft", "1", "0", d)}"
  498. if [ "${PITFT}" = "1" ]; then
  499. install -d ${D}/${sysconfdir}/X11/xorg.conf.d/
  500. install -m 0644 ${UNPACKDIR}/xorg.conf.d/98-pitft.conf ${D}/${sysconfdir}/X11/xorg.conf.d/
  501. install -m 0644 ${UNPACKDIR}/xorg.conf.d/99-calibration.conf ${D}/${sysconfdir}/X11/xorg.conf.d/
  502. fi
  503. }
  504. FILES:${PN}:append:rpi = " ${sysconfdir}/X11/xorg.conf.d/*"
  505. Building off of the previous example, we once again are setting the
  506. :term:`FILESEXTRAPATHS` variable. In this case we are also using
  507. :term:`SRC_URI` to list additional source files to use when ``rpi`` is found in
  508. the list of :term:`OVERRIDES`. The :ref:`ref-tasks-install` task will then perform a
  509. check for an additional :term:`MACHINE_FEATURES` that if set will cause these
  510. additional files to be installed. These additional files are listed in
  511. :term:`FILES` so that they will be packaged.
  512. Prioritizing Your Layer
  513. =======================
  514. Each layer is assigned a priority value. Priority values control which
  515. layer takes precedence if there are recipe files with the same name in
  516. multiple layers. For these cases, the recipe file from the layer with a
  517. higher priority number takes precedence. Priority values also affect the
  518. order in which multiple ``.bbappend`` files for the same recipe are
  519. applied. You can either specify the priority manually, or allow the
  520. build system to calculate it based on the layer's dependencies.
  521. To specify the layer's priority manually, use the
  522. :term:`BBFILE_PRIORITY`
  523. variable and append the layer's root name::
  524. BBFILE_PRIORITY_mylayer = "1"
  525. .. note::
  526. It is possible for a recipe with a lower version number
  527. :term:`PV` in a layer that has a higher
  528. priority to take precedence.
  529. Also, the layer priority does not currently affect the precedence
  530. order of ``.conf`` or ``.bbclass`` files. Future versions of BitBake
  531. might address this.
  532. Providing Global-level Configurations With Your Layer
  533. -----------------------------------------------------
  534. When creating a layer, you may need to define configurations that should take
  535. effect globally in your build environment when the layer is part of the build.
  536. The ``layer.conf`` file is a :term:`configuration file` that affects the build
  537. system globally, so it is a candidate for this use-case.
  538. .. warning::
  539. Providing unconditional global level configuration from the ``layer.conf``
  540. file is *not* a good practice, and should be avoided. For this reason, the
  541. section :ref:`ref-conditional-layer-confs` below shows how the ``layer.conf``
  542. file can be used to provide configurations only if a certain condition is
  543. met.
  544. For example, if your layer provides a Linux kernel recipe named
  545. ``linux-custom``, you may want to make :term:`PREFERRED_PROVIDER_virtual/kernel
  546. <PREFERRED_PROVIDER>` point to ``linux-custom``::
  547. PREFERRED_PROVIDER_virtual/kernel = "linux-custom"
  548. This can be defined in the ``layer.conf`` file. If your layer is at the last
  549. position in the :term:`BBLAYERS` list, it will take precedence over previous
  550. ``PREFERRED_PROVIDER_virtual/kernel`` assignments (unless one is set from a
  551. :term:`configuration file` that is parsed later, such as machine or distro
  552. configuration files).
  553. .. _ref-conditional-layer-confs:
  554. Conditionally Provide Global-level Configurations With Your Layer
  555. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  556. In some cases, your layer may provide global configurations only if some
  557. features it provides are enabled. Since the ``layer.conf`` file is parsed at an
  558. earlier stage in the parsing process, the :term:`DISTRO_FEATURES` and
  559. :term:`MACHINE_FEATURES` variables are not yet available to ``layer.conf``, and
  560. declaring conditional assignments based on these variables is not possible. The
  561. following technique shows a way to bypass this limitation by using the
  562. :term:`USER_CLASSES` variable and a conditional ``require`` command.
  563. In the following steps, let's assume our layer is named ``meta-mylayer`` and
  564. that this layer defines a custom :ref:`distro feature <ref-features-distro>`
  565. named ``mylayer-kernel``. We will set the :term:`PREFERRED_PROVIDER` variable
  566. for the kernel only if our feature ``mylayer-kernel`` is part of the
  567. :term:`DISTRO_FEATURES`:
  568. #. Create an include file in the directory
  569. ``meta-mylayer/conf/distro/include/``, for example a file named
  570. ``mylayer-kernel-provider.inc`` that sets the kernel provider to
  571. ``linux-custom``::
  572. PREFERRED_PROVIDER_virtual/kernel = "linux-custom"
  573. #. Provide a path to this include file in your ``layer.conf``::
  574. META_MYLAYER_KERNEL_PROVIDER_PATH = "${LAYERDIR}/conf/distro/include/mylayer-kernel-provider.inc"
  575. #. Create a new class in ``meta-mylayer/classes-global/``, for example a class
  576. ``meta-mylayer-cfg.bbclass``. Make it conditionally require the file
  577. ``mylayer-kernel-provider.inc`` defined above, using the variable
  578. ``META_MYLAYER_KERNEL_PROVIDER_PATH`` defined in ``layer.conf``::
  579. require ${@bb.utils.contains('DISTRO_FEATURES', 'mylayer-kernel', '${META_MYLAYER_KERNEL_PROVIDER_PATH}', '', d)}
  580. For details on the ``bb.utils.contains`` function, see its definition in
  581. :bitbake_git:`lib/bb/utils.py </tree/lib/bb/utils.py>`.
  582. .. note::
  583. The ``require`` command is designed to not fail if the function
  584. ``bb.utils.contains`` returns an empty string.
  585. #. Back to your ``layer.conf`` file, add the class ``meta-mylayer-cfg`` class to
  586. the :term:`USER_CLASSES` variable::
  587. USER_CLASSES:append = " meta-mylayer-cfg"
  588. This will add the class ``meta-mylayer-cfg`` to the list of classes to
  589. globally inherit. Since the ``require`` command is conditional in
  590. ``meta-mylayer-cfg.bbclass``, even though inherited the class will have no
  591. effect unless the feature ``mylayer-kernel`` is enabled through
  592. :term:`DISTRO_FEATURES`.
  593. This technique can also be used for :ref:`Machine features
  594. <ref-features-machine>` by following the same steps. Though not mandatory, it is
  595. recommended to put include files for :term:`DISTRO_FEATURES` in your layer's
  596. ``conf/distro/include`` and the ones for :term:`MACHINE_FEATURES` in your
  597. layer's ``conf/machine/include``.
  598. Managing Layers
  599. ===============
  600. You can use the BitBake layer management tool ``bitbake-layers`` to
  601. provide a view into the structure of recipes across a multi-layer
  602. project. Being able to generate output that reports on configured layers
  603. with their paths and priorities and on ``.bbappend`` files and their
  604. applicable recipes can help to reveal potential problems.
  605. For help on the BitBake layer management tool, use the following
  606. command::
  607. $ bitbake-layers --help
  608. The following list describes the available commands:
  609. - ``help:`` Displays general help or help on a specified command.
  610. - ``show-layers:`` Shows the current configured layers.
  611. - ``show-overlayed:`` Lists overlayed recipes. A recipe is overlayed
  612. when a recipe with the same name exists in another layer that has a
  613. higher layer priority.
  614. - ``show-recipes:`` Lists available recipes and the layers that
  615. provide them.
  616. - ``show-appends:`` Lists ``.bbappend`` files and the recipe files to
  617. which they apply.
  618. - ``show-cross-depends:`` Lists dependency relationships between
  619. recipes that cross layer boundaries.
  620. - ``add-layer:`` Adds a layer to ``bblayers.conf``.
  621. - ``remove-layer:`` Removes a layer from ``bblayers.conf``
  622. - ``flatten:`` Flattens the layer configuration into a separate
  623. output directory. Flattening your layer configuration builds a
  624. "flattened" directory that contains the contents of all layers, with
  625. any overlayed recipes removed and any ``.bbappend`` files appended to
  626. the corresponding recipes. You might have to perform some manual
  627. cleanup of the flattened layer as follows:
  628. - Non-recipe files (such as patches) are overwritten. The flatten
  629. command shows a warning for these files.
  630. - Anything beyond the normal layer setup has been added to the
  631. ``layer.conf`` file. Only the lowest priority layer's
  632. ``layer.conf`` is used.
  633. - Overridden and appended items from ``.bbappend`` files need to be
  634. cleaned up. The contents of each ``.bbappend`` end up in the
  635. flattened recipe. However, if there are appended or changed
  636. variable values, you need to tidy these up yourself. Consider the
  637. following example. Here, the ``bitbake-layers`` command adds the
  638. line ``#### bbappended ...`` so that you know where the following
  639. lines originate::
  640. ...
  641. DESCRIPTION = "A useful utility"
  642. ...
  643. EXTRA_OECONF = "--enable-something"
  644. ...
  645. #### bbappended from meta-anotherlayer ####
  646. DESCRIPTION = "Customized utility"
  647. EXTRA_OECONF += "--enable-somethingelse"
  648. Ideally, you would tidy up these utilities as follows::
  649. ...
  650. DESCRIPTION = "Customized utility"
  651. ...
  652. EXTRA_OECONF = "--enable-something --enable-somethingelse"
  653. ...
  654. - ``layerindex-fetch``: Fetches a layer from a layer index, along
  655. with its dependent layers, and adds the layers to the
  656. ``conf/bblayers.conf`` file.
  657. - ``layerindex-show-depends``: Finds layer dependencies from the
  658. layer index.
  659. - ``save-build-conf``: Saves the currently active build configuration
  660. (``conf/local.conf``, ``conf/bblayers.conf``) as a template into a layer.
  661. This template can later be used for setting up builds via :term:`TEMPLATECONF`.
  662. For information about saving and using configuration templates, see
  663. ":ref:`dev-manual/custom-template-configuration-directory:creating a custom template configuration directory`".
  664. - ``create-layer``: Creates a basic layer.
  665. - ``create-layers-setup``: Writes out a configuration file and/or a script that
  666. can replicate the directory structure and revisions of the layers in a current build.
  667. For more information, see ":ref:`dev-manual/layers:saving and restoring the layers setup`".
  668. Creating a General Layer Using the ``bitbake-layers`` Script
  669. ============================================================
  670. The ``bitbake-layers`` script with the ``create-layer`` subcommand
  671. simplifies creating a new general layer.
  672. .. note::
  673. - For information on BSP layers, see the ":ref:`bsp-guide/bsp:bsp layers`"
  674. section in the Yocto
  675. Project Board Specific (BSP) Developer's Guide.
  676. - In order to use a layer with the OpenEmbedded build system, you
  677. need to add the layer to your ``bblayers.conf`` configuration
  678. file. See the ":ref:`dev-manual/layers:adding a layer using the \`\`bitbake-layers\`\` script`"
  679. section for more information.
  680. The default mode of the script's operation with this subcommand is to
  681. create a layer with the following:
  682. - A layer priority of 6.
  683. - A ``conf`` subdirectory that contains a ``layer.conf`` file.
  684. - A ``recipes-example`` subdirectory that contains a further
  685. subdirectory named ``example``, which contains an ``example.bb``
  686. recipe file.
  687. - A ``COPYING.MIT``, which is the license statement for the layer. The
  688. script assumes you want to use the MIT license, which is typical for
  689. most layers, for the contents of the layer itself.
  690. - A ``README`` file, which is a file describing the contents of your
  691. new layer.
  692. In its simplest form, you can use the following command form to create a
  693. layer. The command creates a layer whose name corresponds to
  694. "your_layer_name" in the current directory::
  695. $ bitbake-layers create-layer your_layer_name
  696. As an example, the following command creates a layer named ``meta-scottrif``
  697. in your home directory::
  698. $ cd /usr/home
  699. $ bitbake-layers create-layer meta-scottrif
  700. NOTE: Starting bitbake server...
  701. Add your new layer with 'bitbake-layers add-layer meta-scottrif'
  702. If you want to set the priority of the layer to other than the default
  703. value of "6", you can either use the ``--priority`` option or you
  704. can edit the
  705. :term:`BBFILE_PRIORITY` value
  706. in the ``conf/layer.conf`` after the script creates it. Furthermore, if
  707. you want to give the example recipe file some name other than the
  708. default, you can use the ``--example-recipe-name`` option.
  709. The easiest way to see how the ``bitbake-layers create-layer`` command
  710. works is to experiment with the script. You can also read the usage
  711. information by entering the following::
  712. $ bitbake-layers create-layer --help
  713. NOTE: Starting bitbake server...
  714. usage: bitbake-layers create-layer [-h] [--priority PRIORITY]
  715. [--example-recipe-name EXAMPLERECIPE]
  716. layerdir
  717. Create a basic layer
  718. positional arguments:
  719. layerdir Layer directory to create
  720. optional arguments:
  721. -h, --help show this help message and exit
  722. --priority PRIORITY, -p PRIORITY
  723. Layer directory to create
  724. --example-recipe-name EXAMPLERECIPE, -e EXAMPLERECIPE
  725. Filename of the example recipe
  726. Adding a Layer Using the ``bitbake-layers`` Script
  727. ==================================================
  728. Once you create your general layer, you must add it to your
  729. ``bblayers.conf`` file. Adding the layer to this configuration file
  730. makes the OpenEmbedded build system aware of your layer so that it can
  731. search it for metadata.
  732. Add your layer by using the ``bitbake-layers add-layer`` command::
  733. $ bitbake-layers add-layer your_layer_name
  734. Here is an example that adds a
  735. layer named ``meta-scottrif`` to the configuration file. Following the
  736. command that adds the layer is another ``bitbake-layers`` command that
  737. shows the layers that are in your ``bblayers.conf`` file::
  738. $ bitbake-layers add-layer meta-scottrif
  739. NOTE: Starting bitbake server...
  740. Parsing recipes: 100% |##########################################################| Time: 0:00:49
  741. Parsing of 1441 .bb files complete (0 cached, 1441 parsed). 2055 targets, 56 skipped, 0 masked, 0 errors.
  742. $ bitbake-layers show-layers
  743. NOTE: Starting bitbake server...
  744. layer path priority
  745. ==========================================================================
  746. meta /home/scottrif/poky/meta 5
  747. meta-poky /home/scottrif/poky/meta-poky 5
  748. meta-yocto-bsp /home/scottrif/poky/meta-yocto-bsp 5
  749. workspace /home/scottrif/poky/build/workspace 99
  750. meta-scottrif /home/scottrif/poky/build/meta-scottrif 6
  751. Adding the layer to this file
  752. enables the build system to locate the layer during the build.
  753. .. note::
  754. During a build, the OpenEmbedded build system looks in the layers
  755. from the top of the list down to the bottom in that order.
  756. Saving and restoring the layers setup
  757. =====================================
  758. Once you have a working build with the correct set of layers, it is beneficial
  759. to capture the layer setup --- what they are, which repositories they come from
  760. and which SCM revisions they're at --- into a configuration file, so that this
  761. setup can be easily replicated later, perhaps on a different machine. Here's
  762. how to do this::
  763. $ bitbake-layers create-layers-setup /srv/work/alex/meta-alex/
  764. NOTE: Starting bitbake server...
  765. NOTE: Created /srv/work/alex/meta-alex/setup-layers.json
  766. NOTE: Created /srv/work/alex/meta-alex/setup-layers
  767. The tool needs a single argument which tells where to place the output, consisting
  768. of a json formatted layer configuration, and a ``setup-layers`` script that can use that configuration
  769. to restore the layers in a different location, or on a different host machine. The argument
  770. can point to a custom layer (which is then deemed a "bootstrap" layer that needs to be
  771. checked out first), or into a completely independent location.
  772. The replication of the layers is performed by running the ``setup-layers`` script provided
  773. above:
  774. #. Clone the bootstrap layer or some other repository to obtain
  775. the json config and the setup script that can use it.
  776. #. Run the script directly with no options::
  777. alex@Zen2:/srv/work/alex/my-build$ meta-alex/setup-layers
  778. Note: not checking out source meta-alex, use --force-bootstraplayer-checkout to override.
  779. Setting up source meta-intel, revision 15.0-hardknott-3.3-310-g0a96edae, branch master
  780. Running 'git init -q /srv/work/alex/my-build/meta-intel'
  781. Running 'git remote remove origin > /dev/null 2>&1; git remote add origin git://git.yoctoproject.org/meta-intel' in /srv/work/alex/my-build/meta-intel
  782. Running 'git fetch -q origin || true' in /srv/work/alex/my-build/meta-intel
  783. Running 'git checkout -q 0a96edae609a3f48befac36af82cf1eed6786b4a' in /srv/work/alex/my-build/meta-intel
  784. Setting up source poky, revision 4.1_M1-372-g55483d28f2, branch akanavin/setup-layers
  785. Running 'git init -q /srv/work/alex/my-build/poky'
  786. Running 'git remote remove origin > /dev/null 2>&1; git remote add origin git://git.yoctoproject.org/poky' in /srv/work/alex/my-build/poky
  787. Running 'git fetch -q origin || true' in /srv/work/alex/my-build/poky
  788. Running 'git remote remove poky-contrib > /dev/null 2>&1; git remote add poky-contrib ssh://git@push.yoctoproject.org/poky-contrib' in /srv/work/alex/my-build/poky
  789. Running 'git fetch -q poky-contrib || true' in /srv/work/alex/my-build/poky
  790. Running 'git checkout -q 11db0390b02acac1324e0f827beb0e2e3d0d1d63' in /srv/work/alex/my-build/poky
  791. .. note::
  792. This will work to update an existing checkout as well.
  793. .. note::
  794. The script is self-sufficient and requires only python3
  795. and git on the build machine.
  796. .. note::
  797. Both the ``create-layers-setup`` and the ``setup-layers`` provided several additional options
  798. that customize their behavior - you are welcome to study them via ``--help`` command line parameter.