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

	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" \
		INSTALL_MOD_STRIP=1 \
		INSTALL_DTBS_PATH="$pkgdir"/boot/dtbs

	rm -f "$pkgdir"/lib/modules/*/build "$pkgdir"/lib/modules/*/source

	install -D "$builddir"/include/config/kernel.release \
		"$pkgdir"/usr/share/kernel/$_flavor/kernel.release
}

sha512sums="
bfa6147221756ef7078fe24b0c986a77b10c80559fd067e27339ec243749b11b38b9476662b26a45b7778fbc69028762e932e55c40faf4940ed4933fffc06db7  linux-postmarketos-mainline-7.1_rc1.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
57df2f6c37abaf4b621e074fe71b10a02c416e8e11c35c0a56563bb3cd65bac460fce2d05efc7ddac6517a12490d0a199b999b230215f045e9bbbddeb7271e6c  config-mainline.aarch64
e2eca24536ff3e93920f6ac0b82e08f773cb6203b0ae48bcf4eb05a07d5e73a770f45e44d1d8a3ea880145c20837137ead9cd53b81ebd65726a86ea460396acf  config-mainline.armv7
220d709d4ac2093964aa0bfa39ad2cc2e40814efc724a3464fbae4f667cff0feeb3599cf78354829685b0f86a7b597542e2d9acc294255c691d4b112b45f3e8c  config-mainline.loongarch64
a636746ecfe634bd103b5e80edd24a1c172ba82d6cd9e3e26559da4479f09c400b905139baac40c3890f21fa6e592baf7e6028d24b0162f624bc2a1b9a71e563  config-mainline.ppc64le
09033ac295cacb7a9585f6c66d99a891fa53190c780544dad1e0764a69ec4e74b11489dc86ac59d96cd845a55ccf7c0521803355080cd2392ba2146ce3fbf41c  config-mainline.riscv64
f58d453dd7e921ede3c4598576ba95cb16c628b8dbd62695e32bc753950241d0e5bc261c9d5627cb0843f3ebe16387818778a01d23f246f984d02f39f4d30b80  config-mainline.x86
1f5b11248c6c2201c8e411536d5e3587578dfe6dcba6a5a9598bf7242e4970c86b9b8bb1ae6428b525fc10f755a1c0bdbd23e106f6330c1aa4fe6c7ae30e6aa7  config-mainline.x86_64
"
