layers.rst 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021
  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 = "dunfell"
  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. Entering the command causes the script to determine the type of layer
  295. and then to execute a set of specific tests against the layer. The
  296. following list overviews the test:
  297. - ``common.test_readme``: Tests if a ``README`` file exists in the
  298. layer and the file is not empty.
  299. - ``common.test_parse``: Tests to make sure that BitBake can parse the
  300. files without error (i.e. ``bitbake -p``).
  301. - ``common.test_show_environment``: Tests that the global or per-recipe
  302. environment is in order without errors (i.e. ``bitbake -e``).
  303. - ``common.test_world``: Verifies that ``bitbake world`` works.
  304. - ``common.test_signatures``: Tests to be sure that BSP and DISTRO
  305. layers do not come with recipes that change signatures.
  306. - ``common.test_layerseries_compat``: Verifies layer compatibility is
  307. set properly.
  308. - ``bsp.test_bsp_defines_machines``: Tests if a BSP layer has machine
  309. configurations.
  310. - ``bsp.test_bsp_no_set_machine``: Tests to ensure a BSP layer does not
  311. set the machine when the layer is added.
  312. - ``bsp.test_machine_world``: Verifies that ``bitbake world`` works
  313. regardless of which machine is selected.
  314. - ``bsp.test_machine_signatures``: Verifies that building for a
  315. particular machine affects only the signature of tasks specific to
  316. that machine.
  317. - ``distro.test_distro_defines_distros``: Tests if a DISTRO layer has
  318. distro configurations.
  319. - ``distro.test_distro_no_set_distros``: Tests to ensure a DISTRO layer
  320. does not set the distribution when the layer is added.
  321. Enabling Your Layer
  322. ===================
  323. Before the OpenEmbedded build system can use your new layer, you need to
  324. enable it. To enable your layer, simply add your layer's path to the
  325. :term:`BBLAYERS` variable in your ``conf/bblayers.conf`` file, which is
  326. found in the :term:`Build Directory`. The following example shows how to
  327. enable your new ``meta-mylayer`` layer (note how your new layer exists
  328. outside of the official ``poky`` repository which you would have checked
  329. out earlier)::
  330. # POKY_BBLAYERS_CONF_VERSION is increased each time build/conf/bblayers.conf
  331. # changes incompatibly
  332. POKY_BBLAYERS_CONF_VERSION = "2"
  333. BBPATH = "${TOPDIR}"
  334. BBFILES ?= ""
  335. BBLAYERS ?= " \
  336. /home/user/poky/meta \
  337. /home/user/poky/meta-poky \
  338. /home/user/poky/meta-yocto-bsp \
  339. /home/user/mystuff/meta-mylayer \
  340. "
  341. BitBake parses each ``conf/layer.conf`` file from the top down as
  342. specified in the :term:`BBLAYERS` variable within the ``conf/bblayers.conf``
  343. file. During the processing of each ``conf/layer.conf`` file, BitBake
  344. adds the recipes, classes and configurations contained within the
  345. particular layer to the source directory.
  346. Appending Other Layers Metadata With Your Layer
  347. ===============================================
  348. A recipe that appends Metadata to another recipe is called a BitBake
  349. append file. A BitBake append file uses the ``.bbappend`` file type
  350. suffix, while the corresponding recipe to which Metadata is being
  351. appended uses the ``.bb`` file type suffix.
  352. You can use a ``.bbappend`` file in your layer to make additions or
  353. changes to the content of another layer's recipe without having to copy
  354. the other layer's recipe into your layer. Your ``.bbappend`` file
  355. resides in your layer, while the main ``.bb`` recipe file to which you
  356. are appending Metadata resides in a different layer.
  357. Being able to append information to an existing recipe not only avoids
  358. duplication, but also automatically applies recipe changes from a
  359. different layer into your layer. If you were copying recipes, you would
  360. have to manually merge changes as they occur.
  361. When you create an append file, you must use the same root name as the
  362. corresponding recipe file. For example, the append file
  363. ``someapp_3.1.bbappend`` must apply to ``someapp_3.1.bb``. This
  364. means the original recipe and append filenames are version
  365. number-specific. If the corresponding recipe is renamed to update to a
  366. newer version, you must also rename and possibly update the
  367. corresponding ``.bbappend`` as well.
  368. During the build process, BitBake displays an error on startup if it detects a
  369. ``.bbappend`` file that does not have a corresponding recipe with a matching
  370. name. To handle these errors, the best practice is to rename the ``.bbappend``
  371. to match the original recipe version. This also gives you the opportunity to see
  372. if the ``.bbappend`` is still relevant for the new version of the recipe.
  373. Another method it to use the character ``%`` in the ``.bbappend`` filename. For
  374. example, to append information to every ``6.*`` minor versions of the recipe
  375. ``someapp``, the ``someapp_6.%.bbappend`` file can be created. This way, an
  376. error will only be triggered if the ``someapp`` recipe has a major version
  377. update.
  378. Finally, another method to deal with these errors is to use the variable
  379. :term:`BBMASK`, especially in cases where modifying the ``.bbappend`` is not
  380. possible.
  381. Overlaying a File Using Your Layer
  382. ----------------------------------
  383. As an example, consider the main formfactor recipe and a corresponding
  384. formfactor append file both from the :term:`Source Directory`.
  385. Here is the main
  386. formfactor recipe, which is named ``formfactor_0.0.bb`` and located in
  387. the "meta" layer at ``meta/recipes-bsp/formfactor``::
  388. SUMMARY = "Device formfactor information"
  389. DESCRIPTION = "A formfactor configuration file provides information about the \
  390. target hardware for which the image is being built and information that the \
  391. build system cannot obtain from other sources such as the kernel."
  392. SECTION = "base"
  393. LICENSE = "MIT"
  394. LIC_FILES_CHKSUM = "file://${COREBASE}/meta/COPYING.MIT;md5=3da9cfbcb788c80a0384361b4de20420"
  395. PR = "r45"
  396. SRC_URI = "file://config file://machconfig"
  397. S = "${WORKDIR}"
  398. PACKAGE_ARCH = "${MACHINE_ARCH}"
  399. INHIBIT_DEFAULT_DEPS = "1"
  400. do_install() {
  401. # Install file only if it has contents
  402. install -d ${D}${sysconfdir}/formfactor/
  403. install -m 0644 ${S}/config ${D}${sysconfdir}/formfactor/
  404. if [ -s "${S}/machconfig" ]; then
  405. install -m 0644 ${S}/machconfig ${D}${sysconfdir}/formfactor/
  406. fi
  407. }
  408. In the main recipe, note the :term:`SRC_URI`
  409. variable, which tells the OpenEmbedded build system where to find files
  410. during the build.
  411. Here is the append file, which is named ``formfactor_0.0.bbappend``
  412. and is from the Raspberry Pi BSP Layer named ``meta-raspberrypi``. The
  413. file is in the layer at ``recipes-bsp/formfactor``::
  414. FILESEXTRAPATHS:prepend := "${THISDIR}/${PN}:"
  415. By default, the build system uses the
  416. :term:`FILESPATH` variable to
  417. locate files. This append file extends the locations by setting the
  418. :term:`FILESEXTRAPATHS`
  419. variable. Setting this variable in the ``.bbappend`` file is the most
  420. reliable and recommended method for adding directories to the search
  421. path used by the build system to find files.
  422. The statement in this example extends the directories to include
  423. ``${``\ :term:`THISDIR`\ ``}/${``\ :term:`PN`\ ``}``,
  424. which resolves to a directory named ``formfactor`` in the same directory
  425. in which the append file resides (i.e.
  426. ``meta-raspberrypi/recipes-bsp/formfactor``. This implies that you must
  427. have the supporting directory structure set up that will contain any
  428. files or patches you will be including from the layer.
  429. Using the immediate expansion assignment operator ``:=`` is important
  430. because of the reference to :term:`THISDIR`. The trailing colon character is
  431. important as it ensures that items in the list remain colon-separated.
  432. .. note::
  433. BitBake automatically defines the :term:`THISDIR` variable. You should
  434. never set this variable yourself. Using ":prepend" as part of the
  435. :term:`FILESEXTRAPATHS` ensures your path will be searched prior to other
  436. paths in the final list.
  437. Also, not all append files add extra files. Many append files simply
  438. allow to add build options (e.g. ``systemd``). For these cases, your
  439. append file would not even use the :term:`FILESEXTRAPATHS` statement.
  440. The end result of this ``.bbappend`` file is that on a Raspberry Pi, where
  441. ``rpi`` will exist in the list of :term:`OVERRIDES`, the file
  442. ``meta-raspberrypi/recipes-bsp/formfactor/formfactor/rpi/machconfig`` will be
  443. used during :ref:`ref-tasks-fetch` and the test for a non-zero file size in
  444. :ref:`ref-tasks-install` will return true, and the file will be installed.
  445. Installing Additional Files Using Your Layer
  446. --------------------------------------------
  447. As another example, consider the main ``xserver-xf86-config`` recipe and a
  448. corresponding ``xserver-xf86-config`` append file both from the :term:`Source
  449. Directory`. Here is the main ``xserver-xf86-config`` recipe, which is named
  450. ``xserver-xf86-config_0.1.bb`` and located in the "meta" layer at
  451. ``meta/recipes-graphics/xorg-xserver``::
  452. SUMMARY = "X.Org X server configuration file"
  453. HOMEPAGE = "http://www.x.org"
  454. SECTION = "x11/base"
  455. LICENSE = "MIT"
  456. LIC_FILES_CHKSUM = "file://${COREBASE}/meta/COPYING.MIT;md5=3da9cfbcb788c80a0384361b4de20420"
  457. PR = "r33"
  458. SRC_URI = "file://xorg.conf"
  459. S = "${WORKDIR}"
  460. CONFFILES:${PN} = "${sysconfdir}/X11/xorg.conf"
  461. PACKAGE_ARCH = "${MACHINE_ARCH}"
  462. ALLOW_EMPTY:${PN} = "1"
  463. do_install () {
  464. if test -s ${WORKDIR}/xorg.conf; then
  465. install -d ${D}/${sysconfdir}/X11
  466. install -m 0644 ${WORKDIR}/xorg.conf ${D}/${sysconfdir}/X11/
  467. fi
  468. }
  469. Here is the append file, which is named ``xserver-xf86-config_%.bbappend``
  470. and is from the Raspberry Pi BSP Layer named ``meta-raspberrypi``. The
  471. file is in the layer at ``recipes-graphics/xorg-xserver``::
  472. FILESEXTRAPATHS:prepend := "${THISDIR}/${PN}:"
  473. SRC_URI:append:rpi = " \
  474. file://xorg.conf.d/98-pitft.conf \
  475. file://xorg.conf.d/99-calibration.conf \
  476. "
  477. do_install:append:rpi () {
  478. PITFT="${@bb.utils.contains("MACHINE_FEATURES", "pitft", "1", "0", d)}"
  479. if [ "${PITFT}" = "1" ]; then
  480. install -d ${D}/${sysconfdir}/X11/xorg.conf.d/
  481. install -m 0644 ${WORKDIR}/xorg.conf.d/98-pitft.conf ${D}/${sysconfdir}/X11/xorg.conf.d/
  482. install -m 0644 ${WORKDIR}/xorg.conf.d/99-calibration.conf ${D}/${sysconfdir}/X11/xorg.conf.d/
  483. fi
  484. }
  485. FILES:${PN}:append:rpi = " ${sysconfdir}/X11/xorg.conf.d/*"
  486. Building off of the previous example, we once again are setting the
  487. :term:`FILESEXTRAPATHS` variable. In this case we are also using
  488. :term:`SRC_URI` to list additional source files to use when ``rpi`` is found in
  489. the list of :term:`OVERRIDES`. The :ref:`ref-tasks-install` task will then perform a
  490. check for an additional :term:`MACHINE_FEATURES` that if set will cause these
  491. additional files to be installed. These additional files are listed in
  492. :term:`FILES` so that they will be packaged.
  493. Prioritizing Your Layer
  494. =======================
  495. Each layer is assigned a priority value. Priority values control which
  496. layer takes precedence if there are recipe files with the same name in
  497. multiple layers. For these cases, the recipe file from the layer with a
  498. higher priority number takes precedence. Priority values also affect the
  499. order in which multiple ``.bbappend`` files for the same recipe are
  500. applied. You can either specify the priority manually, or allow the
  501. build system to calculate it based on the layer's dependencies.
  502. To specify the layer's priority manually, use the
  503. :term:`BBFILE_PRIORITY`
  504. variable and append the layer's root name::
  505. BBFILE_PRIORITY_mylayer = "1"
  506. .. note::
  507. It is possible for a recipe with a lower version number
  508. :term:`PV` in a layer that has a higher
  509. priority to take precedence.
  510. Also, the layer priority does not currently affect the precedence
  511. order of ``.conf`` or ``.bbclass`` files. Future versions of BitBake
  512. might address this.
  513. Providing Global-level Configurations With Your Layer
  514. -----------------------------------------------------
  515. When creating a layer, you may need to define configurations that should take
  516. effect globally in your build environment when the layer is part of the build.
  517. The ``layer.conf`` file is a :term:`configuration file` that affects the build
  518. system globally, so it is a candidate for this use-case.
  519. .. warning::
  520. Providing unconditional global level configuration from the ``layer.conf``
  521. file is *not* a good practice, and should be avoided. For this reason, the
  522. section :ref:`ref-conditional-layer-confs` below shows how the ``layer.conf``
  523. file can be used to provide configurations only if a certain condition is
  524. met.
  525. For example, if your layer provides a Linux kernel recipe named
  526. ``linux-custom``, you may want to make :term:`PREFERRED_PROVIDER_virtual/kernel
  527. <PREFERRED_PROVIDER>` point to ``linux-custom``::
  528. PREFERRED_PROVIDER_virtual/kernel = "linux-custom"
  529. This can be defined in the ``layer.conf`` file. If your layer is at the last
  530. position in the :term:`BBLAYERS` list, it will take precedence over previous
  531. ``PREFERRED_PROVIDER_virtual/kernel`` assignments (unless one is set from a
  532. :term:`configuration file` that is parsed later, such as machine or distro
  533. configuration files).
  534. .. _ref-conditional-layer-confs:
  535. Conditionally Provide Global-level Configurations With Your Layer
  536. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  537. In some cases, your layer may provide global configurations only if some
  538. features it provides are enabled. Since the ``layer.conf`` file is parsed at an
  539. earlier stage in the parsing process, the :term:`DISTRO_FEATURES` and
  540. :term:`MACHINE_FEATURES` variables are not yet available to ``layer.conf``, and
  541. declaring conditional assignments based on these variables is not possible. The
  542. following technique shows a way to bypass this limitation by using the
  543. :term:`USER_CLASSES` variable and a conditional ``require`` command.
  544. In the following steps, let's assume our layer is named ``meta-mylayer`` and
  545. that this layer defines a custom :ref:`distro feature <ref-features-distro>`
  546. named ``mylayer-kernel``. We will set the :term:`PREFERRED_PROVIDER` variable
  547. for the kernel only if our feature ``mylayer-kernel`` is part of the
  548. :term:`DISTRO_FEATURES`:
  549. #. Create an include file in the directory
  550. ``meta-mylayer/conf/distro/include/``, for example a file named
  551. ``mylayer-kernel-provider.inc`` that sets the kernel provider to
  552. ``linux-custom``::
  553. PREFERRED_PROVIDER_virtual/kernel = "linux-custom"
  554. #. Provide a path to this include file in your ``layer.conf``::
  555. META_MYLAYER_KERNEL_PROVIDER_PATH = "${LAYERDIR}/conf/distro/include/mylayer-kernel-provider.inc"
  556. #. Create a new class in ``meta-mylayer/classes-global/``, for example a class
  557. ``meta-mylayer-cfg.bbclass``. Make it conditionally require the file
  558. ``mylayer-kernel-provider.inc`` defined above, using the variable
  559. ``META_MYLAYER_KERNEL_PROVIDER_PATH`` defined in ``layer.conf``::
  560. require ${@bb.utils.contains('DISTRO_FEATURES', 'mylayer-kernel', '${META_MYLAYER_KERNEL_PROVIDER_PATH}', '', d)}
  561. For details on the ``bb.utils.contains`` function, see its definition in
  562. :bitbake_git:`lib/bb/utils.py </tree/lib/bb/utils.py>`.
  563. .. note::
  564. The ``require`` command is designed to not fail if the function
  565. ``bb.utils.contains`` returns an empty string.
  566. #. Back to your ``layer.conf`` file, add the class ``meta-mylayer-cfg`` class to
  567. the :term:`USER_CLASSES` variable::
  568. USER_CLASSES:append = " meta-mylayer-cfg"
  569. This will add the class ``meta-mylayer-cfg`` to the list of classes to
  570. globally inherit. Since the ``require`` command is conditional in
  571. ``meta-mylayer-cfg.bbclass``, even though inherited the class will have no
  572. effect unless the feature ``mylayer-kernel`` is enabled through
  573. :term:`DISTRO_FEATURES`.
  574. This technique can also be used for :ref:`Machine features
  575. <ref-features-machine>` by following the same steps. Though not mandatory, it is
  576. recommended to put include files for :term:`DISTRO_FEATURES` in your layer's
  577. ``conf/distro/include`` and the ones for :term:`MACHINE_FEATURES` in your
  578. layer's ``conf/machine/include``.
  579. Managing Layers
  580. ===============
  581. You can use the BitBake layer management tool ``bitbake-layers`` to
  582. provide a view into the structure of recipes across a multi-layer
  583. project. Being able to generate output that reports on configured layers
  584. with their paths and priorities and on ``.bbappend`` files and their
  585. applicable recipes can help to reveal potential problems.
  586. For help on the BitBake layer management tool, use the following
  587. command::
  588. $ bitbake-layers --help
  589. The following list describes the available commands:
  590. - ``help:`` Displays general help or help on a specified command.
  591. - ``show-layers:`` Shows the current configured layers.
  592. - ``show-overlayed:`` Lists overlayed recipes. A recipe is overlayed
  593. when a recipe with the same name exists in another layer that has a
  594. higher layer priority.
  595. - ``show-recipes:`` Lists available recipes and the layers that
  596. provide them.
  597. - ``show-appends:`` Lists ``.bbappend`` files and the recipe files to
  598. which they apply.
  599. - ``show-cross-depends:`` Lists dependency relationships between
  600. recipes that cross layer boundaries.
  601. - ``add-layer:`` Adds a layer to ``bblayers.conf``.
  602. - ``remove-layer:`` Removes a layer from ``bblayers.conf``
  603. - ``flatten:`` Flattens the layer configuration into a separate
  604. output directory. Flattening your layer configuration builds a
  605. "flattened" directory that contains the contents of all layers, with
  606. any overlayed recipes removed and any ``.bbappend`` files appended to
  607. the corresponding recipes. You might have to perform some manual
  608. cleanup of the flattened layer as follows:
  609. - Non-recipe files (such as patches) are overwritten. The flatten
  610. command shows a warning for these files.
  611. - Anything beyond the normal layer setup has been added to the
  612. ``layer.conf`` file. Only the lowest priority layer's
  613. ``layer.conf`` is used.
  614. - Overridden and appended items from ``.bbappend`` files need to be
  615. cleaned up. The contents of each ``.bbappend`` end up in the
  616. flattened recipe. However, if there are appended or changed
  617. variable values, you need to tidy these up yourself. Consider the
  618. following example. Here, the ``bitbake-layers`` command adds the
  619. line ``#### bbappended ...`` so that you know where the following
  620. lines originate::
  621. ...
  622. DESCRIPTION = "A useful utility"
  623. ...
  624. EXTRA_OECONF = "--enable-something"
  625. ...
  626. #### bbappended from meta-anotherlayer ####
  627. DESCRIPTION = "Customized utility"
  628. EXTRA_OECONF += "--enable-somethingelse"
  629. Ideally, you would tidy up these utilities as follows::
  630. ...
  631. DESCRIPTION = "Customized utility"
  632. ...
  633. EXTRA_OECONF = "--enable-something --enable-somethingelse"
  634. ...
  635. - ``layerindex-fetch``: Fetches a layer from a layer index, along
  636. with its dependent layers, and adds the layers to the
  637. ``conf/bblayers.conf`` file.
  638. - ``layerindex-show-depends``: Finds layer dependencies from the
  639. layer index.
  640. - ``save-build-conf``: Saves the currently active build configuration
  641. (``conf/local.conf``, ``conf/bblayers.conf``) as a template into a layer.
  642. This template can later be used for setting up builds via :term:`TEMPLATECONF`.
  643. For information about saving and using configuration templates, see
  644. ":ref:`dev-manual/custom-template-configuration-directory:creating a custom template configuration directory`".
  645. - ``create-layer``: Creates a basic layer.
  646. - ``create-layers-setup``: Writes out a configuration file and/or a script that
  647. can replicate the directory structure and revisions of the layers in a current build.
  648. For more information, see ":ref:`dev-manual/layers:saving and restoring the layers setup`".
  649. Creating a General Layer Using the ``bitbake-layers`` Script
  650. ============================================================
  651. The ``bitbake-layers`` script with the ``create-layer`` subcommand
  652. simplifies creating a new general layer.
  653. .. note::
  654. - For information on BSP layers, see the ":ref:`bsp-guide/bsp:bsp layers`"
  655. section in the Yocto
  656. Project Board Specific (BSP) Developer's Guide.
  657. - In order to use a layer with the OpenEmbedded build system, you
  658. need to add the layer to your ``bblayers.conf`` configuration
  659. file. See the ":ref:`dev-manual/layers:adding a layer using the \`\`bitbake-layers\`\` script`"
  660. section for more information.
  661. The default mode of the script's operation with this subcommand is to
  662. create a layer with the following:
  663. - A layer priority of 6.
  664. - A ``conf`` subdirectory that contains a ``layer.conf`` file.
  665. - A ``recipes-example`` subdirectory that contains a further
  666. subdirectory named ``example``, which contains an ``example.bb``
  667. recipe file.
  668. - A ``COPYING.MIT``, which is the license statement for the layer. The
  669. script assumes you want to use the MIT license, which is typical for
  670. most layers, for the contents of the layer itself.
  671. - A ``README`` file, which is a file describing the contents of your
  672. new layer.
  673. In its simplest form, you can use the following command form to create a
  674. layer. The command creates a layer whose name corresponds to
  675. "your_layer_name" in the current directory::
  676. $ bitbake-layers create-layer your_layer_name
  677. As an example, the following command creates a layer named ``meta-scottrif``
  678. in your home directory::
  679. $ cd /usr/home
  680. $ bitbake-layers create-layer meta-scottrif
  681. NOTE: Starting bitbake server...
  682. Add your new layer with 'bitbake-layers add-layer meta-scottrif'
  683. If you want to set the priority of the layer to other than the default
  684. value of "6", you can either use the ``--priority`` option or you
  685. can edit the
  686. :term:`BBFILE_PRIORITY` value
  687. in the ``conf/layer.conf`` after the script creates it. Furthermore, if
  688. you want to give the example recipe file some name other than the
  689. default, you can use the ``--example-recipe-name`` option.
  690. The easiest way to see how the ``bitbake-layers create-layer`` command
  691. works is to experiment with the script. You can also read the usage
  692. information by entering the following::
  693. $ bitbake-layers create-layer --help
  694. NOTE: Starting bitbake server...
  695. usage: bitbake-layers create-layer [-h] [--priority PRIORITY]
  696. [--example-recipe-name EXAMPLERECIPE]
  697. layerdir
  698. Create a basic layer
  699. positional arguments:
  700. layerdir Layer directory to create
  701. optional arguments:
  702. -h, --help show this help message and exit
  703. --priority PRIORITY, -p PRIORITY
  704. Layer directory to create
  705. --example-recipe-name EXAMPLERECIPE, -e EXAMPLERECIPE
  706. Filename of the example recipe
  707. Adding a Layer Using the ``bitbake-layers`` Script
  708. ==================================================
  709. Once you create your general layer, you must add it to your
  710. ``bblayers.conf`` file. Adding the layer to this configuration file
  711. makes the OpenEmbedded build system aware of your layer so that it can
  712. search it for metadata.
  713. Add your layer by using the ``bitbake-layers add-layer`` command::
  714. $ bitbake-layers add-layer your_layer_name
  715. Here is an example that adds a
  716. layer named ``meta-scottrif`` to the configuration file. Following the
  717. command that adds the layer is another ``bitbake-layers`` command that
  718. shows the layers that are in your ``bblayers.conf`` file::
  719. $ bitbake-layers add-layer meta-scottrif
  720. NOTE: Starting bitbake server...
  721. Parsing recipes: 100% |##########################################################| Time: 0:00:49
  722. Parsing of 1441 .bb files complete (0 cached, 1441 parsed). 2055 targets, 56 skipped, 0 masked, 0 errors.
  723. $ bitbake-layers show-layers
  724. NOTE: Starting bitbake server...
  725. layer path priority
  726. ==========================================================================
  727. meta /home/scottrif/poky/meta 5
  728. meta-poky /home/scottrif/poky/meta-poky 5
  729. meta-yocto-bsp /home/scottrif/poky/meta-yocto-bsp 5
  730. workspace /home/scottrif/poky/build/workspace 99
  731. meta-scottrif /home/scottrif/poky/build/meta-scottrif 6
  732. Adding the layer to this file
  733. enables the build system to locate the layer during the build.
  734. .. note::
  735. During a build, the OpenEmbedded build system looks in the layers
  736. from the top of the list down to the bottom in that order.
  737. Saving and restoring the layers setup
  738. =====================================
  739. Once you have a working build with the correct set of layers, it is beneficial
  740. to capture the layer setup --- what they are, which repositories they come from
  741. and which SCM revisions they're at --- into a configuration file, so that this
  742. setup can be easily replicated later, perhaps on a different machine. Here's
  743. how to do this::
  744. $ bitbake-layers create-layers-setup /srv/work/alex/meta-alex/
  745. NOTE: Starting bitbake server...
  746. NOTE: Created /srv/work/alex/meta-alex/setup-layers.json
  747. NOTE: Created /srv/work/alex/meta-alex/setup-layers
  748. The tool needs a single argument which tells where to place the output, consisting
  749. of a json formatted layer configuration, and a ``setup-layers`` script that can use that configuration
  750. to restore the layers in a different location, or on a different host machine. The argument
  751. can point to a custom layer (which is then deemed a "bootstrap" layer that needs to be
  752. checked out first), or into a completely independent location.
  753. The replication of the layers is performed by running the ``setup-layers`` script provided
  754. above:
  755. #. Clone the bootstrap layer or some other repository to obtain
  756. the json config and the setup script that can use it.
  757. #. Run the script directly with no options::
  758. alex@Zen2:/srv/work/alex/my-build$ meta-alex/setup-layers
  759. Note: not checking out source meta-alex, use --force-bootstraplayer-checkout to override.
  760. Setting up source meta-intel, revision 15.0-hardknott-3.3-310-g0a96edae, branch master
  761. Running 'git init -q /srv/work/alex/my-build/meta-intel'
  762. 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
  763. Running 'git fetch -q origin || true' in /srv/work/alex/my-build/meta-intel
  764. Running 'git checkout -q 0a96edae609a3f48befac36af82cf1eed6786b4a' in /srv/work/alex/my-build/meta-intel
  765. Setting up source poky, revision 4.1_M1-372-g55483d28f2, branch akanavin/setup-layers
  766. Running 'git init -q /srv/work/alex/my-build/poky'
  767. 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
  768. Running 'git fetch -q origin || true' in /srv/work/alex/my-build/poky
  769. 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
  770. Running 'git fetch -q poky-contrib || true' in /srv/work/alex/my-build/poky
  771. Running 'git checkout -q 11db0390b02acac1324e0f827beb0e2e3d0d1d63' in /srv/work/alex/my-build/poky
  772. .. note::
  773. This will work to update an existing checkout as well.
  774. .. note::
  775. The script is self-sufficient and requires only python3
  776. and git on the build machine.
  777. .. note::
  778. Both the ``create-layers-setup`` and the ``setup-layers`` provided several additional options
  779. that customize their behavior - you are welcome to study them via ``--help`` command line parameter.