# Reference: <https://postmarketos.org/vendorkernel>
# Kernel config based on: arch/arm64/configs/lineageos_kenzo_defconfig

pkgname="linux-xiaomi-kenzo"
pkgver=3.10.105
pkgrel=0
pkgdesc="Xiaomi Redmi Note 3 kernel fork"
arch="aarch64"
_carch="arm64"
_flavor="xiaomi-kenzo"
url="https://kernel.org"
license="GPL-2.0-only"
options="!strip !check !tracedeps"
makedepends="perl sed installkernel bash gmp-dev bc linux-headers elfutils-dev devicepkg-dev gcc6"

# Compiler: GCC 6 (doesn't boot when compiled with newer versions)
if [ "${CC:0:5}" != "gcc6-" ]; then
	CC="gcc6-$CC"
	HOSTCC="gcc6-gcc"
	CROSS_COMPILE="gcc6-$CROSS_COMPILE"
fi

# Source
_repository="android_kernel_xiaomi_msm8956"
_commit="92430f1a110b9b49e69fb1d3bde81c0f048d0282"
_config="config-${_flavor}.${arch}"
source="
	$pkgname-$_commit.tar.gz::https://github.com/LineageOS/${_repository}/archive/${_commit}.tar.gz
	$_config
	01_fix_gcc6_errors.patch
	02_fix_msm_dba.patch
"
builddir="$srcdir/${_repository}-${_commit}"

prepare() {
	default_prepare
	downstreamkernel_prepare "$srcdir" "$builddir" "$_config" "$_carch" "$HOSTCC"
}

build() {
	unset LDFLAGS
	make ARCH="$_carch" CC="${CC:-gcc}" \
		KBUILD_BUILD_VERSION="$((pkgrel + 1 ))-postmarketOS"
	msg "Combine DTB and Image.gz"
        msg "(Because QCOM's custom Makefile builds into the wrong direcotry)"
        cat "$builddir/arch/$_carch/boot/Image.gz" \
                "$builddir/arch/arm/boot/dts/kenzo-msm8956-mtp.dtb" \
                > "$builddir/arch/$_carch/boot/Image.gz-dtb"
}

package() {
	# kernel.release
	install -D "$builddir/include/config/kernel.release" \
		"$pkgdir/usr/share/kernel/$_flavor/kernel.release"

	# zImage (find the right one)
	cd "$builddir/arch/$_carch/boot"
	_target="$pkgdir/boot/vmlinuz-$_flavor"
	for _zimg in zImage-dtb Image.gz-dtb *zImage Image; do
		[ -e "$_zimg" ] || continue
		msg "zImage found: $_zimg"
		install -Dm644 "$_zimg" "$_target"
		break
	done
	if ! [ -e "$_target" ]; then
		error "Could not find zImage in $PWD!"
		return 1
	fi
}

sha512sums="c2d57468544d9b96e7f55d8358419fecdbbb75ae02e08fb3e008a882750855966d989a79e058888bcdd5542f322fd26ac217cd73c316768183b496f108089b43  linux-xiaomi-kenzo-92430f1a110b9b49e69fb1d3bde81c0f048d0282.tar.gz
9f2737e98e9dd34097bc3e21aab2acd540627e352b431c82c1b1887e3e0c62bd2307d2031c83c7a3b0f8c118c8525dc989c17141410d8645fccdae6da0d53d01  config-xiaomi-kenzo.aarch64
972e98523d6ef06342e69a09e979bdd86904cf7f9a843b519b37791e531b680182564a2ab537c94629c3589f4e775b2c39de04c64e210a0f8b6f10745c3596f1  01_fix_gcc6_errors.patch
7e765cb696fa8c3dfbc305b05cbf1fad85fb9572613d5391e945341dae5974a745f4906c795dc841e5562b19ab77d5257f9741e514af2c2573aa4bb8b262ce51  02_fix_msm_dba.patch"
