maintainer="Rudraksha Gupta <guptarud@gmail.com>"
pkgname=linux-pine64-pinephonepro
pkgver=7.0
_pkgver=7.0
_tagdate=20251104-2007
pkgrel=2
pkgdesc="Mainline kernel for the pinephone pro"
arch="aarch64"
_flavor="${pkgname#linux-}"
url="https://kernel.org"
license="GPL-2.0-only"
options="!strip !check !tracedeps
	pmb:cross-native
	pmb:kconfigcheck-community
	pmb:kconfigcheck-pinephonepro
	"
makedepends="
	bash
	bison
	clang
	findutils
	flex
	gmp-dev
	gzip
	linux-headers
	lld
	llvm
	mpc1-dev
	mpfr-dev
	openssl-dev
	perl
	postmarketos-installkernel
	rsync
	xz
	zstd
	"

case "$CARCH" in
	aarch64*) _carch="arm64" ;;
	arm*) _carch="arm" ;;
esac

# Currently, the megi kernel is the most up-to-date pinephone pro kernel. As postmarketOS doesn't want to use the Megi kernel directly, we can strip the Megi kernel down to it's essentials by doing the following:
#
#    git reset --hard v7.0
#    git merge megi/fusb302-7.0 megi/imx258-7.0 megi/rk818-7.0 megi/rt5640-7.0 megi/rk3399-typec-7.0 megi/typec-extcon-7.0 megi/tcpm-7.0 megi/arasan-7.0 megi/ppp-7.0
#    git cherry-pick ebc624f4    # `arm64: dts: rockchip: rk3399-s: Add DMC table `
#    git rebase v7.0
#
# This reduces the massive amount of patches that megi has, down to the ones that the pinephone pro needs and also bubbles them up to be on top of whatever tag we want.

# Source
_config="config-$_flavor.$CARCH"
source="
	https://codeberg.org/LogicalErzor/linux/archive/ppp-megi-7.0-stripped.tar.gz
	config-$_flavor.aarch64
	"
builddir="$srcdir/linux"

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

build() {
	unset LDFLAGS
	make ARCH="$_carch" LLVM=1 \
		KBUILD_BUILD_VERSION="$((pkgrel + 1 ))"

}

package() {
	mkdir -p "$pkgdir"/boot
	make zinstall modules_install dtbs_install \
		ARCH="$_carch" \
		LLVM=1 \
		INSTALL_MOD_PATH="$pkgdir"/usr \
		INSTALL_MOD_STRIP=1 \
		INSTALL_PATH="$pkgdir"/boot \
		INSTALL_DTBS_PATH="$pkgdir"/boot/dtbs-$_flavor
	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="
60b6b72b01b4c842569655e35a826dd1249e8584b670e102ac26eea8a76bd5b8b29cc87240e30b765273d459cc2114ad6f904750245ef3d2e5f5619ea2fa340c  ppp-megi-7.0-stripped.tar.gz
19661756049d5db085b453a3539c268ed3a6956e69cd19856f41385aa7491c19f0c11582f6168a64edfbe250746bab3f7ffa0b8f2891f9736c53136b7f272c15  config-pine64-pinephonepro.aarch64
"
