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