From 8bddf3b8f0945856e193fcef65f54d1bd4aa050b Mon Sep 17 00:00:00 2001 From: James Christopher Adduono Date: Thu, 28 Feb 2019 20:44:48 -0500 Subject: [PATCH] Add example build/menuconfig scripts Signed-off-by: James Christopher Adduono --- README.md | 12 +++- example_scripts/build.sh | 127 ++++++++++++++++++++++++++++++++++ example_scripts/menuconfig.sh | 95 +++++++++++++++++++++++++ 3 files changed, 233 insertions(+), 1 deletion(-) create mode 100755 example_scripts/build.sh create mode 100755 example_scripts/menuconfig.sh diff --git a/README.md b/README.md index c1ab2514..23910358 100644 --- a/README.md +++ b/README.md @@ -58,4 +58,14 @@ So really all you need is a kernel image and sometimes a dtb.img to build for a Don't forget to add your newly supported device's kernel sources to the kernels.txt file! -Sat Oct 22 21:31:18 EST 2016 +## Building a kernel for your device + +There are scripts in the `example_scripts` folder that you can copy to the root of your device's kernel sources. +They should be modified to match your device. It will make it easier to build your device's kernel outside of an Android source tree. + +The binary output from the build will be self-contained in a `build` folder, with the kernel modules properly stripped and installed with their modprobe data in `build/lib/modules`. + +Using these scripts in your source tree will make it easier for others to make modifications and update your device in the future. It will also increase the likelihood your device will be accepted into the nethunter-devices repository as an officially supported device! + + +Thu Feb 28 20:43:37 EST 2019 diff --git a/example_scripts/build.sh b/example_scripts/build.sh new file mode 100755 index 00000000..11c6ea77 --- /dev/null +++ b/example_scripts/build.sh @@ -0,0 +1,127 @@ +#!/bin/bash +# script for building NetHunter kernels by jcadduono + +################### BEFORE STARTING ################ +# +# download a working toolchain and extract it somewhere and configure this +# file to point to the toolchain's root directory. +# this file should be placed in your kernel source folder with +# the CONFIG section edited to work for your device. +# +# once you've set up the config section how you like it, you can simply run +# ./build.sh [DEVICE] [TARGET] +# +# make a copy of your device's original defconfig file. +# the new defconfig file should follow the format: +# arch/arm64/configs/nethunter_yourdevice_defconfig +# +###################### CONFIG ###################### + +# default device name (change this!) +DEFAULT_DEVICE=yourdevice + +# default target name +DEFAULT_TARGET=nethunter + +# release version (increment this with new releases) +RELEASE_VERSION=1.0 + +# directory containing cross-compile arm64 toolchain (change this!) +TOOLCHAIN=/opt/toolchain/gcc-linaro-5.5.0-2017.10-x86_64_aarch64-linux-gnu + +############## SCARY NO-TOUCHY STUFF ############### + +# root directory of kernel source git repo (default is this script's location) +RDIR=$(pwd) + +CPU_THREADS=$(grep -c "processor" /proc/cpuinfo) +# amount of cpu threads to use in kernel make process +THREADS=$((CPU_THREADS + 1)) + +ABORT() { + [ "$1" ] && echo "Error: $*" + exit 1 +} + +CONTINUE=false +export ARCH=arm64 +export CROSS_COMPILE=$TOOLCHAIN/bin/aarch64-linux-gnu- + +[ -x "${CROSS_COMPILE}gcc" ] || +ABORT "Unable to find gcc cross-compiler at location: ${CROSS_COMPILE}gcc" + +while [ $# != 0 ]; do + if [ "$1" = "--continue" ] || [ "$1" == "-c" ]; then + CONTINUE=true + elif [ ! "$TARGET" ]; then + TARGET=$1 + elif [ ! "$DEVICE" ]; then + DEVICE=$1 + else + echo "Too many arguments!" + echo "Usage: ./build.sh [--continue] [device] [target defconfig]" + ABORT + fi + shift +done + +[ "$DEVICE" ] || DEVICE=$DEFAULT_DEVICE +[ "$TARGET" ] || TARGET=$DEFAULT_TARGET +DEFCONFIG=${TARGET}_${DEVICE}_defconfig + +[ -f "$RDIR/arch/$ARCH/configs/${DEFCONFIG}" ] || +ABORT "Config $DEFCONFIG not found in $ARCH configs!" + +export LOCALVERSION=$TARGET-$DEVICE-$RELEASE_VERSION + +CLEAN_BUILD() { + echo "Cleaning build..." + rm -rf build +} + +SETUP_BUILD() { + echo "Creating kernel config for $LOCALVERSION..." + mkdir -p build + make -C "$RDIR" O=build "$DEFCONFIG" \ + || ABORT "Failed to set up build" +} + +BUILD_KERNEL() { + echo "Starting build for $LOCALVERSION..." + while ! make -C "$RDIR" O=build -j"$THREADS"; do + read -rp "Build failed. Retry? " do_retry + case $do_retry in + Y|y) continue ;; + *) return 1 ;; + esac + done +} + +INSTALL_MODULES() { + grep -q 'CONFIG_MODULES=y' build/.config || return 0 + echo "Installing kernel modules to build/lib/modules..." + while ! make -C "$RDIR" O=build \ + INSTALL_MOD_PATH="." \ + INSTALL_MOD_STRIP=1 \ + modules_install + do + read -rp "Build failed. Retry? " do_retry + case $do_retry in + Y|y) continue ;; + *) return 1 ;; + esac + done + rm build/lib/modules/*/build build/lib/modules/*/source +} + +cd "$RDIR" || ABORT "Failed to enter $RDIR!" + +if ! $CONTINUE; then + CLEAN_BUILD + SETUP_BUILD || + ABORT "Failed to set up build!" +fi + +BUILD_KERNEL && +INSTALL_MODULES && +echo "Finished building $LOCALVERSION!" diff --git a/example_scripts/menuconfig.sh b/example_scripts/menuconfig.sh new file mode 100755 index 00000000..9c70b07a --- /dev/null +++ b/example_scripts/menuconfig.sh @@ -0,0 +1,95 @@ +#!/bin/bash +# script for configuring NetHunter kernels by jcadduono + +################### BEFORE STARTING ################ +# +# download a working toolchain and extract it somewhere and configure this +# file to point to the toolchain's root directory. +# this file should be placed in your kernel source folder with +# the CONFIG section edited to work for your device. +# +# once you've set up the config section how you like it, you can simply run +# ./menuconfig.sh [DEVICE] [TARGET] +# +# make a copy of your device's original defconfig file. +# the new defconfig file should follow the format: +# arch/arm64/configs/nethunter_yourdevice_defconfig +# +###################### CONFIG ###################### + +# default device name (change this!) +DEFAULT_DEVICE=yourdevice + +# default target name +DEFAULT_TARGET=nethunter + +# directory containing cross-compile arm64 toolchain (change this!) +TOOLCHAIN=/opt/toolchain/gcc-linaro-5.5.0-2017.10-x86_64_aarch64-linux-gnu + +############## SCARY NO-TOUCHY STUFF ############### + +# root directory of kernel source git repo (default is this script's location) +RDIR=$(pwd) + +ABORT() { + [ "$1" ] && echo "Error: $*" + exit 1 +} + +export ARCH=arm64 +export CROSS_COMPILE=$TOOLCHAIN/bin/aarch64-linux-gnu- + +[ -x "${CROSS_COMPILE}gcc" ] || +ABORT "Unable to find gcc cross-compiler at location: ${CROSS_COMPILE}gcc" + +while [ $# != 0 ]; do + if [ ! "$DEVICE" ]; then + DEVICE=$1 + elif [ ! "$TARGET" ]; then + TARGET=$1 + else + echo "Too many arguments!" + echo "Usage: ./menuconfig.sh [device] [target defconfig]" + ABORT + fi + shift +done + +[ "$DEVICE" ] || DEVICE=$DEFAULT_DEVICE +[ "$TARGET" ] || TARGET=$DEFAULT_TARGET +DEFCONFIG=${TARGET}_${DEVICE}_defconfig +DEFCONFIG_FILE=$RDIR/arch/$ARCH/configs/$DEFCONFIG + +[ -f "$DEFCONFIG_FILE" ] || +ABORT "Device config $DEFCONFIG not found in $ARCH configs!" + +cd "$RDIR" || ABORT "Failed to enter $RDIR!" + +echo "Cleaning build..." +rm -rf build +mkdir build +make -s -i -C "$RDIR" O=build "$DEFCONFIG" menuconfig +echo "Showing differences between old config and new config" +echo "-----------------------------------------------------" +if command -v colordiff >/dev/null 2>&1; then + diff -Bwu --label "old config" "$DEFCONFIG_FILE" --label "new config" build/.config | colordiff +else + diff -Bwu --label "old config" "$DEFCONFIG_FILE" --label "new config" build/.config + echo "-----------------------------------------------------" + echo "Consider installing the colordiff package to make diffs easier to read" +fi +echo "-----------------------------------------------------" +echo -n "Are you satisfied with these changes? y/N: " +read -r option +case $option in +y|Y) + cp build/.config "$DEFCONFIG_FILE" + echo "Copied new config to $DEFCONFIG_FILE" + ;; +*) + echo "That's unfortunate" + ;; +esac +echo "Cleaning build..." +rm -rf build +echo "Done."