# Kernel config based on: arch/arm64/configs/carbon_huawei_cmr_defconfig

pkgname="linux-huawei-cameron"
pkgver=4.4.23
pkgrel=0
pkgdesc="Huawei Mediapad M5 Pro kernel fork"
arch="aarch64"
_carch="arm64"
_flavor="huawei-cameron"
url="https://kernel.org"
license="GPL2"
options="!strip !check !tracedeps"
makedepends="perl sed installkernel bash gmp-dev bc linux-headers elfutils-dev devicepkg-dev python dtc"
HOSTCC="${CC:-gcc}"
HOSTCC="${HOSTCC#${CROSS_COMPILE}}"

# Source
_repository="android_kernel_huawei_hi3660"
_commit="18cd8140ad09379365bcd9cb50e45662e8747834"
_config="config-${_flavor}.${arch}"
source="
	$pkgname-$_commit.tar.gz::https://github.com/JohnBergago/${_repository}/archive/${_commit}.tar.gz
	$_config
	01_use_linux_alpine_crosscompile.patch
	03_add_compression_methods_to_Kconfig.patch
	04_make_rdr_hisi_adapter_compileable.patch
	05-disable-huawei-bfmr.patch
"
builddir="$srcdir/${_repository}-${_commit}"
outdir="$srcdir/../out"

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

build() {
	unset LDFLAGS
	mkdir "$outdir"
	cp "$srcdir/$_config" "$outdir/.config"
	rm "$builddir/.config"
	make ARCH="$_carch" HOSTCC="$HOSTCC" O="$outdir" oldconfig
	make ARCH="$_carch" CC="${CC:-gcc}" \
		KBUILD_BUILD_VERSION="$((pkgrel + 1 ))-postmarketOS" O="$outdir"
}

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

	# zImage (find the right one)
	cd "$outdir/arch/$_carch/boot"
	_target="$pkgdir/boot/vmlinuz-$_flavor"
	for _zimg in Image.gz 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="1da7c7666f1bf8e3fd3931331b1ebf0ab9520fce47d44749c30abcef55856deaced0ffc74c025b323cc83eb36eb7cf9c514bc65eedbead5d40efd3e16ddc0486  linux-huawei-cameron-18cd8140ad09379365bcd9cb50e45662e8747834.tar.gz
643a78af869217af518f7857d09c5415cb59fd78260ff145eafd55d302467f448f9914029322222db84a0c6e05df420aa2af686ef8b1bff2e8108623d294806e  config-huawei-cameron.aarch64
5473a038ca5703cf4119957ca3af66972c121aa66fe262a3165e5a0e5714037670a96600eedd0488150ff754ead93f43299587554ca6c05022bb55f780d1cd7a  01_use_linux_alpine_crosscompile.patch
86afc1337a34524c49e9beaad6506f7038a7413340abc473f8d209a4d3cd6ebbcf8315ae41902427edf97064742c02e2e5bb705a8bcd36d1cae19f76a509e235  03_add_compression_methods_to_Kconfig.patch
cdc60b6c829df3b014226b9cb506ed71cf005cf5fec8d4339b54a23f052465501aaa4983ec00f33ef301db2e6a70b24a30439b3f6ce8f7c00348c05d3132ec3f  04_make_rdr_hisi_adapter_compileable.patch
6e420722576111a7ba49f265a66de632631c13d1bdb67553916d2156149226526daedc8298d83a38115c0a7189497cdb92b0daa0a263a92e6093f575e855a6e5  05-disable-huawei-bfmr.patch"
