summaryrefslogtreecommitdiffstats
path: root/doc/book-enea-linux-user-guide/doc/application_development.xml
blob: a94c42287ba1bc9a34a887ee86ce891b9a5f0fdc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
<?xml version="1.0" encoding="ISO-8859-1"?>
<chapter id="app_dev">
  <title>Application Development</title>

  <section id="cross_comp_apps">
    <title>Cross-Compiling Applications</title>

    <para>Running <command>make</command> cross-compiles your applications
    according to the environment settings. By installing a Cross-Compilation
    Toolchain for your target, the environment variables are set up for
    cross-compilation. For more details, see <xref
    linkend="install_el_sdk" />.</para>

    <para>Once the cross-compilation environment is set up, you can make your
    applications as usual and get them compiled for your target. Below you see
    how to cross-compile from the command line. If Eclipse is installed, you
    can also cross-compile your code from the Eclipse IDE. For more details,
    see <xref linkend="crosscomp" />.</para>

    <orderedlist>
      <listitem>
        <para>Create a Makefile for your application. Example of a simple
        Makefile and application:</para>

        <programlisting>helloworld:helloworld.o
        $(CC) -o helloworld helloworld.o

clean:
        rm -f *.o helloworld</programlisting>

        <programlisting>#include &lt;stdio.h&gt;

int main(void) {
    printf("Hello World\n");
    return 0;
}</programlisting>
      </listitem>

      <listitem>
        <para>Run <command>make</command> to cross-compile your application
        according to the environment set up:</para>

        <programlisting>$ make</programlisting>

        <tip>
          <para>If the build fails, check if the GNU Make workaround solves
          your problem.</para>
        </tip>
      </listitem>

      <listitem>
        <para>Copy the <filename>helloworld</filename> program to your target
        and run it:</para>

        <programlisting># ./helloworld
Hello World</programlisting>
      </listitem>
    </orderedlist>
  </section>

  <section id="cross_comp_kern_mod">
    <title>Cross-Compiling Kernel modules</title>

    <para>Running <command>make</command> cross-compiles your kernel modules
    according to the environment settings. By installing a Cross-Compilation
    Toolchain for your target, the environment variables are set up for
    cross-compilation. For more details, see <xref
    linkend="install_el_sdk" />.</para>

    <para>Before cross-compiling kernel modules, you need to make sure the
    installed toolchain includes the kernel source tree, which should be
    available at:
    <literal>&lt;sdkdir&gt;/sysroots/&lt;targetarch&gt;-enea-linux/usr/src/kernel</literal>.
    If the kernel source tree is not included in the toolchain, you need to
    add the kernel-dev package into the image recipe enea-image-&lt;name&gt;
    and build the toolchain again.</para>

    <para>Once the cross-compilation environment is set up, you can make your
    kernel modules as usual and get them compiled for your target. Below you
    see how to cross-compile a kernel module.</para>

    <orderedlist>
      <listitem>
        <para>Create a Makefile for the kernel module. Example of a simple
        Makefile and kernel module:</para>

        <programlisting>obj-m := hello.o
PWD := $(shell pwd)

KERNEL_SRC := &lt;full path to kernel source tree&gt;

all: scripts
    $(MAKE) -C $(KERNEL_SRC) M=$(PWD) LDFLAGS="" modules
scripts:
    $(MAKE) -C $(KERNEL_SRC) scripts
clean:
    $(MAKE) -C $(KERNEL_SRC) M=$(PWD) LDFLAGS="" clean</programlisting>

        <programlisting>#include &lt;linux/module.h&gt;       /* Needed by all modules */
#include &lt;linux/kernel.h&gt;       /* Needed for KERN_INFO */
#include &lt;linux/init.h&gt;         /* Needed for the macros */

static int __init hello_start(void)
{
    printk(KERN_INFO "Loading hello module...\n");
    printk(KERN_INFO "Hello, world\n");
    return 0;
}

static void __exit hello_end(void)
{
    printk(KERN_INFO "Goodbye, world\n");
}

module_init(hello_start);
module_exit(hello_end);

MODULE_LICENSE("GPL");</programlisting>
      </listitem>

      <listitem>
        <para>Run <command>make</command> to cross-compile your kernel module
        according to the environment set up:</para>

        <programlisting>$ make</programlisting>
      </listitem>

      <listitem>
        <para>Copy the kernel module <filename>hello.ko</filename> to your
        target and install/remove it:</para>

        <programlisting># insmod hello.ko
