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

pkgname="linux-jolla-sbj"
pkgver=3.4.0
pkgrel=0
pkgdesc="Jolla phone kernel fork"
arch="armhf"
_carch="arm"
_flavor="jolla-sbj"
url="http://releases.sailfishos.org/sources/2.1.0.11/"
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
_hash="2.1.0.11"
_config="config-${_flavor}.${arch}"
source="
	$pkgname-$_hash.tar.bz2::http://releases.sailfishos.org/sources/${_hash}/sailfish-${_hash}-oss-adaptation-sbj.tar.bz2
	$_config
	compiler-gcc6.h
	02_gpu-msm-fix-gcc5-compile.patch
	duplicate-return-address-definition.patch
"
kver="kernel-adaptation-sbj-3.4.108.20161101.1"
kernelarchive="$srcdir/sailfish-${_hash}-oss-adaptation-sbj/kernel-adaptation-sbj/${kver}.tar.bz2"
builddir="$srcdir/$kver"

prepare() {
	cd "$srcdir"
	tar -xf "$kernelarchive"
	cd "$ksrcdir"

	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
}

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="c0ff1641dd6ae4dbf736ddaf543da0cfd51188d36f163a5f519772867a44c19b40ab86043c9ee1598bbf766e05ef8620588dde9ae5ebadce6e03360ebc78e484  linux-jolla-sbj-2.1.0.11.tar.bz2
70230130d6d32aef7c220ed968fa04787ea329e198f304df293c000555c0dc8b94504f70d783ff3b6d0a5589f6612e3dfaaacc2e77c86752cebd3cd1bcf908df  config-jolla-sbj.armhf
d80980e9474c82ba0ef1a6903b434d8bd1b092c40367ba543e72d2c119301c8b2d05265740e4104ca1ac5d15f6c4aa49e8776cb44264a9a28dc551e0d1850dcc  compiler-gcc6.h
7be03a9e78b7ac330a54b1f00509caa0621a95c0c55901878ad757f9dd69cc05ba2c8b5ea987063ae1224f92c4d090d515fa5d369e7755181a4871b0d0f82881  02_gpu-msm-fix-gcc5-compile.patch
9b15bf1f6cb66f54c785b0af6c9db8a7d63257e6fa3eeb0cbf47284334166aa6a7ff93e1b64ab69206c8047d64641199c0f5fcbc257bbe039263252fff45118e  duplicate-return-address-definition.patch"
