# Co-Maintainer: Achill Gilgenast <achill@achill.org>
maintainer="Aelin <aelin@postmarketos.org>"
pkgname=linux-postmarketos-stable
pkgver=7.1.3
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"

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="
0dc615d978425ae4a5cc57d61b81a6b648a30b3d7cde0a3efb1244660ef572bb2f3d2292642346194fcb549a22cde2d2b9af6fe237b22b8eefc1ca905f6e14cb  patch-7.1.3.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
1e7ce8a8182d4bbf62608a82e2a24732c4721a4542c2b0e3f171fd9dc190234c9601ec7c872512a09acd56d5e118eb6a99c7ba1b848b45df3aaec537ab21da0f  config-stable.aarch64
5452624a2926d83e1d8e9444d040a034889fd9a77c3af55572747a61ebeed9167b84f9d7519d5f7f2698a2760d1bddc6269d807638734e624b9d5a219e10f73b  config-stable.armv7
5b1b4931d815987f4172904bfeb8b4ad6422656ebad68037886e3521316c1b746bef7596ce78e4bc741b6a7fa440941e039f633267d9c22f242f2f0b57ebfd19  config-stable.loongarch64
79e2db3c81145cf65e7c76f737a9067f63f295f99591d9faac2c25fab8245d49ffdc062a5e4c1f505837b3615e21dfefb27f4bfce5a825a3efbd7a400c33f1dd  config-stable.ppc64le
9ef899be02514fe570ffa11300220453f39f7da36a9af34ed7d9bf6ba24a8782ce0f36cf59014d7f90a3885abee8b24955624a8e1ff87b717836346253759712  config-stable.riscv64
1168b580ecc28f3f46d36b7046efde92e1e808c0f2caf0056c0b757b7d16c7c0721b20ffa7b11e15191fe07fb30b0f6bc9e62c1763b032df130b035e86094138  config-stable.x86
48b8c89cd57dad20e08f1c15a9d1c490a538a9a1d74949dd374a7802e1596ec89e132c2358b13d9717be2966b764f86aa8c12e56b2007387db3e8025d82b1f8a  config-stable.x86_64
"
