# Co-Maintainer: Achill Gilgenast <achill@achill.org>
maintainer="Aelin <aelin@postmarketos.org>"
pkgname=linux-next
pkgver=7.2_git20260721
pkgrel=0
_nextver="${pkgver#*_git}"
pkgdesc="Linux next 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="next"
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-lockdown
	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
"

# 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="
	https://git.kernel.org/pub/scm/linux/kernel/git/next/linux-next.git/snapshot/linux-next-next-$_nextver.tar.gz

	0001-WIP-arch-powerpc-boot-Support-LLVM-1-in-wrapper-scri.patch
	0002-WIP-arch-powerpc-boot-Add-z-norelro.patch

	config-next.aarch64
	config-next.armv7
	config-next.loongarch64
	config-next.ppc64le
	config-next.riscv64
	config-next.x86
	config-next.x86_64
"
builddir="$srcdir/linux-next-next-$_nextver"

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="
f5d05d0e432719d8db69d885534a2071460a0dc167b7ea3621fc2b2006e988bcfc064c156aa351225d8f13db00f1146746207b21e5b118f969378e9473672915  linux-next-next-20260721.tar.gz
1f11df2480e9f691573283597160b4c806e492cdca4825f3acb3afb70bde43abfab487bcde1f5553b431a702a0039479d041b0ce7859970e706b5a51d2ad8fb0  0001-WIP-arch-powerpc-boot-Support-LLVM-1-in-wrapper-scri.patch
302c20c85559fc9b3b2420ed25be1bfbb76c87f2a470e4141c32e9542cf1f5c6b3b5a7847231a3eaa0f6bc0e5942d0c1599e807175dcf5e63469704c3d6adde3  0002-WIP-arch-powerpc-boot-Add-z-norelro.patch
3adf408e7b6af748b162783492147e1cbc65707eeea2705f6f111513781a1a33dcaa2fdbf258963f1522e2689e5548e63f392a379f2b5e9dce614a41f0bbee70  config-next.aarch64
6b9f5af93b7da44d2d88f19f98c34f301e4e2ced8c1bdb0851c7f1c85da6fad860a5ae71dcd6d2fee011a8d42dcdbc806ece22702fe222c03053541834a65269  config-next.armv7
5e9f7a0a3e5308ac86a3b4eab79861529c7e071838b2ea3a9b05ab7955a6c70f743158c6fdda5372e22944ef56fb6f896b3c943ccd92903ac4e4232f6687b329  config-next.loongarch64
1294acc73216278afbcabcf6647201e56cdfc3267316c7077d988de6793c0e4cacf68ed36ea5dbcc84d1a248facece89e57d0b43de0147d7e0daf7d8d1222268  config-next.ppc64le
468687d008e22f4a7765980f52e2b88bb9ba4e5f86f448c0a4c35218fac02dab2d556083b495690f51c34077a46c22eb3d02f0b2d5c966095bdaef10036b7c3c  config-next.riscv64
6fb6674aa22328cd1b48ea1d928b0ab0a85d6710054a08dd88ce876807f98bc6614a23cfce66640f41954053fb1a8bd51a56359a415ce5f6cfeb49b39ea40333  config-next.x86
a2769b27e1420cedd5e8f516ca9696c771df40f5238a29a3fc69d4fb156b973b15e8405b3913fd568f15ca8cbc4d592445e7e8f9ea3312827e9e2a26cc9b978e  config-next.x86_64
"
