# Kernel config based on defconfig

pkgname=linux-postmarketos-exynos7870
pkgver=6.2.1
pkgrel=0
pkgdesc="Close-to-mainline kernel for Samsung Galaxy A6 2018"
arch="aarch64"
_carch="arm64"
_flavor="exynos7870"
url="https://kernel.org"
license="GPL-2.0-only"
options="!strip !check !tracedeps pmb:cross-native"
makedepends="
	bash
	bc
	bison
	dtbtool-exynos
	flex
	openssl-dev
	perl
"

_patches_repo="https://gitlab.com/sleirsgoevy/a6lte-mainline"
_patches_repo_commit="58a5a02739abf77adf62902c141d0383f7de7172"
_config="config-$_flavor.aarch64"
# Source
source="
	linux-$pkgver.tar.gz::https://cdn.kernel.org/pub/linux/kernel/v6.x/linux-$pkgver.tar.gz
	$_config
	patches-$_patches_repo_commit.tar.gz::$_patches_repo/-/archive/master/a6lte-mainline-$_patches_repo_commit.tar.gz
"
builddir="$srcdir/linux-$pkgver"
_outdir="."

prepare() {
	default_prepare
	for i in "$srcdir"/a6lte-mainline-*/patches/*; do
		patch -p1 < "$i"
	done
	cp "$srcdir"/$_config .config
}

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

	# Build out-of-tree devicetrees
	for i in "$srcdir/"a6lte-mainline-*/*.dts; do
		scripts/dtc/dtc "$i" -o "$_outdir/arch/$_carch/boot/dts/$(basename -s .dts "$i").dtb"
	done

	# Master DTB (deviceinfo_bootimg_qcdt)
	dtbTool-exynos -o "$_outdir/arch/$_carch/boot"/dt.img \
		$(find "$_outdir/arch/$_carch/boot/dts/" -name 'exynos7870-*.dtb')
}

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

	# The bootloader does not support zipped kernels
	make install modules_install \
		ARCH="$_carch" \
		INSTALL_PATH="$pkgdir"/boot \
		INSTALL_MOD_PATH="$pkgdir" \
		INSTALL_MOD_STRIP=1
	rm -f "$pkgdir"/lib/modules/*/build "$pkgdir"/lib/modules/*/source

	install -D include/config/kernel.release \
		"$pkgdir/usr/share/kernel/$_flavor/kernel.release"

	# Install dt.img
	install -Dm066 "$_outdir/arch/$_carch/boot/"dt.img "$pkgdir/boot/dt.img"

	# Rename /boot/vmlinux-... to make postmarketos-mkinitfs happy
	mv "$pkgdir/boot/vmlinux-$pkgver" "$pkgdir/boot/vmlinuz-$pkgver"
}

sha512sums="
d7e11f86b5daf09b9764fc710e3b49e2a2b5b4cd1c74ca3f009d396337f8c20cd3969d1c334bdf0e992acdd1d5c650474a11dddf473f3846e86c1a1649085ce4  linux-6.2.1.tar.gz
4407a2606acc3a35019a45ed1c371543765ee89c865b80ba65791eff191f1401fd49cc7cfade385ffbb19b9dc9b7fb7d78751e796653be3c15b779db99d336a5  config-exynos7870.aarch64
1819881ee8de1b2fc04dfb288bf131ad5998f6cf40d27ae523049a30b01a8edfd26df6970186b725a7ace03bf9bab28bbc26876b4932931259059d63df088de9  patches-58a5a02739abf77adf62902c141d0383f7de7172.tar.gz
"
