# Reference: <https://postmarketos.org/vendorkernel>
# Kernel config based on: arch/arm64/configs/lineageos_crosshatch_defconfig

pkgname="linux-google-crosshatch"
pkgver=4.9.96
pkgrel=1
pkgdesc="Google Pixel 3 XL kernel fork"
arch="aarch64"
_carch="arm64"
_flavor="google-crosshatch"
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 openssl-dev lz4 dtc"

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

# Source
_repository="android_kernel_google_crosshatch"
# Lineage 16.0 branch
_commit="f8cb98bcb606f94a2307f39c21dfb705fe7868d2"
_config="config-${_flavor}.${arch}"
source="
	$pkgname-$_commit.tar.gz::https://github.com/LineageOS/${_repository}/archive/${_commit}.tar.gz
	$_config
	clk-qcom-mdss-fix-in-tree-build.patch
	init-initramfs-disable-do_skip_initramfs.patch
	arm64-boot-disable-building-DTBO-images.patch
	init-ignore-dm-parameter.patch
	sec_ts-disable-touchscreen-firmware-upgrade.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"
}

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

	# zImage (find the right one)
	# Pixel 3 XL uses lz4 to compress kernel.
	cd "$builddir/arch/$_carch/boot"
	_target="$pkgdir/boot/vmlinuz-$_flavor"
	for _zimg in Image.lz4-dtb 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="e5f840cc4f5a1c68150d397de079db51d51943b0ed2858011ed38b834d2fdfa1f88344b3dcbe886dea2a1b5ee959d6e7b918b009762d5e06a9edf807294647e1  linux-google-crosshatch-f8cb98bcb606f94a2307f39c21dfb705fe7868d2.tar.gz
7f051d8f3515fb85178cdd930d7f2ff50130f66bb9eaf4abe54e9050291d35884809dd66676d1ff75de259f1dc4c57dea5bbb37552aa0e3fce1e2b8092b8a6ba  config-google-crosshatch.aarch64
720c5856b7d1d8b3cb20f77507818b7fe1b4950d444f7ed20cb02d255987d8860d5ed293e81ea326f147af13004b524886c733123794ac62978fda13689a5189  clk-qcom-mdss-fix-in-tree-build.patch
0f536e5acd0f5e6d6774918d8e226ae1f4edc486596fc5c93a110b4eb2555e0f1281f460065928a7f7a272b886f61f7967fe60cc4952ed1f4b37143bd2a28962  init-initramfs-disable-do_skip_initramfs.patch
43b1c1bcdd653366a5d0196bf6171f67914c3c417eeba04f4f071e095e8b3191e67ec8e03b1ed1701a9a0efe5b3e134ed7fb464439e9a389aafdcf0064f20a10  arm64-boot-disable-building-DTBO-images.patch
66ac924e2619994dad71a88223d62e911cc90c20f578eb1b0544f115de8367ea9767e7955245c70699f7af040bb8c42f9fa7aff60d4638f1e568a3b3662d30a6  init-ignore-dm-parameter.patch
7c941332085c6a2372ad083400e354c61f38752c2706d07592a7ce6a72aa5d8bb9cb10521fe0c941ac4e49aa3dbd2f9473c42ed2a10ec66b1b50b65e8ad1e0a8  sec_ts-disable-touchscreen-firmware-upgrade.patch"
