################################################################################
#
# kf5.cygclass - functions for building KDE Frameworks 5.x packages
#
# Part of cygport - Cygwin packaging application
# Copyright (C) 2006-2020 Cygport authors
# Provided by the Cygwin project <https://cygwin.com/>
#
# cygport is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# cygport is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with cygport.  If not, see <https://www.gnu.org/licenses/>.
#
################################################################################

#****h* Cygclasses/kf5.cygclass
#  SYNOPSIS
#  inherit kf5
#  DESCRIPTION
#  The KDE project provides a full-featured X desktop with a wide variety
#  of applications, built on a library stack all based on the Qt C++ libraries.
#  Most packages are written in C++ and are built with CMake.
#
#  This cygclass manages the building of most KF5-based packages.
#  INHERITS
#  cmake.cygclass, kde.org.cygclass, qt5.cygclass
#****

inherit cmake qt5 kde.org

export QTDIR=${QT5_QTDIR}
export QT_PLUGIN_PATH=${QT5_PLUGINSDIR}

#****C* kf5.cygclass/kf5_cmake
#  SYNOPSIS
#  kf5_cmake [OPTIONS]
#  DESCRIPTION
#  Runs cygcmake to configure the package with several Cygwin- and KF5-specific
#  options.  Additional options, in the form of -DVARIABLE=VALUE,
#  are passed on to cygcmake.
#  REQUIRES
#  cmake, extra-cmake-modules, libQt5Core-devel, libQt5Gui-devel
#****
kf5_cmake() {
	cygcmake \
		-DPLUGIN_INSTALL_DIR=${QT5_PLUGINSDIR} \
		-DQT_PLUGIN_INSTALL_DIR=${QT5_PLUGINSDIR} \
		-DIMPORTS_INSTALL_DIR=${QT5_IMPORTSDIR} \
		-DQML_INSTALL_DIR=${QT5_QMLDIR} \
		-DKDE_INSTALL_USE_QT_SYS_PATHS=ON \
		-DKDE_INSTALL_LIBEXECDIR=$(__host_prefix)/libexec \
		-DKDE_INSTALL_SYSCONFDIR=$(__host_sysconfdir) \
		-DKDE_INSTALL_LOCALSTATEDIR=$(__host_localstatedir) \
		-DECM_MKSPECS_INSTALL_DIR=${QT5_MKSPECSDIR}/modules \
		"${@}"
}

#****C* kf5.cygclass/kf5_compile
#  SYNOPSIS
#  kf5_compile [OPTIONS]
#  DESCRIPTION
#  Runs kf5_cmake, followed by cygmake or cygninja to compile.  Options, in the
#  form of -DVARIABLE=VALUE, are passed on to kf5_cmake.
#****
kf5_compile() {
	: ${CYGCMAKE_GENERATOR=Ninja}
	kf5_cmake "${@}"
	if [ -f build.ninja ]
	then
		cygninja
	else
		cygmake
	fi
}

#****I* kf5.cygclass/kf5_install
#  SYNOPSIS
#  kf5_install [OPTIONS]
#  DESCRIPTION
#  Installs a KF5 package with cyginstall into $D, with the following addition:
#  * Import libraries for plugins and libkdeinit5_* libraries are removed.
#  Options, if any, are passed on to cyginstall.
#****
kf5_install() {
	if [ -f build.ninja ]
	then
		ninja_install
	else
		cyginstall "${@}"
	fi

	rm -f ${D}/usr/lib/libkdeinit5_*

	# QML plugins are built as SHARED, *not* MODULE because ECM sets
	# CMAKE_SHARED_MODULE_PREFIX to empty (for full-fledged KDE plugins).
	# QML/Quick plugins still need the prefix however, but that results
	# in the installation of import libraries which we don't need.
	if [ -d ${D}${QT5_QMLDIR} ]
	then
		find ${D}${QT5_QMLDIR} -type f -name '*.dll.a' -delete
	fi
}

#****o* kf5.cygclass/src_compile (kf5)
#  DEFINITION
src_compile() {
	cd ${B}
	: ${CYGCMAKE_GENERATOR=Ninja}
	kf5_cmake
	if [ -f build.ninja ]
	then
		cygninja
	else
		cygmake
	fi
}
#****

#****o* kf5.cygclass/src_install (kf5)
#  DEFINITION
src_install() {
	cd ${B}
	kf5_install
}
#****

readonly -f kf5_cmake kf5_compile kf5_install
