working-projects.rst 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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 :wikipedia:`GNU
  11. Autotools-based <GNU_Build_System>` workflow, which is outside of the
  12. :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. #. *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. $ 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. #. *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. Another example is sourcing the environment setup directly in a Yocto
  66. build::
  67. $ source tmp/deploy/images/qemux86-64/environment-setup-core2-64-poky-linux
  68. #. *Create the configure Script:* Use the ``autoreconf`` command to
  69. generate the ``configure`` script::
  70. $ autoreconf
  71. The ``autoreconf``
  72. tool takes care of running the other Autotools such as ``aclocal``,
  73. ``autoconf``, and ``automake``.
  74. .. note::
  75. If you get errors from ``configure.ac``, which ``autoreconf``
  76. runs, that indicate missing files, you can use the "-i" option,
  77. which ensures missing auxiliary files are copied to the build
  78. host.
  79. #. *Cross-Compile the Project:* This command compiles the project using
  80. the cross-compiler. The
  81. :term:`CONFIGURE_FLAGS`
  82. environment variable provides the minimal arguments for GNU
  83. configure::
  84. $ ./configure ${CONFIGURE_FLAGS}
  85. For an Autotools-based
  86. project, you can use the cross-toolchain by just passing the
  87. appropriate host option to ``configure.sh``. The host option you use
  88. is derived from the name of the environment setup script found in the
  89. directory in which you installed the cross-toolchain. For example,
  90. the host option for an ARM-based target that uses the GNU EABI is
  91. ``armv5te-poky-linux-gnueabi``. You will notice that the name of the
  92. script is ``environment-setup-armv5te-poky-linux-gnueabi``. Thus, the
  93. following command works to update your project and rebuild it using
  94. the appropriate cross-toolchain tools::
  95. $ ./configure --host=armv5te-poky-linux-gnueabi --with-libtool-sysroot=sysroot_dir
  96. #. *Make and Install the Project:* These two commands generate and
  97. install the project into the destination directory::
  98. $ make
  99. $ make install DESTDIR=./tmp
  100. .. note::
  101. To learn about environment variables established when you run the
  102. cross-toolchain environment setup script and how they are used or
  103. overridden by the Makefile, see the
  104. :ref:`sdk-manual/working-projects:makefile-based projects` section.
  105. This next command is a simple way to verify the installation of your
  106. project. Running the command prints the architecture on which the
  107. binary file can run. This architecture should be the same
  108. architecture that the installed cross-toolchain supports::
  109. $ file ./tmp/usr/local/bin/hello
  110. #. *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. $ echo ${CC}
  155. $
  156. Running the
  157. SDK setup script for a 64-bit build host and an i586-tuned target
  158. architecture for a ``core-image-sato`` image using the current &DISTRO;
  159. Yocto Project release and then echoing that variable shows the value
  160. established through the script::
  161. $ source /opt/poky/&DISTRO;/environment-setup-i586-poky-linux
  162. $ echo ${CC}
  163. i586-poky-linux-gcc -m32 -march=i586 --sysroot=/opt/poky/&DISTRO;/sysroots/i586-poky-linux
  164. To illustrate variable use, work through this simple "Hello World!"
  165. example:
  166. #. *Create a Working Directory and Populate It:* Create a clean
  167. directory for your project and then make that directory your working
  168. location::
  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. #. *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. Another example is sourcing the environment setup directly in a Yocto
  207. build::
  208. $ source tmp/deploy/images/qemux86-64/environment-setup-core2-64-poky-linux
  209. #. *Create the Makefile:* For this example, the Makefile contains
  210. two lines that can be used to set the :term:`CC` variable. One line is
  211. identical to the value that is set when you run the SDK environment
  212. setup script, and the other line sets :term:`CC` to "gcc", the default
  213. GNU compiler on the build host::
  214. # CC=i586-poky-linux-gcc -m32 -march=i586 --sysroot=/opt/poky/2.5/sysroots/i586-poky-linux
  215. # CC="gcc"
  216. all: main.o module.o
  217. ${CC} main.o module.o -o target_bin
  218. main.o: main.c module.h
  219. ${CC} -I . -c main.c
  220. module.o: module.c module.h
  221. ${CC} -I . -c module.c
  222. clean:
  223. rm -rf *.o
  224. rm target_bin
  225. #. *Make the Project:* Use the ``make`` command to create the binary
  226. output file. Because variables are commented out in the Makefile, the
  227. value used for :term:`CC` is the value set when the SDK environment setup
  228. file was run::
  229. $ make
  230. i586-poky-linux-gcc -m32 -march=i586 --sysroot=/opt/poky/2.5/sysroots/i586-poky-linux -I . -c main.c
  231. i586-poky-linux-gcc -m32 -march=i586 --sysroot=/opt/poky/2.5/sysroots/i586-poky-linux -I . -c module.c
  232. i586-poky-linux-gcc -m32 -march=i586 --sysroot=/opt/poky/2.5/sysroots/i586-poky-linux main.o module.o -o target_bin
  233. From the results of the previous command, you can see that
  234. the compiler used was the compiler established through the :term:`CC`
  235. variable defined in the setup script.
  236. You can override the :term:`CC` environment variable with the same
  237. variable as set from the Makefile by uncommenting the line in the
  238. Makefile and running ``make`` again::
  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. #. *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.