bitbake-user-manual-hello.rst 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. .. SPDX-License-Identifier: CC-BY-2.5
  2. ===================
  3. Hello World Example
  4. ===================
  5. BitBake Hello World
  6. ===================
  7. The simplest example commonly used to demonstrate any new programming
  8. language or tool is the "`Hello
  9. World <http://en.wikipedia.org/wiki/Hello_world_program>`__" example.
  10. This appendix demonstrates, in tutorial form, Hello World within the
  11. context of BitBake. The tutorial describes how to create a new project
  12. and the applicable metadata files necessary to allow BitBake to build
  13. it.
  14. Obtaining BitBake
  15. =================
  16. See the :ref:`bitbake-user-manual/bitbake-user-manual-intro:obtaining bitbake` section for
  17. information on how to obtain BitBake. Once you have the source code on
  18. your machine, the BitBake directory appears as follows::
  19. $ ls -al
  20. total 108
  21. drwxr-xr-x 9 fawkh 10000 4096 feb 24 12:10 .
  22. drwx------ 36 fawkh 10000 4096 mar 2 17:00 ..
  23. -rw-r--r-- 1 fawkh 10000 365 feb 24 12:10 AUTHORS
  24. drwxr-xr-x 2 fawkh 10000 4096 feb 24 12:10 bin
  25. -rw-r--r-- 1 fawkh 10000 16501 feb 24 12:10 ChangeLog
  26. drwxr-xr-x 2 fawkh 10000 4096 feb 24 12:10 classes
  27. drwxr-xr-x 2 fawkh 10000 4096 feb 24 12:10 conf
  28. drwxr-xr-x 5 fawkh 10000 4096 feb 24 12:10 contrib
  29. drwxr-xr-x 6 fawkh 10000 4096 feb 24 12:10 doc
  30. drwxr-xr-x 8 fawkh 10000 4096 mar 2 16:26 .git
  31. -rw-r--r-- 1 fawkh 10000 31 feb 24 12:10 .gitattributes
  32. -rw-r--r-- 1 fawkh 10000 392 feb 24 12:10 .gitignore
  33. drwxr-xr-x 13 fawkh 10000 4096 feb 24 12:11 lib
  34. -rw-r--r-- 1 fawkh 10000 1224 feb 24 12:10 LICENSE
  35. -rw-r--r-- 1 fawkh 10000 15394 feb 24 12:10 LICENSE.GPL-2.0-only
  36. -rw-r--r-- 1 fawkh 10000 1286 feb 24 12:10 LICENSE.MIT
  37. -rw-r--r-- 1 fawkh 10000 229 feb 24 12:10 MANIFEST.in
  38. -rw-r--r-- 1 fawkh 10000 2413 feb 24 12:10 README
  39. -rw-r--r-- 1 fawkh 10000 43 feb 24 12:10 toaster-requirements.txt
  40. -rw-r--r-- 1 fawkh 10000 2887 feb 24 12:10 TODO
  41. At this point, you should have BitBake cloned to a directory that
  42. matches the previous listing except for dates and user names.
  43. Setting Up the BitBake Environment
  44. ==================================
  45. First, you need to be sure that you can run BitBake. Set your working
  46. directory to where your local BitBake files are and run the following
  47. command::
  48. $ ./bin/bitbake --version
  49. BitBake Build Tool Core version 2.3.1
  50. The console output tells you what version
  51. you are running.
  52. The recommended method to run BitBake is from a directory of your
  53. choice. To be able to run BitBake from any directory, you need to add
  54. the executable binary to your binary to your shell's environment
  55. ``PATH`` variable. First, look at your current ``PATH`` variable by
  56. entering the following::
  57. $ echo $PATH
  58. Next, add the directory location
  59. for the BitBake binary to the ``PATH``. Here is an example that adds the
  60. ``/home/scott-lenovo/bitbake/bin`` directory to the front of the
  61. ``PATH`` variable::
  62. $ export PATH=/home/scott-lenovo/bitbake/bin:$PATH
  63. You should now be able to enter the ``bitbake`` command from the command
  64. line while working from any directory.
  65. The Hello World Example
  66. =======================
  67. The overall goal of this exercise is to build a complete "Hello World"
  68. example utilizing task and layer concepts. Because this is how modern
  69. projects such as OpenEmbedded and the Yocto Project utilize BitBake, the
  70. example provides an excellent starting point for understanding BitBake.
  71. To help you understand how to use BitBake to build targets, the example
  72. starts with nothing but the ``bitbake`` command, which causes BitBake to
  73. fail and report problems. The example progresses by adding pieces to the
  74. build to eventually conclude with a working, minimal "Hello World"
  75. example.
  76. While every attempt is made to explain what is happening during the
  77. example, the descriptions cannot cover everything. You can find further
  78. information throughout this manual. Also, you can actively participate
  79. in the :oe_lists:`/g/bitbake-devel`
  80. discussion mailing list about the BitBake build tool.
  81. .. note::
  82. This example was inspired by and drew heavily from
  83. `Mailing List post - The BitBake equivalent of "Hello, World!"
  84. <https://www.mail-archive.com/yocto@yoctoproject.org/msg09379.html>`_.
  85. As stated earlier, the goal of this example is to eventually compile
  86. "Hello World". However, it is unknown what BitBake needs and what you
  87. have to provide in order to achieve that goal. Recall that BitBake
  88. utilizes three types of metadata files:
  89. :ref:`bitbake-user-manual/bitbake-user-manual-intro:configuration files`,
  90. :ref:`bitbake-user-manual/bitbake-user-manual-intro:classes`, and
  91. :ref:`bitbake-user-manual/bitbake-user-manual-intro:recipes`.
  92. But where do they go? How does BitBake find
  93. them? BitBake's error messaging helps you answer these types of
  94. questions and helps you better understand exactly what is going on.
  95. Following is the complete "Hello World" example.
  96. #. **Create a Project Directory:** First, set up a directory for the
  97. "Hello World" project. Here is how you can do so in your home
  98. directory::
  99. $ mkdir ~/hello
  100. $ cd ~/hello
  101. This is the directory that
  102. BitBake will use to do all of its work. You can use this directory
  103. to keep all the metafiles needed by BitBake. Having a project
  104. directory is a good way to isolate your project.
  105. #. **Run BitBake:** At this point, you have nothing but a project
  106. directory. Run the ``bitbake`` command and see what it does::
  107. $ bitbake
  108. ERROR: The BBPATH variable is not set and bitbake did not find a conf/bblayers.conf file in the expected location.
  109. Maybe you accidentally invoked bitbake from the wrong directory?
  110. When you run BitBake, it begins looking for metadata files. The
  111. :term:`BBPATH` variable is what tells BitBake where
  112. to look for those files. :term:`BBPATH` is not set and you need to set
  113. it. Without :term:`BBPATH`, BitBake cannot find any configuration files
  114. (``.conf``) or recipe files (``.bb``) at all. BitBake also cannot
  115. find the ``bitbake.conf`` file.
  116. #. **Setting BBPATH:** For this example, you can set :term:`BBPATH` in
  117. the same manner that you set ``PATH`` earlier in the appendix. You
  118. should realize, though, that it is much more flexible to set the
  119. :term:`BBPATH` variable up in a configuration file for each project.
  120. From your shell, enter the following commands to set and export the
  121. :term:`BBPATH` variable::
  122. $ BBPATH="projectdirectory"
  123. $ export BBPATH
  124. Use your actual project directory in the command. BitBake uses that
  125. directory to find the metadata it needs for your project.
  126. .. note::
  127. When specifying your project directory, do not use the tilde
  128. ("~") character as BitBake does not expand that character as the
  129. shell would.
  130. #. **Run BitBake:** Now that you have :term:`BBPATH` defined, run the
  131. ``bitbake`` command again::
  132. $ bitbake
  133. ERROR: Unable to parse /home/scott-lenovo/bitbake/lib/bb/parse/__init__.py
  134. Traceback (most recent call last):
  135. File "/home/scott-lenovo/bitbake/lib/bb/parse/__init__.py", line 127, in resolve_file(fn='conf/bitbake.conf', d=<bb.data_smart.DataSmart object at 0x7f22919a3df0>):
  136. if not newfn:
  137. > raise IOError(errno.ENOENT, "file %s not found in %s" % (fn, bbpath))
  138. fn = newfn
  139. FileNotFoundError: [Errno 2] file conf/bitbake.conf not found in <projectdirectory>
  140. This sample output shows that BitBake could not find the
  141. ``conf/bitbake.conf`` file in the project directory. This file is
  142. the first thing BitBake must find in order to build a target. And,
  143. since the project directory for this example is empty, you need to
  144. provide a ``conf/bitbake.conf`` file.
  145. #. **Creating conf/bitbake.conf:** The ``conf/bitbake.conf`` includes
  146. a number of configuration variables BitBake uses for metadata and
  147. recipe files. For this example, you need to create the file in your
  148. project directory and define some key BitBake variables. For more
  149. information on the ``bitbake.conf`` file, see
  150. https://git.openembedded.org/bitbake/tree/conf/bitbake.conf.
  151. Use the following commands to create the ``conf`` directory in the
  152. project directory::
  153. $ mkdir conf
  154. From within the ``conf`` directory,
  155. use some editor to create the ``bitbake.conf`` so that it contains
  156. the following::
  157. PN = "${@bb.parse.vars_from_file(d.getVar('FILE', False),d)[0] or 'defaultpkgname'}"
  158. TMPDIR = "${TOPDIR}/tmp"
  159. CACHE = "${TMPDIR}/cache"
  160. STAMP = "${TMPDIR}/${PN}/stamps"
  161. T = "${TMPDIR}/${PN}/work"
  162. B = "${TMPDIR}/${PN}"
  163. .. note::
  164. Without a value for :term:`PN`, the variables :term:`STAMP`, :term:`T`, and :term:`B`, prevent more
  165. than one recipe from working. You can fix this by either setting :term:`PN` to
  166. have a value similar to what OpenEmbedded and BitBake use in the default
  167. ``bitbake.conf`` file (see previous example). Or, by manually updating each
  168. recipe to set :term:`PN`. You will also need to include :term:`PN` as part of the :term:`STAMP`,
  169. :term:`T`, and :term:`B` variable definitions in the ``local.conf`` file.
  170. The ``TMPDIR`` variable establishes a directory that BitBake uses
  171. for build output and intermediate files other than the cached
  172. information used by the
  173. :ref:`bitbake-user-manual/bitbake-user-manual-execution:setscene`
  174. process. Here, the ``TMPDIR`` directory is set to ``hello/tmp``.
  175. .. tip::
  176. You can always safely delete the tmp directory in order to rebuild a
  177. BitBake target. The build process creates the directory for you when you
  178. run BitBake.
  179. For information about each of the other variables defined in this
  180. example, check :term:`PN`, :term:`TOPDIR`, :term:`CACHE`, :term:`STAMP`,
  181. :term:`T` or :term:`B` to take you to the definitions in the
  182. glossary.
  183. #. **Run BitBake:** After making sure that the ``conf/bitbake.conf`` file
  184. exists, you can run the ``bitbake`` command again::
  185. $ bitbake
  186. ERROR: Unable to parse /home/scott-lenovo/bitbake/lib/bb/parse/parse_py/BBHandler.py
  187. Traceback (most recent call last):
  188. File "/home/scott-lenovo/bitbake/lib/bb/parse/parse_py/BBHandler.py", line 67, in inherit(files=['base'], fn='configuration INHERITs', lineno=0, d=<bb.data_smart.DataSmart object at 0x7fab6815edf0>):
  189. if not os.path.exists(file):
  190. > raise ParseError("Could not inherit file %s" % (file), fn, lineno)
  191. bb.parse.ParseError: ParseError in configuration INHERITs: Could not inherit file classes/base.bbclass
  192. In the sample output,
  193. BitBake could not find the ``classes/base.bbclass`` file. You need
  194. to create that file next.
  195. #. **Creating classes/base.bbclass:** BitBake uses class files to
  196. provide common code and functionality. The minimally required class
  197. for BitBake is the ``classes/base.bbclass`` file. The ``base`` class
  198. is implicitly inherited by every recipe. BitBake looks for the class
  199. in the ``classes`` directory of the project (i.e ``hello/classes``
  200. in this example).
  201. Create the ``classes`` directory as follows::
  202. $ cd $HOME/hello
  203. $ mkdir classes
  204. Move to the ``classes`` directory and then create the
  205. ``base.bbclass`` file by inserting this single line::
  206. addtask build
  207. The minimal task that BitBake runs is the ``do_build`` task. This is
  208. all the example needs in order to build the project. Of course, the
  209. ``base.bbclass`` can have much more depending on which build
  210. environments BitBake is supporting.
  211. #. **Run BitBake:** After making sure that the ``classes/base.bbclass``
  212. file exists, you can run the ``bitbake`` command again::
  213. $ bitbake
  214. Nothing to do. Use 'bitbake world' to build everything, or run 'bitbake --help' for usage information.
  215. BitBake is finally reporting
  216. no errors. However, you can see that it really does not have
  217. anything to do. You need to create a recipe that gives BitBake
  218. something to do.
  219. #. **Creating a Layer:** While it is not really necessary for such a
  220. small example, it is good practice to create a layer in which to
  221. keep your code separate from the general metadata used by BitBake.
  222. Thus, this example creates and uses a layer called "mylayer".
  223. .. note::
  224. You can find additional information on layers in the
  225. ":ref:`bitbake-user-manual/bitbake-user-manual-intro:Layers`" section.
  226. Minimally, you need a recipe file and a layer configuration file in
  227. your layer. The configuration file needs to be in the ``conf``
  228. directory inside the layer. Use these commands to set up the layer
  229. and the ``conf`` directory::
  230. $ cd $HOME
  231. $ mkdir mylayer
  232. $ cd mylayer
  233. $ mkdir conf
  234. Move to the ``conf`` directory and create a ``layer.conf`` file that has the
  235. following::
  236. BBPATH .= ":${LAYERDIR}"
  237. BBFILES += "${LAYERDIR}/*.bb"
  238. BBFILE_COLLECTIONS += "mylayer"
  239. BBFILE_PATTERN_mylayer := "^${LAYERDIR_RE}/"
  240. LAYERSERIES_CORENAMES = "hello_world_example"
  241. LAYERSERIES_COMPAT_mylayer = "hello_world_example"
  242. For information on these variables, click on :term:`BBFILES`,
  243. :term:`LAYERDIR`, :term:`BBFILE_COLLECTIONS`, :term:`BBFILE_PATTERN_mylayer <BBFILE_PATTERN>`
  244. or :term:`LAYERSERIES_COMPAT` to go to the definitions in the glossary.
  245. .. note::
  246. We are setting both ``LAYERSERIES_CORENAMES`` and :term:`LAYERSERIES_COMPAT` in this particular case, because we
  247. are using bitbake without OpenEmbedded.
  248. You should usually just use :term:`LAYERSERIES_COMPAT` to specify the OE-Core versions for which your layer
  249. is compatible, and add the meta-openembedded layer to your project.
  250. You need to create the recipe file next. Inside your layer at the
  251. top-level, use an editor and create a recipe file named
  252. ``printhello.bb`` that has the following::
  253. DESCRIPTION = "Prints Hello World"
  254. PN = 'printhello'
  255. PV = '1'
  256. python do_build() {
  257. bb.plain("********************");
  258. bb.plain("* *");
  259. bb.plain("* Hello, World! *");
  260. bb.plain("* *");
  261. bb.plain("********************");
  262. }
  263. The recipe file simply provides
  264. a description of the recipe, the name, version, and the ``do_build``
  265. task, which prints out "Hello World" to the console. For more
  266. information on :term:`DESCRIPTION`, :term:`PN` or :term:`PV`
  267. follow the links to the glossary.
  268. #. **Run BitBake With a Target:** Now that a BitBake target exists, run
  269. the command and provide that target::
  270. $ cd $HOME/hello
  271. $ bitbake printhello
  272. ERROR: no recipe files to build, check your BBPATH and BBFILES?
  273. Summary: There was 1 ERROR message shown, returning a non-zero exit code.
  274. We have created the layer with the recipe and
  275. the layer configuration file but it still seems that BitBake cannot
  276. find the recipe. BitBake needs a ``conf/bblayers.conf`` that lists
  277. the layers for the project. Without this file, BitBake cannot find
  278. the recipe.
  279. #. **Creating conf/bblayers.conf:** BitBake uses the
  280. ``conf/bblayers.conf`` file to locate layers needed for the project.
  281. This file must reside in the ``conf`` directory of the project (i.e.
  282. ``hello/conf`` for this example).
  283. Set your working directory to the ``hello/conf`` directory and then
  284. create the ``bblayers.conf`` file so that it contains the following::
  285. BBLAYERS ?= " \
  286. /home/<you>/mylayer \
  287. "
  288. You need to provide your own information for ``you`` in the file.
  289. #. **Run BitBake With a Target:** Now that you have supplied the
  290. ``bblayers.conf`` file, run the ``bitbake`` command and provide the
  291. target::
  292. $ bitbake printhello
  293. Loading cache: 100% |
  294. Loaded 0 entries from dependency cache.
  295. Parsing recipes: 100% |##################################################################################|
  296. Parsing of 1 .bb files complete (0 cached, 1 parsed). 1 targets, 0 skipped, 0 masked, 0 errors.
  297. NOTE: Resolving any missing task queue dependencies
  298. Initialising tasks: 100% |###############################################################################|
  299. NOTE: No setscene tasks
  300. NOTE: Executing Tasks
  301. ********************
  302. * *
  303. * Hello, World! *
  304. * *
  305. ********************
  306. NOTE: Tasks Summary: Attempted 1 tasks of which 0 didn't need to be rerun and all succeeded.
  307. .. note::
  308. After the first execution, re-running bitbake printhello again will not
  309. result in a BitBake run that prints the same console output. The reason
  310. for this is that the first time the printhello.bb recipe's do_build task
  311. executes successfully, BitBake writes a stamp file for the task. Thus,
  312. the next time you attempt to run the task using that same bitbake
  313. command, BitBake notices the stamp and therefore determines that the task
  314. does not need to be re-run. If you delete the tmp directory or run
  315. bitbake -c clean printhello and then re-run the build, the "Hello,
  316. World!" message will be printed again.