# rmmod hello.ko
# dmesg
[...] Loading hello module...
[...] Hello, world
[...] Goodbye, world</programlisting>
      </listitem>
    </orderedlist>
  </section>

  <section id="use_devtool">
    <title>Add an Application using devtool</title>

    <para>As a prerequisite, you need to install the SDK and set up the
    environment. For this, see <xref linkend="install_el_sdk" />.</para>

    <para>The following section, <ulink
    url="http://www.yoctoproject.org/docs/2.5/sdk-manual/sdk-manual.html#sdk-use-devtool-to-add-an-application">Use
    devtool add to Add an Application</ulink>, in Yocto Project Software
    Development Kit (SDK) Developer's Guide, explains how to use devtool to
    generate recipes from existing application code, edit and build recipes,
    and deploy the output on target. If needed, replace the Yocto version in
    the link.</para>

    <para>This example will show how to generate a recipe from the existing
    application code and Makefile, edit the recipe in order to provide an
    installation path for the application, build the recipe, and deploy the
    output on a target, or create a RPM package from the recipe and install
    the package.</para>

    <orderedlist>
      <listitem>
        <para>Create a simple application and Makefile in
        <literal>&lt;workspace_dir&gt;/my_app</literal>:</para>

        <para><literal>&lt;workspace_dir&gt;/my_app/my_hello_app.c</literal></para>

        <programlisting>#include &lt;stdio.h&gt;

int main(void)
{
    printf("Hello world!\n");
    return 0;
}</programlisting>

        <para><literal>&lt;workspace_dir&gt;/my_app/Makefile</literal></para>

        <programlisting>my_hello_app:</programlisting>
      </listitem>

      <listitem>
        <para>Generate a recipe (my-hello-recipe) using the source tree of the
        application (<literal>&lt;workspace_dir&gt;/my_app</literal>):</para>

        <programlisting>$ devtool add my-hello-recipe &lt;workspace_dir&gt;/my_app
NOTE: Using source tree as build directory since that would be the default for this
recipe
NOTE: Recipe &lt;SDK_dir&gt;/workspace/recipes/my-hello-recipe/my-hello-recipe.bb has been
automatically created; further editing may be required to make it fully functional</programlisting>

        <programlisting>$ cat &lt;SDK_dir&gt;/workspace/recipes/my-hello-recipe/my-hello-recipe.bb
# Recipe created by recipetool
# This is the basis of a recipe and may need further editing in order to be fully
# functional.
# (Feel free to remove these comments when editing.)
#
# Unable to find any files that looked like license statements. Check the
# accompanying documentation and source headers and set LICENSE and
# LIC_FILES_CHKSUM accordingly.
#
# NOTE: LICENSE is being set to "CLOSED" to allow you to at least start building - if
# this is not accurate with respect to the licensing of the software being built (it
# will not be in most cases) you must specify the correct value before using this
# recipe for anything other than initial testing/development!
LICENSE = "CLOSED"
LIC_FILES_CHKSUM = ""

# No information for SRC_URI yet (only an external source tree was specified)
SRC_URI = ""


# NOTE: this is a Makefile-only piece of software, so we cannot generate much of the
# recipe automatically - you will need to examine the Makefile yourself and ensure
# that the appropriate arguments are passed in.

do_configure () {
    # Specify any needed configure commands here
    :
}

do_compile () {
    # You will almost certainly need to add additional arguments here
    oe_runmake
}

do_install () {
    # NOTE: unable to determine what to put here - there is a Makefile but no
    # target named "install", so you will need to define this yourself
    :
}</programlisting>
      </listitem>

      <listitem>
        <para>Edit the recipe to provide an installation path for the
        application:</para>

        <programlisting>$ devtool edit-recipe my-hello-recipe</programlisting>

        <programlisting>$ cat &lt;SDK_dir&gt;/workspace/recipes/my-hello-recipe/my-hello-recipe.bb
...
do_install () {
    # NOTE: unable to determine what to put here - there is a Makefile but no
    # target named "install", so you will need to define this yourself
    install -d ${D}${bindir}
    install -m 0777 ${S}/my_hello_app ${D}${bindir}
}
...</programlisting>
      </listitem>

      <listitem>
        <para>Build the recipe:</para>

        <programlisting>$ devtool build my-hello-recipe</programlisting>

        <para>The recipe build results can be seen here:
        <literal>&lt;SDK_dir&gt;/tmp/work/&lt;arch&gt;-enea-linux/my-hello-recipe/1.0-r0/</literal></para>
      </listitem>

      <listitem>
        <para>Deploy the output (in this case, the application) on
        target:</para>

        <programlisting>$ devtool deploy-target my-hello-recipe root@&lt;target_ip_address&gt;
Parsing recipes..done.
NOTE: Successfully deployed
&lt;SDK_dir&gt;/tmp/work/&lt;arch&gt;-enea-linux/my-hello-recipe/1.0-r0/image</programlisting>

        <para>As an alternative you can create a RPM package:</para>

        <programlisting>$ devtool package my-hello-recipe
...
NOTE: Your packages are in &lt;SDK_dir&gt;/tmp/deploy/deb</programlisting>

        <para>Then copy the RPM package on the target and install it using
        rpm:</para>

        <programlisting># rpm -i my-hello-recipe-1.0-r0.1.&lt;arch&gt;.rpm</programlisting>
      </listitem>
    </orderedlist>
  </section>
</chapter>