# Co-Maintainer: Achill Gilgenast <achill@achill.org>
maintainer="Aelin <aelin@postmarketos.org>"
pkgname=linux-postmarketos-stable
pkgver=7.1.1
pkgrel=0
_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-selinux
	pmb:kconfigcheck-uefi
	pmb:kconfigcheck-virt
"
makedepends="
	bash
	bison
	clang
	clang-libclang
	diffutils
	elfutils-dev
	findutils
	flex
	kmod
	linux-headers
	lld
	llvm
	openssl-dev
	pahole
	perl
	postmarketos-installkernel
	python3
	rust
	rust-bindgen
	rust-src
	rustfmt
	sed
	zstd
"
replaces="linux-stable"

# powerpc boot wrapper patches:
# Necessary for compilation without cross GCC in PATH, cherry-picked from
# Nathan's fork, he does not have time to upstream them right now but would be
# happy if someone else did:
# https://lore.kernel.org/linuxppc-dev/aXs8sgAcci9FKKpx@luna/
source="
	linux-$_kver.tar.xz::https://cdn.kernel.org/pub/linux/kernel/v${pkgver%%.*}.x/linux-$_kver.tar.xz

	0001-WIP-arch-powerpc-boot-Support-LLVM-1-in-wrapper-scri.patch
	0002-WIP-arch-powerpc-boot-Use-z-notext-for-pseries-image.patch
	0003-WIP-arch-powerpc-boot-Add-z-norelro.patch

	0004-resolve_btfids-preserve-tag-and-parameter-names-when.patch

	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"/usr \
		INSTALL_MOD_STRIP=1 \
		INSTALL_DTBS_PATH="$pkgdir"/boot/dtbs

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

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

sha512sums="
0be71a50fdbaa423f06c7c4f0364e0f49cfc4757d40d6605d6de4b1e6d66ff3f4949209274d6c4914a364798faf46b25478019e982c6cca131ee5bd3936e0363  patch-7.1.1.patch.xz
d4d9c45261813559341c67ea53d4a47f5a04d1ad15e3c2b8222a09b5483380d2bfd31b8c675e144db2eb91f560d6cb28f66187a5ea4f24f6dfb82a4ad48064cc  linux-7.1.tar.xz
1f11df2480e9f691573283597160b4c806e492cdca4825f3acb3afb70bde43abfab487bcde1f5553b431a702a0039479d041b0ce7859970e706b5a51d2ad8fb0  0001-WIP-arch-powerpc-boot-Support-LLVM-1-in-wrapper-scri.patch
26521a9c9ea769534095dc4e306babadc8bcbaf03d7faa523c377e41ba0f742ecd0490fa27791d895f729c3ae8845297dca59b4db2475e7f078e689c447430bc  0002-WIP-arch-powerpc-boot-Use-z-notext-for-pseries-image.patch
302c20c85559fc9b3b2420ed25be1bfbb76c87f2a470e4141c32e9542cf1f5c6b3b5a7847231a3eaa0f6bc0e5942d0c1599e807175dcf5e63469704c3d6adde3  0003-WIP-arch-powerpc-boot-Add-z-norelro.patch
4e705e038a91466a86fc5317b0028c2220aa1e7a4807e5d86db2f9ac815a927c59666a8f21ed50c4632147d3ce183d8042378a76f95341dbcd3db1047faf39e3  0004-resolve_btfids-preserve-tag-and-parameter-names-when.patch
79b4e8ae57466255b77a188ee30664948f8a6220e023dc73e48b91df4827f7bc823a31dc7fca2e15224bf46efacaf79a9bf5508eb273e07c50fc52fb9d9fc5f8  config-stable.aarch64
11abca864d534c2f30e111a7d5ddbf906f8e6e4ca7823ef8e34adfd8291d0a951f739719bc1ac12828c48c2c0573d4c0d57c20c2a19cb0f76d5616c55f81b27e  config-stable.armv7
f4c2cdde44f06a8e265b1407455aa1ae3fd5bb8a6969161e4b7d1b041840900f96809082474646564caf3ba8d823a0769e761d8d5d20591fb71eb4588053f018  config-stable.loongarch64
d0182dd24c90313f874f7e49178bbe068dfe7833ce607bbdeb828d698de4730a00eb056f59c16d67b5e1dbacff3ce37cf82c7fb30ee97126c72a5ea148a4e3ac  config-stable.ppc64le
446c15f65ba9490c9c08b1cb86e16889bf30dd9088139769fb87ea22914584220a0ccdbac265eb83bb69fa61ff0dd2843a9e4b5024fec49ac34769500f5a6a23  config-stable.riscv64
fa5b5b9e2eb514f79a5a768c4ccd62224279512ec6e824bc74da988c870576b8c65d7267904694f2bbe99365184642981e305da1b55b73efafcda27e1e19bd2d  config-stable.x86
bd2d32158f9354328b9f48d0fbf81748e98eee1a809cbbd7271dea041522213b94466245a4675f11baf8471e02690dc808f7ca9e42a5b80855e33e65292b244d  config-stable.x86_64
"
