kernelenv.sh for mainlining
Created by: ollieparanoid
We have a mainlining guide in the wiki now, that explains how to set up the development environment. Compiling the kernel is done outside of the pmbootstrap
chroots, because that makes the workflow much better when working on mainline (e.g. you run make
once, and it only compiles what changed instead of the whole package).
I figured it would be nice if we had a script pmbootstrap/helpers/kernelenv.sh
that does the following when we source it, to make the setup easier:
- set up the native chroot with a GCC cross compiler, with the appropriate arch as read from the selected device's deviceinfo
- write wrappers that make it possible with some black magic to use the cross compilers from outside of the chroots, even if the host system uses glibc
- export right
ARCH
andCROSSCOMPILE
(these are picked up by the kernel'sMakefile
) to point to the wrappers
So that way people wouldn't need to figure out the right cross compiler package name (and other packages, such as bc or openssl-dev) for their distro and install it - the helper script does it all. And it is almost working, the only problem I'm facing is that when using the script, the -isystem include
option passed to GCC doesn't work anymore. This is used by the kernel's Makefile to use the include
folder bundled with linux for system libc headers, instead of the ones from musl or glibc.
I've been spending too much time on that already (it's a nice to have after all, the current guide for setting everything up does work), but maybe someone can figure that last bit out so I'm leaving it here for reference. The code isn't the prettiest and could be improved once this actually works.
Usage:
- copy to
pmbootstrap/helpers/kernelenv.sh
- clone Linux source
- in the Linux folder, run:
source ~/code/pmbootstrap/helpers/kernelenv.sh
- run
make
to compile the kernel with the cross compiler from the native chroot
#!/bin/sh -e
# Alias for pmbootstrap
pmbootstrap_dir="$(dirname "$0")/.."
alias pmbootstrap="$pmbootstrap_dir/pmbootstrap.py"
# Deviceinfo path
device="$(pmbootstrap config device)"
deviceinfo="$pmbootstrap_dir/aports/device/device-$device/deviceinfo"
if [ -e "$deviceinfo" ]; then
# Source deviceinfo
. "$deviceinfo"
echo "Device: $device"
echo "Arch: $deviceinfo_arch"
# Install cross-compiler in chroot
chroot="$(pmbootstrap config work)/chroot_native"
flag="$chroot/tmp/kernelenv/setup_done"
if ! [ -e "$flag" ]; then
echo "Setting up Alpine chroot (details: 'pmbootstrap log')"
pmbootstrap -q chroot -- apk -q add \
abuild \
bc \
binutils-"$deviceinfo_arch" \
gcc-"$deviceinfo_arch" \
gcc \
binutils \
linux-headers \
libressl-dev \
musl-dev
mkdir -p "$chroot/tmp/kernelenv"
touch "$flag"
fi
# Cross-compiler prefix
prefix="$(source "$chroot/usr/share/abuild/functions.sh";
arch_to_hostspec "$deviceinfo_arch")"
# Create wrappers for programs that GCC executes
gcc_wrapper_dir="$chroot/tmp/kernelenv/libexec/gcc/"
mkdir -p "$gcc_wrapper_dir"
for i in cc1 lto-wrapper lto1 collect2; do
wrapper="$gcc_wrapper_dir/$i"
{
echo "#!/bin/sh"
echo "$chroot/lib/ld-musl-x86_64.so.1" \
"--library-path=$chroot/lib/:$chroot/usr/lib" \
"$chroot/usr/libexec/gcc/armv6-alpine-linux-muslgnueabihf/6.4.0/$i" \
'"$@"'
} > "$wrapper"
chmod +x "$wrapper"
done
# Create wrappers for GCC and friends
cpath="$chroot/usr/include/"
cpath="$cpath:$chroot/usr/lib/gcc/$prefix/6.4.0/include"
echo "CPATH: $cpath"
mkdir -p "$chroot/tmp/kernelenv"
for i in $prefix-gcc $prefix-as $prefix-ld $prefix-ar $prefix-nm \
$prefix-strip $prefix-objcopy $prefix-objdump; do
wrapper="$chroot/tmp/kernelenv/$i"
{
echo "#!/bin/sh"
echo "export GCC_EXEC_PREFIX=$gcc_wrapper_dir"
echo "export CPATH=$cpath"
echo "$chroot/lib/ld-musl-x86_64.so.1" \
"--library-path=$chroot/lib/:$chroot/usr/lib" \
"$chroot/usr/bin/$i" '"$@"'
} > "$wrapper"
chmod +x "$wrapper"
done
# Export arch and cross-compiler path
case "$deviceinfo_arch" in
aarch64*) export ARCH="arm64" ;;
arm*) export ARCH="arm" ;;
esac
export CROSS_COMPILE="$chroot/tmp/kernelenv/$prefix-"
else
echo "ERROR: Please select a valid device in 'pmbootstrap init' first."
fi