# Co-Maintainer: Achill Gilgenast <achill@achill.org>
maintainer="Aelin <aelin@postmarketos.org>"
pkgname=linux-postmarketos-lts
pkgver=6.18.36
pkgrel=0
_kver=${pkgver%.*}
pkgdesc="LTS 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="lts"
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
"
replaces="linux-lts"

# 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 is a backport that makes ftrace work when built with Clang, which is
# required for passing kconfigcheck.
source="
	linux-$_kver.tar.xz::https://cdn.kernel.org/pub/linux/kernel/v${pkgver%%.*}.x/linux-$_kver.tar.xz

	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-powerpc64-ftrace-fixes-clang-build-issues.patch

	config-lts.aarch64
	config-lts.armv7
	config-lts.loongarch64
	config-lts.ppc64le
	config-lts.riscv64
	config-lts.x86
	config-lts.x86_64
"
builddir="$srcdir/linux-$_kver"
_defconfig="defconfig"

if [ "${pkgver%.0}" = "$pkgver" ]; then
	# Prepend to apply first
	source="patch-$pkgver.patch.xz::https://cdn.kernel.org/pub/linux/kernel/v${pkgver%%.*}.x/patch-$pkgver.xz $source"
fi

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="
525ddfabea97a6a2b9d86175573c3a7d180d4944a732905bcfeb5d8af26426519784c53740a8927e3af11f1740d85a754e879809aa9cb61f98d1cb1cf2f6285a  patch-6.18.36.patch.xz
88599ffdec96d150c1feb9b261ba93bb0301a9d0e1ad6bef7aeab1f5372cbfc57d8b43c7e902bd8f76921d1dbd8189663c142ea869e51d0e2b483b150ee00fe0  linux-6.18.tar.xz
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
166f9151618780afb269822d2b0c7580830fc47b20aa1ea08006a119a5234277fba5dbc2d0be469f3285bf8debd2dcfd078c2f92b206d1491620d94432c7b11b  0004-powerpc64-ftrace-fixes-clang-build-issues.patch
c5bcbb3d023b6276f2186b2cfa703a3226c410fbce74e31a40bee9ddc7946098eaa9991472a26a055ce2fe036018e4ed99c450b917c13fd08ce1338785e90c87  config-lts.aarch64
1ea5e475c5a902825239fe65ca88a7c9130b9af6823f4f29371a4f71b514eb0f92e08ec2cc72ca3feb7d71d85c758a8a559c8b6e4673103c4a5e1f6ee5b741cd  config-lts.armv7
834643dbce02bd404d351d049d895dfa44b6fd9b028ac92e32866de0a052a20a20e94591a68273e939b8fbc62e01e8bdbaa38360b5bc4dfef2646984b354aad5  config-lts.loongarch64
2003c872fa2213464fe6e36f43bfd9908eab6e5e69d78ca0be522438ed14c45a1ec367f13f5b54d4983f95bbafa914ef8eba0518c107248e2dcb3e4733de6ea7  config-lts.ppc64le
10421658d8b3ccdc6ae38439932e0ff61ce207c0b0e4996b4a9e77ac2115b3b4186bfbf722ec1933d889de2b64f84dac1f4ac34a68525d5d0ba9ac8d394a2510  config-lts.riscv64
1fde92b5188cc4a6ae3cfd707c4d8e45132fb488bb6e5414bd2cad80e016b540a6af4c10cb27fc47e98bc65892ab0bb4bef58b537332b5b47204665e0f59c7a1  config-lts.x86
f9c29b21ec1037b8b471e637a02e82223066f224fd6acb099bbe3fa07f1242fdb60eb1613cc6642d1ad54a7dedccb432624ae3c7fefb484bede2ac919cfba4d1  config-lts.x86_64
"
