Set Up Your Gentoo PC for Cross Compilation with crossdev - sakaki-/gentoo-on-rpi-64bit GitHub Wiki

Leverage the power of your PC as a cross-compilation tool for your RPi3!

Introduction

If you have a PC running Gentoo Linux, you can easily set it up for cross-compilation using the crossdev tool: it can build RPi4/RPi3-compatible binaries much faster than the RPi3 itself can. For example, you can compile a (64-bit) kernel for your RPi3 in around 10 minutes on a modern PC.

It isn't hard to set up - all the steps you'll need are provided in this guide.

Creating the Cross-Compilation Toolchain

If you haven't already done so, begin by emerging crossdev on your PC:

gentoo_pc ~ # emerge --ask --verbose sys-devel/crossdev

Next, since in the absence of any specific instructions crossdev will use the lowest priority overlay it finds in /etc/portage/repos.conf/ as home (almost certainly not what you want!), we need to set up an overlay specifically for crossdev's use (this only needs to be done once). Begin by creating the file /etc/portage/repos.conf/crossdev.conf and editing it to contain the following text:

[crossdev]

location = /usr/local/portage-crossdev
priority = 10
masters = gentoo
auto-sync = no

Then to prepare /usr/local/portage-crossdev for use, issue:

gentoo_pc ~ # mkdir -pv /usr/local/portage-crossdev
gentoo_pc ~ # crossdev --stable -t aarch64-unknown-linux-gnu --init-target -oO /usr/local/portage-crossdev

If the above command issues warnings about missing attributes, these may safely be ignored; the --init-target action will fix them.

Next, if your main Portage tree does not use checksummed manifests (and for avoidance of doubt, this will not apply to most users, who should therefore skip this step), you'll also need to issue:

gentoo_pc ~ # echo 'thin-manifests = true' >> /usr/local/portage-crossdev/metadata/layout.conf

Finally, ensure that your cross-gcc compiler will not be built with incompatible USE flags. Issue:

gentoo_pc ~ # echo "cross-aarch64-unknown-linux-gnu/gcc cxx multilib fortran -mudflap nls openmp -sanitize -vtv" >> /etc/portage/package.use/crossdev

And that's the preparation done! Now, we can use crossdev to build a cross-toolchain for our target architecture. As the arm64 architecture has matured somewhat on Gentoo lately, we'll proceed using the 'stable branch' tools (i.e., non-bleeding-edge versions of gcc etc.). Issue:

gentoo_pc ~ # crossdev --stable -t aarch64-unknown-linux-gnu -oO /usr/local/portage-crossdev

This may take a little time to run. Once it completes, congratulations, you have a shiny new cross-compilation toolchain on your PC! There's just one last step: ensure that you have an appropriate profile in place in your cross-environment. To do this, issue:

gentoo_pc ~ # cd /usr/aarch64-unknown-linux-gnu/etc/portage
gentoo_pc portage # rm -f make.profile
gentoo_pc portage # ln -s /usr/portage/profiles/default/linux/arm64/17.0/desktop make.profile

and you're done!

Checking the Toolchain

We next need to check that the toolchain's default links are set up correctly. Issue:

gentoo_pc ~ # aarch64-unknown-linux-gnu-gcc --version
gentoo_pc ~ # aarch64-unknown-linux-gnu-c++ --version
gentoo_pc ~ # aarch64-unknown-linux-gnu-g++ --version

If any of these respond with an error (and mostly, they will not), you will need to run:

gentoo_pc ~ # gcc-config -l
 [1] aarch64-unknown-linux-gnu-5.4.0

 [2] x86_64-pc-linux-gnu-4.9.4
 [3] x86_64-pc-linux-gnu-5.4.0 *

(that's an 'ell') to see a list of your currently available compiler configurations. Your output may well vary from that shown above, but you should see an entry of the form aarch64-unknown-linux-gnu-x.y.z: this is the new cross-gcc that has just been built (you will see other versions for your PC's native gcc also). Suppose that the version displayed was aarch64-unknown-linux-gnu-5.4.0 (as in entry [1] in the above example) - then, to create the correct default links to it, you would issue:

gentoo_pc ~ # gcc-config aarch64-unknown-linux-gnu-5.4.0
gentoo_pc ~ # source /etc/profile

(Adjust this as needed for your particular aarch64-unknown-linux-gnu-x.y.z tuple.) Retry the three --version tests now, and ensure that the links work correctly.

Next Steps

Now that you have a toolchain, you can make use of it to:

  • compile a 64-bit RPi kernel directly on your PC (see this guide for the RPi4, and this one for the RPi3); and
  • set up distcc so that your PC can act as a distributed compilation server for your RPi3 (see this guide).