working-projects.rst 14 KB

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