# these notes give an idea of how to build a postmarketos kernel natively on the arm device itself
# the aarch64 build of a linux-amazon-douglas kernel on a dougals tablet running debian is used as an example later on

# setting up pmbootstrap as a user:

sudo apt-get install python3-pip python3-argcomplete
pip3 install --user pmbootstrap

# add the following lines to your .bashrc and source it:
--- snip ---
# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/.local/bin" ] ; then
    PATH="$HOME/.local/bin:$PATH"
fi
eval "$(register-python-argcomplete3 pmbootstrap)"
--- snip ---

# setup pmbootstrap:
pmbootstrap init

# the next steps are inspired by: https://wiki.postmarketos.org/index.php?title=User:Alexeymin/Kernel_development_in_chroot_using_abuild&mobileaction=toggle_view_desktop

pmbootstrap checksum linux-amazon-douglas
# in case kernel config changes are needed
pmbootstrap kconfig edit
# the below step for some reason did not fully work for me as some commands like ld, objcopy, nm, ar and strip were not where the build expected them ...
pmbootstrap build --force linux-amazon-douglas
# now: ctrl-c after it has installed everything, unpacked and patched the kernel sources and starts to compile the kernel
pmbootstrap chroot

# inside of the chroot - fix the names to they end up as expected (guessed from the build errors before)
cd /usr/bin
ln -sf ld gcc6-aarch64-alpine-linux-musl-ld 
ln -sf objcopy gcc6-aarch64-alpine-linux-musl-objcopy
ln -sf strip gcc6-aarch64-alpine-linux-musl-strip
ln -sf gcc6-gcc-nm gcc6-aarch64-alpine-linux-musl-nm
ln -sf gcc6-gcc-ar gcc6-aarch64-alpine-linux-musl-ar

# the above is the aarch64 case, for the armv7 case (ford on ford) it looks like
cd /usr/bin
ln -sf ld gcc4-armv7-alpine-linux-musleabihf-ld 
ln -sf objcopy gcc4-armv7-alpine-linux-musleabihf-objcopy
ln -sf strip gcc4-armv7-alpine-linux-musleabihf-strip
ln -sf gcc4-gcc-nm gcc4-armv7-alpine-linux-musleabihf-nm
ln -sf gcc4-gcc-ar gcc4-armv7-alpine-linux-musleabihf-ar

su pmos

export HOME=/home/pmos
cd /home/pmos/build
export ARCH=arm64
export CARCH=aarch64
export CROSS_COMPILE=gcc6-aarch64-alpine-linux-musl-
export CC=gcc6-aarch64-alpine-linux-musl-gcc
abuild -df build

# the above is the aarch64 case, for the armv7 case (ford on ford) it looks like
export HOME=/home/pmos
cd /home/pmos/build
export ARCH=arm
export CARCH=armv7
export CROSS_COMPILE=gcc4-armv7-alpine-linux-musleabihf-
export CC=gcc4-armv7-alpine-linux-musleabihf-gcc
abuild -df build

# alternative manual steps for the build step (untested):
cd /home/pmos/build/src/linux...
make ARCH=arm64 CROSS_COMPILE=aarch64-alpine-linux-musl- defconfig
make ARCH=arm64 CROSS_COMPILE=aarch64-alpine-linux-musl- menuconfig
make ARCH=arm64 CROSS_COMPILE=aarch64-alpine-linux-musl- -j9
make ARCH=arm64 mrproper

# create a apk package which will be needed later
abuild rootpkg

# the result should either way be a package here:
# ${HOME}/.local/var/pmbootstrap/chroot_native/mnt/pmbootstrap-packages/aarch64/linux-amazon-douglas-3.18.19-r5.apk
# this one needs to be copied to ${HOME}/.local/var/pmbootstrap/cache_apk_aarch64
# from here on the steps are done outside of the pmbootstrap chroot again
sudo cp ${HOME}/.local/var/pmbootstrap/chroot_native/mnt/pmbootstrap-packages/aarch64/linux-amazon-douglas-3.18.19-r5.apk ${HOME}/.local/var/pmbootstrap/cache_apk_aarch64

# after the manual compilation and copy the pmbootstrap packages needs to get reindexed:
pmbootstrap index

# now the build can continue:
pmbootstrap checksum device-amazon-douglas
pmbootstrap build --force device-amazon-douglas

# create a boot.img, root fs etc. to take the files we want out of later
pmbootstrap install

# resulting root fs will now be here: ${HOME}/.local/var/pmbootstrap/chroot_rootfs_amazon-douglas

# writing the boot.img to the filesystem should be done via twrp only (at least on the amazon tablets)
# as this takes care of some special actions to keep the bootloader unlocked ... if it is written directly
# to the boot partition it might result in a bricked device, boot loops, loosing the bootloader unlock etc.



# just as a reference here are some example steps to build kernels on a regular (i.e. x86_64) pmbootstrap setup:

# example samsung gt510
pmbootstrap init
pmbootstrap kconfig edit postmarketos-qcom-msm8916 --arch aarch64
pmbootstrap checksum linux-postmarketos-qcom-msm8916
pmbootstrap build linux-postmarketos-qcom-msm8916 --force
pmbootstrap checksum device-samsung-gt510
pmbootstrap build device-samsung-gt510 --force
pmbootstrap install
# best as root
cd pmboostrap-dir/chroot_rootfs_samsung-gt510
tar czf /tmp/boot-and-modules-samsung-gt510.tar.gz boot lib/modules

# example amazon suez
pmbootstrap init
pmbootstrap kconfig edit
pmbootstrap checksum linux-amazon-suez
pmbootstrap build linux-amazon-suez --force
pmbootstrap checksum device-amazon-suez
pmbootstrap build device-amazon-suez --force
pmbootstrap install
# best as root
cd pmboostrap-dir/chroot_rootfs_amazon-suez
# collect all the files from boot and lib/modules required - see systems/tablet_amazon_suez/get-files.sh
# or newer approach:
tar czf /tmp/boot-and-modules-amazon-suez.tar.gz boot lib/modules
