SUMMARY="A cross-platform application and UI framework"
DESCRIPTION="Qt is a cross-platform application framework that is widely \
used for developing software with a graphical user interface, and also used \
for developing non-GUI programs such as command-line tools and consoles for \
servers."
HOMEPAGE="https://qt.io/"
COPYRIGHT="2015-2021 The Qt Company Ltd."
LICENSE="GNU LGPL v3
	GNU GPL v3
	GNU FDL v1"
REVISION="1"
SOURCE_URI="https://download.qt.io/official_releases/qt/${portVersion%.*}/$portVersion/submodules/qtbase-everywhere-src-$portVersion.tar.xz"
CHECKSUM_SHA256="85ab9180180c2eaf84cd11ae4c6d5a6a69f2f8fd7260aaccfd91a3e7e7232c1a"
SOURCE_DIR="qtbase-everywhere-src-$portVersion"
srcGitRev2="2a460261e192d8cb6f76b4e3be8d02a966c336e9"
SOURCE_URI_2="https://github.com/threedeyes/qt6-haikuplugins/archive/$srcGitRev2.tar.gz"
CHECKSUM_SHA256_2="dc50123f3c6c83208df6a30f6765c0dc0fc7b7cb741a8e1fb51d99db7ecec9bd"
SOURCE_DIR_2="qt6-haikuplugins-$srcGitRev2"
PATCHES="qt6_base-$portVersion.patchset"

ARCHITECTURES="all !x86_gcc2"
SECONDARY_ARCHITECTURES="x86"

PROVIDES="
	qt6_base$secondaryArchSuffix = $portVersion compat >= 6
	lib:libQt6Concurrent$secondaryArchSuffix = $portVersion compat >= 6
	lib:libQt6Core$secondaryArchSuffix = $portVersion compat >= 6
	lib:libQt6DBus$secondaryArchSuffix = $portVersion compat >= 6
	lib:libQt6EglFSDeviceIntegration$secondaryArchSuffix = $portVersion compat >= 6
	lib:libQt6Gui$secondaryArchSuffix = $portVersion compat >= 6
	lib:libQt6Network$secondaryArchSuffix = $portVersion compat >= 6
	lib:libQt6OpenGL$secondaryArchSuffix = $portVersion compat >= 6
	lib:libQt6OpenGLWidgets$secondaryArchSuffix = $portVersion compat >= 6
	lib:libQt6PrintSupport$secondaryArchSuffix = $portVersion compat >= 6
	lib:libQt6Sql$secondaryArchSuffix = $portVersion compat >= 6
	lib:libQt6Test$secondaryArchSuffix = $portVersion compat >= 6
	lib:libQt6Widgets$secondaryArchSuffix = $portVersion compat >= 6
	lib:libQt6Xml$secondaryArchSuffix = $portVersion compat >= 6
	"
REQUIRES="
	haiku$secondaryArchSuffix
	haiku_svg_icon_theme
	lib:libcrypto$secondaryArchSuffix
	lib:libdouble_conversion$secondaryArchSuffix
	lib:libegl$secondaryArchSuffix
	lib:libfreetype$secondaryArchSuffix
	lib:libfontconfig$secondaryArchSuffix
	lib:libgl$secondaryArchSuffix
	lib:libharfbuzz$secondaryArchSuffix
	lib:libicudata$secondaryArchSuffix >= 66
	lib:libicui18n$secondaryArchSuffix >= 66
	lib:libicuuc$secondaryArchSuffix >= 66
	lib:libintl$secondaryArchSuffix
	lib:libpcre2_16$secondaryArchSuffix
	lib:libpng16$secondaryArchSuffix
	lib:libssl$secondaryArchSuffix
	lib:libz$secondaryArchSuffix
	cmd:qsystray
	cmd:qnotify
	"

PROVIDES_devel="
	qt6_base${secondaryArchSuffix}_devel = $portVersion compat >= 6
	devel:libQt6Concurrent$secondaryArchSuffix = $portVersion compat >= 6
	devel:libQt6Core$secondaryArchSuffix = $portVersion compat >= 6
	devel:libQt6DBus$secondaryArchSuffix = $portVersion compat >= 6
	devel:libQt6DeviceDiscoverySupport$secondaryArchSuffix = $portVersion compat >= 6
	devel:libQt6EdidSupport$secondaryArchSuffix = $portVersion compat >= 6
	devel:libQt6EglFSDeviceIntegration$secondaryArchSuffix = $portVersion compat >= 6
	devel:libQt6FBSupport$secondaryArchSuffix = $portVersion compat >= 6
	devel:libQt6Gui$secondaryArchSuffix = $portVersion compat >= 6
	devel:libQt6Network$secondaryArchSuffix = $portVersion compat >= 6
	devel:libQt6OpenGL$secondaryArchSuffix = $portVersion compat >= 6
	devel:libQt6OpenGLWidgets$secondaryArchSuffix = $portVersion compat >= 6
	devel:libQt6PrintSupport$secondaryArchSuffix = $portVersion compat >= 6
	devel:libQt6Sql$secondaryArchSuffix = $portVersion compat >= 6
	devel:libQt6Test$secondaryArchSuffix = $portVersion compat >= 6
	devel:libQt6Widgets$secondaryArchSuffix = $portVersion compat >= 6
	devel:libQt6Xml$secondaryArchSuffix = $portVersion compat >= 6
	cmd:qmake6$secondaryArchSuffix = $portVersion compat >= 6
	cmd:qtpaths6$secondaryArchSuffix = $portVersion compat >= 6
	"
