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