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

	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" \
		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="
b242e9ea9c744a61529abb2447d3eb8de31167272b669e132666447a619b24730c4a9fcdb6d7a4132b5aa83cc9ee47f85d7d4d72e25aef383f8929cc7107acd6  patch-6.18.18.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
6b11c769104b173d5f91b77396808ee0adea11458dd67c63fb280d31ff903fe0c1885d8dbfcd82baf25204013b151375392cfdd02058bf98189dcd1f12e519f0  config-lts.aarch64
2d4f878f902e09cad92e839b44138fc49083dae626f4adcad7f80e6ee58e19bef0171258a61df9130da6155bdaeb3e75b1bef88766c50b59e052b5b7f0c4e526  config-lts.armv7
678f24eecaeee883b83d4dde3412543928e06528cb696d90a226ed03b5a79f9a352074ed08a0c0fd57a70cf6d9532576f5aaac904f032e8395f3e4d206d073f2  config-lts.loongarch64
146c8a065ed7042bec29fd678b3d7eb8869266b0eb56701b1ed3e2e698f9490b57b1cab82b22c9b92249336556909e11a18da0d94f65f4458d08960f545260a0  config-lts.ppc64le
20b66884baac96242ea3aa1de02242f3b04f5ddffb1b07c0edeea5051f795c87556f844cedc16a5435ff4d2e19663cc4b8b1d8535b6512742cc08b504c39a227  config-lts.riscv64
616cb2c8b46907d166de29ab1bf0eea342e240b9d853a7d1ca52fbdce4424148c934c5051b126d592a0f4f5310803ade2b2470bfa94a5076eb0428ae183e3562  config-lts.x86
e5d32487f40a66a057e5199490297521610a961e4f1ed75c30faff7a5296198a7cc7fbe5ac90d17479e9aba474c5d3d35ccfb4990849981418c7b9ec56c25740  config-lts.x86_64
"
