Thursday 6 February 2014

Cross-compiling Toolchain !!

Hi friends...

Today we'll go little bit deeper to understand the tools which are very essential to build embedded Linux kernel for your device...

We'll first try to understand the need of these tools and then we'll learn how to get these tools... :)

Toolchain : If you will see towards the tools such as GCC, GDB, Strace, Ltrace etc which are present in your UBUNTU workstation  is a native toolchain.  That means this toolchain  will run on your workstation and generates code for your workstation( example for x86 processor on your syytem)

For embedded systems (Boards like Beagle bone or other...), we can not use this native toolchain, because the target is little different in terms of storage, memory and in the terms of architecture. Target may not support executable created by native toolchain..

So cross-compiling toolchains are generally used. This cross compiling toolchain will  run on your workstation but it will generate code for your target(according to your target processor architecture..).






Components inside the toolchain :

Binutils : Binutils is a set of tools to generate and manipulate binaries for a given CPU architecture.
ex : as, ld, ar, ranlib etc...

Kernel headers : The C library and compiled programs needs to interact with the kernel. For example to get available system calls and their
numbers or for available data structures, etc.

Thats why compiling the C library requires kernel headers, and many applications also require them.

GCC : GNU Compiler Collection, the famous free software compiler . Can compile C, C++, Ada, Fortran, Java, Objective-C, Objective-C++, and generate code for a large number of CPU architectures, including ARM, AVR, Black n, CRIS, FRV, M32, MIPS, MN10300, PowerPC, SH, v850, i386, x86 64, IA64, Xtensa, etc.

C library : The C library is a very important component of a Linux system Interface between the applications andthe kernel.  It provides the well-known standard C API to ease application development

Several C libraries are available: glibc, uClibc, eglibc, dietlibc, newlib, etc. The choice of the C library for ousr tool chain must be made at the time of the cross-compiling toolchain generation.

We will use uLibc in our toolchain because the Size of uLibc is 4 times smaller than glibc...


Obtaining a Toolchain : 

Now we got enough information about cross-compile tool chain... So before getting our own toolchain for our target which is arm processor, lets see how to get toolchain...

So basically there are two ways to getting our own tool chain...

1) Get a pre-compiled toolchain : Building your own  cross-compiling toolchain by yourself is a di fficult task. It Can take days or weeks, because of omplicated con guration.Lots of component we need like kernel headers and C library source etc...
Thats why people use to install pre-compiled toolchain available in the internet. 

Determine what toolchain you need: CPU, endianism, C library, component versions, ABI, soft oat or hard
flooat, etc.Possible choices for pre-compiled toolchain are Sourcery CodeBench toolchains, Linaro toolchains etc...
For more information about tool chains click here.

For Ubuntu 12.04 you can install a prebuilt toolchain like this very easily follo below steps...

sudo apt-get install gcc-arm-linux-gnueabi
 
Now find out the path and name of the cross-compiler executable by looking at the contents of the package:

dpkg -L gcc-arm-linux-gnueabi 
 
Now u can use your installed toolchain.... :)


2) Build our own toolchain : Its a complicated process which may take few week or few days to understand and to do successfully, because for building toolchain we need to integrate all components of tool chai like c-library, GCC, kernel  headres etc ando also we have to configure it and build it... which is little complecated but more interesting... 

So lets try to build our own toolchain for ARM processor, follow below steps....

a) Install the packages needed for building toolchain, Embedded Linux and u-boot:
sudo apt-get install autoconf automake libtool libexpat1-dev \ libncurses5-dev bison flex patch curl cvs texinfo  build-essential subversion gawk python-dev gperf

b) Download source code of the latest version of toolchain from crosstool-ng website click here to download( which is crosstool-ng-1.19.0.tar.bz2)....

c) Extract toolchain 

 $ tar -jxvf crosstool-ng-1.19.0.bz2 

d) It will give one directory name as crosstool-ng-1.19.0

$cd crosstool-ng-1.19.0

e) Install toolchain using...

$./configure --enable-local 
$make 
$make install

f) Configure the toolchain using...

$./ct-ng arm-unknown-linux-uclibcgnueabi

Then, to refine the configuration, let’s run the menuconfig interface:

$./ct-ng menuconfig

g) In Path and misc options: 
• Change Prefix directory to /usr/local/xtools/${CT_TARGET}. This is the place where the toolchain will be installed. 
• Change Maximum log level to see to DEBUG so that we can have more details on what happened during the build in case something went wrong. 

In Toolchain options: 

• Set Tuple’s alias to arm-linux. This way, we will be able to use the compiler as arm-linux-gcc instead of arm-unknown-linux-uclibcgnueabi-gcc, which is much longer to type. In Debug facilities:
• Enable gdb, strace and ltrace. 
• Remove the other options (dmalloc and duma). 
• In gdb options: – Make sure that the Cross-gdb and Build a static gdbserver options are enabled; the other options are not needed. – Set gdb version to 7.4.1.

h) Produce the toolchain...

$./ct-ng build

And wait...

This 8 steps must be done under non-root user... In super user login it will not work...

So i told building our own toolchain is a complicated... why ? because when you will build tool chai by above step you'll get make error several time that is...  "Source archives not found on the Internet"

 It is frequent that Crosstool-ng aborts because it can’t find a source archive on the Internet, when such an archive has moved or has been replaced by more recent versions. New Crosstoolng versions ship with updated URLs, but in the same time we have do our work... :)

So, If this happens to you, what you can do is search for the source archive by yourself on the Internet, and copy such an archive to the same directory where it is unable to find archive. Then, all you have to do is run ./ct-ng build again, and it will use the source archive that you downloaded.


So this is how we can play with tool chain... I hope you will do it successfully... keep trying n dont ever give up !!

Thats it for today !!
Gud Bye :)

Abhishek Mourya

No comments:

Post a Comment