diff --git a/docs/module/ECMInstallQmlModules.rst b/docs/module/ECMInstallQmlModules.rst new file mode 100644 --- /dev/null +++ b/docs/module/ECMInstallQmlModules.rst @@ -0,0 +1 @@ +.. ecm-module:: ../../modules/ECMInstallQmlModules.cmake diff --git a/modules/ECMInstallQmlModules.cmake b/modules/ECMInstallQmlModules.cmake new file mode 100644 --- /dev/null +++ b/modules/ECMInstallQmlModules.cmake @@ -0,0 +1,134 @@ +#.rst: +# ECMInstallQmlModules +# -------------------- +# +# Installs a QML C++ plugin, while generating and installing also a matching +# "qmldir" file. +# +# :: +# +# ecm_install_qmlplugin( +# IDENTIFIER +# DESTINATION +# [DEPENDS [ [...]]] +# [DESIGNERSUPPORTED]) +# +# ``IDENTIFIER`` specifies the module identifier of the module implemented +# by the plugin. +# +# ``DESTINATION`` specifies the directory on disk to which the module plugin +# will be installed, without the module-specific sub-path. +# If using the :kde-module:`KDEInstallDirs` module, one passes +# ``${KDE_INSTALL_QMLDIR}`` as value usually. +# +# ``DEPENDS`` specifies dependencies which should be noted in the "qmldir" +# file. Each ```` argument is a string with both the module identifier +# of the dependency and its version (e.g. ``"bar.example 1.0"``). +# +# ``DESIGNERSUPPORTED`` specifies that the "qmldir" file should get the +# ``designersupported`` flag added. +# +# Example usage: +# +# .. code-block:: cmake +# +# add_library(fooexampleplugin SHARED ${fooexampleplugin_SRCS}) +# +# target_link_libraries(fooexampleplugin +# Qt5::Qml +# ) +# +# ecm_install_qmlplugin(fooexampleplugin +# IDENTIFIER foo.example +# DESTINATION "${KDE_INSTALL_QMLDIR}" +# DEPENDS +# "bar.example 42.0" +# DESIGNERSUPPORTED +# ) +# +# Since 5.59.0. + + +#============================================================================= +# Copyright 2019 Friedrich W. H. Kossebau +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# +# 1. Redistributions of source code must retain the copyright +# notice, this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# 3. The name of the author may not be used to endorse or promote products +# derived from this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR +# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES +# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. +# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, +# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF +# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + +function(ecm_install_qmlplugin _target) + set(options + DESIGNERSUPPORTED + ) + set(oneValueArgs + DESTINATION + IDENTIFIER + ) + set(multiValueArgs + DEPENDS + ) + cmake_parse_arguments(ARGS "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}) + + # check args + if (NOT TARGET ${_target}) + message(FATAL_ERROR "No known target passed to ecm_install_qmlmodule: ${_target}") + endif() + if(ARGS_UNPARSED_ARGUMENTS) + message(FATAL_ERROR "Unexpected arguments to ecm_install_qmlmodule: ${ARGS_UNPARSED_ARGUMENTS}") + endif() + if(NOT ARGS_DESTINATION) + message(FATAL_ERROR "No DESTINATION argument given to ecm_install_qmlmodule") + endif() + if(NOT ARGS_IDENTIFIER) + message(FATAL_ERROR "No IDENTIFIER argument given to ecm_install_qmlmodule") + endif() + + # generate qmldir file + set(qmldir_FILE "${CMAKE_CURRENT_BINARY_DIR}/qmldir") # TODO: support multiple in same dir? + file(WRITE ${qmldir_FILE} "module ${ARGS_IDENTIFIER}\n") + + # TODO: is there a way to directly get the effective name? + get_target_property(_plugin_name ${_target} LIBRARY_OUTPUT_NAME) + if(NOT _plugin_name) + get_target_property(_plugin_name ${_target} OUTPUT_NAME) + endif() + if(NOT _plugin_name) + set(_plugin_name ${_target}) + endif() + file(APPEND ${qmldir_FILE} "plugin ${_plugin_name}\n") + + foreach(_depends ${ARGS_DEPENDS}) + file(APPEND ${qmldir_FILE} "depends ${_depends}\n") + endforeach() + + if(ARGS_DESIGNERSUPPORTED) + file(APPEND ${qmldir_FILE} "designersupported\n") + endif() + + # install + string(REPLACE "." "/" _module_path ${ARGS_IDENTIFIER}) + set(_install_dir "${ARGS_DESTINATION}/${_module_path}") + + install(TARGETS ${_target} DESTINATION ${_install_dir}) + install(FILES ${qmldir_FILE} DESTINATION ${_install_dir}) +endfunction()