Skip to content

Instantly share code, notes, and snippets.

@A2L5E0X1
Created June 11, 2022 16:01
Show Gist options
  • Save A2L5E0X1/82576642681f21a65e0d2744b7e1ffa5 to your computer and use it in GitHub Desktop.
Save A2L5E0X1/82576642681f21a65e0d2744b7e1ffa5 to your computer and use it in GitHub Desktop.
Android kernel buildscript
#!/bin/bash
#
# Copyright (C) 2022 A2L5E0X1
#
# Simple script for building Android kernels with multiple arch support and custom toolchain option
#
#
# Changelog:
#
# rev 1: initial upload, everything working fine!
#
# Config - only touch if you know what you are doing
KERNEL_OBJ=out
TARGET_OUT_DIR=$2_out
THREADS=$(nproc --all)
GIT=$(which git)
TOOLCHAIN_BRANCH=android-9.0.0_r61
LOCAL_PATH=`pwd`
# Bypasses
SKIP_TOOLCHAIN_RECLONE=true
SKIP_KERNEL_OBJ_CLEAN=false
# Simplify the parameters - DO NOT TOUCH
ARCH=$1
KERNEL_CONFIG=$2
CUSTOM_TOOLCHAIN=$3
CUSTOM_CC=$4
# Intro
echo "~~~ Simple Kernel Buildscript ~~~"
echo "~~~ by A2L5E0X1 ~~~"
sleep 1
# Check for parameters
if [ -z $ARCH ] || [ -z $KERNEL_CONFIG ]; then
echo "Usage: $(basename $0) <arch> <defconfig> [toolchain path] [custom_cc]"
echo "Example: $(basename $0) arm64 sample_defconfig /home/nobody/kernel/toolchain/aarch64-linux-android-4.9 aarch64-linux-android-"
exit 1
fi
# Some CUSTOM_TOOLCHAIN checks
if [ ! -z $CUSTOM_TOOLCHAIN ]; then
# Check if CUSTOM_CC is defined when CUSTOM_TOOLCHAIN is defined
if [ -z $CUSTOM_CC ]; then
echo "ERROR: CUSTOM_TOOLCHAIN defined but CUSTOM_CC isn't!"
exit 1
fi
# Check if CUSTOM_TOOLCHAIN is actually present
if [ ! -f $CUSTOM_TOOLCHAIN/bin/${CUSTOM_CC}gcc ]; then
echo "ERROR: CUSTOM_TOOLCHAIN or CUSTOM_CC is invalid! Clone the toolchain or fix the path!"
exit 1
fi
fi
# Check if the script is located in an actual kernel source
if [ ! -f Makefile ]; then
echo "ERROR: This doesn't look like a kernel source! Please put this script into a kernel source!"
exit 1
fi
# Check if the defconfig exists and if the arch is correct
if [ ! -f arch/$ARCH/configs/$KERNEL_CONFIG ]; then
echo "ERROR: Either the defconfig doesn't exist or the arch is wrong. Please correct it!"
exit 1
fi
# Check for git
if [ ! -f $(which git) ]; then
echo "ERROR: git is not installed. Please install it or set PATH!"
exit 1
fi
# Choose and clone toolchain
if [ -z $CUSTOM_TOOLCHAIN ]; then
# Delete existing toolchain
if [ -d kernel_toolchain ] && [ "$SKIP_TOOLCHAIN_RECLONE" != "true" ]; then
rm -rf kernel_toolchain
fi
# Choose toolchain
if [ $ARCH = "arm" ]; then
export CROSS_COMPILE="arm-linux-android-"
export TOOLCHAIN="arm/arm-linux-android-4.9"
export TOOLCHAIN_PATH="kernel_toolchain/arm-linux-androideabi-4.9"
elif [ $ARCH = "arm64" ]; then
export CROSS_COMPILE="aarch64-linux-android-"
export TOOLCHAIN="aarch64/aarch64-linux-android-4.9"
export TOOLCHAIN_PATH="kernel_toolchain/aarch64-linux-androideabi-4.9"
elif [ $ARCH = "x86" ]; then
export CROSS_COMPILE="x86_64-linux-android-"
export TOOLCHAIN="x86_64/x86_64-linux-android-4.9"
export TOOLCHAIN_PATH="kernel_toolchain/x86_64-linux-androideabi-4.9"
else
echo "Couldn't find toolchain for your arch. I would recommend you to use a custom toolchain!"
exit 1
fi
# Check for darwin
if [ "$OSTYPE" = "darwin*" ]; then
export TOOLCHAIN_OSTYPE="darwin-x86"
else
export TOOLCHAIN_OSTYPE="linux-x86"
fi
# Clone toolchain
"$GIT" clone https://android.googlesource.com/platform/prebuilts/gcc/"$TOOLCHAIN_OSTYPE"/"$TOOLCHAIN" -b "$TOOLCHAIN_BRANCH" "$TOOLCHAIN_PATH" --depth=1
else
# Export custom toolchain cc
export CROSS_COMPILE=$CUSTOM_CC
export TOOLCHAIN_PATH=$CUSTOM_TOOLCHAIN
fi
# Create OUT_DIR
if [ ! -d $KERNEL_OBJ ]; then
mkdir $KERNEL_OBJ
elif [ $SKIP_KERNEL_OBJ_CLEAN != true ]; then
rm -rf $KERNEL_OBJ; mkdir $KERNEL_OBJ
fi
# Export toolchain to PATH
export PATH="$LOCAL_PATH/$TOOLCHAIN_PATH/bin:$PATH"
# Start kernel build
echo "~ "
echo "~ INFO: building $KERNEL_CONFIG..."
echo "~ "
make ARCH=$ARCH O=$KERNEL_OBJ -j$THREADS $KERNEL_CONFIG
if [ ! -f $KERNEL_OBJ/.config ]; then
echo "ERROR: Building $KERNEL_CONFIG failed! Check the log above!"
exit 1
fi
echo "~ "
echo "~ INFO: building kernel..."
echo "~ "
make ARCH=$ARCH O=$KERNEL_OBJ -j$THREADS
# Check if kernel built successfully. If yes, copy to TARGET_OUT_DIR.
if [ ! -f $KERNEL_OBJ/arch/$ARCH/boot/Image ]; then
echo "ERROR: Kernel build failed! Check the log above!"
echo "Buildissues related to yylloc? Try this commit: https://github.com/hi6250-8/android_kernel_huawei_hi6250/commit/cb65db6f628c08f34f0ca95f49d054779b58cc1d"
exit 1
else
if [ -d $TARGET_OUT_DIR ]; then
rm -rf $TARGET_OUT_DIR
fi
cp -r $KERNEL_OBJ/arch/$ARCH/boot $TARGET_OUT_DIR
if [ $(find $KERNEL_OBJ -name "*.ko") != "" ]; then
mkdir $TARGET_OUT_DIR/modules
for module in $(find $KERNEL_OBJ -name "*.ko"); do cp $module $TARGET_OUT_DIR/modules/; done
fi
fi
echo "~ "
echo "~ INFO: Successfully built kernel! You can find it in $TARGET_OUT_DIR."
echo "~ "
echo "* Kernel binaries"
ls $TARGET_OUT_DIR
if [ -d $TARGET_OUT_DIR/arch/$ARCH/boot/dts ]; then
echo "* Kernel DTBs"
find $TARGET_OUT_DIR -name '*.dtb*'
fi
if [ -d $TARGET_OUT_DIR/modules ]; then
echo "* Kernel Modules"
ls $TARGET_OUT_DIR/modules
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment