From Gentoo Wiki
Jump to: navigation, search

It is posible to build Gentoo RAP on your Android device from scratch. Actually, the precompiled tarball is itself made like this.

This guide is for development only, you are assumed to be familiar with Gentoo, Prefix, and toolchain

A working Gentoo is needed to follow this guide.

The target prefix directory EPREFIX is assumed to be /data/gentoo. The target architecture CTARGET is assumed to be armv7a-hardfloat-linux-gnueabi.


If, as usually the case, no compiler is available on the device, you'll need to cross compile one. It is often useful to get the compiler into the EPREFIX as well, which is a process of cross triplet-prefix.

Cross triplet refers to the normal cross compiling as covered by the Embedded Handbook. While cross prefix in Prefix project is to build a package from an existing Gentoo instance into a different prefix. Cross triplet-prefix is doing the two things at once.

Prepare cross toolchain

Follow the corresponding session of Embedded Handbook.

Cross build

Follow the cross triplet counterpart, make sure to use an appropriate RAP profile, and preferably, turn off the nls and acl USE flags. In addition the new EPREFIX should be exported.

user $export EPREFIX=/data/gentoo

Make sure to export EPREFIX again upon opening a new shell.

Kernel headers and glibc

user $armv7a-hardfloat-linux-gnueabi-emerge sys-kernel/linux-headers sys-libs/glibc

Tune cross toolchain

ELFs needs to point to the dynamic linker in EPREFIX. Cross toolchain, which by default does not respect EPREFIX, should be tuned. It is done by tweaking specs file. An helper script rt-sysroot eases the tweaking.

FILE rt-sysrootruntime sysroot tuner
#!/usr/bin/env bash

: ${CTARGET:=$(portageq envvar CHOST)}
: ${BPREFIX:=$(portageq envvar PORTAGE_CONFIGROOT)}
: ${EPREFIX:=$(portageq envvar EPREFIX)}

specs_path=$(dirname $(${CTARGET}-gcc -print-libgcc-file-name))/specs
osd=$(${CTARGET}-gcc -print-multi-os-directory)

        [[ -e $1 ]] || mkdir -p $1
        echo $(realpath $1)

dlprefix=$(std_path ${ROOT}lib/${osd})
libprefix=$(std_path ${ROOT}usr/lib/${osd})

${CTARGET}-gcc -dumpspecs | sed -r \
        -e "s,[^:]*(/lib/ld-linux[^.]*\.so(.[:digit:]+)?),${EPREFIX}\1,g" \
        -e "/^\*self_spec:/{n;s,$, -I${ROOT}usr/include -L${libprefix} -L${dlprefix},;}" \
        > ${specs_path}

echo "writing to ${specs_path}
set runtime sysroot to: ${EPREFIX}
compile time sysroot to: ${ROOT}"

The helper essentially let the cross compiler search for the headers and libraries in ${ROOT}/${EPREFIX} and point the interpreter field of ELFs to the dynamic linker inside EPREFIX.

Run the script:

user $CTARGET=armv7a-hardfloat-linux-gnueabi ./rt-sysroot

binutils and gcc

Build everything else:

user $armv7a-hardfloat-linux-gnueabi-emerge sys-devel/gcc sys-devel/binutils

Now you have a functioning native toolchain inside EPREFIX for the target device, copy the ROOT directory over the target device and test out.


The remaining process is essentially bootstraping a Gentoo RAP into EPREFIX.

On the target device with a compiler in PATH, get the bootstrap script and execute it:

user $chmod 755
user $./

Follow the prompts, give the correct EPREFIX, and wait till it finishes. Then a fresh Gentoo is built from scratch.