# Maintainer: Casey Connolly <kcxt@postmarketos.org>
# Co-Maintainer: Joel Selvaraj <joelselvaraj.oss@gmail.com>
# Stable Linux kernel with patches for SDM845 devices
# Kernel config based on: arch/arm64/configs/defconfig and sdm845.config

_flavor="postmarketos-qcom-sdm845"
pkgname=linux-$_flavor
pkgver=6.14.0_rc5
pkgrel=2
pkgdesc="Mainline Kernel fork for SDM845 devices"
arch="aarch64"
_carch="arm64"
url="https://gitlab.com/sdm845-mainline/linux"
license="GPL-2.0-only"
options="!check !tracedeps
	pmb:cross-native
	pmb:kconfigcheck-community"
makedepends="bash bison findutils flex installkernel openssl-dev perl zstd python3"

_config="config-$_flavor.$arch"
_tag="sdm845-6.14-rc5-r2"

# Source
source="
	linux-$_tag.tar.gz::https://gitlab.com/sdm845-mainline/linux/-/archive/$_tag/linux-$_tag.tar.gz
	$_config
	0001-enable-judy-uart.patch
"
builddir="$srcdir/linux-$_tag"

prepare() {
	default_prepare
	cp "$srcdir/config-$_flavor.$arch" .config
}

build() {
	unset LDFLAGS
	make ARCH="$_carch" CC="${CC:-gcc}" \
		KBUILD_BUILD_VERSION="$((pkgrel + 1 ))-$_flavor"
}

package() {
	if [ -e "$builddir/arch/$_carch/boot/vmlinuz.efi" ]; then
		# ZBOOT EFI decompressor for EFI booting
		install -Dm644 "$builddir/arch/$_carch/boot/vmlinuz.efi" \
			"$pkgdir/boot/linux.efi"

		# Old GZIP'd kernel image for boot.img compatibility
		install -Dm644 "$builddir/arch/$_carch/boot/vmlinuz" \
			"$pkgdir/boot/vmlinuz"
	else
		echo "WARNING: CONFIG_ZBOOT not enabled!"
		install -Dm644 "$builddir/arch/$_carch/boot/Image.gz" \
			"$pkgdir/boot/vmlinuz"
	fi

	make modules_install dtbs_install \
		ARCH="$_carch" \
		INSTALL_PATH="$pkgdir"/boot/ \
		INSTALL_MOD_PATH="$pkgdir" \
		INSTALL_MOD_STRIP=1 \
		INSTALL_DTBS_PATH="$pkgdir"/boot/dtbs-tmp

	mkdir -p "$pkgdir"/boot/dtbs/qcom
	cp -r "$pkgdir"/boot/dtbs-tmp/qcom/sdm8* "$pkgdir"/boot/dtbs/qcom
	rm -rf "$pkgdir"/boot/dtbs-tmp

	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="
e68ab4fbb912882bffb1630ebb6d90bed58ec89645e60b395672b94850bbeb8cfa98b4bf646624729ac462777544b6682554864e0fc24f9c994ea39c2feb524b  linux-sdm845-6.14-rc5-r2.tar.gz
56d17ed221eb624ea58643a239f1225594f44126a1c6af0e6ba9b57c74c4f10e7d9089d7b7fd1a3eef375a15b3263b28e821587cee25911593c05f5bacf5caeb  config-postmarketos-qcom-sdm845.aarch64
1c353a76129639b9df0c66d77bcda71fbea31995166054d97b9347b890809f92e2ef0ab52a04cb2008e9945790c1b8d1f4cc6a935741be3e6790c43dcea99a81  0001-enable-judy-uart.patch
"
