summaryrefslogtreecommitdiffstats
path: root/documentation/sdk-manual/working-projects.rst
diff options
context:
space:
mode:
Diffstat (limited to 'documentation/sdk-manual/working-projects.rst')
-rw-r--r--documentation/sdk-manual/working-projects.rst404
1 files changed, 0 insertions, 404 deletions
diff --git a/documentation/sdk-manual/working-projects.rst b/documentation/sdk-manual/working-projects.rst
deleted file mode 100644
index 7df73b1b17..0000000000
--- a/documentation/sdk-manual/working-projects.rst
+++ /dev/null
@@ -1,404 +0,0 @@
1.. SPDX-License-Identifier: CC-BY-SA-2.0-UK
2
3********************************
4Using the SDK Toolchain Directly
5********************************
6
7You can use the SDK toolchain directly with Makefile and Autotools-based
8projects.
9
10Autotools-Based Projects
11========================
12
13Once you have a suitable :ref:`sdk-manual/intro:the cross-development toolchain`
14installed, it is very easy to develop a project using the :wikipedia:`GNU
15Autotools-based <GNU_Build_System>` workflow, which is outside of the
16:term:`OpenEmbedded Build System`.
17
18The following figure presents a simple Autotools workflow.
19
20.. image:: figures/sdk-autotools-flow.png
21 :align: center
22 :width: 70%
23
24Follow these steps to create a simple Autotools-based "Hello World"
25project:
26
27.. note::
28
29 For more information on the GNU Autotools workflow, see the same
30 example on the
31 GNOME Developer
32 site.
33
34#. *Create a Working Directory and Populate It:* Create a clean
35 directory for your project and then make that directory your working
36 location::
37
38 $ mkdir $HOME/helloworld
39 $ cd $HOME/helloworld
40
41 After setting up the directory, populate it with files needed for the flow.
42 You need a project source file, a file to help with configuration,
43 and a file to help create the Makefile, and a README file:
44 ``hello.c``, ``configure.ac``, ``Makefile.am``, and ``README``,
45 respectively.
46
47 Use the following command to create an empty README file, which is
48 required by GNU Coding Standards::
49
50 $ touch README
51
52 Create the remaining
53 three files as follows:
54
55 - ``hello.c``::
56
57 #include <stdio.h>
58
59 int main()
60 {
61 printf("Hello World!\n");
62 return 0;
63 }
64
65 - ``configure.ac``::
66
67 AC_INIT(hello,0.1)
68 AM_INIT_AUTOMAKE([foreign])
69 AC_PROG_CC
70 AC_CONFIG_FILES(Makefile)
71 AC_OUTPUT
72
73 - ``Makefile.am``::
74
75 bin_PROGRAMS = hello
76 hello_SOURCES = hello.c
77
78#. *Source the Cross-Toolchain Environment Setup File:* As described
79 earlier in the manual, installing the cross-toolchain creates a
80 cross-toolchain environment setup script in the directory that the
81 SDK was installed. Before you can use the tools to develop your
82 project, you must source this setup script. The script begins with
83 the string "environment-setup" and contains the machine architecture,
84 which is followed by the string "poky-linux". For this example, the
85 command sources a script from the default SDK installation directory
86 that uses the 32-bit Intel x86 Architecture and the &DISTRO; Yocto
87 Project release::
88
89 $ source /opt/poky/&DISTRO;/environment-setup-i586-poky-linux
90
91 Another example is sourcing the environment setup directly in a Yocto
92 build::
93
94 $ source tmp/deploy/images/qemux86-64/environment-setup-core2-64-poky-linux
95
96#. *Create the configure Script:* Use the ``autoreconf`` command to
97 generate the ``configure`` script::
98
99 $ autoreconf
100
101 The ``autoreconf``
102 tool takes care of running the other Autotools such as ``aclocal``,
103 ``autoconf``, and ``automake``.
104
105 .. note::
106
107 If you get errors from ``configure.ac``, which ``autoreconf``
108 runs, that indicate missing files, you can use the "-i" option,
109 which ensures missing auxiliary files are copied to the build
110 host.
111
112#. *Cross-Compile the Project:* This command compiles the project using
113 the cross-compiler. The
114 :term:`CONFIGURE_FLAGS`
115 environment variable provides the minimal arguments for GNU
116 configure::
117
118 $ ./configure ${CONFIGURE_FLAGS}
119
120 For an Autotools-based
121 project, you can use the cross-toolchain by just passing the
122 appropriate host option to ``configure.sh``. The host option you use
123 is derived from the name of the environment setup script found in the
124 directory in which you installed the cross-toolchain. For example,
125 the host option for an ARM-based target that uses the GNU EABI is
126 ``armv5te-poky-linux-gnueabi``. You will notice that the name of the
127 script is ``environment-setup-armv5te-poky-linux-gnueabi``. Thus, the
128 following command works to update your project and rebuild it using
129 the appropriate cross-toolchain tools::
130
131 $ ./configure --host=armv5te-poky-linux-gnueabi --with-libtool-sysroot=sysroot_dir
132
133#. *Make and Install the Project:* These two commands generate and
134 install the project into the destination directory::
135
136 $ make
137 $ make install DESTDIR=./tmp
138
139 .. note::
140
141 To learn about environment variables established when you run the
142 cross-toolchain environment setup script and how they are used or
143 overridden by the Makefile, see the
144 :ref:`sdk-manual/working-projects:makefile-based projects` section.
145
146 This next command is a simple way to verify the installation of your
147 project. Running the command prints the architecture on which the
148 binary file can run. This architecture should be the same
149 architecture that the installed cross-toolchain supports::
150
151 $ file ./tmp/usr/local/bin/hello
152
153#. *Execute Your Project:* To execute the project, you would need to run
154 it on your target hardware. If your target hardware happens to be
155 your build host, you could run the project as follows::
156
157 $ ./tmp/usr/local/bin/hello
158
159 As expected, the project displays the "Hello World!" message.
160
161Makefile-Based Projects
162=======================
163
164Simple Makefile-based projects use and interact with the cross-toolchain
165environment variables established when you run the cross-toolchain
166environment setup script. The environment variables are subject to
167general ``make`` rules.
168
169This section presents a simple Makefile development flow and provides an
170example that lets you see how you can use cross-toolchain environment
171variables and Makefile variables during development.
172
173.. image:: figures/sdk-makefile-flow.png
174 :align: center
175 :width: 70%
176
177The main point of this section is to explain the following three cases
178regarding variable behavior:
179
180- *Case 1 --- No Variables Set in the Makefile Map to Equivalent
181 Environment Variables Set in the SDK Setup Script:* Because matching
182 variables are not specifically set in the ``Makefile``, the variables
183 retain their values based on the environment setup script.
184
185- *Case 2 --- Variables Are Set in the Makefile that Map to Equivalent
186 Environment Variables from the SDK Setup Script:* Specifically
187 setting matching variables in the ``Makefile`` during the build
188 results in the environment settings of the variables being
189 overwritten. In this case, the variables you set in the ``Makefile``
190 are used.
191
192- *Case 3 --- Variables Are Set Using the Command Line that Map to
193 Equivalent Environment Variables from the SDK Setup Script:*
194 Executing the ``Makefile`` from the command line results in the
195 environment variables being overwritten. In this case, the
196 command-line content is used.
197
198.. note::
199
200 Regardless of how you set your variables, if you use the "-e" option
201 with ``make``, the variables from the SDK setup script take precedence::
202
203 $ make -e target
204
205
206The remainder of this section presents a simple Makefile example that
207demonstrates these variable behaviors.
208
209In a new shell environment variables are not established for the SDK
210until you run the setup script. For example, the following commands show
211a null value for the compiler variable (i.e.
212:term:`CC`)::
213
214 $ echo ${CC}
215
216 $
217
218Running the
219SDK setup script for a 64-bit build host and an i586-tuned target
220architecture for a ``core-image-sato`` image using the current &DISTRO;
221Yocto Project release and then echoing that variable shows the value
222established through the script::
223
224 $ source /opt/poky/&DISTRO;/environment-setup-i586-poky-linux
225 $ echo ${CC}
226 i586-poky-linux-gcc -m32 -march=i586 --sysroot=/opt/poky/&DISTRO;/sysroots/i586-poky-linux
227
228To illustrate variable use, work through this simple "Hello World!"
229example:
230
231#. *Create a Working Directory and Populate It:* Create a clean
232 directory for your project and then make that directory your working
233 location::
234
235 $ mkdir $HOME/helloworld
236 $ cd $HOME/helloworld
237
238 After
239 setting up the directory, populate it with files needed for the flow.
240 You need a ``main.c`` file from which you call your function, a
241 ``module.h`` file to contain headers, and a ``module.c`` that defines
242 your function.
243
244 Create the three files as follows:
245
246 - ``main.c``::
247
248 #include "module.h"
249 void sample_func();
250 int main()
251 {
252 sample_func();
253 return 0;
254 }
255
256 - ``module.h``::
257
258 #include <stdio.h>
259 void sample_func();
260
261 - ``module.c``::
262
263 #include "module.h"
264 void sample_func()
265 {
266 printf("Hello World!");
267 printf("\n");
268 }
269
270#. *Source the Cross-Toolchain Environment Setup File:* As described
271 earlier in the manual, installing the cross-toolchain creates a
272 cross-toolchain environment setup script in the directory that the
273 SDK was installed. Before you can use the tools to develop your
274 project, you must source this setup script. The script begins with
275 the string "environment-setup" and contains the machine architecture,
276 which is followed by the string "poky-linux". For this example, the
277 command sources a script from the default SDK installation directory
278 that uses the 32-bit Intel x86 Architecture and the &DISTRO_NAME; Yocto
279 Project release::
280
281 $ source /opt/poky/&DISTRO;/environment-setup-i586-poky-linux
282
283 Another example is sourcing the environment setup directly in a Yocto
284 build::
285
286 $ source tmp/deploy/images/qemux86-64/environment-setup-core2-64-poky-linux
287
288#. *Create the Makefile:* For this example, the Makefile contains
289 two lines that can be used to set the :term:`CC` variable. One line is
290 identical to the value that is set when you run the SDK environment
291 setup script, and the other line sets :term:`CC` to "gcc", the default
292 GNU compiler on the build host::
293
294 # CC=i586-poky-linux-gcc -m32 -march=i586 --sysroot=/opt/poky/2.5/sysroots/i586-poky-linux
295 # CC="gcc"
296 all: main.o module.o
297 ${CC} main.o module.o -o target_bin
298 main.o: main.c module.h
299 ${CC} -I . -c main.c
300 module.o: module.c module.h
301 ${CC} -I . -c module.c
302 clean:
303 rm -rf *.o
304 rm target_bin
305
306#. *Make the Project:* Use the ``make`` command to create the binary
307 output file. Because variables are commented out in the Makefile, the
308 value used for :term:`CC` is the value set when the SDK environment setup
309 file was run::
310
311 $ make
312 i586-poky-linux-gcc -m32 -march=i586 --sysroot=/opt/poky/2.5/sysroots/i586-poky-linux -I . -c main.c
313 i586-poky-linux-gcc -m32 -march=i586 --sysroot=/opt/poky/2.5/sysroots/i586-poky-linux -I . -c module.c
314 i586-poky-linux-gcc -m32 -march=i586 --sysroot=/opt/poky/2.5/sysroots/i586-poky-linux main.o module.o -o target_bin
315
316 From the results of the previous command, you can see that
317 the compiler used was the compiler established through the :term:`CC`
318 variable defined in the setup script.
319
320 You can override the :term:`CC` environment variable with the same
321 variable as set from the Makefile by uncommenting the line in the
322 Makefile and running ``make`` again::
323
324 $ make clean
325 rm -rf *.o
326 rm target_bin
327 #
328 # Edit the Makefile by uncommenting the line that sets CC to "gcc"
329 #
330 $ make
331 gcc -I . -c main.c
332 gcc -I . -c module.c
333 gcc main.o module.o -o target_bin
334
335 As shown in the previous example, the
336 cross-toolchain compiler is not used. Rather, the default compiler is
337 used.
338
339 This next case shows how to override a variable by providing the
340 variable as part of the command line. Go into the Makefile and
341 re-insert the comment character so that running ``make`` uses the
342 established SDK compiler. However, when you run ``make``, use a
343 command-line argument to set :term:`CC` to "gcc"::
344
345 $ make clean
346 rm -rf *.o
347 rm target_bin
348 #
349 # Edit the Makefile to comment out the line setting CC to "gcc"
350 #
351 $ make
352 i586-poky-linux-gcc -m32 -march=i586 --sysroot=/opt/poky/2.5/sysroots/i586-poky-linux -I . -c main.c
353 i586-poky-linux-gcc -m32 -march=i586 --sysroot=/opt/poky/2.5/sysroots/i586-poky-linux -I . -c module.c
354 i586-poky-linux-gcc -m32 -march=i586 --sysroot=/opt/poky/2.5/sysroots/i586-poky-linux main.o module.o -o target_bin
355 $ make clean
356 rm -rf *.o
357 rm target_bin
358 $ make CC="gcc"
359 gcc -I . -c main.c
360 gcc -I . -c module.c
361 gcc main.o module.o -o target_bin
362
363 In the previous case, the command-line argument overrides the SDK
364 environment variable.
365
366 In this last case, edit Makefile again to use the "gcc" compiler but
367 then use the "-e" option on the ``make`` command line::
368
369 $ make clean
370 rm -rf *.o
371 rm target_bin
372 #
373 # Edit the Makefile to use "gcc"
374 #
375 $ make
376 gcc -I . -c main.c
377 gcc -I . -c module.c
378 gcc main.o module.o -o target_bin
379 $ make clean
380 rm -rf *.o
381 rm target_bin
382 $ make -e
383 i586-poky-linux-gcc -m32 -march=i586 --sysroot=/opt/poky/2.5/sysroots/i586-poky-linux -I . -c main.c
384 i586-poky-linux-gcc -m32 -march=i586 --sysroot=/opt/poky/2.5/sysroots/i586-poky-linux -I . -c module.c
385 i586-poky-linux-gcc -m32 -march=i586 --sysroot=/opt/poky/2.5/sysroots/i586-poky-linux main.o module.o -o target_bin
386
387 In the previous case, the "-e" option forces ``make`` to
388 use the SDK environment variables regardless of the values in the
389 Makefile.
390
391#. *Execute Your Project:* To execute the project (i.e. ``target_bin``),
392 use the following command::
393
394 $ ./target_bin
395 Hello World!
396
397 .. note::
398
399 If you used the cross-toolchain compiler to build
400 target_bin
401 and your build host differs in architecture from that of the
402 target machine, you need to run your project on the target device.
403
404 As expected, the project displays the "Hello World!" message.