pkgname=libhybris
pkgver=1.0_git20191001
pkgrel=1
arch="x86 armhf armv7 aarch64"
url="https://github.com/libhybris/libhybris"
license="Apache"
makedepends="autoconf automake libtool wayland-dev linux-headers bsd-compat-headers
	libx11-dev libxcb-dev libxext-dev
	android-headers-9.0 android-headers-7.1 android-headers-4.4"
depends_dev="bsd-compat-headers android-headers-9.0"
_commit="eaa6884ba95a1d5d0303119ffdc48952a098059b"
source="$pkgname-$_commit.tar.gz::https://github.com/libhybris/libhybris/archive/$_commit.tar.gz
	0001-Make-libhybris-compile-with-musl.patch
	0002-tests-Regression-test-for-EGL-glibc-TLS-conflict.patch
	0003-PATCH-v2-Implement-X11-EGL-platform-based-on-wayland.patch
	0004-Build-test-hwcomposer-7-caf.patch
"

pkgdesc="libhybris allows to use bionic-based HW adaptations"
subpackages="$pkgname-dev $pkgname-egl $pkgname-gles $pkgname-libwayland-egl:_wayland
	$pkgname-9.0:_specific $pkgname-7.1:_specific $pkgname-4.4:_specific"

if [ "$CARCH" == "aarch64" ]; then
subpackages="$pkgname-dev $pkgname-egl $pkgname-gles $pkgname-libwayland-egl:_wayland
	$pkgname-9.0:_specific $pkgname-7.1:_specific"
elif [ "$CARCH" == "x86" ]; then
subpackages="$pkgname-dev $pkgname-egl $pkgname-gles $pkgname-libwayland-egl:_wayland
	$pkgname-9.0:_specific $pkgname-7.1:_specific $pkgname-4.4:_specific"
fi

options="!check !strip !tracedeps"

builddir="$srcdir/$pkgname-$_commit"
_tmppkgdir="$srcdir/tmpinstall"
_main_headers_ver=9.0

_bins_specific="test_audio test_camera test_egl_configs	test_gps
	test_hwcomposer	test_hwcomposer_7_caf test_input test_lights test_media test_nfc
	test_recorder test_sensors test_sf test_vibrator test_wifi"

_libs_specific="libcamera.so.1.0.0 libhardware.so.2.0.0
	libhybris-common.so.1.0.0 libhybris-eglplatformcommon.so.1.0.0
	libhybris-hwcomposerwindow.so.1.0.0 libis.so.1.0.0 libmedia.so.1
	libmedia.so.1.0.0 libnfc_ndef_nxp.so.1.0.0 libnfc_nxp.so.1.0.0
	libsf.so.1.0.0 libsync.so.2.0.0 libui.so.1.0.0 libvibrator.so.1.0.0
	libwifi.so.1.0.0
	libhybris/eglplatform_fbdev.so libhybris/eglplatform_hwcomposer.so
	libhybris/eglplatform_null.so libhybris/eglplatform_wayland.so
	libhybris/eglplatform_x11.so"

build() {
	cd "$builddir/hybris"

	NOCONFIGURE=1 ./autogen.sh

	if [ "$CARCH" == "armhf" -o "$CARCH" == "armv7" ]; then
		_vers="9.0 7.1 4.4"
		_ldpath="/usr/libexec/droid-hybris/system/lib:/vendor/lib:/system/lib"
		_arch="arm"
		_enable_quirks="--enable-mali-quirks \
				--enable-adreno-quirks"
	elif [ "$CARCH" == "aarch64" ]; then
		_vers="9.0 7.1"
		_ldpath="/usr/libexec/droid-hybris/system/lib64:/vendor/lib64:/system/lib64"
		_arch="arm64"
		_enable_quirks="--enable-mali-quirks \
				--enable-adreno-quirks"
	elif [ "$CARCH" == "x86" ]; then
		_vers="9.0 7.1 4.4"
		_ldpath="/usr/libexec/droid-hybris/system/lib:/vendor/lib:/system/lib"
		_arch="x86"
	fi

	for _headers_ver in $_vers; do
		msg "building $pkgname-$_headers_ver"
		./configure \
			--prefix=/usr \
			--enable-wayland \
			--enable-trace \
			--enable-debug \
			--enable-experimental \
			--with-android-headers=/usr/include/android-$_headers_ver \
			--with-default-hybris-ld-library-path=$_ldpath \
			--enable-arch=$_arch \
			$_enable_quirks \
			--enable-property-cache
		make clean
		make
		make DESTDIR="${_tmppkgdir}/$pkgname-$_headers_ver" install
	done
}

