#!/bin/bash
set -eE -o pipefail          # -E ⇒ trap inherits in functions/subshells

stage=2                      # 1 = build, 2 = infrastructure

trap 'case $stage in
          0) exit 0;;        # any build command succeeded
          1) exit 1;;        # any build command failed
          2) exit 2;;        # any test command failed
          *) exit $?;        # fallback for unexpected errors
      esac' ERR

# Build metadata:
#   arch: x86_64
#   compiler: gcc-12
#   defconfig: allnoconfig
#   fragments: ['kselftest']
#   src_tarball: https://files-staging.kernelci.org/linux-kernelci-staging-stable-staging-stable-20250806.1.tar.gz
#   apijobname: kbuild-gcc-12-x86-allnoconfig

# Set environment variables
export ARCH=x86_64
export INSTALL_MOD_PATH=_modules_
export INSTALL_MOD_STRIP=1
export INSTALL_DTBS_PATH=_dtbs_
export CC=gcc-12
export HOSTCC=gcc-12
set -x
touch /tmp/kci/artifacts/build.log
tail -f /tmp/kci/artifacts/build.log &
tailpid=$!
exec >>/tmp/kci/artifacts/build.log 2>&1
# Download tarball
cd /tmp/kci
wget -c -t 10 --retry-on-host-error "https://files-staging.kernelci.org/linux-kernelci-staging-stable-staging-stable-20250806.1.tar.gz" -O linux.tgz
tar -xzf linux.tgz -C /tmp/kci/linux --strip-components=1

# Starting job config_defconfig
echo -----log:config_defconfig-----
echo job:config_defconfig=running >> /tmp/kci/artifacts/state.txt
echo jobsts:config_defconfig=$(date +%s) >> /tmp/kci/artifacts/state.txt
cd /tmp/kci/linux
make allnoconfig
# Finishing job config_defconfig
echo job:config_defconfig=done >> /tmp/kci/artifacts/state.txt
echo jobets:config_defconfig=$(date +%s) >> /tmp/kci/artifacts/state.txt

# Starting job config_fragments
echo -----log:config_fragments-----
echo job:config_fragments=running >> /tmp/kci/artifacts/state.txt
echo jobsts:config_fragments=$(date +%s) >> /tmp/kci/artifacts/state.txt
./scripts/kconfig/merge_config.sh -m .config /tmp/kci/artifacts/fragments/0.config
./scripts/kconfig/merge_config.sh -m .config /tmp/kci/artifacts/fragments/1.config
make olddefconfig
cp .config /tmp/kci/artifacts/
cd ..
# Finishing job config_fragments
echo job:config_fragments=done >> /tmp/kci/artifacts/state.txt
echo jobets:config_fragments=$(date +%s) >> /tmp/kci/artifacts/state.txt

# Starting job fetch_firmware
echo -----log:fetch_firmware-----
echo job:fetch_firmware=running >> /tmp/kci/artifacts/state.txt
echo jobsts:fetch_firmware=$(date +%s) >> /tmp/kci/artifacts/state.txt
cp /data/linux-firmware.tar.gz .
tar -xzf linux-firmware.tar.gz
cd linux-firmware
./copy-firmware.sh /tmp/kci/firmware
cd ..
# Finishing job fetch_firmware
echo job:fetch_firmware=done >> /tmp/kci/artifacts/state.txt
echo jobets:fetch_firmware=$(date +%s) >> /tmp/kci/artifacts/state.txt

# Starting job build_kernel
echo -----log:build_kernel-----
echo job:build_kernel=running >> /tmp/kci/artifacts/state.txt
echo jobsts:build_kernel=$(date +%s) >> /tmp/kci/artifacts/state.txt
cd /tmp/kci/linux
stage=1
make -j$(nproc) bzImage  > >(tee /tmp/kci/artifacts/build_kimage.log) 2> >(tee /tmp/kci/artifacts/build_kimage_stderr.log >&1)
stage=2
cd ..
# Finishing job build_kernel
echo job:build_kernel=done >> /tmp/kci/artifacts/state.txt
echo jobets:build_kernel=$(date +%s) >> /tmp/kci/artifacts/state.txt

# Starting job build_modules
echo -----log:build_modules-----
echo job:build_modules=running >> /tmp/kci/artifacts/state.txt
echo jobsts:build_modules=$(date +%s) >> /tmp/kci/artifacts/state.txt
cd /tmp/kci/linux
set +eE -o pipefail
trap - ERR

grep "CONFIG_MODULES=y" .config
if [ $? -eq 0 ]; then

set -eE -o pipefail

