# Template file for 'qt'
pkgname=qt
version=4.8.7
revision=23
_distname=qt-everywhere-opensource-src
patch_args="-Np1"
wrksrc=${_distname}-${version}
homepage="http://qt.io/"
short_desc="A cross-platform application and UI framework"
maintainer="Juan RP <xtraeme@voidlinux.eu>"
license="GPL-3, LGPL-2.1"
distfiles="http://download.qt.io/official_releases/qt/${version%.*}/${version}/${_distname}-${version}.tar.gz"
checksum=e2882295097e47fe089f8ac741a95fef47e0a73a3f3cdf21b56990638f626ea0

hostmakedepends="perl sqlite pkg-config"
makedepends="
 MesaLib-devel libressl-devel unixodbc-devel libXv-devel libXinerama-devel
 libXrandr-devel libXcursor-devel glib-devel icu-devel cups-devel sqlite-devel
 alsa-lib-devel libmng-devel libSM-devel gtk+-devel libnotify-devel
 libmysqlclient-devel postgresql-libs-devel pulseaudio-devel tslib-devel"
depends="qtchooser hicolor-icon-theme icu-libs"
replaces="qt-qtconfig>=0"

CXXFLAGS="-std=gnu++98 -Wno-deprecated"

if [ -n "$CROSS_BUILD" ]; then
	hostmakedepends+=" qt-qmake qt-host-tools"
	LDFLAGS="-L${XBPS_CROSS_BASE}/usr/lib"
fi

do_configure() {
	local _opts _spec

	_opts+=" -confirm-license"
	_opts+=" -opensource"
	_opts+=" -prefix /usr"
	_opts+=" -sysconfdir /etc"
	_opts+=" -bindir /usr/lib/qt/bin"
	_opts+=" -datadir /usr/share/qt"
	_opts+=" -docdir /usr/share/doc/qt"
	_opts+=" -plugindir /usr/lib/qt/plugins"
	_opts+=" -importdir /usr/lib/qt/imports"
	_opts+=" -translationdir /usr/share/qt/translations"

	if [ -n "$CROSS_BUILD" ]; then
		_opts+=" -force-pkg-config"
		_opts+=" -continue"
		_opts+=" -xplatform ${CXX}"
		_opts+=" -exceptions"
		_opts+=" -make libs"
		_opts+=" -make tools"
		_opts+=" -make docs"
		_opts+=" -make translations"
		_opts+=" -qt3support"
		case "$XBPS_TARGET_MACHINE" in
			armv5tel)
				_opts+=" -arch arm"
				_spec="arm-linux-gnueabi-c++"
				;;
			armv6l)
				_opts+=" -arch arm"
				_spec="arm-linux-gnueabihf-c++"
				;;
			armv6l-musl)
				_opts+=" -arch arm"
				_spec="arm-linux-musleabihf-c++"
				;;
			armv7l)
				_opts+=" -arch arm"
				_spec="armv7l-linux-gnueabihf-c++"
				;;
			armv7l-musl)
				_opts+=" -arch arm"
				_spec="armv7l-linux-musleabihf-c++"
				;;
			aarch64)
				_opts+=" -arch aarch64"
				_spec="aarch64-linux-gnu-c++"
				;;
			aarch64-musl)
				_opts+=" -arch aarch64"
				_spec="aarch64-linux-musl-c++"
				;;
			mips-musl)
				_opts+=" -arch mips"
				_spec="mips-linux-musl-c++"
				;;
			mipsel-musl)
				_opts+=" -arch mips"
				_spec="mipsel-linux-musl-c++"
				;;
			mipselhf-musl)
				_opts+=" -arch mips"
				_spec="mipsel-linux-muslhf-c++"
				;;
			*-musl)
				_opts+=" -arch arm"
				_spec="${XBPS_TARGET_MACHINE}-linux-musl-c++"
				;;
			*)
				_opts+=" -arch arm"
				_spec="${XBPS_TARGET_MACHINE}-linux-gnu-c++"
				;;
		esac
		mkdir -p mkspecs/${_spec}
		cat > mkspecs/${_spec}/qmake.conf << _EOF
#
# qmake specification for ${_spec}
#
MAKEFILE_GENERATOR         = UNIX
TARGET_PLATFORM            = unix
TEMPLATE                   = app
CONFIG                     += qt warn_on release incremental link_prl gdb_dwarf_index
QT                         += core gui
QMAKE_INCREMENTAL_STYLE    = sublib

