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