package() {
	cd "$builddir/hybris"

	# make DESTDIR="${pkgdir}" install
	mkdir -p "${pkgdir}"
	cp -a ${_tmppkgdir}/$pkgname-$_main_headers_ver/* "${pkgdir}"

	for _bin in ${_bins_specific}; do
		rm "${pkgdir}/usr/bin/${_bin}" || true
	done

	for _lib in ${_libs_specific}; do
		rm "${pkgdir}/usr/lib/${_lib}"
	done
}

_specific() {
	depends="$pkgname"

	for _bin in ${_bins_specific}; do
		install -Dm755 "${_tmppkgdir}/${subpkgname}/usr/bin/${_bin}" \
			"${subpkgdir}/usr/bin/${_bin}" || true
	done

	for _lib in ${_libs_specific}; do
		install -Dm755 "${_tmppkgdir}/${subpkgname}/usr/lib/${_lib}" \
			"${subpkgdir}/usr/lib/${_lib}"
	done
}

egl() {
	depends="$pkgname"
	pkgdesc="libhybris libEGL runtime libraries"
	install="$subpkgname.post-install"

	install -d "$subpkgdir"/usr/lib/libhybris-egl
	mv "$pkgdir"/usr/lib/libEGL.so.* \
		"$subpkgdir"/usr/lib/libhybris-egl/
}

gles() {
	depends="$pkgname"
	pkgdesc="libhybris libGLESv2 runtime libraries"
	install -d "$subpkgdir"/usr/lib/libhybris-egl
	mv "$pkgdir"/usr/lib/libGLES*.so.* \
		"$subpkgdir"/usr/lib/libhybris-egl/
}

_wayland() {
	depends="$pkgname"
	pkgdesc="libhybris libwayland-egl library"
	mkdir -p "$subpkgdir"/usr/lib/libhybris-egl
	mv "$pkgdir"/usr/lib/libwayland-egl.so.* "$subpkgdir"/usr/lib/libhybris-egl/ \
		|| return 1
}

dev() {
	default_dev

	# Avoid conflicts with mesa-dev
	install -d "$subpkgdir"/usr/lib/libhybris-egl

	mv "$subpkgdir"/usr/lib/lib*GL*.so "$subpkgdir"/usr/lib/libhybris-egl
	mv "$subpkgdir"/usr/lib/libwayland-egl.so "$subpkgdir"/usr/lib/libhybris-egl

	cd "$subpkgdir"/usr/lib/pkgconfig
	rm -f egl.pc glesv*.pc wayland-egl.pc

	cd "$subpkgdir"/usr/include
	# Move libhybris-provided headers into hybris dir
	mv CL EGL GLES GLES2 KHR VG hybris

	# Symlink eglhybris.h
	mkdir -p EGL
	cd EGL
	ln -s ../hybris/EGL/eglhybris.h .
}

sha512sums="598e4a788d38116437d24052b2153abd980d11fc8a277b5a0655ab96a6b8803388260e4621200f015570dcfa0ebb66fb68eeeb99948f1d9d4b9a04aa71d06540  libhybris-eaa6884ba95a1d5d0303119ffdc48952a098059b.tar.gz
a1b707c9aaa7bde2907d04742d7d582df57949415e1afe16c3b461671421a18643008e19aa006e9e67b9644392ec4e2d58c6de725d2db2df077a49526f2893ce  0001-Make-libhybris-compile-with-musl.patch
64fa516c823859c062d175a51fc86846dc85f4e98989cf12c7cb8c9a13ffffb748f6152543f1a6e22f5fe78eb619880262b3c8ff3b6cbd314241fec16ef2eff5  0002-tests-Regression-test-for-EGL-glibc-TLS-conflict.patch
db7bb0ffc04042b32d870db364df4d38d9769978f26d71f98dd2569b4cae7fb3839c7a0c421b465f931a57fcde38b8eca091b7ae0062e32a7e09b93a701397f3  0003-PATCH-v2-Implement-X11-EGL-platform-based-on-wayland.patch
eda57804fefa1d586e90fb2198c310f65e6dbc7e09b5d1dd7d15df4ab152c4c8915c212230a92c35ec8a013d3ec7a7b1d289653bedb46b7eb2850a477ccebe16  0004-Build-test-hwcomposer-7-caf.patch"
