Why & How to create Custom Toolchain ?

Hamza Ali Imran
Emumba
Published in
6 min readAug 10, 2019

--

When there are toolchains available for your board why would you need to make your own and how to make it ?

A toolchain is a set of different tools that are linked together for compiling applications. The components of a toolchain may include tools like gcc, binutils, and glibc, etc. A toolchain may also contain other tools like debuggers etc. They are of different types depending on the systems on which they execute, called host systems and the systems for which they produce compiled files, called target systems.

Cross-compiler toolchains are the one which runs on some host system but produces compiled files for another type of target systems. The process of compiling code for a different type of system is called cross-compilation. Usually, cross-compilation is used for compiling tools for development boards like Raspberry Pi or Banana Pi, etc, which have fewer resources especially RAM.

Companies usually provide toolchains for their boards and even third-party toolchains from Linaro are also present for a few boards. But still, we compile custom toolchains. The question arises why? The answer is simple and that is customization. Toolchains that are made by others have a particular version of tools like gcc, g++, glibc, etc. Moreover, in the case of systems that run Linux, kernel versions for toolchains are simply there we can’t select version of our own choice. Now if we want to have control over these packages then the only way left to us is to compile our own toolchain.

Luckily there are tools available that make this task very easy for us. In this article, we will be sharing our experience of compiling a custom toolchain for Banana Pi BPI-R-18-AI (Allwinner SoC-Only 3-Mic Far-Field Dev Kit) by using Crosstool-NG tool.

Crosstool-NG is a tool that helps us making custom toolchains for our systems. The good thing about this tool is; we can choose kernel, gcc , glibc versions, etc according to our choice. Even the choice of using uclibc or glibc is available.

If you clone Crosstool-NG from master branch of its git repository you will get its 1.24 version which community claims is stable version but we have very different experience.

So we don’t want you to get into trouble which we have faced. Its 1.23 version is stable and works fine and smoothly.

Lets get to the actually work !

Tools Required to Run Crosstool-NG

I worked on Linux Mint-19.1 (which is based on Ubuntu) but this procedure should work for any Debian based system. We first need to install few dependencies which are required to make Crosstool-NG work.

Type following commands to install dependencies.

Clone and Install Crosstool-NG

Now as we have all dependence we can now install Crosstool-NG. First we need to download its source code and then compile and install it. Following are the commands to do that.

Crosstool-NG already has few sample configuration settings available in it for different systems . In-order to check the list of available system type following command

$ ct-ng list-samples

If you get output like shown below then you have followed everything correctly. If that is not the case, you have to go back and see where you have made a mistake.

Luckily the system for which are interested is already present in the list and that is “aarch64-unknown-linux-gnueabi”.

Customizing Configurations

We do have predefined configurations for the system we are interested in generating a toolchain for. But the main purpose of generating it was to have control on versions of glibc , kernel, gcc and etc. Now we are going to modifying the predefined configurations according to our needs.

There is a Ubuntu Mate Image available for Banana Pi BPI-R-18-AI (Allwinner SoC-Only 3-Mic Far-Field Dev Kit) we have to actually compile some tools to run on this board on this OS. So we had to compiled a toolchain which should work well with such a setup. So we manually checked some information form the board. Following was that information which we found.

Kernel

4.4.89-BPI-M64-Kernel

GCC Version

gcc (Ubuntu/Linaro 5.4.0–6ubuntu1~16.04.11) 5.4.0 20160609

glibc Version

(Ubuntu GLIBC 2.23–0ubuntu11) 2.23

Architecture of Processor

aarch64 GNU/Linux

In Crosstool-NG we have a list of versions available for particular tools. We can select one out of them . So in our case we had the exact versions of glibc and gcc available but in case of kernel we didn’t had the exact version. On checking with “uname -r” on board it was showing 4.4.89 version of kernel but if we compile file on board and check file’s details by “file” command we found that native toolchain running on system was compiled for kernel 3.7.0. This showed that backwards compatibility was there. We could select the nearest version to 4.4.89 which was 4.4.59 or we could go for 3.10.105. We selected 3.10.105.

First we need to load systems of “aarch64-unknown-linux-gnueabi” type following command for it.

$ ct-ng aarch64-unknown-linux-gnueabi

you will see following output.

Type the following command to open edit menu where you can make changes.

$ ct-ng menuconfig

Menu looks like this.

Now we just need to go to the menu of C-compiler, C-library and Operating System to select the desired versions .

For changing glibc version , move into “C-library” and simply select 2.25.

For selecting gcc version , move to “C compiler” and then go to “gcc version” and select 5.4.0. Following images show these steps

For selecting Kernel version go to “Operating Systems” and then move to “Linux kernel version” and select 3.10.105 . Steps are shown below

Then simply Save and exit.

Building Toolchain

Now we are ready to hit build. Following is the command to do it.

$ ct-ng build

It can take couple of hours to complete. Output toolchain will be created inside a directory named “x-tool” at home location of your system. Note the compilation time majorly depends on your system. It is mentioned in Crosstool-NG’s documentation that it can take up-to 24 hours. On our system which have 8 core Xeon processor with 16 GB of RAM. It took about an hour.

Conclusion

You can generate custom cross-toolchains which can have exact versions of tools you want them to have. Crosstool-NG is a very easy to use tool to accomplish this task and our blog is a complete step by step guide to make you compile your own custom cross-toolchain.

--

--