# Co-Maintainer: Achill Gilgenast <achill@achill.org>
maintainer="Aelin <aelin@postmarketos.org>"
pkgname=linux-postmarketos-mainline
pkgver=7.1_rc6
pkgrel=0
_rel=${pkgver#*_p}
_kver=${pkgver%_p*}
_kver=${_kver//_/-}
pkgdesc="Mainline 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="mainline"
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
"

# 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/
#
# Patch #4 fixes 32-bit platform builds and is taken from here:
# https://lore.kernel.org/all/20260517-hid-core-fix-size_t-specifier-v1-1-bfdd959ec383@kernel.org/
source="
	$pkgname-$pkgver.tar.gz::https://git.kernel.org/torvalds/t/linux-${pkgver/_/-}.tar.gz

	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-mainline.aarch64
	config-mainline.armv7
	config-mainline.loongarch64
	config-mainline.ppc64le
	config-mainline.riscv64
	config-mainline.x86
	config-mainline.x86_64
"
builddir="$srcdir/linux-${pkgver/_/-}"
_defconfig="defconfig"

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="
a97365ae9d00e86cc93086f1f12a6bed6d38a80c1011131667571b833a008441b0492326315c734514519f58c9176d14ddce0cd9ada417fac77be0ffb090c0ec  linux-postmarketos-mainline-7.1_rc6.tar.gz
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
993dd434acdbe9a9ab90dac94c793cc45de4d15bf05e568c911317badb5b4ba03ef43a3da4029a3366e8dde5eaacf2a9a95bc2016a4a2684ec2be45a0281b24c  config-mainline.aarch64
ad9f35caa6014bc55649a63e5bfd24bacce9677f503c7ddf09188203ecc8680da301a1cb1057a384e749474087098acdfe1282c4af86959dbf2037f122c3874d  config-mainline.armv7
c92b8248c38d9978937cd785f5d1dbefd1c7218f02930525f033e25528d9a7a1f9c6e63357dc35c64daf46394e8e3a5e2e6d67e578164ebf4e278b79ce5c8480  config-mainline.loongarch64
60f46b91ef6d495fbc7d31c1d5eb9e536279b6f95fb9edf3153ed515d7b45bded160e4b28d8f4c75f043f6efaa4dc324a43ab2859e05c3ce36e61ffcc37efada  config-mainline.ppc64le
8813a42c1c5769e2a9e0482d03588e55ca0bf2be4000e472813b9144971017996222989ec79a6697d3b70504da8257579f592d7c3b57f41bac72ebac7c1488a5  config-mainline.riscv64
3640777c86d2eb7b9892718f102f953373d1d8f91348c4f6bfc49f4394970cfa1668f2aed9819280d4ae5cf3c4881a80df78848dcace3e93659fcb43159ca693  config-mainline.x86
d3a04f74a3305b38c02732edcc960dcbc2d39a046cf4dc53ab7f53b23942135b9834b88a3a144a55993e4a2756d9f11fcba67d64b307f05e48ef0fc16e84e40c  config-mainline.x86_64
"