include(../common/linux.conf)
include(../common/gcc-base-unix.conf)
QMAKE_CC                      = ${CC}
QMAKE_LINK_C                  = ${CC}
QMAKE_LINK_C_SHLIB            = ${CC}
QMAKE_CFLAGS_RELEASE_WITH_DEBUGINFO += -O2 -g
QMAKE_CXX                     = ${CXX}
QMAKE_LINK                    = ${CXX}
QMAKE_LINK_SHLIB              = ${CXX}
QMAKE_CXXFLAGS_RELEASE_WITH_DEBUGINFO += -O2 -g
QMAKE_PCH_OUTPUT_EXT          = .gch
QMAKE_CFLAGS_PRECOMPILE       = -x c-header -c \${QMAKE_PCH_INPUT} -o \${QMAKE_PCH_OUTPUT}
QMAKE_CFLAGS_USE_PRECOMPILE   = -include \${QMAKE_PCH_OUTPUT_BASE}
QMAKE_CXXFLAGS_PRECOMPILE     = -x c++-header -c \${QMAKE_PCH_INPUT} -o \${QMAKE_PCH_OUTPUT}
QMAKE_CXXFLAGS_USE_PRECOMPILE = -include \${QMAKE_PCH_OUTPUT_BASE}
QMAKE_LFLAGS_RELEASE          += -Wl,-O1 -Wl,-rpath,${wrksrc}/lib
QMAKE_LFLAGS_NOUNDEF          += -Wl,--no-undefined

load(qt_config)
_EOF
		cat > mkspecs/${_spec}/qplatformdefs.h << _EOF
#include "../linux-g++/qplatformdefs.h"
_EOF

		# Force enable qfilesystemwatcher_{i,d}notify
		cat >> src/corelib/io/io.pri << _EOF
