working-projects.rst 14 KB

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