# Co-Maintainer: Achill Gilgenast <achill@achill.org>
maintainer="Aelin <aelin@postmarketos.org>"
pkgname=linux-postmarketos-stable
pkgver=7.0.10
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="
076c369ec654ff69b701a5f7123d4d4309c48781407a23012e629b7fe7c1dd16d7cf6cc0f069534902eef2e27d394516ed36cb103716bf0bdeca5a4345e4b805  patch-7.0.10.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
17497ec370ee5566610e3c298f8656fe92613597ad85e7cb5c2844d6231375278fe20a67d29d2b02a12a94eee23c4f1e9f3c60782f95a258415ce644fa24dcc6  config-stable.aarch64
7be282b25811a35a3c6e5ef62c082904322467b11bc6215fb08e19910096f264d39cdcec31ab493a8eda19e2ee2aa975794a8603e6c0bfc653b2e8bf465724a0  config-stable.armv7
ca4aa0f645face7de9ffa26393cfe13b64bc9aa80c903dba16e9aa5f3870f23e55c9ee0aef5171ebe3e9bdcd58f9a23246c2323800682b1a9e1ea2aaa5fb94a9  config-stable.loongarch64
279e4353c19757aebd04888048d5a1af543c85f7c445b3c3933d3e0dd8a6a364969bc24e8b23c35919c032ee6d762ad62f912f7f2d79d3cd7142bfcf1b47d3be  config-stable.ppc64le
2d2dd2b07f9d9b492b4759db828a0533d848aa121b8bb28d4f0bbb79cc4f81c3aff28f66006ed0d097c9c0225227d07eef1eb70822a84e5c21e5e0ede7b7a1ad  config-stable.riscv64
2ffc5a79804466ca3cc1151fceef5e52b373d90fb11ea08e53a3b29f5e0a2498177653970c952503de9222e455d7f450491a133e7be08d0bf2d28e34ce0e740f  config-stable.x86
ecd9e4460557a81dbb91439f98e4ec0fb84c09a0f06543a9eb0460f91bb0817c287506d190a5598b8606ebcd6e16d74fc636185f94f7c133735b8bd924f14bec  config-stable.x86_64
"
