#!/bin/bash
# shellcheck disable=SC2034 disable=SC2154 disable=SC2155 disable=SC2048 disable=SC2086 disable=SC2295
# ^^^^^ These have been deliberately disabled throughout this entire PKGBUILD, please don't remove them :)
# Maintainer: JeremyStarTM <jeremystartm@staropensource.de>
# Maintainer: Josip Ponjavic <josipponjavic at gmail dot com>

### BUILD OPTIONS

# Set to anything but null to activate
# Tweak kernel options prior to a build via nconfig
: "${_makenconfig:=""}"

# Set to anything but null to activate
# Tweak kernel options prior to a build via xconfig
: "${_makexconfig:=""}"

# Set to anything but null to activate
# Copies the final kernel configuration before building
: "${_copyfinalconfig:=""}"

# Set to anything but null to activate
# Only compile active modules to VASTLY reduce the number of modules built and
# the build time.
#
# To keep track of which modules are needed for your specific system/hardware,
# give module_db a try: https://aur.archlinux.org/packages/modprobed-db
# This PKGBUILD reads the database kept if it exists
#
# More at this wiki page ---> https://wiki.archlinux.org/index.php/Modprobed-db
: "${_localmodcfg:=""}"

# Optionally select a sub architecture by number or leave blank which will
# require user interaction during the build. Note that the generic (default)
# option is 40.
#
#  1. AMD Opteron/Athlon64/Hammer/K8 (MK8)
#  2. AMD Opteron/Athlon64/Hammer/K8 with SSE3 (MK8SSE3)
#  3. AMD 61xx/7x50/PhenomX3/X4/II/K10 (MK10)
#  4. AMD Barcelona (MBARCELONA)
#  5. AMD Bobcat (MBOBCAT)
#  6. AMD Jaguar (MJAGUAR)
#  7. AMD Bulldozer (MBULLDOZER)
#  8. AMD Piledriver (MPILEDRIVER)
#  9. AMD Steamroller (MSTEAMROLLER)
#  10. AMD Excavator (MEXCAVATOR)
#  11. AMD Zen (MZEN)
#  12. AMD Zen 2 (MZEN2)
#  13. AMD Zen 3 (MZEN3)
#  14. AMD Zen 4 (MZEN4)
#  15. Intel P4 / older Netburst based Xeon (MPSC)
#  16. Intel Core 2 (MCORE2)
#  17. Intel Atom (MATOM)
#  18. Intel Nehalem (MNEHALEM)
#  19. Intel Westmere (MWESTMERE)
#  20. Intel Silvermont (MSILVERMONT)
#  21. Intel Goldmont (MGOLDMONT)
#  22. Intel Goldmont Plus (MGOLDMONTPLUS)
#  23. Intel Sandy Bridge (MSANDYBRIDGE)
#  24. Intel Ivy Bridge (MIVYBRIDGE)
#  25. Intel Haswell (MHASWELL)
#  26. Intel Broadwell (MBROADWELL)
#  27. Intel Skylake (MSKYLAKE)
#  28. Intel Skylake X (MSKYLAKEX)
#  29. Intel Cannon Lake (MCANNONLAKE)
#  30. Intel Ice Lake (MICELAKE)
#  31. Intel Cascade Lake (MCASCADELAKE)
#  32. Intel Cooper Lake (MCOOPERLAKE)
#  33. Intel Tiger Lake (MTIGERLAKE)
#  34. Intel Sapphire Rapids (MSAPPHIRERAPIDS)
#  35. Intel Rocket Lake (MROCKETLAKE)
#  36. Intel Alder Lake (MALDERLAKE)
#  37. Intel Raptor Lake (MRAPTORLAKE)
#  38. Intel Meteor Lake (MMETEORLAKE)
#  39. Intel Emerald Rapids (MEMERALDRAPIDS)
#  40. Generic-x86-64 (GENERIC_CPU)
#  41. Generic-x86-64-v2 (GENERIC_CPU2)
#  42. Generic-x86-64-v3 (GENERIC_CPU3)
#  43. Generic-x86-64-v4 (GENERIC_CPU4)
#  44. Intel-Native optimizations autodetected by the compiler (MNATIVE_INTEL)
#  45. AMD-Native optimizations autodetected by the compiler (MNATIVE_AMD)
: "${_subarch:=""}"

# Enable compiling with LLVM
# Be warned: This is untested
: "${_use_llvm_lto:=""}"

# Enable/Disable debug options
# Set 'y' to enable, 'n' to force disable debug options if already enabled in your
# .config file or leave empty to ignore debug options.
: "${_debug:=""}"

### BUILD OPTIONS END

