# Co-Maintainer: Achill Gilgenast <achill@achill.org>
maintainer="Aelin <aelin@postmarketos.org>"
pkgname=linux-postmarketos-lts
pkgver=6.18.37
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="
60a224dc9592b99a9f9bc37ed36c972c3bb8394c89aed7c75e14a05df3beb46e15d305603bfed9018db9b23e3b53eb17138342ad987a80519c792948fd410562  patch-6.18.37.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
523695cdf09de5f75bee48dfdb0cdb896cf38eba6b59853ba56731c5ca7b71a27090c962032e02b3e1dfe8965a076a692014ad126d58c255c5901f72b265fe9f  config-lts.aarch64
2d7d13c989516971a6d447fd4eb0b921138666056c04f423e34cbe8965736c68953bcddb934f58237c20d80a8bb9e8513b07d7bac53cdd0dbc60a0721133476e  config-lts.armv7
4cf932350672b3364c939bbf495b73c25177930bda2cf6ab6d2d82c05d7b04da89ecef9a130665927651930fdd30794b2ac3b25562a0f73b2b26809da67d1d7e  config-lts.loongarch64
03d9bae4168bd5250db73437185fbad5e4a96ec8b4cdccb35869da9f664820d00262e3b2d9c61e260db0f0eced7f1f9aa169b4ab00bd96aec83b2a7dec384943  config-lts.ppc64le
39e75c3ecf83e4210b14470ba160ad87b60e26f80f33a6f2a9b0c7bb7c2ab36895711dc029e312e57ee92f2715e68bd37fd9ceeca301e0782be966b6b62c8eab  config-lts.riscv64
65646969e16f82506dcf55aa010c872977ca4b584b4e48d741e4affbc59a9e3aae66ba3b212f0ce9daec09d37539cf595c835bcad6f8a4af82f1c48755862441  config-lts.x86
9427aa22c9b3c3aa9910cede8c84f5467cc7c6334d9dc7a658a449c03ee7f13a641672fbb331016855e1ddc3672bbe43e48a4f1398a22564fe8e55970ba8a15a  config-lts.x86_64
"
