# Reference: <https://postmarketos.org/vendorkernel>
# Kernel config based on: arch/arm64/configs/pinetab2_defconfig
# Co-Maintainer: Stefan Hansson <newbyte@postmarketos.org>
maintainer="Dang Huynh <dang.huynh@mainlining.org>"
pkgname=linux-pine64-pinetab2
pkgver=7.1.3
pkgrel=0
_danctnix_kernel_rel=1
pkgdesc="Pine64 PineTab 2 mainline"
arch="aarch64"
_carch="arm64"
_flavor="pine64-pinetab2"
url="https://kernel.org"
license="GPL-2.0-only"
options="!strip !check !tracedeps
	pmb:cross-native"
depends="kmod"
makedepends="
	bison
	clang
	elfutils-dev
	findutils
	flex
	linux-headers
	lld
	llvm
	pahole
	postmarketos-installkernel
	openssl-dev
	perl
	python3
	zstd
"

# Source
_tags="v$pkgver-danctnix$_danctnix_kernel_rel"
_config="config-$_flavor.$arch"
source="
	linux-$pkgver.tar.xz::https://cdn.kernel.org/pub/linux/kernel/v${pkgver%%.*}.x/linux-$pkgver.tar.xz
	$_tags.patch.zst::https://codeberg.org/DanctNIX/linux-pinetab2/releases/download/$_tags/$_tags.patch.zst
	$_config
	libbpf-Fix-UAF-in-strset__add_str.patch
"
builddir="$srcdir/linux-$pkgver"
_outdir="out"

prepare() {
	# Decrunch the patch file and apply it to the tree
	zstd -d "$(readlink $srcdir/$_tags.patch.zst)" -o "$srcdir/$_tags.patch"
	patch -p1 -i "$srcdir/$_tags.patch"

	default_prepare

	cp "$srcdir/config-$_flavor.$CARCH" .config
}

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

package() {
	make dtbs_install modules_install zinstall \
		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
}

sha512sums="
1c5e7e16c73f4942dcdc1783987e65351259d1f2d4ff39c91182529e2f9e689b62b3df3ec89b61514c473eb13ecb004a3825716b9c583fc41e25fc01fe569b30  linux-7.1.3.tar.xz
fdc3de811e281ea448088ca3d8ff879fe6955e9ee7c9b1afdcf8ad0637b56ea3e7e6183d3443e8c25968805320cd41a50f7a4bc82b50a4c6e6f13343075e6d81  v7.1.3-danctnix1.patch.zst
94c5355e00e97ccc4f0ef52541bbd51dc64615ea6c1379dc07066d4524ca57c3a15956d1f8d66870cfb453489446e287e0cf7438b873bced4974637f3deca289  config-pine64-pinetab2.aarch64
e2555390d48cbbdc32a7d3f0c27599ff88f2fe3e786d1d0086caaa5b092cc3edcb6e95e2fb07034edb3793f6dc954da0662c723202eb7e2c4956c99b50919b12  libbpf-Fix-UAF-in-strset__add_str.patch
"