SOURCES += io/qfilesystemwatcher_inotify.cpp io/qfilesystemwatcher_dnotify.cpp
HEADERS += io/qfilesystemwatcher_inotify_p.h io/qfilesystemwatcher_dnotify_p.h
_EOF

		# qmake CFLAGS/LDFLAGS
		sed -i configure \
			-e '/outpath\/qmake\".*\"\$MAKE\")/s/)/ CC=gcc CXX=g++ LINK=g++ QMAKE_CFLAGS="$XBPS_CFLAGS" QMAKE_CXXFLAGS="$XBPS_CXXFLAGS" QMAKE_LFLAGS="$XBPS_LDFLAGS" )/' \
			-e 's/\(setBootstrapVariable\s\+\|EXTRA_C\(XX\)\?FLAGS=.*\)QMAKE_C\(XX\)\?FLAGS_\(DEBUG\|RELEASE\).*/:/' \
			-e 's/5\*\|4\*\|3\.4\*)/6*|&/'
		export PKG_CONFIG_PATH=${XBPS_CROSS_BASE}/usr/lib/pkgconfig:${XBPS_CROSS_BASE}/usr/share/pkgconfig
		export PKG_CONFIG_LIBDIR=${XBPS_CROSS_BASE}/usr/lib/pkgconfig
		export PKG_CONFIG_SYSROOT_DIR=${XBPS_CROSS_BASE}

	else
		# qmake CFLAGS/LDFLAGS
		sed -i -e '/outpath\/qmake\".*\"\$MAKE\")/s/)/ QMAKE_CFLAGS="$CFLAGS" QMAKE_CXXFLAGS="$CXXFLAGS" QMAKE_LFLAGS="$LDFLAGS" )/' \
			-e 's/\(setBootstrapVariable\s\+\|EXTRA_C\(XX\)\?FLAGS=.*\)QMAKE_C\(XX\)\?FLAGS_\(DEBUG\|RELEASE\).*/:/' \
			configure
	fi

	_opts+=" -nomake demos"
	_opts+=" -nomake examples"
	_opts+=" -openssl-linked"
	_opts+=" -xmlpatterns"
	_opts+=" -no-webkit"
	_opts+=" -gtkstyle"
	_opts+=" -system-sqlite"
	_opts+=" -shared"
	_opts+=" -graphicssystem raster"
	_opts+=" -no-openvg"
	_opts+=" -no-phonon"
	_opts+=" -no-phonon-backend"
	_opts+=" -no-pch"
	_opts+=" -no-rpath"
	_opts+=" -optimized-qmake"
	_opts+=" -reduce-relocations"
	_opts+=" -dbus-linked"
	_opts+=" -v"

	echo "=== ./configure ${_opts}"
	export LD_LIBRARY_PATH="${wrksrc}/lib:${LD_LIBRARY_PATH}"
	export LD="$CXX"
	export QMAKE_CFLAGS="${CFLAGS}"
	export QMAKE_CXXFLAGS="${CXXFLAGS}"
	export QMAKE_LFLAGS="${LDFLAGS} -Wl,-rpath,${wrksrc}/lib"
	./configure ${_opts}
}
do_build() {
	local _f
	if [ -n "$CROSS_BUILD" ]; then
		# Replace binaries for host
		find -name Makefile\* -exec sed -i "{}" -e"s;/builddir/.*/bin/;/usr/lib/qt/bin/;g" \;
		# Build qmake for the target
		echo "=== Building qmake for the target ${XBPS_TARGET_MACHINE}"
		mv bin/qmake{,-host}
		sed -i qmake/Makefile \
			-e"s;^\(CC =\).*;\1 ${CC};" \
			-e"s;^\(CXX =\).*;\1 ${CXX};" \
			-e"s;^\(QMAKE_CFLAGS =\).*;\1 ${CFLAGS};" \
			-e"s;^\(QMAKE_CXXFLAGS =\).*;\1 ${CXXFLAGS};" \
			-e"s;^\(QMAKE_LFLAGS =\).*;\1 ${LDFLAGS};"
		cd qmake
		make clean
		make ${makejobs}
		cd ${wrksrc}
		mv bin/qmake{,-target}
		mv bin/qmake{-host,}
		cd ${wrksrc}
	fi
	export LD_LIBRARY_PATH="${wrksrc}/lib:${LD_LIBRARY_PATH}"
	export LD="$CXX"
	make ${makejobs}
}
do_install() {
	local hicolordir=${DESTDIR}/usr/share/icons/hicolor

	make INSTALL_ROOT=${DESTDIR} install

	# install missing qatomic_aarch64.h
	# TODO: find better fix (add to install targets?)
	install -D -m644 src/corelib/arch/qatomic_aarch64.h ${DESTDIR}/usr/include/QtCore/
	install -D -m644 src/corelib/arch/qatomic_aarch64.h ${DESTDIR}/usr/include/Qt/

	# delete tests
	rm -rf ${DESTDIR}/usr/tests

	# install missing icons and desktop files
	for icon in tools/linguist/linguist/images/icons/linguist-*-32.png ; do
		size=$(echo $(basename ${icon}) | cut -d- -f2)
		install -D -m644 ${icon} \
			${hicolordir}/${size}x${size}/apps/linguist.png
	done
	install -Dm644 src/gui/dialogs/images/qtlogo-64.png \
		${hicolordir}/64x64/apps/qtlogo.png
	install -Dm644 src/gui/dialogs/images/qtlogo-64.png \
		${DESTDIR}/usr/share/pixmaps/qtlogo.png
	install -Dm644 tools/assistant/tools/assistant/images/assistant.png \
		${hicolordir}/32x32/apps/assistant.png
	install -Dm644 tools/designer/src/designer/images/designer.png \
		${hicolordir}/128x128/apps/designer.png
	install -d ${DESTDIR}/usr/share/applications
	install -m644 ${FILESDIR}/*.desktop \
		${DESTDIR}/usr/share/applications

	vlicense LGPL_EXCEPTION.txt

	vmkdir usr/bin
	for f in ${DESTDIR}/usr/lib/qt/bin/*; do
		ln -s /usr/lib/qt/bin/$(basename $f) ${DESTDIR}/usr/bin/$(basename $f)-qt4
	done

	# Remove wrong stuff
	rm -rf ${DESTDIR}/usr/{examples,demos}

	# Clean generated qmake.conf reference to builddir
	find -name qmake.conf -exec sed -i "{}" -e"s; -Wl,-rpath,/builddir.*;;" \;

	# For a cross build replace qmake with the target binary
	if [ -f ${wrksrc}/bin/qmake-target ]; then
		mv ${wrksrc}/bin/qmake-target ${DESTDIR}/usr/lib/qt/bin/qmake
		ln -s /usr/lib/qt/bin/qmake ${DESTDIR}/usr/bin/qmake-qt4
	fi
}

qt-doc_package() {
	depends="qt-devel-tools>=${version}_${revision}"
	short_desc+=" - Documentation"
	pkg_install() {
		vmove usr/share/doc/qt
	}
}
qt-designer_package() {
	depends="hicolor-icon-theme qt-designer-libs>=${version}_${revision}"
	short_desc+=" - GUI designer"
	pkg_install() {
		vmove usr/bin/designer-qt4
		vmove usr/lib/qt/bin/designer
		vmove usr/lib/qt/plugins/designer
		vmove "usr/share/icons/hicolor/128x128/apps/designer.*"
		vmove "usr/share/applications/designer.*"
		vmove "usr/share/qt/translations/designer*"
	}
}
qt-designer-libs_package() {
	depends="${sourcepkg}>=${version}_${revision}"
	short_desc+=" - GUI designer libraries"
	pkg_install() {
		vmove usr/lib/libQtDesigner.so.*
		vmove usr/lib/libQtDesignerComponents.so.*
	}
}
qt-designer-devel_package() {
	depends="qt-designer-libs>=${version}_${revision}"
	short_desc+=" - GUI designer development"
	pkg_install() {
		vmove usr/lib/libQtDesigner.so
		vmove usr/lib/libQtDesignerComponents.so
	}
}
qt-host-tools_package() {
	short_desc+=" - host tools"
	pkg_install() {
		for f in lrelease lupdate moc qdbuscpp2xml qdbusxml2cpp qt3to4 \
			rcc uic uic3; do
			if [ -f "${DESTDIR}/usr/lib/qt/bin/${f}" ]; then
				vmove usr/lib/qt/bin/${f}
			fi
		done
	}
}
qt-devel-tools_package() {
	depends="hicolor-icon-theme"
	short_desc+=" - development tools"
	pkg_install() {
		for f in assistant lconvert linguist pixeltool qcollectiongenerator \
			qhelpgenerator qhelpconverter xmlpatterns xmlpatternsvalidator \
			qttracereplay qdoc3 qmlviewer; do
			if [ -f "${DESTDIR}/usr/lib/qt/bin/${f}" ]; then
				vmove usr/bin/${f}-qt4
				vmove usr/lib/qt/bin/${f}
			fi
		done
		vmove usr/share/icons/hicolor
		for f in linguist assistant; do
			vmove usr/share/applications/${f}.desktop
			[ ! -f ${DESTDIR}/usr/share/pixmaps/${f}.png ] && continue
			vmove usr/share/pixmaps/${f}.png
		done
		vmove usr/share/qt/phrasebooks
	}
}
qt-devel_package() {
	depends="qt-qmake qt-devel-tools qt-host-tools libX11-devel icu-devel qt>=${version}_${revision}"
	short_desc+=" - development files"
	pkg_install() {
		for f in lrelease lupdate moc qdbuscpp2xml qdbusxml2cpp qt3to4 \
			rcc uic uic3; do
			if [ -f "${DESTDIR}/usr/bin/${f}-qt4" ]; then
				vmove usr/bin/${f}-qt4
			fi
		done
		vmove usr/include
		vmove "usr/lib/*.so"
		vmove "usr/lib/*.a"
		vmove "usr/lib/*.prl"
		vmove usr/lib/pkgconfig
	}
}

qt-plugin-odbc_package() {
	short_desc+=" - ODBC plugin"
	pkg_install() {
		vmove usr/lib/qt/plugins/sqldrivers/libqsqlodbc.so
	}
}
qt-plugin-pgsql_package() {
	short_desc+=" - PostgreSQL plugin"
	pkg_install() {
		vmove usr/lib/qt/plugins/sqldrivers/libqsqlpsql.so
	}
}
qt-plugin-mysql_package() {
	short_desc+=" - MySQL plugin"
	pkg_install() {
		vmove usr/lib/qt/plugins/sqldrivers/libqsqlmysql.so
	}
}
qt-plugin-sqlite_package() {
	short_desc+=" - SQLite plugin"
	pkg_install() {
		vmove usr/lib/qt/plugins/sqldrivers/libqsqlite.so
	}
}
qt-qmake_package() {
	short_desc+=" - qmake Makefile generator tool"
	pkg_install() {
		vmove usr/bin/qmake-qt4
		vmove usr/lib/qt/bin/qmake
		vmove usr/share/qt/mkspecs
	}
}
