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

	0004-resolve_btfids-preserve-tag-and-parameter-names-when.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="
10c0cfd70a7a5731963cb4c7538396f6ed637d7db532a1c04b00628af47f4b81ffb3d92200e64ba1ce04047e4b2e39e6845c4067762e85ad9df13f2740c10397  linux-postmarketos-mainline-7.1.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
4e705e038a91466a86fc5317b0028c2220aa1e7a4807e5d86db2f9ac815a927c59666a8f21ed50c4632147d3ce183d8042378a76f95341dbcd3db1047faf39e3  0004-resolve_btfids-preserve-tag-and-parameter-names-when.patch
f460fde145405470e7490fb788bef42220aa0147bf14461398bd7830fb5194b903c284c608f07b5c9c0b6b070364f8af8b16dfb4be3f5437f2a0124c438dbf82  config-mainline.aarch64
c24951f5b8af210399f76b553dada2407bf4d132517a567b170301bd6753eb01194523f855e415b186c6a101bb14daa033a17b4e041cbaee3d2c5612e83fbf2f  config-mainline.armv7
0d2f2019a76b416f9b739fb7f041b0fc80cf20d70f9e84e21aacd0f8a7ad84c16985ef9af56a882c9e502754813b6387147c0625093e9faa0ddb9262e3e74cbf  config-mainline.loongarch64
306ea675fe79a72b4e00eaa6da8cf56aff06bb8398da139b8fd9aa2211fed2d58fc2ca7cef34ce8c252314f0218f54ad070d4561ff50a01fb0c88714d04017e8  config-mainline.ppc64le
e25fe9289d1bfd5bddf1719da3dcb1d0aa1ca86c314106723d9f4ac873d475fb7968532ca1d8a58a702b5215136cb9922732a5f886e94d68e342dc3fc5075da4  config-mainline.riscv64
4bc3d9eee700e58b200067c9b7c52e0a1e92aae0e08210275af63b556070e2384f311f62e15658197b1cd3d29fb3543737e20fba7077d8fb0aba881209ef30c2  config-mainline.x86
5e488b98ac2816d37eb1ad36d05016892b6b99d5b3c626d50619a7896bbc63fc7562fb0a0e62b488aa9fe1661d5938c7786e27df8b53e0f223e27129f2b6c09c  config-mainline.x86_64
"
