Build a (Cross) Toolchain With Crosstool-NG
Introduction
This is the first post in a series of posts (in a tutorial) about building an embedded Linux system for a Raspberry Pi 3 Model B+. A summary post for the tutorial can be found here. The summary post also talks about …
- what is required to follow along,
- the
<project/root/dir>, - some terms that will be used throughout the series,
- a link to an article that explains how to prepare the SD card,
- some hints on how to use the UART interface of the Raspberry Pi.
This post is about building a cross toolchain that is needed to build the embedded Linux system afterwards. To do so, I used the tool Crosstool-NG website [1].
Let’s get started.
Install Crosstool-NG
You can either install Crosstool-NG directly on your host system or use a container with Crosstool-NG installed in it. I used a container. In order to do so, I wrote a Containerfile to build a (Podman) container. You can find it the repository containing the Containerfile and a short description on how to use the container here. If you use the container, you can skip forward to the next section.
If you want to install Crosstool-NG directly on your host system, do the following:
Before you can install Crosstool-NG, you need to make sure some other required packages are installed on your system. Assuming you use an Ubuntu system, you can use the following command to install these prerequisites:
apt-get install \
autoconf automake bison bzip2 ca-certificates cmake flex g++ \
gawk gcc gettext git gperf help2man libncurses5-dev libstdc++6 libtool \
libtool-bin make patch python3-dev rsync texinfo unzip wget xz-utils
Afterwards you can download, build, and install Crosstool-NG from source like so:
cd <project/root/dir>
git clone https://github.com/crosstool-ng/crosstool-ng
cd crosstool-ng
git checkout crosstool-ng-<version>
./bootstrap
./configure --prefix=${PWD}
make
make install
As mentioned before <project/root/dir> should be some dedicated directory in
your home directory. Other than that, replace <version> with the newest
version of Crosstool-NG (at the time of writing this is 1.26.0). The
--prefix option determines where crosstool-ng will be installed. When using
${PWD} it will be install in the current working directory under ./bin. If
you want to install crosstool-ng somewhere else, replace ${PWD} with a path
of your liking. You just have to make sure you have right permissions for that
directory.
Building a (Cross) Toolchain
If you didn’t install Crosstool-NG into a directory that is in your PATH,
it helps to prepend your PATH variable for the time you need to work with
Crosstool-NG. If you used --prefix=${PWD} (as suggested earlier), the command
to do so, would be:
PATH=<project/root/dir>/crosstool-ng/bin:$PATH
Instead of prepending your PATH, you can also replace ct-ng in the
following commands with ./bin/ct-ng (at least as long as you haven’t changed
the current working directory after building crosstool-ng).
Additionally, set the following two environment variables (in the container (see above they are set already):
export CT_ALLOW_BUILD_AS_ROOT_SURE=1
export CT_PREFIX=</project/root/dir>/x-tools
The first one allows you to build toolchains without being root (not sure if this is only needed in containers), the second one is the path in which the built toolchains can be found after they are generated.
All right, make sure you’re in the <project/root/dir>/crosstool-ng/
directory. If you use the container and started it from the
<project/root/dir> (which you should have done 😊), create the
crosstool-ng directory inside the /data directory and change into it.
Note: The following command will delete a file called .config in your current working directory. In case you have been working on a configuration for a toolchain before, you might want to save this .config file for later use.
For good measure you should run:
ct-ng distclean
before you start the process of building a new toolchain.
The first thing you need to do to build a toolchain with Crosstool-NG is to configure the toolchain you want to build. This can be quite complex. It helps to use a sample configuration and, if needed, adjust it. To list all sample configuration that come with Crosstool-NG, run:
ct-ng list-samples
For my Raspberry Pi 3+, I used the aarch64-rpi3-linux-gnu sample. The following command sets this up:
ct-ng aarch64-rpi3-linux-gnu
This command generates a .config file. To configure the soon to be built toolchain, you can either edit this file manually, or better use the following command to launch a terminal based configuration tool:
ct-ng menuconfig
All I change here was setting the Linux version to 4.19.287, since I wanted
to use a version 4.19 Linux kernel. The corresponding setting can be found
under Operating System -> Version linux.
When you are satisfied with the configuration, the final step is to actually build the toolchain. This is as simple as running:
ct-ng build
Once this command terminates and everything went to plan, you should find the newly built toolchain under: </project/root/dir>/x-tools/aarch64-rpi3-linux-gnu/.
By now, if you run tree -L 2 in your <project/root/dir>, you should see the
following (you might have to leave the container 😉):
$> tree -L 2
.
├── crosstool-ng
│ ├── bin
│ ├── ... (output shortened)
└── x-tools
└── aarch64-rpi3-linux-gnu
Well, have fun with your newly built toolchain. The next step will be to Build U-Boot For a Raspberry Pi 3 Model B+.
Change Log
2025-02-01:
- Made the post fit into the series of posts about embedded Linus on a Raspberry Pi 3 Model B+.
2025-01-20:
- Fixed the note about deleting the file
.config. It spanned a little bit too far. - Rewrote the introduction.
- Reorder the section about installing Crosstool-NG.
- Made some minor improvments.
2023-11-02:
- Added the section Project Setup.
- Adjusted some paths in the explanation according to the Project Setup sections.
2023-11-01:
- Fixed some typos.
- Moved the note about deleting the
.configfile in front of the command, to make sure it is read before the command is run.
Take care,
Andreas
References
- Crosstool-NG, “crosstool-NG.” [Online]. Available at: https://crosstool-ng.github.io/. [Accessed: 13-Oct-2023].
Leave a comment