bitbake-user-manual-hello.rst 17 KB

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