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

pkgname="linux-xiaomi-mido"
pkgver=3.18.31
pkgrel=0
pkgdesc="Xiaomi Redmi Note 4 kernel fork"
arch="aarch64"
_carch="arm64"
_flavor="xiaomi-mido"
url="https://kernel.org"
license="GPL2"
options="!strip !check !tracedeps"
makedepends="perl sed installkernel bash gmp-dev bc linux-headers elfutils-dev"
HOSTCC="${CC:-gcc}"
HOSTCC="${HOSTCC#${CROSS_COMPILE}}"

# Source
_repository="android_kernel_xiaomi_msm8953"
_commit="3180dc01dc9577ff3655aaa8af2faa312e4e85f3"
_config="config-${_flavor}.${arch}"
source="
	$pkgname-$_commit.tar.gz::https://github.com/LineageOS/${_repository}/archive/${_commit}.tar.gz
	$_config
	compiler-gcc6.h
	fix_recordmcount.patch
	01_fix_gcc6_errors.patch
	02_fix_msm_dba.patch
	03_fix_rndis_ipa_trace_header_not_found.patch
	05_fix_u_f_header.patch
	05_fix_mdp3_ctrl_off.patch
	06_fix_usb_gadget_function.patch
"
builddir="$srcdir/${_repository}-${_commit}"

prepare() {
	default_prepare

	# gcc6 support
	cp -v "$srcdir/compiler-gcc6.h" "$builddir/include/linux/"

	# Remove -Werror from all makefiles
	local i
	local makefiles="$(find . -type f -name Makefile)
		$(find . -type f -name Kbuild)"
	for i in $makefiles; do
		sed -i 's/-Werror-/-W/g' "$i"
		sed -i 's/-Werror//g' "$i"
	done

	# Prepare kernel config ('yes ""' for kernels lacking olddefconfig)
	cp "$srcdir"/$_config "$builddir"/.config
	yes "" | make ARCH="$_carch" HOSTCC="$HOSTCC" oldconfig
}

build() {
	unset LDFLAGS
	make ARCH="$_carch" CC="${CC:-gcc}" \
		KBUILD_BUILD_VERSION="$((pkgrel + 1 ))-postmarketOS"
	msg "Combine DTB and Image.gz"
	msg "(Image.gz-dtb without DTB itself ¯\_(ツ)_/¯)"
	rm "$builddir/arch/$_carch/boot/Image.gz-dtb"
	cat "$builddir/arch/$_carch/boot/Image.gz" \
		"$builddir/arch/arm/boot/dts/qcom/msm8953-qrd-sku3-mido.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 Image.gz-dtb; 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="3d986bd4afc7e1f7544a0eaeb30414b358de1e4f8f484ce7f72e7b3f763a7c047d6feb374fda526a69200ae4ae23317330eea1be019c1f604c053a6c38d5c1c0  linux-xiaomi-mido-3180dc01dc9577ff3655aaa8af2faa312e4e85f3.tar.gz
bac83814af97a5506ba42251c52732e11f52b9a3b4d858ae7f984075dbcce40221ae03273be175a6bbac427197eaf1d40a992b5acc66bed8fe00dcbdbb0bf67f  config-xiaomi-mido.aarch64
d80980e9474c82ba0ef1a6903b434d8bd1b092c40367ba543e72d2c119301c8b2d05265740e4104ca1ac5d15f6c4aa49e8776cb44264a9a28dc551e0d1850dcc  compiler-gcc6.h
6aa11a75f422ac5c20cddfce23bff81940e61e65bc86fe1070c60714a6ccf631b2da70bff20e2b88e723706f0f233eb03540a8d9389adffd495592e8ab6bd82a  fix_recordmcount.patch
972e98523d6ef06342e69a09e979bdd86904cf7f9a843b519b37791e531b680182564a2ab537c94629c3589f4e775b2c39de04c64e210a0f8b6f10745c3596f1  01_fix_gcc6_errors.patch
7e765cb696fa8c3dfbc305b05cbf1fad85fb9572613d5391e945341dae5974a745f4906c795dc841e5562b19ab77d5257f9741e514af2c2573aa4bb8b262ce51  02_fix_msm_dba.patch
9c549714f03c7a7666e8aa7508a48cfdccab7cf3e97fb59c4df8fa97f75492365926f82c4a677c50780d68d4905504dcccbdd6de6344bd8d4b88b2331933ed46  03_fix_rndis_ipa_trace_header_not_found.patch
b408f157cc025d410b38f5aee9be977e670e1ac3f195f6c3a6681446fa0c3887067d62a4f729cfcd058fab42fca355b0265764dc2d21515f6e023ed025f55d41  05_fix_u_f_header.patch
fbc61e379403990167853f4c4fbdfa22d7e9661a7eedecf2a75eec2a8008991a82d0a61be844ab4b1c88383f3fa79b43eff10b1039e44ad5a758a173cbf6386b  05_fix_mdp3_ctrl_off.patch
1f23466d1a864f2d2c67ae4250b0c166ecb7755779bbdad6732808a001cc2826cc1d013c2281d2ca1d5abd7d60d13783f0b9f11bb8359971f2b30b911e627220  06_fix_usb_gadget_function.patch"
