Skip to content

Instantly share code, notes, and snippets.

@thesamesam
Last active April 24, 2024 13:01
Show Gist options
  • Save thesamesam/f43490fa3e290701c09073b0befbe21f to your computer and use it in GitHub Desktop.
Save thesamesam/f43490fa3e290701c09073b0befbe21f to your computer and use it in GitHub Desktop.
Gentoo cross compilation instructions (crossdev -> stage3)

Cross instructions

These instructions cover:

  1. building a cross toolchain
  2. populating the new ROOT
  3. making it chrootable using qemu-user to easily run "native" builds or tests (this is like a stage3 at the end)

You do not need to follow through to the end if you don't need a stage3-like root.

Set up the toolchain

$ crossdev riscv64-unknown-linux-gnu

Avoid dependency hell and populate the ROOT

Emerge sys-libs/glibc (no, @system / @world won't make it realise it can/should do this, don't ask why, I don't know):

$ riscv64-unknown-linux-gnu-emerge -v1 sys-libs/glibc
$ riscv64-unknown-linux-gnu-emerge -v1 @system

There's now already a bunch of things in /usr/riscv64-unknown-linux-gnu but we want to make it useful to chroot into and do things.

Profile setup

Choosing profile

Run: PORTAGE_CONFIGROOT=/usr/riscv64-unknown-linux-gnu eselect profile list

Pick a nice looking one, like default/linux/riscv/20.0/rv64gc/lp64.

$ PORTAGE_CONFIGROOT=/usr/riscv64-unknown-linux-gnu eselect profile set default/linux/riscv/20.0/rv64gc/lp64

Avoid circular deps

$ USE="-filecaps" riscv64-unknown-linux-gnu-emerge -v1 @system

Chroot in

Set up QEMU

In /etc/portage/package.use/qemu, put:

app-emulation/qemu static-user # optional but makes life easier
*/* QEMU_USER_TARGETS: riscv32 riscv64

Then emerge -v app-emulation/qemu

binfmt

See https://wiki.gentoo.org/wiki/Embedded_Handbook/General/Compiling_with_qemu_user_chroot.

  1. Set up binfmt appropriately for riscv64 (TODO: explain how, but wiki link above covers it pretty much)
  2. cp /usr/bin/qemu-riscv64 /usr/riscv64-unknown-linux-gnu/usr/bin/

Do it!

  1. chroot in to /usr/riscv64-unknown-linux-gnu using your preferred method
  2. update /etc/portage/make.conf to drop ROOT, PORTAGE_TMPDIR, PKG_CONFIG_DIR
  3. emerge -a -uvDU @world (may need to do @system first)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment