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

	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="
73b7b77f955000b3c4ccc05d311ac16052454fe6e4eff6bd4bba9ad1978eea24901b8247c0344017d370053f8fa0f9e7bd1d9a387452283a60febeda54b3d0f4  patch-7.0.12.patch.xz
8c85b708342f8f09ca25b2234b22e93b62b144a9ef919fbb21827f52fa47498e778c79db1559524dd7fb360f95b2ce174171443c1859656c281be41d2b6c5a0b  linux-7.0.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
f490011ac6d302f0f2da2e2e7aa93f90521b0aba09f18d694823840a3d7f694a350ec6fc4730ac081c6313371b3eb3fb0acff5b271008d6324a77d86662b0d1b  config-stable.aarch64
1ec74ffab7b15bdb35e10acf2bcd9e18e805f6ffa5694974beead88fc54204af8751ec98d2b043e505a4c54e046dba951d4d4cb097924d6361563b0ddca02758  config-stable.armv7
667713bca192671eacc65c47c24f9e24f04d8048604b5631cd5f8794556acf54ddaabdd642c25abdcec79efea080a9a132473b53c40d913ca1f68b25524b7014  config-stable.loongarch64
7242de9810747f0413fe54ed36b74f4307fc608092b03139b260bbcb5fb0ee9a77da0ddebb0da351e1ba2a48c98e9ffd9b9ae698936b7c02d28ffae67a5a145b  config-stable.ppc64le
cd19026f3da9054679f7cff8224a95530e88a6cc79afb575ed58ae5a97c6876dc3df758d186dc9cbf2c0fb28b8d89d8e6bf659401323d7bf7c6c0bad13412127  config-stable.riscv64
bbd425558d7c1dc56aeb23ad6640f0903c931655bc36aafea9123695454767ae5dbb2cf2e554df457f0b335aee56aca748a35a23d01c70479e6a68c517d8a382  config-stable.x86
5b0601d01a1eca4cdde5ab74d451f271ef312f33dd2bebe48ed9c5819c230d78d583db51a4a68c659ffcb214702ac8ede1b15eb1c14bf918a403276a065f46e7  config-stable.x86_64
"