REQUIRES_devel="
	haiku${secondaryArchSuffix}_devel
	qt6_base$secondaryArchSuffix == $portVersion base
	devel:libgl$secondaryArchSuffix
	"

BUILD_REQUIRES="
	haiku${secondaryArchSuffix}_devel
	devel:libcrypto$secondaryArchSuffix
	devel:libdouble_conversion$secondaryArchSuffix >= 3
	devel:libegl$secondaryArchSuffix
	devel:libfontconfig$secondaryArchSuffix
	devel:libfreetype$secondaryArchSuffix
	devel:libgl$secondaryArchSuffix
	devel:libglu$secondaryArchSuffix
	devel:libharfbuzz$secondaryArchSuffix
	devel:libicuuc$secondaryArchSuffix >= 66
	devel:libjpeg$secondaryArchSuffix
	devel:libpcre2_16$secondaryArchSuffix
	devel:libpng16$secondaryArchSuffix
	devel:libsqlite3$secondaryArchSuffix
	devel:libssl$secondaryArchSuffix
	devel:libxml2$secondaryArchSuffix
	devel:libz$secondaryArchSuffix
	"
BUILD_PREREQUIRES="
	cmd:awk
	cmd:cmake
	cmd:find
	cmd:g++$secondaryArchSuffix
	cmd:ld$secondaryArchSuffix
	cmd:make
	cmd:ninja
	cmd:perl
	cmd:pkg_config$secondaryArchSuffix
	cmd:python3.7
	cmd:sed
	cmd:xargs
	"

BUILD()
{
	export DISABLE_ASLR=1

	cmake \
		-B build \
		-S $sourceDir \
		-G Ninja \
		-DCMAKE_BUILD_TYPE=Release \
		-DCMAKE_C_FLAGS=-fPIC \
		-DCMAKE_CXX_FLAGS=-fPIC \
		-DCMAKE_INSTALL_PREFIX=$prefix \
		-DBUILD_WITH_PCH=ON \
		-DINSTALL_BINDIR=$libDir/Qt6 \
		-DINSTALL_PUBLICBINDIR=$binDir \
		-DINSTALL_PLUGINSDIR=$addOnsDir/Qt6 \
		-DINSTALL_LIBDIR=$libDir \
		-DINSTALL_LIBEXECDIR=$libDir/Qt6 \
		-DINSTALL_DOCDIR=$docDir \
		-DINSTALL_ARCHDATADIR=$dataDir/Qt6 \
		-DINSTALL_DATADIR=$dataDir/Qt6 \
		-DINSTALL_INCLUDEDIR=$includeDir/Qt6 \
		-DINSTALL_MKSPECSDIR=$dataDir/Qt6/mkspecs \
		-DINSTALL_EXAMPLESDIR=$dataDir/Qt6/examples \
		-DINSTALL_QMLDIR=$dataDir/Qt6/qml \
		-DINSTALL_TRANSLATIONSDIR=$dataDir/Qt6/translations \
		-DQT_QMAKE_TARGET_MKSPEC=haiku-g++ \
		-DQT_FEATURE_openssl_linked=ON \
		-DQT_FEATURE_system_sqlite=ON \
		-DQT_FEATURE_glib=OFF \
		-DFEATURE_rpath=OFF

	ninja -C build $jobArgs
}

INSTALL()
{
	ninja -C build install

	# build platform plugins
	PATH=$PATH:$binDir:$libDir/Qt6
	mkdir -p $sourceDir2/build $preferencesDir
	cd $sourceDir2/build
	qmake6 ../src
	make $jobArgs
	make install
	cp preferences/Qt6Configurator $preferencesDir

	prepareInstalledDevelLibs \
		libQt6Concurrent libQt6Core libQt6DBus libQt6EglFSDeviceIntegration \
		libQt6Gui libQt6Network libQt6OpenGL libQt6OpenGLWidgets \
		libQt6PrintSupport libQt6Sql libQt6Test libQt6Widgets libQt6Xml \
		libQt6FbSupport libQt6DeviceDiscoverySupport

	cd $libDir
	for i in lib*.so.6.*;do
		ln -fs $i $(echo $i | cut -f1,2 -d.)
	done

	# fix paths for static libs
	sed -i 's,${_IMPORT_PREFIX}/lib',$developDir/lib, \
		$libDir/cmake/Qt6FbSupportPrivate/Qt6FbSupportPrivateTargets-release.cmake
	sed -i 's,${_IMPORT_PREFIX}/lib',$developDir/lib, \
		$libDir/cmake/Qt6DeviceDiscoverySupportPrivate/Qt6DeviceDiscoverySupportPrivateTargets-release.cmake

	mkdir -p $binDir
	ln -s -t $binDir $libDir/Qt6/{qmake6,qtpaths6}

	rm -rf $(dirname "$docDir")

	packageEntries devel \
		$developDir \
		$binDir \
		$libDir/cmake \
		$dataDir/Qt6/mkspecs

	addPreferencesDeskbarSymlink $preferencesDir/Qt6Configurator "Qt6 Configurator"
}
