From 2c4268555557828912f881cabefa1ff4d817e049 Mon Sep 17 00:00:00 2001 From: Locietta Date: Thu, 19 Dec 2024 20:48:23 +0800 Subject: [PATCH] build.sh: refactor & fix ccache #102 remove IS_LTS env in *.yml, but not change build.sh accordingly, which makes build.sh always selects to non-ccache branch. Add option parser like that in setup.sh and modify *.yml, which should fix ccache. Also remove old SMT detection code. `nproc` itself already respects SMT. Like with 8C,16T processor, `nproc` will return 16. `nproc --all` basically is same as `nproc` but it will ignore cgroup limitation forced by containers like docker, and also on motherboards supporting hotplug cpus it will return max possible cpu slots instead of the number of installed count. So yeah, sometimes `nproc --all` returns a bigger number than `nproc`, but those "extra" cpus aren't actually available for use. So let's just use `nproc`. But use 2*nproc might still make sense if compilation is actually IO-bounded. I tested nproc vs 2*nproc on my private repo's action. It seems like no noticable difference though. Maybe we should revisit the thread count decision someday later. (cherry picked from commit f05d3d5c422738ad90237753b9bd60396ded6238) --- .github/workflows/build-lts.yml | 2 +- .github/workflows/build.yml | 2 +- build.sh | 66 +++++++++++++++++++++++++++++---- setup.sh | 6 +-- 4 files changed, 64 insertions(+), 12 deletions(-) diff --git a/.github/workflows/build-lts.yml b/.github/workflows/build-lts.yml index e379efd..e05b58b 100644 --- a/.github/workflows/build-lts.yml +++ b/.github/workflows/build-lts.yml @@ -79,7 +79,7 @@ jobs: - name: Build kernel if: ${{ env.REBUILD_FLAG }} run: | - cd linux && ../build.sh + cd linux && ../build.sh --branch LTS mv arch/x86/boot/bzImage ../${{ matrix.image-name }} cd .. && sha256sum ${{ matrix.image-name }} > ${{ matrix.image-name }}.sha256 diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 42665f5..9615b4e 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -92,7 +92,7 @@ jobs: - name: Build kernel if: ${{ env.REBUILD_FLAG }} run: | - cd linux && ../build.sh + cd linux && ../build.sh --branch MAIN mv arch/x86/boot/bzImage ../${{ matrix.image-name }} cd .. && sha256sum ${{ matrix.image-name }} > ${{ matrix.image-name }}.sha256 diff --git a/build.sh b/build.sh index 6b7ed39..837797d 100755 --- a/build.sh +++ b/build.sh @@ -1,15 +1,67 @@ -# -if [ "$(cat /sys/devices/system/cpu/smt/active)" = "1" ]; then - export LOGICAL_CORES=$(($(nproc --all) * 2)) -else - export LOGICAL_CORES=$(nproc --all) +#!/bin/bash + +usage() { + echo "Usage: build.sh [options]" + echo "Options:" + echo " -b|--branch - Specify the Xanmod kernel branch to build. (Default: MAIN)" + echo " -j|--jobs - Specify the number of jobs to use for parallel compilation. (Default: $(nproc))" + echo " -h - Show this help message." +} + +# Parse options with getopt +temp=$(getopt -o 'b:j:h' --long 'branch:,jobs:,help' -n 'build.sh' -- "$@") +if [ $? -ne 0 ]; then + # unsupported options provided + # NOTE: error log not needed, will be reported by getopt + usage + exit 1 fi +eval set -- "$temp" +unset temp +while true; do + case "$1" in + '-b'|'--branch') + BRANCH="$2" + shift 2 + # check if branch is LTS or MAIN + if [ "$BRANCH" != "LTS" ] && [ "$BRANCH" != "MAIN" ]; then + echo "Invalid branch: $BRANCH" + exit 2 + fi + continue + ;; + '-j'|'--jobs') + LOGICAL_CORES="$2" + shift 2 + continue + ;; + '-h'|'--help') + usage + exit 0 + ;; + '--') + shift + break + ;; + *) + # should not happen + echo "Unhandled option: $1" + exit 1 + ;; + esac +done +# Default values +BRANCH=${BRANCH:-MAIN} + +LOGICAL_CORES=${LOGICAL_CORES:-$(nproc)} + +# # Compile # -if [ "$IS_LTS" = "NO" ]; then +if [ "$BRANCH" = "MAIN" ]; then echo -e "Using $LOGICAL_CORES jobs for this non-LTS build..." make CC='ccache clang -Qunused-arguments -fcolor-diagnostics' LLVM=1 LLVM_IAS=1 -j$LOGICAL_CORES -else +elif [ "$BRANCH" = "LTS" ]; then echo -e "Using $LOGICAL_CORES jobs for this LTS build..." make LLVM=1 LLVM_IAS=1 -j$LOGICAL_CORES fi diff --git a/setup.sh b/setup.sh index 41fd9c4..e6f14ee 100755 --- a/setup.sh +++ b/setup.sh @@ -104,10 +104,10 @@ loop_apply_patch $PATCH_DIR # Apply LTS/MAIN specific patches loop_apply_patch $SPESIFIC_PATCH_DIR -if [ "$BRANCH" = "LTS" ]; then - cp ../wsl2_defconfig.LTS ./arch/x86/configs/wsl2_defconfig -else +if [ "$BRANCH" = "MAIN" ]; then cp ../wsl2_defconfig.MAIN ./arch/x86/configs/wsl2_defconfig +elif [ "$BRANCH" = "LTS" ]; then + cp ../wsl2_defconfig.LTS ./arch/x86/configs/wsl2_defconfig fi make LLVM=1 LLVM_IAS=1 wsl2_defconfig