Skip to content

May 15, 2012

How To: Build a Custom Toolchain Using Crosstool-ng

This was originally posted at dodgejcr.com, however with the site going down and because of how useful I found it, I felt I should repost it as well as walk you through some of the problems I had creating my toolchain. Please note that this is setup around building a toolchain for the HTC Evo Shift which is on the ARMv7 processor.  If you wanted to build a toolchain for something else you would change a few small things, however the underlying process would be the same.

This was originally posted by Dodgejcr On December 11, 2011 (with some minor adjustments by myself):

If you read my How To for compiling a kernel, you should already know what a toolchain is and the purpose of it. If not, I will brief you quickly. The toolchain is a group of binaries that allow you to compile software for a platform that is different than what you are compiling on. EX: Compiling a kernel for an ARMv7 phone (target) on a computer that is either x86 or x64 (host). Its made up of a-lot of tools for different parts of the compile but the crucial parts of it is the GCC, GNU Compiler Collection, and the C-Library (glibc or the preferred uClibC for embedded platforms). It is a very crucial prerequisite for building something for an ARM platform.

To build it, we will first need to grab the latest tarball from HERE. At the time of this writing, the current release is version 1.15.2 and will be whats used in this how to. Go ahead and download the latest to your home directory. Once you have that downloaded, you will need to untar the archive.

tar -xjf crosstool-ng-1.15.2.tar.bz2 # Untar the archive

I highly recommend you familiarize yourself with the tab-completion feature of bash. It will save you time and prevent typos in commands. For example, to run the above command quicker, type ‘tar -xjf crosstool’ and then press the tab key. It will automatically complete the filename. If you have more than one file or directory that contains ‘crosstool’, pressing tab twice usually list the matching files so you can finish typing or narrow down the names. Now moving on. Once you get that extracted, you will find that it outputs a folder named ‘crosstool-ng-1.15.2? go ahead and cd into that folder.

cd crosstool-ng-1.15.2 # Change directory to the crosstool-ng directory, A great time to use the tab-completion

Next we need to grab all dependencies that crosstool needs. Since most people start linux with a beginner distro, they usually use ubuntu or some derivative of it (mint, kubuntu, xubuntu, etc.) So here are the dependencies needed and how to install them on ubuntu.

sudo apt-get install flex bison ncurses-dev texinfo gcc gperf patch libtool automake g++ # Install dependencies [added all the packages after gcc which I needed ~Sean]

If using a different distro, check your package list for the same packages. Several distros come with these tools available in a development package.

Now that you have the dependencies, its time to configure. Configure basically sets the build environment up so that the software can be compiled. It checks for required software and makes sure everything is working correctly to increase your chances of a successful build.

./configure --prefix=$HOME/ctng # Configure crosstool-ng to the /home/username/ctng directory.

If for some reason this errors out, it will most likely be because of a missing dependency. If so, pay attention to the error message and install the missing program.

After a successful configure, the next step is to build the source.

make # Build the source

This may take some time. Once this finishes, its time to install crosstool-ng.

make install # Install the application

After its installed, its a good idea to temporarily put crosstool’s bin directory into your $PATH so that you can call the required binaries from any location.

export PATH=$PATH:$HOME/ctng/bin # Add /home/username/ctng/bin/ to your path temporarily

Once installed, we can use crosstool-ng to build a toolchain. We need to set up a temporary folder for the building process

cd ~ # Change directory back to your home directory

mkdir build # Make a new directory named ‘build’

cd build/ # Go into the build directory

Crosstool-ng comes with some sample configs for several different platforms to make our life easier. Most are set up perfectly for what we need so lets grab them from the samples folder.

cp ../ctng/lib/ct-ng.1.15.2/samples/arm-cortex_a8-linux-gnueabi/* . # Copy everything from the arm-cortex_a8-gnueabi folder into the current directory

mv crosstool.config .config # Rename the sample config to .config

cp ../ctng/lib/ct-ng.1.15.2/samples/arm-unknown-linux-uclibcgnueabi/uClibc-0.9.30.2.config . # Copy a sample uClibc config into the current directory

Note the periods ‘.’ at the end of the copy commands. That is required as it annotates to move the files to the current directory. Once the sample configuration files are in place, its time to start compiling your toolchain. Just like kernels and other software, we start by configuring it to our needs.

ct-ng menuconfig # Start the menu editor for the .config

ct-ng is a simple Makefile renamed for convenience. Once the menu comes up, there are just a few things we need to change for the HTC Evo Shift. Its not a bad idea to scroll through the options and research each to have a better understanding of whats included and how everything comes together but we will not be going into that here. Use the menu to change the following.

C-library -> C library -> uClibc
C-library -> uClibc version -> 0.9.30.2
C-library -> Configuration file -> ${HOME}/build/uClibc-0.9.30.2.config

This will set the compiler to use uClibc as the C-Library which is the much preferred C-Library for small embedded devices such as a phone or tablet. It also points the compiler to the config for uClibc that you copied to ‘/home/username/build/’ earlier. Once these values are changed, go ahead and back out of the menu choosing to save the config upon exit.

Once the config is set up properly, its time to build the toolchain. Since the makefile is named ct-ng, we will use that command paired with a -j flag to specify how many jobs to do simultaneously. I wont get into how to use the -j flag but I will tell you to use no more than the number of cores in your cpu + 2. For example, for a dual core cpu, you would want no more than 4 jobs running simultaneously. So lets build:

ct-ng build.4 # Build the toolchain specifying the jobs (4)

This will take a long time. It has to download several tarballs of source (Linux Kernel source and several different source archives for other software). The amount of time this takes will depend greatly on your connection speed and speed of your cpu. Once it is finished, It will return you back to the shell prompt and you will have a complete toolchain located at ‘/home/username/x-tools’. To clean up, you can delete all other directories you created, ctng, build, crosstool-ng-1.15.2. Enjoy your toolchain and use this to go build something that can make someone’s life easier.

One problem which I ran into was that crosstool was having issues downloading the tarball for duma, in order to resolve this issue i downloaded it manually to the directory that contained all the tarballs it was downloading.  If you are in the build folder you will want to do this:

cd .build/tarball    #    cd to the directory which contains the tarballs
wget sourceforge.net/projects/duma/files/duma/2.5.15/duma_2_5_15.tar.gz    #    download the tarball for duma manually

After about 3 hours of running I had the error come up where it could not find exapt (which at this point I was quite pissed and homicidal, just saying), more specifically this error:

[ERROR]    configure: error: expat is missing or unusable

After doing some googling I wasn’t able to find much information on this error.  However I was able to figure out that I was missing a package with the help of Dodgejcr and Inxane.  I needed to install the libexpat1-dev package from the repos, so if you get the error above run this command and then start the build again (isn’t making toolchains fun!):

sudo apt-get install libexpat1-dev

And with any luck you now have a toolchain for an HTC Evo Shift (or possibly another phone on the same processor).  When dodgejcr is able to get his site back up I will point a link to his site, but I would like to thank him for the directions, as well as inxane for helping me get past the expat problem.  My toolchain built completely (from downloading to compiling and installing) in about an hour and a 45 minutes, this is a long process so you have to be patient.

Share your thoughts, post a comment.

(required)
(required)

Note: HTML is allowed. Your email address will never be published.

Subscribe to comments

%d bloggers like this: