# Kernel config based on: arch/arm/configs/chaozu_defconfig

pkgname="linux-bq-chaozu"
pkgver=3.18.31
pkgrel=2
pkgdesc="Aquaris U kernel fork"
arch="armhf"
_carch="arm"
_flavor="bq-chaozu"
url="https://kernel.org"
license="GPL2"
options="!strip !check !tracedeps"
makedepends="perl sed installkernel bash gmp-dev bc linux-headers elfutils-dev gcc6"

# Compiler: this kernel was only tested with GCC6. Feel free to make a merge
# request if you find out that it is booting working with newer GCCs as
# well. See <https://postmarketos.org/vendorkernel> for instructions.
if [ "${CC:0:5}" != "gcc6-" ]; then
	CC="gcc6-$CC"
	HOSTCC="gcc6-gcc"
	CROSS_COMPILE="gcc6-$CROSS_COMPILE"
fi

# Source
_repository="android_kernel_bq_msm8937"
_commit="9fe7be06bdbbad2d9cb86d9b135d082b4d28b9ea"
_config="config-${_flavor}.${arch}"
source="
	$pkgname-$_commit.tar.gz::https://github.com/LineageOS/${_repository}/archive/${_commit}.tar.gz
	00_fix_rndis_ipa_trace_header_not_found.patch
	01_prima_gcc6.patch
	02_prima_misleading_indentation.patch
	03_fix_u_f_header.patch
	04_fix_usb_gadget_function.patch
	$_config"
builddir="$srcdir/${_repository}-${_commit}"

prepare() {
	default_prepare

	# 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
}

menuconfig() {
	cd "$builddir"
	make ARCH="$_carch" menuconfig
	cp .config "$startdir"/$_config
}

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

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="c72365057ddc94d0fd051f0a40f12e6eb0ba31b8fc0ad673c352be0924bc96c643726afac5042fd63eca5d79c4366b706da0addad3aa521867c323a3eec542cd  linux-bq-chaozu-9fe7be06bdbbad2d9cb86d9b135d082b4d28b9ea.tar.gz
9c549714f03c7a7666e8aa7508a48cfdccab7cf3e97fb59c4df8fa97f75492365926f82c4a677c50780d68d4905504dcccbdd6de6344bd8d4b88b2331933ed46  00_fix_rndis_ipa_trace_header_not_found.patch
184875cf13a180d1c4bbe27d8ad24a8d6bd2e36ec35f9c4326809f1c8938ecebf3844c36ef71f4a0af9077e2506a82b2b4383ffd020dd1918aba1689d77b0c01  01_prima_gcc6.patch
8cfd728ccf2bf19450dad5d9fe482ba2461dcf43d25440572a1df73b5a2599f4556ddcafd7cd594cb11c8da5767887d4c1585863c8b36335e7b371f7b735307d  02_prima_misleading_indentation.patch
b408f157cc025d410b38f5aee9be977e670e1ac3f195f6c3a6681446fa0c3887067d62a4f729cfcd058fab42fca355b0265764dc2d21515f6e023ed025f55d41  03_fix_u_f_header.patch
1f23466d1a864f2d2c67ae4250b0c166ecb7755779bbdad6732808a001cc2826cc1d013c2281d2ca1d5abd7d60d13783f0b9f11bb8359971f2b30b911e627220  04_fix_usb_gadget_function.patch
aaf5c6267f0e39ec584b545b50217fde76a2fd4900316219a062c0900a8d422372bbcfd353708fb2346042cf342e9fa9de75f724c7d732e9961bd2aacbe2798b  config-bq-chaozu.armhf"
