#!/usr/bin/env bash # script requires: git tar xz cpio make patch rpm python3 wget # kernel requires: gcc flex bison make openssl-devel elfutils-libelf-devel bc perl-devel double-conversion-devel qt5-qtbase-devel libXi-devel rpm-build DISTRO_VERSION="44" BUILD_VERSION=${KERNEL_VERSION:=7.3}-300.fc${DISTRO_VERSION}.ms-r1 HEADERS=/usr/src/kernels/$BUILD_VERSION JOBS=$(nproc) BUILD=$(pwd) KERNEL_TREE=$BUILD/linux-${KERNEL_VERSION} KERNEL_PATCHES=$BUILD/linux-cix-main DRIVERS=$BUILD/drivers LOGS=$BUILD/logs CMDLINE_COMMON="console=tty0 console=ttyAMA0,115200n8 cma=512M clk_ignore_unused=1 cpufreq.default_governor=schedutil" CMDLINE_POWERSAVE="pcie_aspm=powersave pcie_aspm_policy=default $CMDLINE_COMMON" CMDLINE="pcie_aspm=off $CMDLINE_COMMON" log() { printf '[%s] %s\n' "$(date '+%H:%M:%S')" "$*"; } die() { printf '[%s] ERROR: %s\n' "$(date '+%H:%M:%S')" "$*"; exit 1; } run() { local l=/tmp/$BUILD_VERSION.build.run.log; "$@" > "$l" 2>&1 || die "failed: $(cat $l | tail -n 1)"; } sub() { local s=/tmp/$BUILD_VERSION.build.sub.sh; echo "$(declare -p); $(declare -f)" > $s; echo "source $s 2>/dev/null && $1"; } get() { local d=/tmp/$1; [ -f "$d" ] || { run wget -q -O $d "$2/$1"; cat $d; return; }; log "using cached file $d" >&2; cat $d; } purge() { log "removing modules" rm -rf "/lib/modules/$BUILD_VERSION" "$HEADERS" || die "failed" log "removing boot images and config files" find /boot -name "*-$BUILD_VERSION*" -delete || die "failed" } setup() { log "uninstalling (if exists) legacy kernel ${BUILD_VERSION} from previous build(s)" purge log "copying header files" cp -r "$KERNEL_TREE" "$HEADERS" || die "failed" log "installing kernel modules" make -C "$HEADERS" -j"$JOBS" modules_install install >"$LOGS/install.log" 2>&1 || die "failed (see logs/install.log)" log "installing drivers" for drv in "${DRV[@]}"; do make -C "$HEADERS" -j"$JOBS" M=$drv modules_install install >>"$LOGS/install.log" 2>&1 || die "failed (see logs/install.log)" done log "updating bootloader configuration" local id="$(grubby --info=$(grubby --default-index) | grep 'id=' | awk -F'\"' '{print $2}')" local c1=/boot/loader/entries/$id.conf local c2=/boot/loader/entries/$id.powersave.conf cp $c1 $c2 sed -i "/^options/s/$/ $CMDLINE/" $c1 sed -i "/^options/s/$/ $CMDLINE_POWERSAVE/" $c2 sed -i "/^title/s/$/ Powersave Mode/" $c2 log "cleaning up" make -C "$HEADERS" clean >/dev/null } [ -z "$UNINSTALL" ] || { pkexec bash -c "$(sub purge)" && log "done"; exit 0; } [ -z "$CLEAN" ] || rm -rf $KERNEL_TREE $KERNEL_PATCHES $DRIVERS $LOGS mkdir -p "$DRIVERS" "$LOGS" log "building linux kernel ${KERNEL_VERSION} (arm64) with CIX mainline patches" rebase() { run git remote add $1 https://github.com/$1/$(basename $(git remote get-url origin)) run git fetch $1 git config user.email "build@ms-r1.local" git config user.name "Build" git rebase $1/$2 >/dev/null 2>&1 || die "could not rebase branch $1:$2" } if [ ! -d "$KERNEL_PATCHES" ]; then log "cloning CIX linux mainline" run git clone --depth=1 https://github.com/cixtech/cix-linux-main.git $KERNEL_PATCHES cd $KERNEL_PATCHES log "head: $(git show -s --oneline)" fi if [ ! -d "$DRIVERS/npu" ]; then log "cloning NPU driver" run git clone --depth=1 --branch=cix_mainline_dev https://github.com/cixtech/cix_opensource__npu_driver.git $DRIVERS/npu cd $DRIVERS/npu log "head: $(git show -s --oneline)" fi if [ ! -d "$DRIVERS/vpu" ]; then log "cloning VPU driver" run git clone --depth=1 --branch=cix_mainline_dev https://github.com/cixtech/cix_opensource__vpu_driver.git $DRIVERS/vpu cd $DRIVERS/vpu rebase EvilOlaf fix-strncpy # v4l2: replace strncpy with strscpy log "head: $(git show -s --oneline)" fi if [ ! -d "$KERNEL_TREE" ]; then log "downloading linux kernel source tree" get "linux-${KERNEL_VERSION}.tar.xz" "https://www.kernel.org/pub/linux/kernel/v${KERNEL_VERSION:0:1}.x/" \ | tar -xf - --xz -C $BUILD fi if [ ! -f "$KERNEL_TREE/.config-stock" ] && [ -z "$CONFIG" ]; then log "loading local kernel config" yes "" | make -C "$KERNEL_TREE" localmodconfig >/dev/null 2>&1 cp $KERNEL_TREE/.config $KERNEL_TREE/.config-stock fi if [ ! -f "$KERNEL_TREE/.patches-cix" ]; then log "applying patches" err=0; output="$LOGS/patches.log"; echo "" >$output for p in "$KERNEL_PATCHES/patches-${KERNEL_VERSION:0:3}"/*.patch; do if ! patch -d "$KERNEL_TREE" -p1 --forward --batch -s <"$p" >>$output 2>&1 then log "warning: patch failed: $p"; err=$((err+1)); fi done log $(if (( err == 0 )); then echo "all patches applied"; else echo "warning: $err error(s) reported (see logs/patches.log)"; fi) find "$KERNEL_TREE" \( -name '*.orig' -o -name '*.rej' \) -delete touch "$KERNEL_TREE/.patches-cix" fi if [ ! -f "$KERNEL_TREE/.config.ms-r1" ] || [ -n "$REBUILD" ]; then log "configuring kernel" cd $KERNEL_TREE make mrproper >/dev/null cp $KERNEL_PATCHES/config/config-${KERNEL_VERSION:0:3}.defconfig .config-cix cp $CONFIG .config-stock 2>/dev/null run ./scripts/kconfig/merge_config.sh -m -O . .config-cix .config-stock run ./scripts/kconfig/merge_config.sh -m -O . .config "$BUILD/config/ms-r1.fragment" CROSS=$(if [[ "$(uname -m)" != "aarch64" ]]; then echo "aarch64-linux-gnu-"; fi) make ARCH=arm64 CROSS_COMPILE=$CROSS olddefconfig >/dev/null 2>&1 cp .config .config.ms-r1 fi log "compiling kernel" make -C "$KERNEL_TREE" ARCH=arm64 CROSS_COMPILE=$CROSS -j"$JOBS" "$LOGS/build.log" 2>&1 || die "failed (see logs/build.log)" log "compiling drivers" for m in $(find $DRIVERS -name "dkms.conf" -print); do cd "$(dirname $m)"; log "in: $PWD" options=$(bash -c "source $m && echo \$MAKE" | cut -d' ' -f4- -) IFS=' ' read -ra array <<< "$options" make clean >/dev/null 2>&1 make -C "$KERNEL_TREE" -j"$JOBS" M=$PWD "${array[@]}" >>"$LOGS/build.log" 2>&1 || die "failed (see logs/build.log)" DRV+=($PWD) done [ -z "$INSTALL" ] || { pkexec bash -c "$(sub setup)" && log "done"; exit 0; } log "done" log "note: run this script with INSTALL=1 to trigger kernel install (requires root privileges)"