_kerneloptimized_version=ko2
_kernel_major=6.8
_kernel_minor=1
_gcc_more_v='20240221.2'
_srcname_linux=linux-${_kernel_major}
_srcname_clpatches=${_kernel_major}.1-1418
pkgbase=linux-jstm-optimized
pkgver=linux${_kernel_major}.${_kernel_minor}+clear${_srcname_clpatches//-/_}+${_kerneloptimized_version}
pkgrel=1
pkgdesc="Optimized Linux kernel. Includes Clear Linux's kernel config and some tweaks made by JeremyStarTM."
arch=('x86_64')
url="https://git.staropensource.de/JeremyStarTM/kernel-optimized"
license=(GPL-2.0-only)
makedepends=('bc' 'cpio' 'git' 'libelf' 'pahole' 'xmlto')
if [ -n "$_use_llvm_lto" ]; then
  makedepends+=(clang llvm lld python)
fi
options=(!strip !debug)
if [ "${_debug}" == "y" ]; then
  options=(!strip)
fi
source=(
  "https://cdn.kernel.org/pub/linux/kernel/v6.x/linux-${_kernel_major}.tar.xz"
  "https://cdn.kernel.org/pub/linux/kernel/v6.x/linux-${_kernel_major}.tar.sign"
  "https://cdn.kernel.org/pub/linux/kernel/v6.x/patch-${_kernel_major}.${_kernel_minor}.xz"
  "cl-linux::git+https://github.com/clearlinux-pkgs/linux.git#tag=${_srcname_clpatches}"
  "more-uarches-$_gcc_more_v.tar.gz::https://github.com/graysky2/kernel_compiler_patch/archive/$_gcc_more_v.tar.gz"
)

if [ -n "$_use_llvm_lto" ]; then
  BUILD_FLAGS=(
    LLVM=1
    LLVM_IAS=1
  )
fi

export KBUILD_BUILD_HOST=archlinux
export KBUILD_BUILD_USER=$pkgbase
export KBUILD_BUILD_TIMESTAMP="$(date -Ru${SOURCE_DATE_EPOCH:+d @$SOURCE_DATE_EPOCH})"

prepare() {
    echo ":: prepare: prepare() called"
    echo ":: prepare: cding into \"${_srcname_linux}\""
    cd "${_srcname_linux}" || exit 1
    
    ### Add upstream patches
    echo ":: prepare: adding upstream patches"
    patch -Np1 -i ../patch-${_kernel_major}.${_kernel_minor} || true

    ### Setting version
    echo ":: prepare: updating version"
    echo "-$pkgrel" > localversion.10-pkgrel
    echo "${pkgbase#linux}" > localversion.20-pkgname

    ### Add Clear Linux patches
    echo ":: prepare: patching kernel with Clear Linux patches"
    # shellcheck disable=SC2013
    for i in $(grep '^Patch' "${srcdir}"/cl-linux/linux.spec|grep -Ev '^Patch0132|^Patch0109|^Patch0118|^Patch0113|^Patch0138|^Patch0139|^Patch0147' | sed -n 's/.*: //p'); do
        if [ -n "$_use_llvm_lto" ]; then
            if [ "${i}" == "0162-extra-optmization-flags.patch" ] ; then
                continue
            fi
        fi
        echo ":: prepare: applying patch ${i}..."
        patch -Np1 -i "$srcdir/cl-linux/${i}" || true
    done

    ### Copy config
    echo ":: prepare: copying kernel-optimized kconfig into kernel repository"
    cp -Tf "${startdir}/kconfig" ./.config

    if [ -n "$_use_llvm_lto" ]; then
        echo ":: prepare: enabling LLVM support"
        scripts/config -d LTO_NONE \
                       -e LTO \
                       -e LTO_CLANG \
                       -e ARCH_SUPPORTS_LTO_CLANG \
                       -e ARCH_SUPPORTS_LTO_CLANG_THIN \
                       -e HAS_LTO_CLANG \
                       -e LTO_CLANG_THIN \
                       -e HAVE_GCC_PLUGINS
    fi

    if [ "$_debug" == "y" ]; then
        echo ":: prepare: enabling debugging features"
        scripts/config -e DEBUG_INFO \
                       -e DEBUG_INFO_BTF \
                       -e DEBUG_INFO_DWARF4 \
                       -e PAHOLE_HAS_SPLIT_BTF \
                       -e DEBUG_INFO_BTF_MODULES
    elif [ "$_debug" == "n" ]; then
        echo ":: prepare: disabling debugging features"
        scripts/config -d DEBUG_INFO \
                       -d DEBUG_INFO_BTF \
                       -d DEBUG_INFO_DWARF4 \
                       -d PAHOLE_HAS_SPLIT_BTF \
                       -d DEBUG_INFO_BTF_MODULES
    fi

    echo ":: prepare: running olddefconfig"
    make ${BUILD_FLAGS[*]} olddefconfig
    diff -u $srcdir/cl-linux/config .config || :

    # https://github.com/graysky2/kernel_compiler_patch
    # make sure to apply after olddefconfig to allow the next section
    echo ":: prepare: patching kernel with kernel_compiler_patch"
    patch -Np1 -i "$srcdir/kernel_compiler_patch-$_gcc_more_v/more-uarches-for-kernel-6.8-rc4+.patch"

    if [ -n "$_subarch" ]; then
        # user wants a subarch so apply choice defined above interactively via 'yes'
        echo ":: prepare: selecting sub architecture (automatic)"
        yes "$_subarch" | make ${BUILD_FLAGS[*]} oldconfig
    else
        # no subarch defined so allow user to pick one
        echo ":: prepare: selecting sub architecture (intervention required)"
        make ${BUILD_FLAGS[*]} oldconfig
    fi

    ### Optionally load needed modules for the make localmodconfig
    # See https://aur.archlinux.org/packages/modprobed-db
    if [ -n "$_localmodcfg" ]; then
        if [ -e "$HOME/.config/modprobed.db" ]; then
            echo ":: prepare: executing localmodconfig"
            make ${BUILD_FLAGS[*]} LSMOD=$HOME/.config/modprobed.db localmodconfig
        else
            echo ":: prepare: error: no modprobed.db found at \"${HOME}/.config/modprobed.db\", skipping"
        fi
    fi

    echo ":: prepare: writing kernel version"
    make -s kernelrelease > version

    [[ -z "$_makexconfig" ]] || (
        echo ":: prepare: executing xconfig"
        make ${BUILD_FLAGS[*]} xconfig
    )
    [[ -z "$_makenconfig" ]] || (
        echo ":: prepare: executing nconfig"
        make ${BUILD_FLAGS[*]} nconfig
    )

    ### Save configuration for later reuse
    [[ -z "$_copyfinalconfig" ]] || (
        echo ":: prepare: copying configuration file into kernel-optimized repository"
        cp -Tf ./.config "${startdir}/kconfig-final"
    )
    echo ":: prepare: prepare() finished"
}

build() {
    echo ":: build: build() called"
    echo ":: build: cding into \"${_srcname_linux}\""
    cd "${_srcname_linux}" || exit 1
    echo ":: build: starting compilation (make ${BUILD_FLAGS[*]} all)"
    make ${BUILD_FLAGS[*]} all
    echo ":: build: build() finished"
}

_package() {
    echo ":: _package: _package() called"
    echo ":: _package: updating package information"
    pkgdesc="${pkgdesc} This package includes the kernel and compiled modules."
    depends=('coreutils' 'kmod' 'initramfs')
    optdepends=('wireless-regdb: to set the correct wireless channels of your country'
                'linux-firmware: firmware images needed for some devices'
                'modprobed-db: Keeps track of EVERY kernel module that has ever been probed - useful for those of us who make localmodconfig')
    provides=(VIRTUALBOX-GUEST-MODULES WIREGUARD-MODULE KSMBD-MODULE)
    install=linux.install

    echo ":: _package: cding into \"${_srcname_linux}\""
    cd "$_srcname_linux" || exit 1

    local modulesdir="$pkgdir/usr/lib/modules/$(<version)"

    echo ":: _package: installing boot image"
    # systemd expects to find the kernel here to allow hibernation
    # https://github.com/systemd/systemd/commit/edda44605f06a41fb86b7ab8128dcf99161d2344
    install -Dm644 "$(make -s image_name)" "$modulesdir/vmlinuz"

    # Used by mkinitcpio to name the kernel
    echo "$pkgbase" | install -Dm644 /dev/stdin "$modulesdir/pkgbase"

    echo ":: _package: installing modules"
    ZSTD_CLEVEL=19 make ${BUILD_FLAGS[*]} INSTALL_MOD_PATH="$pkgdir/usr" INSTALL_MOD_STRIP=1 \
        DEPMOD=/doesnt/exist modules_install  # Suppress depmod

    # remove build link
    rm "$modulesdir"/build
    
    echo ":: _package: _package() finished"
}

_package-headers() {
    echo ":: _package-headers: _package-headers() called"
    echo ":: _package-headers: updating package information"
    pkgdesc="${pkgdesc} This package includes various headers and scripts for building modules."
    depends=(pahole)

    echo ":: _package-headers: cding into \"${_srcname_linux}\""
    cd "${_srcname_linux}" || exit 1
    local builddir="$pkgdir/usr/lib/modules/$(<version)/build"

    echo ":: _package-headers: installing build files"
    install -Dt "$builddir" -m644 .config Makefile Module.symvers System.map \
        localversion.* version vmlinux
    install -Dt "$builddir/kernel" -m644 kernel/Makefile
    install -Dt "$builddir/arch/x86" -m644 arch/x86/Makefile
    cp -t "$builddir" -a scripts

    # required when STACK_VALIDATION is enabled
    install -Dt "$builddir/tools/objtool" tools/objtool/objtool

    # required when DEBUG_INFO_BTF_MODULES is enabled
    if [ -f tools/bpf/resolve_btfids/resolve_btfids ]; then
        install -Dt "$builddir/tools/bpf/resolve_btfids" tools/bpf/resolve_btfids/resolve_btfids
    fi

    echo ":: _package-headers: installing headers"
    cp -t "$builddir" -a include
    cp -t "$builddir/arch/x86" -a arch/x86/include
    install -Dt "$builddir/arch/x86/kernel" -m644 arch/x86/kernel/asm-offsets.s

    install -Dt "$builddir/drivers/md" -m644 drivers/md/*.h
    install -Dt "$builddir/net/mac80211" -m644 net/mac80211/*.h

    # https://bugs.archlinux.org/task/13146
    install -Dt "$builddir/drivers/media/i2c" -m644 drivers/media/i2c/msp3400-driver.h

    # https://bugs.archlinux.org/task/20402
    install -Dt "$builddir/drivers/media/usb/dvb-usb" -m644 drivers/media/usb/dvb-usb/*.h
    install -Dt "$builddir/drivers/media/dvb-frontends" -m644 drivers/media/dvb-frontends/*.h
    install -Dt "$builddir/drivers/media/tuners" -m644 drivers/media/tuners/*.h

    # https://bugs.archlinux.org/task/71392
    install -Dt "$builddir/drivers/iio/common/hid-sensors" -m644 drivers/iio/common/hid-sensors/*.h

    echo ":: _package-headers: installing KConfig files"
    find . -name 'Kconfig*' -exec install -Dm644 {} "$builddir/{}" \;

    echo ":: _package-headers: removing unneeded architectures"
    local arch
    for arch in "$builddir"/arch/*/; do
        [[ $arch = */x86/ ]] && continue
        echo "Removing $(basename "$arch")"
        rm -r "$arch"
    done

    echo ":: _package-headers: removing documentation"
    rm -r "$builddir/Documentation"

    echo ":: _package-headers: removing broken symlinks"
    find -L "$builddir" -type l -printf 'Removing %P\n' -delete

    echo ":: _package-headers: removing loose objects"
    find "$builddir" -type f -name '*.o' -printf 'Removing %P\n' -delete

    echo ":: _package-headers: stripping build tools"
    local file
    while read -rd '' file; do
        case "$(file -Sib "$file")" in
            application/x-sharedlib\;*)      # Libraries (.so)
                strip -v $STRIP_SHARED "$file" ;;
            application/x-archive\;*)        # Libraries (.a)
                strip -v $STRIP_STATIC "$file" ;;
            application/x-executable\;*)     # Binaries
                strip -v $STRIP_BINARIES "$file" ;;
            application/x-pie-executable\;*) # Relocatable binaries
                strip -v $STRIP_SHARED "$file" ;;
        esac
    done < <(find "$builddir" -type f -perm -u+x ! -name vmlinux -print0)

    echo ":: _package-headers: stripping vmlinux"
    strip -v $STRIP_STATIC "$builddir/vmlinux"

    echo ":: _package-headers: adding symlink"
    mkdir -p "$pkgdir/usr/src"
    ln -sr "$builddir" "$pkgdir/usr/src/$pkgbase"
    
    echo ":: _package-headers: _package-headers() finished"
}

pkgname=("$pkgbase" "$pkgbase-headers")
for _p in "${pkgname[@]}"; do
  eval "package_$_p() {
    $(declare -f "_package${_p#$pkgbase}")
    _package${_p#$pkgbase}
  }"
done

sha256sums=('c969dea4e8bb6be991bbf7c010ba0e0a5643a3a8d8fb0a2aaa053406f1e965f3'
            'SKIP'
            'dfdfbf3045be48db3b99038a72eb2e6906a31dc11672230b4f45597de2e8cf11'
            'SKIP'
            '1d3ac3e581cbc5108f882fcdc75d74f7f069654c71bad65febe5ba15a7a3a14f')

validpgpkeys=(
  'ABAF11C65A2970B130ABE3C479BE3E4300411886'  # Linus Torvalds
  '647F28654894E3BD457199BE38DBBDC86092693E'  # Greg Kroah-Hartman
  '83BC8889351B5DEBBB68416EB8AC08600F108CDF'  # Jan Alexander Steffens (heftig)
)