Skip to content

Cross compilation toolchain

Quentin Delhaye edited this page Apr 4, 2023 · 18 revisions

We want to make programs and kernel modules that will run on the board, but build them on our local computer. To achieve this, we need a cross-compilation toolchain, i.e. a suite of tools that build stuff meant for another platform. In particular, we will need:

  • A gcc cross-compilation toolchain
  • The Linux kernel source code corresponding to the one running on the board
  • To build said kernel using the toolchain

Install the right cross-compilation toolchain

The BBB is rocking a an AM3358 Sitara processor from Texas Instruments, based on an Arm Cortex-A8. As such, we can fetch the GNU toolchain directly from Arm. In particular, we will use the arm-none-linux-gnueabihf version 12.2.rel1. The naming convention for the toolchain is <architecture>-<vendor>-<ABI> with:

  • <architecture> the target architecture, arm in our case.
  • <vendor> a specific vendor release, none in our case.
  • <ABI> to tell if the toolchain is meant to be used in a linux environment or bare-metal (i.e. directly on the hardware), so that the compiler knows if there will be libraries alongside the program to link them or not. In our case, the programs will run on a Linux OS, so our ABI is linux-gnueabihf. The hf part at the end means floats are managed in hardware.

Extract the content of the archive in a new directory and add it to your PATH environment variable (check how to make it permanent for your distribution).

$ sudo mkdir -p /opt/arm-gnu-toolchain-12.2.rel1-x86_64-arm-none-linux-gnueabihf
$ sudo chown $LOGNAME:$LOGNAME /opt/arm-gnu-toolchain-12.2.rel1-x86_64-arm-none-linux-gnueabihf
$ tar -xf arm-gnu-toolchain-12.2.rel1-x86_64-arm-none-linux-gnueabihf.tar.xz -C /opt/arm-gnu-toolchain-12.2.rel1-x86_64-arm-none-linux-gnueabihf
$ export PATH=/opt/arm-gnu-toolchain-12.2.rel1-x86_64-arm-none-linux-gnueabihf/bin:$PATH
$ arm-none-linux-gnueabihf-gcc -v

The last command should help you make sure the process worked.

Compile the kernel locally

To cross-build a kernel module meant for another platform, we need the Linux kernel corresponding to that very platform.

First, check the exact kernel version running on your board:

debian@bbb:~$ uname -r
4.19.94-ti-r42

If you installed the pre-built image, it should be version 4.19.x. Download the full source tree at that specific version from Beagleboard GitHub or from TI.

Now to compile a Linux kernel, we need a configuration. It tells us which kernel modules should be compiled, formats to use, platform to target, etc. Luckily, it can be retrieved from a running system. From the board:

debian@beaglbone:~$ mkdir ~/LKM
debian@beaglbone:~$ cd ~/LKM
debian@beaglbone:~$ sudo modprobe configs
debian@beaglbone:~$ zcat /proc/config.gz > .config

Then from your computer, navigate to the folder where you extracted the Linux source tree and fetch the file (see how to do it):

$ scp debian@192.168.7.2:/home/debian/LKM/.config .

From the same folder, we are now ready to compile the kernel:

$ make CROSS_COMPILE=arm-none-linux-gnueabihf- ARCH=arm -j 8

The -j 8 argument tells gcc how many CPU cores it can use in parallel. Don't set more than you actually have. It might ask you for some extra module to compile, accept the default prompt (simply press Return).

Clone this wiki locally