# Reference: <https://postmarketos.org/vendorkernel>
# Kernel config based on: arch/arm64/configs/defconfig (+ pmos.config, devices.config)
maintainer="snlavaise <snlavaise@gmail.com>"
_flavor="postmarketos-qcom-sm8550"
pkgname=linux-$_flavor
pkgver=6.13_rc3
pkgrel=1
pkgdesc="Mainline kernel fork for Qualcomm SM8550 (Snapdragon 8 Gen 2) devices"
arch="aarch64"
_carch="arm64"
url="https://gitlab.postmarketos.org/soc/qcom-sm8550/linux"
license="GPL-2.0-only"
options="
	!check
	!strip
	!tracedeps
	pmb:cross-native
	pmb:kconfigcheck-community
	pmb:kconfigcheck-uefi
"
makedepends="
	bash
	bison
	clang
	elfutils-dev
	findutils
	flex
	linux-headers
	lld
	llvm
	openssl-dev
	pahole
	perl
	postmarketos-installkernel
	python3
	zstd
"

# Source: snapshot of the postmarketOS SM8550 bring-up tree (branch: bringup).
# Bump _commit to move forward; then run 'pmbootstrap checksum linux-postmarketos-qcom-sm8550'.
_commit="22591bbc21da5cc0c6e37503b77bd2d852fe241c"
source="
	$pkgname-$_commit.tar.gz::https://gitlab.postmarketos.org/soc/qcom-sm8550/linux/-/archive/$_commit/linux-$_commit.tar.gz
	sm8550-motorola-rtwo.dts
	rtwo.config
"
builddir="$srcdir/linux-$_commit"

prepare() {
	default_prepare

	# The Motorola rtwo (Moto Edge 40 Pro) device tree is not upstream yet, so
	# drop it into the tree and register it in the Makefile before configuring.
	local _dtsdir="$builddir/arch/$_carch/boot/dts/qcom"
	cp "$srcdir/sm8550-motorola-rtwo.dts" "$_dtsdir/sm8550-motorola-rtwo.dts"
	if ! grep -q "sm8550-motorola-rtwo.dtb" "$_dtsdir/Makefile"; then
		sed -i '/sm8550-mtp\.dtb/a dtb-$(CONFIG_ARCH_QCOM) += sm8550-motorola-rtwo.dtb' \
			"$_dtsdir/Makefile"
	fi

	# Generate .config from the in-tree upstream defconfig plus the postmarketOS
	# and device config fragments shipped by the kernel repo, then merge the
	# rtwo bring-up fragment on top.
	# To "freeze" this config: build once, copy "$builddir"/.config to
	# config-$_flavor.$CARCH, add it to source= and switch prepare() to a plain
	# 'cp "$srcdir/config-$_flavor.$CARCH" .config' (like the other qcom kernels).
	cp "$srcdir/rtwo.config" arch/$_carch/configs/rtwo.config
	make ARCH="$_carch" LLVM=1 HOSTLDFLAGS="-fuse-ld=lld" \
		defconfig pmos.config devices.config rtwo.config
}

build() {
	unset LDFLAGS
	make ARCH="$_carch" LLVM=1 HOSTLDFLAGS="-fuse-ld=lld" \
		KBUILD_BUILD_VERSION="$((pkgrel + 1))-$_flavor"
}

package() {
	mkdir -p "$pkgdir"/boot

	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"
		# gzip'd image for Android boot.img compatibility
		install -Dm644 "$builddir/arch/$_carch/boot/vmlinuz" \
			"$pkgdir/boot/vmlinuz"
	else
		echo "WARNING: CONFIG_EFI_ZBOOT not enabled!"
		install -Dm644 "$builddir/arch/$_carch/boot/Image.gz" \
			"$pkgdir/boot/vmlinuz"
	fi

	make modules_install dtbs_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
}

# The kernel tarball checksum is filled in by
# 'pmbootstrap checksum linux-postmarketos-qcom-sm8550' (it downloads ~250 MB).
sha512sums="
19f6d0ee8481e34b0b8c27f07f3023182cda9e2a3d67e40989b1f300b7e82d7ea606fb112599dd9399e0d5f973d43b289d8932a4b578b6ffe4168af05e19f795  linux-postmarketos-qcom-sm8550-22591bbc21da5cc0c6e37503b77bd2d852fe241c.tar.gz
02d8dc574f20027a4aace5238fa879bb2d305551f8583f2b5b37d5388ab9ba6c7337f6f2bb4e23ddb3d852a3195fb4cddfc9791e7c6a33e60bee552b028d101c  sm8550-motorola-rtwo.dts
eae6e9b61bfedc72d4b6c0fdcb1260a18b439d3407d35dc44ec43b526601ad21fc4cb8d6077b3b5723348e1bb002b968afdb770581ad2578abd554f0fc4d6070  rtwo.config
"
