# Co-Maintainer: Achill Gilgenast <achill@achill.org>
maintainer="Aelin <aelin@postmarketos.org>"
pkgname=linux-next
pkgver=7.2_git20260717
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="
a9129ef9101b507e18a05b3aa3de6be685a558c6bb14cf88a04edcc8014787f6d0f5c2719511aa102bcc19de3529e481f997a7b33bf42c0d52c116a20e2157af  linux-next-next-20260717.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
5cb39be14d369b0ff0877616c72ee3739f7481fe0fc90c2cb7a0fe0d54d636e5f23ae5d91b8661ef3d2e9cd3d714b7a1617ac3e17e3876af1bcd3a7a5a780bde  config-next.aarch64
110e5c69415ef6dbc5717acb493dbff3f4c7928e743fa2389012b580b70d66a4f1ce11680628535e3069b356faf7c8a532e8190889e9063087c4a9b9cd579479  config-next.armv7
f1b118d1cee2925d0ec3adbedf4b20052979459c6225f6b78706f88eb4462e864b049786405d965e36c522978dc2466487d33ed2c15f104b957c826c49ce8442  config-next.loongarch64
ba807c61115f77237b1680d8b30a8ad2b4319e23fd47b26d19b7ce249e6e983b83a4fc53c0d54f700fc81ad897a23a4dd50ff8e507f2e20a81dbdc0d340783c4  config-next.ppc64le
a5a212586fba86a407b563468a511dd001db001d0e94f80f5756b8b581768373fb9fa5afad938831737b5bebcff04f6412716d2dc6bd7c8eedb45ac02acf24ea  config-next.riscv64
10904eb31bdc778579c8e963374b0d9a8cb82f8c0ed930b3c05ec5635400ad64a97a22c0c0c145b391b4b099a0ca26d77c7719f052989024e9bd77f235dd3319  config-next.x86
4f630add3afdfd87a7ef5a8a048a09be514c1d5e6bbebb1d0f8c79334af4ce4dc2b0ba910cb868fd5de45afaa6e1f9507fe8487d9bc772b939f7b134ac1f71cf  config-next.x86_64
"