trap 'case $stage in
          0) exit 0;;        # success
          1) exit 1;;        # any build command failed
          2) exit 2;;        # any test command failed
          *) exit $?;        # fallback for unexpected errors
      esac' ERR

stage=1
make -j$(nproc) modules  > >(tee /tmp/kci/artifacts/build_modules.log) 2> >(tee /tmp/kci/artifacts/build_modules_stderr.log >&1)
stage=2
fi

set -eE -o pipefail

trap 'case $stage in
          0) exit 0;;        # success
          1) exit 1;;        # any build command failed
          2) exit 2;;        # any test command failed
          *) exit $?;        # fallback for unexpected errors
      esac' ERR

cd ..
# Finishing job build_modules
echo job:build_modules=done >> /tmp/kci/artifacts/state.txt
echo jobets:build_modules=$(date +%s) >> /tmp/kci/artifacts/state.txt

# Starting job build_kselftest
echo -----log:build_kselftest-----
echo job:build_kselftest=running >> /tmp/kci/artifacts/state.txt
echo jobsts:build_kselftest=$(date +%s) >> /tmp/kci/artifacts/state.txt
cd /tmp/kci/linux
stage=1
echo Ignore error in next command, if any
make -j$(nproc) kselftest-gen_tar  > >(tee /tmp/kci/artifacts/build_kselftest.log) 2> >(tee /tmp/kci/artifacts/build_kselftest_stderr.log >&1) || true
stage=2
# Finishing job build_kselftest
echo job:build_kselftest=done >> /tmp/kci/artifacts/state.txt
echo jobets:build_kselftest=$(date +%s) >> /tmp/kci/artifacts/state.txt

# Starting job package_kimage
echo -----log:package_kimage-----
echo job:package_kimage=running >> /tmp/kci/artifacts/state.txt
echo jobsts:package_kimage=$(date +%s) >> /tmp/kci/artifacts/state.txt
cd /tmp/kci/linux
echo Ignore error in next command, if any
cp arch/x86_64/boot/bzImage ../artifacts || true
echo Ignore error in next command, if any
cp vmlinux ../artifacts || true
cd ..
# Finishing job package_kimage
echo job:package_kimage=done >> /tmp/kci/artifacts/state.txt
echo jobets:package_kimage=$(date +%s) >> /tmp/kci/artifacts/state.txt

# Starting job package_modules
echo -----log:package_modules-----
echo job:package_modules=running >> /tmp/kci/artifacts/state.txt
echo jobsts:package_modules=$(date +%s) >> /tmp/kci/artifacts/state.txt
cd /tmp/kci/linux
set +eE -o pipefail
trap - ERR

grep "CONFIG_MODULES=y" .config
if [ $? -eq 0 ]; then

set -eE -o pipefail

trap 'case $stage in
          0) exit 0;;        # success
          1) exit 1;;        # any build command failed
          2) exit 2;;        # any test command failed
          *) exit $?;        # fallback for unexpected errors
      esac' ERR

stage=1
make modules_install
stage=2
tar -C _modules_ -cJf /tmp/kci/artifacts/modules.tar.xz .
cd ..
rm -rf _modules_
else

set -eE -o pipefail

trap 'case $stage in
          0) exit 0;;        # success
          1) exit 1;;        # any build command failed
          2) exit 2;;        # any test command failed
          *) exit $?;        # fallback for unexpected errors
      esac' ERR

echo "No modules to install"
cd ..
fi

set -eE -o pipefail

trap 'case $stage in
          0) exit 0;;        # success
          1) exit 1;;        # any build command failed
          2) exit 2;;        # any test command failed
          *) exit $?;        # fallback for unexpected errors
      esac' ERR

# Finishing job package_modules
echo job:package_modules=done >> /tmp/kci/artifacts/state.txt
echo jobets:package_modules=$(date +%s) >> /tmp/kci/artifacts/state.txt

# Starting job package_kselftest
echo -----log:package_kselftest-----
echo job:package_kselftest=running >> /tmp/kci/artifacts/state.txt
echo jobsts:package_kselftest=$(date +%s) >> /tmp/kci/artifacts/state.txt
echo Ignore error in next command, if any
cp /tmp/kci/linux/tools/testing/selftests/kselftest_install/kselftest-packages/kselftest.tar.gz /tmp/kci/artifacts/ || true
# Finishing job package_kselftest
echo job:package_kselftest=done >> /tmp/kci/artifacts/state.txt
echo jobets:package_kselftest=$(date +%s) >> /tmp/kci/artifacts/state.txt

echo Build script is completed, tail will be killed now
stage=0
set +eE -o pipefail
trap - ERR

kill $tailpid || true
stage=0
kill $tailpid || true
