# Kernel config based on defconfig and exynos7870.config
pkgname=linux-postmarketos-exynos7870
pkgver=6.15
pkgrel=0
pkgdesc="Close-to-mainline kernel for Samsung Exynos 7870 devices"
arch="aarch64"
_carch="arm64"
_flavor="postmarketos-exynos7870"
url="https://gitlab.com/exynos7870-mainline/linux"
license="GPL-2.0-only"
options="!strip !check !tracedeps pmb:cross-native"
makedepends="
	bash
	bc
	bison
	flex
	openssl-dev
	perl
	postmarketos-installkernel
"

_owner="exynos7870-mainline"
_repo="mainline-patches"
_commit="f7853439cb79a711df615e3e7afa7f1d71c659a8"
_config="config-$_flavor.aarch64"
_kmajver="$(echo "$pkgver" | sed 's/\..*$//')"

# Source
source="
	$_config
	linux-$pkgver.tar.gz::https://cdn.kernel.org/pub/linux/kernel/v$_kmajver.x/linux-$pkgver.tar.gz
	patches-$_commit.tar.gz::https://gitlab.com/$_owner/$_repo/-/archive/$_commit/$_repo-$_commit.tar.gz
"
builddir="$srcdir/linux-$pkgver"

prepare() {
	default_prepare
	cp "$srcdir/$_config" .config

	# Apply the patches to the mainline kernel.
	for patchfile in "$srcdir/$_repo"-*/patches/*; do
		patch -p1 < "$patchfile"
	done

	makefile="$builddir/arch/arm64/boot/dts/exynos/Makefile"

	for dts in "$srcdir/$_repo"-*/devicetrees/*dts*; do
		dtb="$(basename "$dts" | sed 's/\.dts$/.dtb/')"

		# Move the devicetree sources in the kernel tree.
		cp "$dts" "arch/arm64/boot/dts/exynos/"

		# Mention the devicetree blobs in the Makefile for
		# compilation if they're absent.
		if ! grep -q "$dtb" "$makefile"; then
			echo "dtb-\$(CONFIG_ARCH_EXYNOS) += $dtb" >> "$makefile"
		fi
	done
}

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

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

	# The bootloader does not support zipped kernels
	make install modules_install dtbs_install \
		ARCH="$_carch" \
		INSTALL_PATH="$pkgdir/boot" \
		INSTALL_MOD_PATH="$pkgdir" \
		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"

	install -Dm644 "$builddir/arch/$_carch/boot/Image" \
		"$pkgdir/boot/vmlinuz"
}

sha512sums="
979b4bd800809113e57836fa42cb1d98937209a1ffa92fbf7d165345b8674eddafed3f0ba1fabb86ce16b7be0459d1b510c84d54113ad4575bc65c3ad6eda60a  config-postmarketos-exynos7870.aarch64
a12a16c08c8867397c69c8eeb5a1a9195253cbcce8d0a9cc731c24d027d79040a9d2b598bcc2c6fca0f4a1f019216e652d2fc1a00025b15c064b7165a61ee9c3  linux-6.15.tar.gz
2037823d632544bd53bbfd5c0da02a2714918a7c58bbb147c3b64b415a2f5c6ed66600856928b895a2b79918e2c5b681d8d94c02ff5c022330e2511f0fbf781d  patches-f7853439cb79a711df615e3e7afa7f1d71c659a8.tar.gz
"
