# Co-Maintainer: Achill Gilgenast <achill@achill.org>
maintainer="Aelin <aelin@postmarketos.org>"
pkgname=linux-postmarketos-stable
pkgver=6.18.8
pkgrel=1
_kver=${pkgver%.*}
pkgdesc="Stable Kernel"
# armhf: pmbootstrap cannot dynamically choose a defconfig, so it ends up using multi_v7_defconfig which isn't correct
# s390x: kconfigcheck is not ready for it yet
arch="all !armhf !s390x"
_flavor="stable"
url="https://kernel.org"
license="GPL-2.0-only"
options="
	!check
	!strip
	!tracedeps
	pmb:cross-native
	pmb:generic-kernel
	pmb:kconfigcheck-community
	pmb:kconfigcheck-immutable
	pmb:kconfigcheck-rust
	pmb:kconfigcheck-uefi
	pmb:kconfigcheck-virt
"
makedepends="
	bison
	clang
	clang-libclang
	diffutils
	elfutils-dev
	findutils
	flex
	linux-headers
	lld
	llvm
	openssl-dev
	pahole
	perl
	postmarketos-installkernel
	python3
	rust
	rust-bindgen
	rust-src
	rustfmt
	sed
	zstd
"
replaces="linux-stable"

# Source
source="
	linux-$_kver.tar.xz::https://cdn.kernel.org/pub/linux/kernel/v${pkgver%%.*}.x/linux-$_kver.tar.xz

	config-stable.aarch64
	config-stable.armv7
	config-stable.loongarch64
	config-stable.ppc64le
	config-stable.riscv64
	config-stable.x86
	config-stable.x86_64
"
builddir="$srcdir/linux-$_kver"
_defconfig="defconfig"

if [ "${pkgver%.0}" = "$pkgver" ]; then
	# Prepend to apply first
	source="patch-$pkgver.patch.xz::https://cdn.kernel.org/pub/linux/kernel/v${pkgver%%.*}.x/patch-$pkgver.xz $source"
fi

case "$CARCH" in
	aarch64*) _carch="arm64" ;;
	arm*) _carch="arm" ;;
	loongarch*) _carch="loongarch" ;;
	ppc*) _carch="powerpc" ;;
	riscv*) _carch="riscv" ;;
	s390x) _carch="s390" ;;
	x86) _carch="i386" ;;
	x86_64) _carch="x86_64" ;;
esac

prepare() {
	default_prepare
	cp "$srcdir/config-$_flavor.$CARCH" .config
}

build() {
	unset LDFLAGS
	make ARCH="$_carch" LLVM=1 \
		KBUILD_BUILD_VERSION="$((pkgrel + 1 ))-$_flavor"
}

package() {
	local _install
	case "$CARCH" in
		arm*|aarch64|riscv*) _install="zinstall dtbs_install" ;;
		*) _install="install" ;;
	esac

	make modules_install $_install \
		ARCH="$_carch" \
		LLVM=1 \
		INSTALL_PATH="$pkgdir"/boot/ \
		INSTALL_MOD_PATH="$pkgdir" \
		INSTALL_MOD_STRIP=1 \
		INSTALL_DTBS_PATH="$pkgdir"/boot/dtbs

	rm -f "$pkgdir"/lib/modules/*/build "$pkgdir"/lib/modules/*/source

	install -D "$builddir"/include/config/kernel.release \
		"$pkgdir"/usr/share/kernel/$_flavor/kernel.release
}

sha512sums="
7c75c98dcae76acfae4d5b566509392ff91906ca813260f55b0b6425ff2267080289d2d1d19be656db161e0718a3073d9dc53288b779158c006ac84cac8f9b57  patch-6.18.8.patch.xz
88599ffdec96d150c1feb9b261ba93bb0301a9d0e1ad6bef7aeab1f5372cbfc57d8b43c7e902bd8f76921d1dbd8189663c142ea869e51d0e2b483b150ee00fe0  linux-6.18.tar.xz
61c90ff284e8a2c62d5a46dd64e23ce2942c9ce6ad0606ed985ffd105edbceba941d9d9bdf0f5b1454d6ace47bae44ac0caf654cbc820874a011d067746bd8cc  config-stable.aarch64
e476217fc6949cf90d8106c97fd16de0ce14bfccb9247b6caf6fcf89d9bb2e1d276f4fc4cbd5f73a09dcd0d0cfeb10f4effd0071916389c94152895e68d197c1  config-stable.armv7
3edf5d7f2046a1651e404b3837042bad2bc96ae3a79abebe78d68b183bf7f4ced3b0ef54803b4cc74123c3103b383b62257450b28cdd21261b1a49ff21d75075  config-stable.loongarch64
fbf9945149c3b3b271b9270d708a8cffe93fcec3fc9b2aad8818c5b8f1592395056ca1a217ce6057000f2f9ac4809e7846d26844681c71afb7a813920698a335  config-stable.ppc64le
7a5bc4b62e3569b10a62f276cd0bba7fe3818dae82c84cffaeab074f590d2532ef1009e2ae8051c2e077127bf851adc9c1cd31c71c1f9c603f00fd936b99fa86  config-stable.riscv64
62232d9437fced904ce12bde8fe3e35a0ddfc611264deb39bbc18bb98cc734278e27dfe79f7d5791d5c7a2a563a72fb156d52cd75480f247d450835df9528750  config-stable.x86
cfe7e323f2ed1c2f69afa87d23f66aee9c95a34eaa4c0419efc07d9b0c47b1267eea02a2591660d63aee15673c4cb78e84e006068be59f270b2aad87dd3315a6  config-stable.x86_64
"
