# Reference: <https://postmarketos.org/vendorkernel>
# Kernel config based on: arch/arm/configs/sp7731gea-dt_defconfig

pkgname="linux-zte-p731a20"
pkgver=3.10.17
pkgrel=0
pkgdesc="ZTE_P731A20 kernel"
arch="armv7"
_carch="arm"
_flavor="zte-p731a20"
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 dtbtool"

# Compiler: latest GCC from Alpine
HOSTCC="${CC:-gcc}"
HOSTCC="${HOSTCC#${CROSS_COMPILE}}"

# Source
_repository="android_kernel_zte_P731A20"
_commit="a1b2e761517f0e2bed0dd5e1a94053d7c648614d"
_config="config-${_flavor}.${arch}"
source="
	$pkgname-$_commit.tar.gz::https://github.com/blade-af3/${_repository}/archive/${_commit}.tar.gz
	$_config
	gcc7-give-up-on-ilog2-const-optimizations.patch
	gcc8-fix-put-user.patch
	00_return_address.patch
	fix_cross_compile.patch
	qcom,msm-id_fix.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" \
		CFLAGS_MODULE=-fno-pic

	# Generate master DTB (deviceinfo_bootimg_qcdt)
	dtbTool -s 2048 -p "scripts/dtc/" -o "arch/arm/boot/dt.img" "arch/arm/boot/"
}

package() {
	# kernel.release
	install -D "$builddir/include/config/kernel.release" \
		"$pkgdir/usr/share/kernel/$_flavor/kernel.release"
		
	install -Dm644 "$builddir/arch/$_carch/boot/zImage" \
		"$pkgdir/boot/vmlinuz-$_flavor"
	
	# Master DTB (deviceinfo_bootimg_qcdt)
	install -Dm644 "$builddir/arch/arm/boot/dt.img" \
		"$pkgdir/boot/dt.img"

echo "--[ Installing modules ]--"
	make ARCH="$_carch" CC="${CC:-gcc}" \
		KBUILD_BUILD_VERSION="$((pkgrel + 1))-postmarketOS" \
		INSTALL_MOD_PATH="$pkgdir" INSTALL_MOD_STRIP=1 modules_install
}

sha512sums="e29e9b242c235fe48278643a5ee4bb487c6a7cab09b23a16f796b945371da5505cdd3791b322837d29df4a401a91373f0a65c01794ad4576b2ab925487cdc64d  linux-zte-p731a20-a1b2e761517f0e2bed0dd5e1a94053d7c648614d.tar.gz
ed722be637b335cdb5b2a7a231a5253235c250cd904506808bc99a560db2b91f61b52104d4943164f2223afac5d50df23222ada16d4ed3affdd014e97ff555eb  config-zte-p731a20.armv7
77eba606a71eafb36c32e9c5fe5e77f5e4746caac292440d9fb720763d766074a964db1c12bc76fe583c5d1a5c864219c59941f5e53adad182dbc70bf2bc14a7  gcc7-give-up-on-ilog2-const-optimizations.patch
197d40a214ada87fcb2dfc0ae4911704b9a93354b75179cd6b4aadbb627a37ec262cf516921c84a8b1806809b70a7b440cdc8310a4a55fca5d2c0baa988e3967  gcc8-fix-put-user.patch
c40eaa11547f5bc2b1ff965506bdfe3015ff16e4b5ad9ccb3b8134ceafd1d32407c4ef2b213e02e036ce1e9bdcbfe768dff7d4b054bec77705a176c1f783b6f5  00_return_address.patch
2bedb0af8a23f5febc5f23a41c91174bffa5cb3b9af699fd61c86089a5bead77a08aca23daa62a3b7e93845e9cf86a1f5cc2a5fb4c4a56af09cf5befa2287d31  fix_cross_compile.patch
0b3ae537793ebd6d24dfca9ec81674fc14b2aece4592bf2700795382f059501285a3e673c02a7114e8239040d41e096b10b819173f14ef837346ed60b0f55574  qcom,msm-id_fix.patch"
