| Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 1 | <!DOCTYPE chapter PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN" | 
|  | 2 | "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd" | 
|  | 3 | [<!ENTITY % poky SYSTEM "../poky.ent"> %poky; ] > | 
|  | 4 |  | 
|  | 5 | <chapter id='sdk-working-projects'> | 
|  | 6 |  | 
|  | 7 | <title>Using the SDK Toolchain Directly</title> | 
|  | 8 |  | 
|  | 9 | <para> | 
|  | 10 | You can use the SDK toolchain directly with Makefile, | 
|  | 11 | Autotools, and <trademark class='trade'>Eclipse</trademark> based | 
|  | 12 | projects. | 
| Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 13 | This chapter covers the first two, while the | 
|  | 14 | "<link linkend='sdk-eclipse-project'>Developing Applications Using <trademark class='trade'>Eclipse</trademark></link>" | 
|  | 15 | Chapter covers the latter. | 
| Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 16 | </para> | 
|  | 17 |  | 
|  | 18 | <section id='autotools-based-projects'> | 
|  | 19 | <title>Autotools-Based Projects</title> | 
|  | 20 |  | 
|  | 21 | <para> | 
|  | 22 | Once you have a suitable cross-toolchain installed, it is very easy | 
|  | 23 | to develop a project outside of the OpenEmbedded build system. | 
|  | 24 | This section presents a simple "Helloworld" example that shows how | 
|  | 25 | to set up, compile, and run the project. | 
|  | 26 | </para> | 
|  | 27 |  | 
|  | 28 | <section id='creating-and-running-a-project-based-on-gnu-autotools'> | 
|  | 29 | <title>Creating and Running a Project Based on GNU Autotools</title> | 
|  | 30 |  | 
|  | 31 | <para> | 
|  | 32 | Follow these steps to create a simple Autotools-based project: | 
|  | 33 | <orderedlist> | 
|  | 34 | <listitem><para> | 
|  | 35 | <emphasis>Create your directory:</emphasis> | 
|  | 36 | Create a clean directory for your project and then make | 
|  | 37 | that directory your working location: | 
|  | 38 | <literallayout class='monospaced'> | 
|  | 39 | $ mkdir $HOME/helloworld | 
|  | 40 | $ cd $HOME/helloworld | 
|  | 41 | </literallayout> | 
|  | 42 | </para></listitem> | 
|  | 43 | <listitem><para> | 
|  | 44 | <emphasis>Populate the directory:</emphasis> | 
|  | 45 | Create <filename>hello.c</filename>, | 
|  | 46 | <filename>Makefile.am</filename>, | 
|  | 47 | and <filename>configure.ac</filename> files as follows: | 
|  | 48 | <itemizedlist> | 
|  | 49 | <listitem><para> | 
|  | 50 | For <filename>hello.c</filename>, include | 
|  | 51 | these lines: | 
|  | 52 | <literallayout class='monospaced'> | 
|  | 53 | #include <stdio.h> | 
|  | 54 |  | 
|  | 55 | main() | 
|  | 56 | { | 
|  | 57 | printf("Hello World!\n"); | 
|  | 58 | } | 
|  | 59 | </literallayout> | 
|  | 60 | </para></listitem> | 
|  | 61 | <listitem><para> | 
|  | 62 | For <filename>Makefile.am</filename>, | 
|  | 63 | include these lines: | 
|  | 64 | <literallayout class='monospaced'> | 
|  | 65 | bin_PROGRAMS = hello | 
|  | 66 | hello_SOURCES = hello.c | 
|  | 67 | </literallayout> | 
|  | 68 | </para></listitem> | 
|  | 69 | <listitem><para> | 
|  | 70 | For <filename>configure.in</filename>, | 
|  | 71 | include these lines: | 
|  | 72 | <literallayout class='monospaced'> | 
|  | 73 | AC_INIT(hello,0.1) | 
|  | 74 | AM_INIT_AUTOMAKE([foreign]) | 
|  | 75 | AC_PROG_CC | 
|  | 76 | AC_PROG_INSTALL | 
|  | 77 | AC_OUTPUT(Makefile) | 
|  | 78 | </literallayout> | 
|  | 79 | </para></listitem> | 
|  | 80 | </itemizedlist> | 
|  | 81 | </para></listitem> | 
|  | 82 | <listitem><para> | 
|  | 83 | <emphasis>Source the cross-toolchain | 
|  | 84 | environment setup file:</emphasis> | 
|  | 85 | As described earlier in the manual, installing the | 
|  | 86 | cross-toolchain creates a cross-toolchain | 
|  | 87 | environment setup script in the directory that the SDK | 
|  | 88 | was installed. | 
|  | 89 | Before you can use the tools to develop your project, | 
|  | 90 | you must source this setup script. | 
|  | 91 | The script begins with the string "environment-setup" | 
|  | 92 | and contains the machine architecture, which is | 
|  | 93 | followed by the string "poky-linux". | 
|  | 94 | Here is an example that sources a script from the | 
|  | 95 | default SDK installation directory that uses the | 
|  | 96 | 32-bit Intel x86 Architecture and the | 
|  | 97 | &DISTRO_NAME; Yocto Project release: | 
|  | 98 | <literallayout class='monospaced'> | 
|  | 99 | $ source /opt/poky/&DISTRO;/environment-setup-i586-poky-linux | 
|  | 100 | </literallayout> | 
|  | 101 | </para></listitem> | 
|  | 102 | <listitem><para> | 
|  | 103 | <emphasis>Generate the local aclocal.m4 | 
|  | 104 | files and create the configure script:</emphasis> | 
|  | 105 | The following GNU Autotools generate the local | 
|  | 106 | <filename>aclocal.m4</filename> files and create the | 
|  | 107 | configure script: | 
|  | 108 | <literallayout class='monospaced'> | 
|  | 109 | $ aclocal | 
|  | 110 | $ autoconf | 
|  | 111 | </literallayout> | 
|  | 112 | </para></listitem> | 
|  | 113 | <listitem><para> | 
|  | 114 | <emphasis>Generate files needed by GNU coding | 
|  | 115 | standards:</emphasis> | 
|  | 116 | GNU coding standards require certain files in order | 
|  | 117 | for the project to be compliant. | 
|  | 118 | This command creates those files: | 
|  | 119 | <literallayout class='monospaced'> | 
|  | 120 | $ touch NEWS README AUTHORS ChangeLog | 
|  | 121 | </literallayout> | 
|  | 122 | </para></listitem> | 
|  | 123 | <listitem><para> | 
|  | 124 | <emphasis>Generate the configure file:</emphasis> | 
|  | 125 | This command generates the | 
|  | 126 | <filename>configure</filename>: | 
|  | 127 | <literallayout class='monospaced'> | 
|  | 128 | $ automake -a | 
|  | 129 | </literallayout> | 
|  | 130 | </para></listitem> | 
|  | 131 | <listitem><para> | 
|  | 132 | <emphasis>Cross-compile the project:</emphasis> | 
|  | 133 | This command compiles the project using the | 
|  | 134 | cross-compiler. | 
|  | 135 | The | 
|  | 136 | <ulink url='&YOCTO_DOCS_REF_URL;#var-CONFIGURE_FLAGS'><filename>CONFIGURE_FLAGS</filename></ulink> | 
|  | 137 | environment variable provides the minimal arguments for | 
|  | 138 | GNU configure: | 
|  | 139 | <literallayout class='monospaced'> | 
|  | 140 | $ ./configure ${CONFIGURE_FLAGS} | 
|  | 141 | </literallayout> | 
|  | 142 | </para></listitem> | 
|  | 143 | <listitem><para> | 
|  | 144 | <emphasis>Make and install the project:</emphasis> | 
|  | 145 | These two commands generate and install the project | 
|  | 146 | into the destination directory: | 
|  | 147 | <literallayout class='monospaced'> | 
|  | 148 | $ make | 
|  | 149 | $ make install DESTDIR=./tmp | 
|  | 150 | </literallayout> | 
|  | 151 | </para></listitem> | 
|  | 152 | <listitem><para> | 
|  | 153 | <emphasis>Verify the installation:</emphasis> | 
|  | 154 | This command is a simple way to verify the installation | 
|  | 155 | of your project. | 
|  | 156 | Running the command prints the architecture on which | 
|  | 157 | the binary file can run. | 
|  | 158 | This architecture should be the same architecture that | 
|  | 159 | the installed cross-toolchain supports. | 
|  | 160 | <literallayout class='monospaced'> | 
|  | 161 | $ file ./tmp/usr/local/bin/hello | 
|  | 162 | </literallayout> | 
|  | 163 | </para></listitem> | 
|  | 164 | <listitem><para> | 
|  | 165 | <emphasis>Execute your project:</emphasis> | 
|  | 166 | To execute the project in the shell, simply enter | 
|  | 167 | the name. | 
|  | 168 | You could also copy the binary to the actual target | 
|  | 169 | hardware and run the project there as well: | 
|  | 170 | <literallayout class='monospaced'> | 
|  | 171 | $ ./hello | 
|  | 172 | </literallayout> | 
|  | 173 | As expected, the project displays the "Hello World!" | 
|  | 174 | message. | 
|  | 175 | </para></listitem> | 
|  | 176 | </orderedlist> | 
|  | 177 | </para> | 
|  | 178 | </section> | 
|  | 179 |  | 
|  | 180 | <section id='passing-host-options'> | 
|  | 181 | <title>Passing Host Options</title> | 
|  | 182 |  | 
|  | 183 | <para> | 
|  | 184 | For an Autotools-based project, you can use the cross-toolchain | 
|  | 185 | by just passing the appropriate host option to | 
|  | 186 | <filename>configure.sh</filename>. | 
|  | 187 | The host option you use is derived from the name of the | 
|  | 188 | environment setup script found in the directory in which you | 
|  | 189 | installed the cross-toolchain. | 
|  | 190 | For example, the host option for an ARM-based target that uses | 
|  | 191 | the GNU EABI is <filename>armv5te-poky-linux-gnueabi</filename>. | 
|  | 192 | You will notice that the name of the script is | 
|  | 193 | <filename>environment-setup-armv5te-poky-linux-gnueabi</filename>. | 
|  | 194 | Thus, the following command works to update your project and | 
|  | 195 | rebuild it using the appropriate cross-toolchain tools: | 
|  | 196 | <literallayout class='monospaced'> | 
|  | 197 | $ ./configure --host=armv5te-poky-linux-gnueabi \ | 
|  | 198 | --with-libtool-sysroot=<replaceable>sysroot_dir</replaceable> | 
|  | 199 | </literallayout> | 
|  | 200 | <note> | 
|  | 201 | If the <filename>configure</filename> script results in | 
|  | 202 | problems recognizing the | 
|  | 203 | <filename>--with-libtool-sysroot=</filename><replaceable>sysroot-dir</replaceable> | 
|  | 204 | option, regenerate the script to enable the support by | 
|  | 205 | doing the following and then run the script again: | 
|  | 206 | <literallayout class='monospaced'> | 
|  | 207 | $ libtoolize --automake | 
|  | 208 | $ aclocal -I ${OECORE_TARGET_SYSROOT}/usr/share/aclocal [-I <replaceable>dir_containing_your_project-specific_m4_macros</replaceable>] | 
|  | 209 | $ autoconf | 
|  | 210 | $ autoheader | 
|  | 211 | $ automake -a | 
|  | 212 | </literallayout> | 
|  | 213 | </note> | 
|  | 214 | </para> | 
|  | 215 | </section> | 
|  | 216 | </section> | 
|  | 217 |  | 
|  | 218 | <section id='makefile-based-projects'> | 
|  | 219 | <title>Makefile-Based Projects</title> | 
|  | 220 |  | 
|  | 221 | <para> | 
|  | 222 | For Makefile-based projects, the cross-toolchain environment | 
|  | 223 | variables established by running the cross-toolchain environment | 
|  | 224 | setup script are subject to general <filename>make</filename> | 
|  | 225 | rules. | 
|  | 226 | </para> | 
|  | 227 |  | 
|  | 228 | <para> | 
|  | 229 | To illustrate this, consider the following four cross-toolchain | 
|  | 230 | environment variables: | 
|  | 231 | <literallayout class='monospaced'> | 
|  | 232 | <ulink url='&YOCTO_DOCS_REF_URL;#var-CC'>CC</ulink>=i586-poky-linux-gcc -m32 -march=i586 --sysroot=/opt/poky/&DISTRO;/sysroots/i586-poky-linux | 
|  | 233 | <ulink url='&YOCTO_DOCS_REF_URL;#var-LD'>LD</ulink>=i586-poky-linux-ld --sysroot=/opt/poky/&DISTRO;/sysroots/i586-poky-linux | 
|  | 234 | <ulink url='&YOCTO_DOCS_REF_URL;#var-CFLAGS'>CFLAGS</ulink>=-O2 -pipe -g -feliminate-unused-debug-types | 
|  | 235 | <ulink url='&YOCTO_DOCS_REF_URL;#var-CXXFLAGS'>CXXFLAGS</ulink>=-O2 -pipe -g -feliminate-unused-debug-types | 
|  | 236 | </literallayout> | 
|  | 237 | Now, consider the following three cases: | 
|  | 238 | <itemizedlist> | 
|  | 239 | <listitem><para> | 
|  | 240 | <emphasis>Case 1 - No Variables Set in the | 
|  | 241 | <filename>Makefile</filename>:</emphasis> | 
|  | 242 | Because these variables are not specifically set in the | 
|  | 243 | <filename>Makefile</filename>, the variables retain their | 
|  | 244 | values based on the environment. | 
|  | 245 | </para></listitem> | 
|  | 246 | <listitem><para> | 
|  | 247 | <emphasis>Case 2 - Variables Set in the | 
|  | 248 | <filename>Makefile</filename>:</emphasis> | 
|  | 249 | Specifically setting variables in the | 
|  | 250 | <filename>Makefile</filename> during the build results in | 
|  | 251 | the environment settings of the variables being | 
|  | 252 | overwritten. | 
|  | 253 | </para></listitem> | 
|  | 254 | <listitem><para> | 
|  | 255 | <emphasis>Case 3 - Variables Set when the | 
|  | 256 | <filename>Makefile</filename> is Executed from the | 
|  | 257 | Command Line:</emphasis> | 
|  | 258 | Executing the <filename>Makefile</filename> from the | 
|  | 259 | command-line results in the variables being overwritten | 
|  | 260 | with command-line content regardless of what is being set | 
|  | 261 | in the <filename>Makefile</filename>. | 
|  | 262 | In this case, environment variables are not considered | 
|  | 263 | unless you use the "-e" flag during the build: | 
|  | 264 | <literallayout class='monospaced'> | 
|  | 265 | $ make -e <replaceable>file</replaceable> | 
|  | 266 | </literallayout> | 
|  | 267 | If you use this flag, then the environment values of the | 
|  | 268 | variables override any variables specifically set in the | 
|  | 269 | <filename>Makefile</filename>. | 
|  | 270 | </para></listitem> | 
|  | 271 | </itemizedlist> | 
|  | 272 | <note> | 
|  | 273 | For the list of variables set up by the cross-toolchain | 
|  | 274 | environment setup script, see the | 
|  | 275 | "<link linkend='sdk-running-the-sdk-environment-setup-script'>Running the SDK Environment Setup Script</link>" | 
|  | 276 | section. | 
|  | 277 | </note> | 
|  | 278 | </para> | 
|  | 279 | </section> | 
| Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 280 | </chapter> | 
|  | 281 | <!-- | 
|  | 282 | vim: expandtab tw=80 ts=4 | 
|  | 283 | --> |