# Co-Maintainer: Achill Gilgenast <achill@achill.org>
maintainer="Aelin <aelin@postmarketos.org>"
pkgname=linux-postmarketos-lts
pkgver=6.18.48
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-lockdown
	pmb:kconfigcheck-rust
	pmb:kconfigcheck-selinux
	pmb:kconfigcheck-uefi
	pmb:kconfigcheck-virt
"
makedepends="
	bash
	bison
	clang
	clang-libclang
	diffutils
	elfutils-dev
	findutils
	flex
	grep
	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 HOSTLDFLAGS="-fuse-ld=lld" \
		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 -Dm644 "$builddir"/include/config/kernel.release \
		"$pkgdir"/usr/share/kernel/"$_flavor"/kernel.release
}

sha512sums="
18b4d5a32a430f8599cf821e5f873e9d669e4994a21f0a98084f1780bc233a13250f69c162f440ec9ac62551b03532f99332ce9ef90b98b77e69b96c63a05a48  patch-6.18.48.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
0682c1089dfcbe020f773bddbc7f2f8f2b4c98dc50241eb683c5ecb252e03cb0d16195dfac3aef62586c5d467fc1325c6085ac667087a11d1f934caf7aa70289  config-lts.aarch64
a3c1ca2eaf4c6f6b7e464ed72a86cc5a62ab225cc9db4bbb2550fbda980d672f82944370a6987e9ce0b739ea8a21c21520f9bbf93d7c3b98012a640863e7ad86  config-lts.armv7
8f416ba4dade7f915143fa0387517ddf1b5ddb36e5360b07187d603632a0ed730841ac70dd922de0c03ed2c94d1e564815a02956e7940b061445bf609a8977be  config-lts.loongarch64
8e57b2016214d217f59e25f40c4b69e4f47bf085896e29fce92ff180c548ef67568bdb6c7e7d3793a557f83c3c70ffc14216c9c827386e8b675451b4636fc450  config-lts.ppc64le
a8b757b169796d2ace12baaa22af3a4cd9ad22143c2bcaa231db87ec916c4adc09cb052cefd59aa9873c21406a535ec1688d54724b001c79fd66332abed78d44  config-lts.riscv64
3f354430ea34f6459604139d90025614f6c38e6b5a721bbb9ceddbb16e7e67fc475da8dfb99cdd3dbedaff33db757c1145604c63aab92ca69cddd2241e63b086  config-lts.x86
d86cda3e3d04486dbea14c9ff74ac2dcab19180426f80a95c6a925d77182bb9608391e700f6e38c4549bb2bc3e80065f01794a46078dc9921f0202bbeca5aad8  config-lts.x86_64
"
