# Maintainer: Martijn Braam <martijn@brixit.nl>

# This is seperate from linux-edge so we can control the release cycle better.
# If this kernel is updated and fails to boot it's quite hard to recover the
# rk3399 devices due to the boot order. This kernel also has a kconfig that has
# more modules built-in that are required for successfully booting on these
# devices.

# It's important that CONFIG_ROCKCHIP_CDN_DP is _disabled_ because it causes
# boot failure

pkgname=linux-postmarketos-rockchip
pkgver=5.18
pkgrel=2
pkgdesc="Mainline kernel for rockchip devices"
arch="aarch64 armv7"
_flavor="${pkgname#linux-}"
url="https://kernel.org"
license="GPL-2.0-only"
options="!strip !check !tracedeps
	pmb:cross-native
	pmb:kconfigcheck-anbox
	pmb:kconfigcheck-nftables
	pmb:kconfigcheck-containers
	pmb:kconfigcheck-zram
	"
makedepends="
	bison
	devicepkg-dev
	findutils
	flex
	installkernel
	openssl-dev
	perl
	rsync
	gzip
	xz
	gmp-dev
	mpc1-dev
	mpfr-dev
	"

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

# Source
_config="config-$_flavor.$CARCH"
case $pkgver in
	*.*.*)	_kernver=${pkgver%.0};;
	*.*)	_kernver=$pkgver;;
esac

source="
	https://cdn.kernel.org/pub/linux/kernel/v${_kernver%%.*}.x/linux-$_kernver.tar.xz
	0007-block-partitions-efi-Add-support-for-IGNOREME-GPT-si.patch

	config-$_flavor.aarch64
	config-$_flavor.armv7
"
builddir="$srcdir/linux-$_kernver"

prepare() {
	default_prepare
	REPLACE_GCCH=0 \
		. downstreamkernel_prepare
}

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

}

package() {
	downstreamkernel_package "$builddir" "$pkgdir" "$_carch" "$_flavor"

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

	# not all armv7 devices (google-veyon-jerry) support booting gzipped kernels yet
	if [ $_carch = arm64 ]; then
		gzip -v "$pkgdir"/boot/vmlinuz
		mv "$pkgdir"/boot/vmlinuz.gz "$pkgdir"/boot/vmlinuz
	else
		mv "$pkgdir"/boot/vmlinuz "$pkgdir"/boot/vmlinuz
	fi
}


sha512sums="
dbbc9d1395898a498fa4947fceda1781344fa5d360240f753810daa4fa88e519833e2186c4e582a8f1836e6413e9e85f6563c7770523b704e8702d67622f98b5  linux-5.18.tar.xz
265b05bc542edb290ec15cd5f50132698a32e80d30ce28ce3ad7e990b7c07df92c4556122505b07a6aa582a919459872d1d874f4ed422d2aff2cdfb51889e720  0007-block-partitions-efi-Add-support-for-IGNOREME-GPT-si.patch
9633e576fa04ed0ca38797082c15413016d273d6b601aa2f424001e7c33d2887090e27badb9d32ff6b3bf931ca0a994afda1b8971f14be76f64149b87db562de  config-postmarketos-rockchip.aarch64
4eb095196a5e8e8e42de6d3e362c99498d1c7601c7ce91ea532abe9288e2e71ef20549dec22825ff547129c7c615a6a837e39dc64e9260ccadaf5c6bcf52bf58  config-postmarketos-rockchip.armv7
"
