|
| 1 | +#!/bin/bash |
| 2 | +# |
| 3 | +# Compile script for kernel |
| 4 | +# |
| 5 | + |
| 6 | +# Exit immediately if a command exits with a non-zero status. |
| 7 | +set -e |
| 8 | + |
| 9 | +# Start builtin bash timer |
| 10 | +SECONDS=0 |
| 11 | + |
| 12 | +# --- Helper Functions --- |
| 13 | + |
| 14 | +check_variables() { |
| 15 | + if [ -z "$KSU_BASE" ]; then |
| 16 | + echo "Warning: KSU_BASE environment variable is not set." |
| 17 | + echo "Please set it to the name of the .config fragment to apply for compiling KSU" |
| 18 | + echo "Example: export KSU_BASE=\"rksu\"" |
| 19 | + echo "Assuming building with NoSU now..." |
| 20 | + fi |
| 21 | +} |
| 22 | + |
| 23 | +setup_environment() { |
| 24 | + echo "Setting up build environment..." |
| 25 | + export ARCH=arm64 |
| 26 | + export KBUILD_BUILD_USER=vbajs |
| 27 | + export KBUILD_BUILD_HOST=tbyool |
| 28 | + |
| 29 | + export GCC64_DIR=$PWD/gcc64 |
| 30 | + export GCC32_DIR=$PWD/gcc32 |
| 31 | +} |
| 32 | + |
| 33 | +setup_toolchain() { |
| 34 | + echo "Setting up toolchains..." |
| 35 | + |
| 36 | + # Setup Clang |
| 37 | + if [ ! -d "$PWD/clang" ]; then |
| 38 | + echo "Cloning Clang..." |
| 39 | + git clone https://gitlab.com/crdroidandroid/android_prebuilts_clang_host_linux-x86_clang-r547379.git --depth=1 -b 15.0 clang |
| 40 | + else |
| 41 | + echo "Local clang dir found, using it." |
| 42 | + fi |
| 43 | + |
| 44 | + # Setup GCC |
| 45 | + if [ ! -d "$PWD/gcc32" ] && [ ! -d "$PWD/gcc64" ]; then |
| 46 | + echo "Downloading GCC..." |
| 47 | + ASSET_URLS=$(curl -s "https://api.github.com/repos/mvaisakh/gcc-build/releases/latest" | grep "browser_download_url" | cut -d '"' -f 4 | grep -E "eva-gcc-arm.*\.xz") |
| 48 | + for url in $ASSET_URLS; do |
| 49 | + wget --content-disposition -L "$url" |
| 50 | + done |
| 51 | + |
| 52 | + for file in eva-gcc-arm*.xz; do |
| 53 | + # The files are actually just plain tarballs named as .xz |
| 54 | + if [[ "$file" == *arm64* ]]; then |
| 55 | + tar -xf "$file" && mv gcc-arm64 gcc64 |
| 56 | + else |
| 57 | + tar -xf "$file" && mv gcc-arm gcc32 |
| 58 | + fi |
| 59 | + rm -rf "$file" |
| 60 | + done |
| 61 | + else |
| 62 | + echo "Local gcc dirs found, using them." |
| 63 | + fi |
| 64 | +} |
| 65 | + |
| 66 | +update_path() { |
| 67 | + echo "Updating PATH..." |
| 68 | + export GCC64_DIR=$PWD/gcc64 |
| 69 | + export GCC32_DIR=$PWD/gcc32 |
| 70 | + export PATH="$PWD/clang/bin/:$GCC64_DIR/bin/:$GCC32_DIR/bin/:/usr/bin:$PATH" |
| 71 | +} |
| 72 | + |
| 73 | +compile_kernel() { |
| 74 | + echo -e "\nStarting compilation..." |
| 75 | + |
| 76 | + # 1. Make the base defconfig |
| 77 | + make O=out ARCH=arm64 sweet_defconfig |
| 78 | + if [ -z "$KSU_BASE" ]; then |
| 79 | + make O=out ARCH=arm64 vendor/{$KSU_BASE}.config |
| 80 | + fi |
| 81 | + |
| 82 | + # 3. Run the main build |
| 83 | + make -j$(nproc --all) \ |
| 84 | + O=out \ |
| 85 | + ARCH=arm64 \ |
| 86 | + LLVM=1 \ |
| 87 | + LLVM_IAS=1 \ |
| 88 | + CROSS_COMPILE=$GCC64_DIR/bin/aarch64-elf- \ |
| 89 | + CROSS_COMPILE_COMPAT=$GCC32_DIR/bin/arm-eabi- |
| 90 | +} |
| 91 | + |
| 92 | +package_output() { |
| 93 | + echo -e "\nPackaging outputs..." |
| 94 | + |
| 95 | + local kernel="out/arch/arm64/boot/Image" |
| 96 | + local dtbo="out/arch/arm64/boot/dtbo.img" |
| 97 | + local dtb="out/arch/arm64/boot/dtb.img" |
| 98 | + |
| 99 | + if [ ! -f "$kernel" ] || [ ! -f "$dtbo" ] || [ ! -f "$dtb" ]; then |
| 100 | + echo -e "\nCompilation failed! Output files not found." |
| 101 | + exit 1 |
| 102 | + fi |
| 103 | + |
| 104 | + # Copy outputs to current directory with KSU_BASE prefix if exists |
| 105 | + if [ -z "$KSU_BASE" ]; then |
| 106 | + cp "$kernel" "./${KSU_BASE}-Image" |
| 107 | + else |
| 108 | + cp "$kernel" "./Image" |
| 109 | + fi |
| 110 | + cp "$dtbo" "./dtbo.img" |
| 111 | + cp "$dtb" "./dtb.img" |
| 112 | + |
| 113 | + echo "Outputs copied to root directory with prefix '${KSU_BASE}'" |
| 114 | +} |
| 115 | + |
| 116 | +print_summary() { |
| 117 | + echo -e "\nCompleted in $((SECONDS / 60)) minute(s) and $((SECONDS % 60)) second(s) !" |
| 118 | +} |
| 119 | + |
| 120 | +# --- Main Execution --- |
| 121 | + |
| 122 | +main() { |
| 123 | + check_variables |
| 124 | + setup_environment |
| 125 | + setup_toolchain |
| 126 | + update_path |
| 127 | + compile_kernel |
| 128 | + package_output |
| 129 | + print_summary |
| 130 | +} |
| 131 | + |
| 132 | +# Run the main function |
| 133 | +main |
0 commit comments