Skip to content

Instantly share code, notes, and snippets.

@Elletroperf
Forked from chaptsand/kernel-build.sh
Last active January 15, 2025 18:54
Show Gist options
  • Select an option

  • Save Elletroperf/60c34f4094f4f473263a007fd12ace0b to your computer and use it in GitHub Desktop.

Select an option

Save Elletroperf/60c34f4094f4f473263a007fd12ace0b to your computer and use it in GitHub Desktop.
Build a QCOM GKI kernel with external modules and generate the module.load files
#!/bin/bash
set -e
KERNEL_TOOLS=/home/mezaque/android15/voltage/prebuilts/kernel-build-tools/linux-x86/bin/
CLANG_PATH=/home/mezaque/android15/voltage/prebuilts/clang/host/linux-x86/clang-r522817/bin/
export PATH="$KERNEL_TOOLS:$CLANG_PATH:$PATH"
TARGET=$1
if [[ -z "$TARGET" ]]; then
echo "Usage: $0 <target>"
exit 1
fi
KERNEL_SRC=$(pwd)
O=out
OUT_DIR=$KERNEL_SRC/$O
INSTALL_MOD_PATH=modules_out
TARGET_DEFCONFIG="${TARGET}_defconfig"
TARGET_KERNEL_EXT_MODULE_ROOT="$KERNEL_SRC/../sm8550-modules"
FIRST_STAGE_MODULES_LIST="modules.list.msm.kalama"
RECOVERY_EXT_MODULES="msm_drm.ko"
case "$TARGET" in
dm1q|dm2q)
TARGET_KERNEL_EXT_MODULES="
qcom/opensource/mmrm-driver
qcom/opensource/mm-drivers/hw_fence
qcom/opensource/mm-drivers/msm_ext_display
qcom/opensource/mm-drivers/sync_fence
qcom/opensource/audio-kernel
qcom/opensource/camera-kernel
qcom/opensource/dataipa/drivers/platform/msm
qcom/opensource/datarmnet/core
qcom/opensource/datarmnet-ext/aps
qcom/opensource/datarmnet-ext/offload
qcom/opensource/datarmnet-ext/shs
qcom/opensource/datarmnet-ext/perf
qcom/opensource/datarmnet-ext/perf_tether
qcom/opensource/datarmnet-ext/sch
qcom/opensource/datarmnet-ext/wlan
qcom/opensource/securemsm-kernel
qcom/opensource/display-drivers/msm
qcom/opensource/eva-kernel
qcom/opensource/video-driver
qcom/opensource/graphics-kernel
qcom/opensource/wlan/platform
qcom/opensource/wlan/qcacld-3.0/.qca6490
qcom/opensource/bt-kernel"
;;
dm3q)
TARGET_KERNEL_EXT_MODULES="
qcom/opensource/mmrm-driver
qcom/opensource/mm-drivers/hw_fence
qcom/opensource/mm-drivers/msm_ext_display
qcom/opensource/mm-drivers/sync_fence
qcom/opensource/audio-kernel
qcom/opensource/camera-kernel
qcom/opensource/dataipa/drivers/platform/msm
qcom/opensource/datarmnet/core
qcom/opensource/datarmnet-ext/aps
qcom/opensource/datarmnet-ext/offload
qcom/opensource/datarmnet-ext/shs
qcom/opensource/datarmnet-ext/perf
qcom/opensource/datarmnet-ext/perf_tether
qcom/opensource/datarmnet-ext/sch
qcom/opensource/datarmnet-ext/wlan
qcom/opensource/securemsm-kernel
qcom/opensource/display-drivers/msm
qcom/opensource/eva-kernel
qcom/opensource/video-driver
qcom/opensource/graphics-kernel
qcom/opensource/wlan/platform
qcom/opensource/wlan/qcacld-3.0/.kiwi_v2
qcom/opensource/bt-kernel"
;;
*)
echo "Error: Unknown target $TARGET"
exit 1
;;
esac
function section() {
local input="$1"
local length=${#input}
printf '%*s\n' "$length" '' | tr ' ' '='
echo "$input"
printf '%*s\n' "$length" '' | tr ' ' '='
}
function kernel_make() {
make -j$(nproc) \
O=$O \
ARCH=arm64 \
LLVM=1 \
LLVM_IAS=1 \
CROSS_COMPILE=aarch64-linux-gnu- \
CROSS_COMPILE_ARM32=arm-linux-gnueabi- \
INSTALL_MOD_PATH=$INSTALL_MOD_PATH \
$@ < /dev/null
}
function ext_module_make() {
module_path=$TARGET_KERNEL_EXT_MODULE_ROOT/$1
kernel_make \
OUT_DIR=$OUT_DIR \
KERNEL_SRC=$KERNEL_SRC \
KERNEL_UAPI_HEADERS_DIR=$OUT_DIR \
M=$(realpath --relative-to=$KERNEL_SRC $module_path) \
-C $module_path \
${@:2}
}
function get_modlib_file_path() {
local file_path=$(echo $OUT_DIR/$INSTALL_MOD_PATH/**/**/**/$1)
if [[ $(echo $file_path | wc -w) -ne 1 ]]; then
echo "None or multiple files were found for $1 on modlib: $file_path"
exit 1
fi
echo $file_path
}
function generate_module_deps() {
modules_dep_file=$(get_modlib_file_path modules.dep)
if [[ "$(grep -e "^$1:" -e "/$1:" $modules_dep_file)" == "" ]]; then
echo "Needed $1 was not found in modules.dep"
exit 1
fi
module_data=$(grep -e "^$1:" -e "/$1:" $(get_modlib_file_path modules.dep))
module_name=$(basename $1)
if [[ -f $2 ]] && [[ "$(grep "^$module_name" $2)" != "" ]]; then
return
fi
echo $module_name >> $2
# Generate the module dependencies
module_deps=$(echo $module_data | cut -d ":" -f 2)
for dep in $module_deps; do
generate_module_deps $dep $2
done
}
function generate_modules_load() {
modules_order_file=$(get_modlib_file_path modules.order)
modules_dep_file=$(get_modlib_file_path modules.dep)
rm -f $OUT_DIR/modules.load.*
#
# First stage modules
# Get modules from FIRST_STAGE_MODULES_LIST and remove non existing ones
#
echo "Generating first stage modules list at $OUT_DIR/modules.load.vendor_boot"
for mod in $(cat $FIRST_STAGE_MODULES_LIST); do
if [[ "$(grep -e "^$mod:" -e "/$mod:" $(get_modlib_file_path modules.dep))" != "" ]]; then
generate_module_deps $mod $OUT_DIR/modules.load.vendor_boot
fi
done
#
# Recovery modules (first stage + ext recovery modules)
#
echo "Generating recovery modules list at $OUT_DIR/modules.load.recovery"
cat $modules_order_file | rev | cut -d / -f 1 | rev > $OUT_DIR/modules.load.recovery
for ext_mod in $RECOVERY_EXT_MODULES; do
generate_module_deps $ext_mod $OUT_DIR/modules.load.recovery
done
#
# Vendor DLKM modules (non-first-stage modules + extra/ modules)
#
echo "Generating vendor DLKM modules list at $OUT_DIR/modules.load"
ext_modules=$(cat $modules_dep_file | cut -d ":" -f 1)
for ext_mod in $ext_modules; do
generate_module_deps $ext_mod $OUT_DIR/modules.load
done
# Remove first stage modules from vendor_dlkm
for mod in $(cat $OUT_DIR/modules.load.vendor_boot); do
sed -i /$mod/d $OUT_DIR/modules.load
done
}
# Removing modules "zram.ko" "zsmalloc.ko"
function remove_modules_from_load_files() {
local modules_to_remove=("zram.ko" "zsmalloc.ko")
local files_to_edit=("$OUT_DIR/modules.load.vendor_boot" "$OUT_DIR/modules.load.recovery" "$OUT_DIR/modules.load")
for file in "${files_to_edit[@]}"; do
if [[ -f $file ]]; then
echo "Removing specified modules from $file"
for mod in "${modules_to_remove[@]}"; do
sed -i "/$mod/d" "$file"
done
fi
done
}
if [[ -z $TARGET_DEFCONFIG ]] || [[ -z $TARGET_KERNEL_EXT_MODULES ]] || [[ -z $TARGET_KERNEL_EXT_MODULE_ROOT ]] || [[ -z $FIRST_STAGE_MODULES_LIST ]]; then
echo "Misconfigured target, make sure that all needed variables are set"
exit 1
fi
section "Kernel config"
make O=$O ARCH=arm64 $TARGET_DEFCONFIG
section "Kernel build"
kernel_make
section "Kernel modules install"
kernel_make modules_install
section "External kernel modules build"
echo "Module root is $TARGET_KERNEL_EXT_MODULE_ROOT"
for module in $TARGET_KERNEL_EXT_MODULES; do
section "Building $module"
ext_module_make $module
section "Installing $module"
ext_module_make $module modules_install
done
section "Generating modules.load files"
generate_modules_load
section "Removing unwanted modules"
remove_modules_from_load_files
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment