# Co-Maintainer: Achill Gilgenast <achill@achill.org>
maintainer="Aelin <aelin@postmarketos.org>"
pkgname=linux-postmarketos-mainline
pkgver=6.19_rc8
pkgrel=0
_rel=${pkgver#*_p}
_kver=${pkgver%_p*}
_kver=${_kver//_/-}
pkgdesc="Mainline 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="mainline"
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
"

# Source
source="
	$pkgname-$pkgver.tar.gz::https://git.kernel.org/torvalds/t/linux-${pkgver/_/-}.tar.gz

	config-mainline.aarch64
	config-mainline.armv7
	config-mainline.loongarch64
	config-mainline.ppc64le
	config-mainline.riscv64
	config-mainline.x86
	config-mainline.x86_64
"
builddir="$srcdir/linux-${pkgver/_/-}"
_defconfig="defconfig"

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="
f714bcaabb13cda21f5e126b7d3d245d3660a03792e81a7cbf36fe13a3d5a7d9e92aa8285a498e5bde4030f406ed0e65137a2d389c9d0c52c25f0b3edca684bf  linux-postmarketos-mainline-6.19_rc8.tar.gz
bf17cfe133c4b4462cd77c87b6bf40545aa0451830cf68d9cfe2535da8016a4de1c5f0b17cdcd84d0032d26d80db28d07cf3949b9e32d0e7320af10595dedb16  config-mainline.aarch64
f56993b3e8761d212bd9a644b0b5025a8c9944cd9b6b2108ce9237d90e63476e95b2e780e9356d5fceea90abe3c1bbea6f0110eef80c2b4f8001b10dec59b1e8  config-mainline.armv7
a3f152e3430caf525e6e44842f2e4a7fbdae3165ccca0659c4297bba4c4b3823e5eacf56d784d9788a0379a270b29b042fa86430419e5a088fa7bb0f7d8569f0  config-mainline.loongarch64
2c187be95ec40388bb93e1dd3ef7b2d3879acc01f5a1a78e4d45988d1679db958dc059c4c6d785e0a56e9ac28155bae4f4c5e0677812fdc986d6e680fffd1873  config-mainline.ppc64le
baf4698ee9a7d3121003c5e37f737b5e78cc6f48a9cc329f70d5420a60d68460df407fc70991bdf39be019c3920830e0e33af4330f9b47d99a1322e40740ee19  config-mainline.riscv64
8d48e80e75826b97fbc0823b42438af7543260dcc2e97f9c434148f14194280f1392e8d6b18d20fed8310ddc24490d58484f65022e5c5fdfbd4d5736828cf570  config-mainline.x86
dfb6d476f170801c632ce5619086a34be889d45d032cde1b7d36e29ec9810055b5978b224d89cbbbad4e91545b1c8c83db832c6378394bb2b9279bd9ec1959c5  config-mainline.x86_64
"
