User:Immolo/RPIzero

From Gentoo Wiki
Jump to:navigation Jump to:search

This guide will build a clang and musl Gentoo system for a 32bit ARM device like the RPI Zero W. This is only tested as working with ~arm so this process is intended for users that have used Gentoo for some time.

Prerequisite

Using the Embedded Handbook/General/Compiling with qemu user chroot page as template, the user should have the latest stage3-armv6j_hardfp-musl-openrc ready to build the image on a faster machine.

Setting up

Configs

Set the chroot up as a normal install for Gentoo would go:

root #emerge --sync
root #nano /etc/portage/make.conf

Below is how make.conf should look for the first build of llvm/clang:

FILE /etc/portage/make.confmake.conf
COMMON_FLAGS="-Os -pipe"
CFLAGS="${COMMON_FLAGS}"
CXXFLAGS="${COMMON_FLAGS}"
FCFLAGS="${COMMON_FLAGS}"
FFLAGS="${COMMON_FLAGS}"

CHOST="armv6j-unknown-linux-musleabihf"

ACCEPT_KEYWORDS="~arm"

USE="hwloc"
LC_MESSAGES=C

EMERGE_DEFAULT_OPTS="$EMERGE_DEFAULT_OPTS --exclude app-emulation/qemu"
FEATURES="-pid-sandbox -network-sandbox"

Adding a MAKEOPTS value for the number of jobs the computer can build for can also be added at this stage but no other optimizations should be set now.

First build with GCC

GCC needs to compile llvm/clang but this causes some issues with compiling future programs so this workaround is needed.

root #emerge --ask sys-devel/llvm sys-devel/clang

Second build

Now clang can rebuild itself by following these steps:

root #nano /etc/portage/make.conf
FILE /etc/portage/make.confmake.conf
COMMON_FLAGS="-Os -pipe -march=armv6j"
CFLAGS="${COMMON_FLAGS}"
CXXFLAGS="${COMMON_FLAGS}"
FCFLAGS="${COMMON_FLAGS}"
FFLAGS="${COMMON_FLAGS}"

CHOST="armv6j-unknown-linux-musleabihf"

ACCEPT_KEYWORDS="~arm"

USE="hwloc"
LC_MESSAGES=C

EMERGE_DEFAULT_OPTS="$EMERGE_DEFAULT_OPTS --exclude app-emulation/qemu"
FEATURES="-pid-sandbox -network-sandbox"

#Use clang by default
CC="clang"
CXX="clang++"
AR="llvm-ar"
NM="llvm-nm"
RANLIB="llvm-ranlib"

LDFLAGS="${LDFLAGS} -fuse-ld=lld -rtlib=compiler-rt -unwindlib=libunwind -Wl,--as-needed"
root #emerge --ask sys-devel/llvm sys-devel/clang

Rebuild @world

Now llvm/clang is setup then Gentoo can be configured for the desired device, in the case of this example a Raspberry Pi Zero W is the target so it will be optimized for low RAM usage with -Os and Link Time Optimization will be used. The person following this can change to the needs of the targeted system.

root #nano /etc/portage/make.conf
FILE /etc/portage/make.confmake.conf
COMMON_FLAGS="-Os -pipe -march=armv6j -mfpu=vfp -mfloat-abi=hard -fomit-frame-pointer -flto"
CFLAGS="${COMMON_FLAGS}"
CXXFLAGS="${COMMON_FLAGS}"
FCFLAGS="${COMMON_FLAGS}"
FFLAGS="${COMMON_FLAGS}"

CHOST="armv6j-unknown-linux-musleabihf"

ACCEPT_KEYWORDS="~arm"

USE="hwloc lto pgo"
LC_MESSAGES=C

EMERGE_DEFAULT_OPTS="$EMERGE_DEFAULT_OPTS --exclude app-emulation/qemu"
FEATURES="-pid-sandbox -network-sandbox"

#Use clang by default
CC="clang"
CXX="clang++"
AR="llvm-ar"
NM="llvm-nm"
RANLIB="llvm-ranlib"

LDFLAGS="${LDFLAGS} -fuse-ld=lld -rtlib=compiler-rt -unwindlib=libunwind -Wl,--as-needed"

Finally, use --emptytree to begin rebuilding every package on the system:

root #emerge --ask --emptytree @world