Difference between revisions of "EBC Exercise 08a Cross-Compiling"

From eLinux.org
Jump to: navigation, search
m (Updated for using compile loaded with kernel)
(55 intermediate revisions by 6 users not shown)
Line 1: Line 1:
 
[[Category:ECE497]]
 
[[Category:ECE497]]
 
[[Category: BeagleBoard]]
 
[[Category: BeagleBoard]]
 +
{{YoderHead}}
  
This class is about developing software for embedded Linux. The [http://elinux.org/Main_Page eLinux site] is a good source for embedded Linux in general. There are many ongoing embedded efforts going on many platforms. Poke around the site a while to get a feel for what's happening.
+
This class is about developing software for embedded Linux. So far we have been doing all of our development on the Beagle. This works well for small (and not so small) programs. However, we are now moving into kernel development and that's best done on a more powerful host computer. In [[EBC_Exercise_08_Installing_Development_Tools_4.4]] you learned how to download and install the cross compilers and the source for kernel and u-boot. Now we'll use those tools.
  
We are going to use the [http://www.angstrom-distribution.org Ångström Distribution]It's available many platforms. Look around the site, you may recognize some of them.
+
== Cross compiling Hello World ==
 +
Normally when you compile you compile on the machine that will run the code.  You can compile and run on the Bone, but sometimes (like when compiling the kernel) it's better to use a more powerful machine for the compilingFirst we'll compile ''helloWorld.c'' on the host computer and run it there, then we'll cross compile it on the host to run on the Bone.
  
== Get my setup from dfs ==
+
If you've set up your git repository you will find it in '''helloWorld.c''' when you do a''' git pull'''.  Compile and run it on your host to be sure it works.
  
I like to keep all things Beagle in a BeagleBoard directory. Here's how to match my setup:
+
host$ '''gcc helloWorld.c'''
<pre>
+
host$ '''file a.out'''
$ cd
+
a.out: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV),
$ mkdir -p BeagleBoard/oe
+
  dynamically linked (uses shared libs), for GNU/Linux 2.6.24,
$ cd BeagleBoard
+
  BuildID[sha1]=0x357e34e90f7c32d414368d69cc06d0aed59acf1c, not stripped
$ sftp username@dfs.rose-hulman.edu
+
host$ '''./a.out'''
Connecting to dfs.rose-hulman.edu...
+
Hello, World! Main is executing at 0x400524
username@rose-hulman.edu's password:
+
This address (0x7fff8260bdf8) is in our stack frame
sftp> cd Users/Y/yoder/Shared/BeagleBoard/oe
+
This address (0x601038) is in our bss section
sftp> ls
+
This address (0x601020) is in our data section
sftp> get oebb.sh.tar.gz
 
sftp> get sources.tar.gz
 
sftp> get build.tar.gz
 
exit
 
$ cd oe
 
$ tar xvf oebb.sh.tar.gz
 
$ tar xvf sources.tar.gz
 
$ tar xvf build.tar.gz
 
</pre>
 
  
Once you have everything in place you need to create the script that will get the environmental variables.
+
Now that you know it's working, let's cross compile it. First set the paths to find the cross-compiler. Put the following in a file, call it '''~/crossCompileEnv.sh'''.
  
<pre>
+
export ARCH=arm
$ rm -rf build/conf
+
export CROSS_COMPILE=arm-linux-gnueabihf-
$ rm ~/.oe/environment-2008
+
export PATH=$PATH:~/BeagleBoard/bb-kernel/dl/gcc-linaro-5.3-2016.02-x86_64_arm-linux-gnueabihf/bin
$ ./oebb.sh config beagleboard
+
Make sure the PATH you use goes to the bin directory where the cross compiler is installed.
</pre>
 
The first two commands remove the configuration files that were set up for my machine. The third command creates them for your machine. It will also create the file <code>~/.oe/environment-2008</code> and make sure your repositories are up to date.
 
  
One last thing is to
+
Now ''source'' the file and compile again. (Note: you only have to source once per terminal session.)
 +
host$ '''source ~/crossCompileEnv.sh'''
 +
host$ '''${CROSS_COMPILE}gcc helloWorld.c'''
 +
host$ '''file a.out'''
 +
a.out: ELF 32-bit LSB  executable, ARM, EABI5 version 1 (SYSV),
 +
  dynamically linked (uses shared libs), for GNU/Linux 2.6.32,
 +
  BuildID[sha1]=b9222cbcee442470c7b89ac294e392a631c82264, not stripped
  
<pre>
+
The '''file''' command tells what's in the file.  In this case we have an ARM executable. Success!  Now copy to your Beagle and run
$ cd build/tmp-angstrom_2008_1
 
$ rm saved_tmpdir
 
</pre>
 
The <code>saved_tmpdir</code> file remembers where you last had your TMPDIRRemoving the file makes it forget that you've moved things since the time I built everything.
 
  
== Compile the Kernel and u-boot via bitbake ==
+
host$ '''scp a.out root@192.168.7.2:.'''
 +
host$ '''ssh root@192.168.7.2 ./a.out'''
 +
Hello, World! Main is executing at 0x8374
 +
This address (0xbeb32d4c) is in our stack frame
 +
This address (0x10650) is in our bss section
 +
This address (0x10648) is in our data section
  
Before bitbaking be sure to source this file:
+
The '''scp''' copies ''a.out'' to the beagle and the '''ssh''' runs the ''a.out'' on the beagle. Notice the addresses are very different from the host version.
<pre>
 
$ source ~/.oe/environment-2008
 
</pre>
 
If you want to configure the kernel, you need to prevent the kernel sources from being deleted:
 
<pre>
 
cd build/conf
 
gedit local.conf
 
</pre>
 
Find the line near the top that says <code>INHERIT += " rm_work "</code> and comment it out.
 
<pre>
 
# INHERIT += " rm_work "
 
</pre>
 
Now you can now compile your own kernel and u-boot:
 
<pre>
 
$ time bitbake linux-omap-psp-2.6.32
 
$ time bitbake u-boot
 
</pre>
 
I've added the <code>time</code> command to the bitbake to see how long it takes.  Tell me how long it takes on your system.
 
  
== Finding the Kernel to copy it ==
+
{{YoderFoot}}
 
 
Once your compile has finished, where is uImage?  One way to find it is
 
 
 
<pre>
 
$ cd ~/BeagleBoard/oe
 
$ find . -name "*uImage*"
 
</pre>
 
You'll find several files with uImage in the name.  The one you want is
 
 
 
<pre>
 
$ cd ~/BeagleBoard/oe/build/tmp-angstrom_2008
 
$ ls
 
</pre>
 
 
 
Here you'll see several directories here.  If you <code>cd work</code> you will find the source code.  We are going to go this way.
 
<pre>
 
$ cd deploy/glibc
 
</pre>
 
 
 
Here you will find some more directories worth exploring.  What do you find in <code>sources</code>?  Finally
 
<pre>
 
$ cd images/beagleboard
 
</pre>
 
Now you have found uImage.  Load away.
 
 
 
== Compile via make ==
 
 
 
When you use bitbake it sets up all the paths to use the correct cross compilers.  You can also build the kernel or u-boot by using '''make''' if you set the paths yourself.  Here's what you need to do.  Put the following in a file, call it ~/BeagleBoard/oe/'''crossCompileEnv.sh'''. Make sure the path is correct for your system.
 
<pre>
 
PATH=~/BeagleBoard/oe/build/tmp-angstrom_2008_1/sysroots/i686-linux/usr/armv7a/bin:$PATH  # add cross tools to your path
 
export ARCH=arm
 
export CROSS_COMPILE=arm-angstrom-linux-gnueabi-
 
</pre>
 
Then ''source'' the file and ''cd'' to the kernel directory and try a ''make''.
 
<pre>
 
$ source ~/BeagleBoard/oe/crossCompileEnv.sh
 
$ cd ~/BeagleBoard/oe/build/tmp-angstrom_2008_1/work/beagleboard-angstrom-linux-gnueabi/linux-omap-psp-2.6.32-r88+gitra6bad4464f985fdd3bed72e1b82dcbfc004d7869/git
 
$ make xconfig
 
</pre>
 
To make the kernel and u-boot run
 
<pre>
 
$ make uImage
 
$ make u-boot
 
</pre>
 
Where do these put the new uImage?  It's not where bitbake puts them.  Here's how I found them.
 
<pre>
 
$ find . -name uImage
 
</pre>
 
 
 
If you would prefer to maintain your own kernel source tree outside of OE, see these directions:
 
[[BeagleBoardLinuxKernel]] Alternatively it is possible to run the official omap branch of the linux kernel.  Take a peek at this page:
 
[[BeagleBoard#Linux_kernel]].
 
 
 
== Here's what I did to create the files above ==
 
 
 
You don't have to do the following.  This is what I have already done.
 
 
 
Instructions for building Ångström are given [http://www.angstrom-distribution.org/building-angstrom here]; however there are a few changes you have to make.  Here's what I did. 
 
 
 
=== Get the setup scripts ===
 
 
 
I have everything in a <code>~/BeagleBoard</code>, so here is how I setup things:
 
<pre>
 
$ cd
 
$ mkdir -p BeagleBoard
 
$ cd BeagleBoard
 
$ git clone git://git.angstrom-distribution.org/setup-scripts oe
 
$ cd oe
 
</pre>
 
This creates a directory for open embedded (<code>oe</code>) and gets the script to download Ångström.  What files do you see?
 
 
 
Edit <code>oebb.sh</code> and replace every place you find
 
 
 
<pre>
 
git://git.openembedded.org/openembedded
 
</pre>
 
 
 
with
 
 
 
<pre>
 
git://git.openembedded.org/openembedded.git
 
</pre>
 
 
 
Also replace
 
 
 
<pre>
 
INHERIT += "rm_work"
 
</pre>
 
 
 
with
 
<pre>
 
# INHERIT += "rm_work"
 
</pre>
 
 
 
Save the file and then run
 
 
 
<pre>
 
MACHINE=beagleboard ./oebb.sh update
 
</pre>
 
This will bring in all the tools you need.  It took about 30 minutes on my machine in the afternoon or 3 minutes at 6:15am.
 
 
 
=== Point to the git snapshot that was used to create the image ===
 
 
 
The original SD card image came from [http://beagleboard-validation.s3.amazonaws.com/deploy/201008201549/sd/list.html here].  The file [http://beagleboard-validation.s3.amazonaws.com/deploy/201008201549/sd/ec2build.sh ec2buld.sh] shows how the image was build.  There are a couple of interesting things in this file. 
 
 
 
Line 564 shows an additional git repository was used.
 
 
 
<pre>
 
$ cd sources/openembedded
 
$ git remote add myrepo git://gitorious.org/~Jadon/angstrom/jadon-openembedded.git
 
$ git remote update
 
</pre>
 
 
 
Run this git command to get access to this repository. (It took about 2 minutes at 6:20am.) Line 21 shows the hash tag for the build.
 
 
 
<pre>
 
ANGSTROM_REPO_ID=24805033b1205acc35f8b4d75cc42f8b9c2a1b38
 
</pre>
 
 
 
If we pull from this tag we will have the same files as were used to generate the SD image.  So do the following
 
 
 
<pre>
 
$ git checkout 24805033b1205acc35f8b4d75cc42f8b9c2a1b38
 
</pre>
 
This takes about 10 seconds.
 
 
 
=== Compile the Kernel ===
 
 
 
Now if you do
 
 
 
<pre>
 
$ source ~/.oe/enviroment-2008
 
$ cd ~/BeagleBoard/oe
 
$ ./oebb.sh config beagleboard
 
$ bitbake linux-omap-psp-2.6.32
 
</pre>
 
you should get a kernel that will work.
 
 
 
To ensure you keep the kernel source, check your .../oe/build/conf/local.conf and comment out the line that says
 
<pre>
 
INHERIT += "rm_work"
 
</pre>
 
If you accidentally run bitbake without commenting out the above line, after it runs you can comment out the INHERIT line and then run
 
<pre>
 
$ bitbake -c clean linux-omap-psp-2.6.32
 
$ bitbake -f -c compile linux-omap-psp-2.6.32
 
</pre>
 
This second run should take less time (one timing: first run took 3.5 hours; the recompiling took 25 minutes).
 

Revision as of 12:02, 20 July 2016

thumb‎ Embedded Linux Class by Mark A. Yoder


This class is about developing software for embedded Linux. So far we have been doing all of our development on the Beagle. This works well for small (and not so small) programs. However, we are now moving into kernel development and that's best done on a more powerful host computer. In EBC_Exercise_08_Installing_Development_Tools_4.4 you learned how to download and install the cross compilers and the source for kernel and u-boot. Now we'll use those tools.

Cross compiling Hello World

Normally when you compile you compile on the machine that will run the code. You can compile and run on the Bone, but sometimes (like when compiling the kernel) it's better to use a more powerful machine for the compiling. First we'll compile helloWorld.c on the host computer and run it there, then we'll cross compile it on the host to run on the Bone.

If you've set up your git repository you will find it in helloWorld.c when you do a git pull. Compile and run it on your host to be sure it works.

host$ gcc helloWorld.c
host$ file a.out
a.out: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), 
 dynamically linked (uses shared libs), for GNU/Linux 2.6.24, 
 BuildID[sha1]=0x357e34e90f7c32d414368d69cc06d0aed59acf1c, not stripped
host$ ./a.out
Hello, World! Main is executing at 0x400524
This address (0x7fff8260bdf8) is in our stack frame
This address (0x601038) is in our bss section
This address (0x601020) is in our data section

Now that you know it's working, let's cross compile it. First set the paths to find the cross-compiler. Put the following in a file, call it ~/crossCompileEnv.sh.

export ARCH=arm
export CROSS_COMPILE=arm-linux-gnueabihf-
export PATH=$PATH:~/BeagleBoard/bb-kernel/dl/gcc-linaro-5.3-2016.02-x86_64_arm-linux-gnueabihf/bin

Make sure the PATH you use goes to the bin directory where the cross compiler is installed.

Now source the file and compile again. (Note: you only have to source once per terminal session.)

host$ source ~/crossCompileEnv.sh
host$ ${CROSS_COMPILE}gcc helloWorld.c
host$ file a.out
a.out: ELF 32-bit LSB  executable, ARM, EABI5 version 1 (SYSV), 
 dynamically linked (uses shared libs), for GNU/Linux 2.6.32, 
 BuildID[sha1]=b9222cbcee442470c7b89ac294e392a631c82264, not stripped

The file command tells what's in the file. In this case we have an ARM executable. Success! Now copy to your Beagle and run

host$ scp a.out root@192.168.7.2:.
host$ ssh root@192.168.7.2 ./a.out
Hello, World! Main is executing at 0x8374
This address (0xbeb32d4c) is in our stack frame
This address (0x10650) is in our bss section
This address (0x10648) is in our data section

The scp copies a.out to the beagle and the ssh runs the a.out on the beagle. Notice the addresses are very different from the host version.




thumb‎ Embedded Linux Class by Mark A. Yoder