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

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="
0ea26f5c06f1c5ceba6e2c468129e0195eb314c56abeedc7064bef0596f3ca96da4e220312a8a8b20cde210aceec304f47fb8927c54238dbdb5d387aefb0a9d8  patch-6.18.38.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
78ae9bc73f8cb7088ba14c7f068bc34ad4e58d09ac10b7ab81947014417516d0bf80993e7c79671046606564336e09cdad3afc394c5c724e507a8a572bff283d  config-lts.aarch64
9d880ce99f0517940e9f343da72d17605c3fff4ae5cab2f8cd8949c3458c529b8d2a7a6159ee8c5e04450a53a4d0c454a22a97863389942da7ea7953b9cdea39  config-lts.armv7
807de497b259fdb555dae56f1727f30d333c15c89c8adce1d5cd01cde2bd06a8423d10f9937c4cad13fad4cea9ccbf61716109d08680e80c090d48ae9178e25c  config-lts.loongarch64
e534389df5bb52ed08cddd4343a4b53ad8eb9ac5244783f63d6e82a69289a83abc5ce1a4b27a0c2fdddcd28314d37f6b72f353c020df1e64413fd4b4effc2374  config-lts.ppc64le
ddc183fc9ed7b5b3460998e92872565fee6f942ef7b74ded608fd96a6a7fcb4d98157a7fcd91f8d440b4c8c5acf369a5b1f5d677afb31988b924df04bb7635d0  config-lts.riscv64
b6634a7da30033f8d2d22aca1ce624d14d84b8438a246a3d809a8e8df8d245ae093ee2cf1149725db1a05b51b8c8741d5a66197d5fa7250cd490ba828e04a0e4  config-lts.x86
b376f9466336d5cf7773ff5d9746cb5633e00c7380f1e84b0a4632e426371c44b62c96957ed72b8f3d7986bc9e0075707178bdeb20cfa85b4042afb82bcba9ef  config-lts.x86_64
"
