diff options
author | Alexandru Croitor <alexandru.croitor@qt.io> | 2019-04-24 17:14:25 +0200 |
---|---|---|
committer | Alexandru Croitor <alexandru.croitor@qt.io> | 2019-05-02 07:30:42 +0000 |
commit | 9b0b464e82071338134700edfa190bf998846e4e (patch) | |
tree | 4c0390bb52624fdd063f9495bd939c1c4c7d498f /cmake | |
parent | 42d3b21c92525ea6a430c67e577a5991d679fa1d (diff) |
Write find_dependency() calls in Qt Module config files
This change introduces a new function called qt_find_package()
which can take an extra option called PROVIDED_TARGETS, which
associates targets with the package that defines those targets.
This is done by setting the INTERFACE_QT_PACKAGE_NAME and
INTERFACE_QT_PACKAGE_VERSION properties on the imported targets.
This information allows us to generate appropriate find_dependency()
calls in a module's Config file for third party libraries.
For example when an application links against QtCore, it should also
link against zlib and atomic libraries. In order to do that, the
library locations first have to be found by CMake. This is achieved by
embedding find_dependency(ZLIB) and find_dependency(Atomic) in
Qt5CoreDependencies.cmake which is included by Qt5CoreConfig.cmake.
The latter is picked up when an application project contains
find_package(Qt5Core), and thus all linking dependencies are resolved.
The information 'which package provides which targets' is contained
in the python json2cmake conversion script. The generated output of
the script contains qt_find_package() calls that represent that
information.
The Qt5CoreDependencies.cmake file and which which dependencies it
contains is generated at the QtPostProcess stop.
Note that for non-static Qt builds, we only need to propagate public
3rd party libraries. For static builds, we need all third party
libraries.
In order for the INTERFACE_QT_PACKAGE_NAME property to be read in any
scope, the targets on which the property is set, have to be GLOBAL.
Also for applications and other modules to find all required third
party libraries, we have to install all our custom Find modules, and
make sure they define INTERFACE IMPORTED libraries, and not just
IMPORTED libraries.
Change-Id: I694d6e32d05b96d5e241df0156fc79d0029426aa
Reviewed-by: Tobias Hunger <tobias.hunger@qt.io>
Diffstat (limited to 'cmake')
-rw-r--r-- | cmake/FindPCRE2.cmake | 2 | ||||
-rw-r--r-- | cmake/FindPPS.cmake | 2 | ||||
-rw-r--r-- | cmake/FindSlog2.cmake | 2 | ||||
-rw-r--r-- | cmake/FindWrapDoubleConversion.cmake | 3 | ||||
-rw-r--r-- | cmake/FindWrapOpenGL.cmake | 4 | ||||
-rw-r--r-- | cmake/FindWrapRt.cmake | 2 | ||||
-rw-r--r-- | cmake/FindZSTD.cmake | 1 | ||||
-rw-r--r-- | cmake/QtBaseGlobalTargets.cmake | 9 | ||||
-rw-r--r-- | cmake/QtBuild.cmake | 56 | ||||
-rw-r--r-- | cmake/QtModuleConfig.cmake.in | 5 | ||||
-rw-r--r-- | cmake/QtModuleDependencies.cmake.in | 34 | ||||
-rw-r--r-- | cmake/QtPostProcess.cmake | 53 |
12 files changed, 167 insertions, 6 deletions
diff --git a/cmake/FindPCRE2.cmake b/cmake/FindPCRE2.cmake index 7e45c963d7..3977532124 100644 --- a/cmake/FindPCRE2.cmake +++ b/cmake/FindPCRE2.cmake @@ -5,7 +5,7 @@ find_path(PCRE2_INCLUDE_DIRS pcre2.h) if (PCRE2_LIBRARIES STREQUAL "PCRE2_LIBRARIES-NOTFOUND" OR PCRE2_INCLUDE_DIRS STREQUAL "PCRE2_INCLUDE_DIRS-NOTFOUND") set(PCRE2_FOUND 0) else() - add_library(PCRE2 INTERFACE) + add_library(PCRE2 INTERFACE IMPORTED) target_link_libraries(PCRE2 INTERFACE ${PCRE2_LIBRARIES}) target_include_directories(PCRE2 INTERFACE ${PCRE2_INCLUDE_DIRS}) set(PCRE2_FOUND 1) diff --git a/cmake/FindPPS.cmake b/cmake/FindPPS.cmake index b1e418f227..c3360fa82c 100644 --- a/cmake/FindPPS.cmake +++ b/cmake/FindPPS.cmake @@ -11,7 +11,7 @@ find_package_handle_standard_args(PPS DEFAULT_MSG PPS_INCLUDE_DIR PPS_LIBRARY) mark_as_advanced(PPS_INCLUDE_DIR PPS_LIBRARY) if(PPS_FOUND) - add_library(__PPS IMPORTED) + add_library(__PPS INTERFACE IMPORTED) target_link_libraries(__PPS INTERFACE ${PPS_LIBRARY}) target_include_directories(__PPS INTERFACE ${PPS_INCLUDE_DIR}) diff --git a/cmake/FindSlog2.cmake b/cmake/FindSlog2.cmake index 8ff00e4157..dfb6635a39 100644 --- a/cmake/FindSlog2.cmake +++ b/cmake/FindSlog2.cmake @@ -11,7 +11,7 @@ find_package_handle_standard_args(Slog2 DEFAULT_MSG Slog2_INCLUDE_DIR Slog2_LIBR mark_as_advanced(Slog2_INCLUDE_DIR Slog2_LIBRARY) if(Slog2_FOUND) - add_library(__Slog2 IMPORTED) + add_library(__Slog2 INTERFACE IMPORTED) target_link_libraries(__Slog2 INTERFACE ${Slog2_LIBRARY}) target_include_directories(__Slog2 INTERFACE ${Slog2_INCLUDE_DIR}) diff --git a/cmake/FindWrapDoubleConversion.cmake b/cmake/FindWrapDoubleConversion.cmake index 749965a7b8..cfb70a0981 100644 --- a/cmake/FindWrapDoubleConversion.cmake +++ b/cmake/FindWrapDoubleConversion.cmake @@ -1,9 +1,10 @@ include(CheckCXXSourceCompiles) -add_library(WrapDoubleConversion INTERFACE) +add_library(WrapDoubleConversion INTERFACE IMPORTED) find_package(double-conversion) if (double-conversion_FOUND) + include(FeatureSummary) set_package_properties(double-conversion PROPERTIES TYPE REQUIRED) target_link_libraries(WrapDoubleConversion INTERFACE double-conversion::double-conversion) set(WrapDoubleConversion_FOUND 1) diff --git a/cmake/FindWrapOpenGL.cmake b/cmake/FindWrapOpenGL.cmake index c97ba0e466..b3c60ba953 100644 --- a/cmake/FindWrapOpenGL.cmake +++ b/cmake/FindWrapOpenGL.cmake @@ -6,7 +6,7 @@ if(TARGET WrapOpenGL) return() endif() -add_library(WrapOpenGL INTERFACE) +add_library(WrapOpenGL INTERFACE IMPORTED) if(QT_FEATURE_opengles2) find_package(GLESv2) @@ -16,3 +16,5 @@ else() target_link_libraries(WrapOpenGL INTERFACE OpenGL::GL) endif() set(WrapOpenGL_FOUND ON) + +set_property(TARGET WrapOpenGL PROPERTY IMPORTED_GLOBAL TRUE) diff --git a/cmake/FindWrapRt.cmake b/cmake/FindWrapRt.cmake index dee41e0617..ef5475c53d 100644 --- a/cmake/FindWrapRt.cmake +++ b/cmake/FindWrapRt.cmake @@ -18,7 +18,7 @@ int main(int argc, char *argv[]) { cmake_pop_check_state() -add_library(WrapRt INTERFACE) +add_library(WrapRt INTERFACE IMPORTED) if (LIBRT_FOUND) target_link_libraries(WrapRt INTERFACE "${LIBRT}") endif() diff --git a/cmake/FindZSTD.cmake b/cmake/FindZSTD.cmake index 040e8c1642..4f4590d357 100644 --- a/cmake/FindZSTD.cmake +++ b/cmake/FindZSTD.cmake @@ -43,6 +43,7 @@ endif() mark_as_advanced(ZSTD_INCLUDE_DIRS ZSTD_LIBRARIES) +include(FeatureSummary) set_package_properties(ZSTD PROPERTIES URL "https://github.com/facebook/zstd" DESCRIPTION "ZSTD compression library") diff --git a/cmake/QtBaseGlobalTargets.cmake b/cmake/QtBaseGlobalTargets.cmake index 613dc49242..d50d1e2c2b 100644 --- a/cmake/QtBaseGlobalTargets.cmake +++ b/cmake/QtBaseGlobalTargets.cmake @@ -95,3 +95,12 @@ install(FILES install(DIRECTORY cmake/3rdparty DESTINATION "${config_install_dir}" ) + +# Install our custom Find modules, which will be used by the find_dependency() calls +# inside the generated ModuleDependencies cmake files. +install(DIRECTORY cmake/ + DESTINATION "${config_install_dir}" + FILES_MATCHING PATTERN "Find*.cmake" + PATTERN "tests" EXCLUDE + PATTERN "3rdparty" EXCLUDE +) diff --git a/cmake/QtBuild.cmake b/cmake/QtBuild.cmake index 0057190360..f5775f2b1f 100644 --- a/cmake/QtBuild.cmake +++ b/cmake/QtBuild.cmake @@ -1365,3 +1365,59 @@ endfunction() function(add_qt_docs qdocFile) # TODO endfunction() + +macro(qt_find_package) + # Get the target names we expect to be provided by the package. + cmake_parse_arguments(arg "" "" "PROVIDED_TARGETS;COMPONENTS" ${ARGN}) + + # Get the version if specified. + set(package_version "") + if(${ARGC} GREATER_EQUAL 2) + if(${ARGV1} MATCHES "^[0-9\.]+$") + set(package_version "${ARGV1}") + endif() + endif() + + if(arg_COMPONENTS) + # Re-append components to forward them. + list(APPEND arg_UNPARSED_ARGUMENTS "COMPONENTS;${arg_COMPONENTS}") + endif() + + # Call original function without our custom arguments. + find_package(${arg_UNPARSED_ARGUMENTS}) + + if(${ARGV0}_FOUND AND arg_PROVIDED_TARGETS) + # If package was found, associate each target with its package name. This will be used + # later when creating Config files for Qt libraries, to generate correct find_dependency() + # calls. Also make the provided targets global, so that the properties can be read in + # all scopes. + foreach(qt_find_package_target_name ${arg_PROVIDED_TARGETS}) + if(TARGET ${qt_find_package_target_name}) + set_target_properties(${qt_find_package_target_name} + PROPERTIES INTERFACE_QT_PACKAGE_NAME ${ARGV0}) + if(package_version) + set_target_properties(${qt_find_package_target_name} + PROPERTIES INTERFACE_QT_PACKAGE_VERSION ${ARGV1}) + endif() + + if(arg_COMPONENTS) + set_target_properties(${qt_find_package_target_name} + PROPERTIES + INTERFACE_QT_PACKAGE_COMPONENTS ${arg_COMPONENTS}) + endif() + + get_property(is_global TARGET ${qt_find_package_target_name} PROPERTY + IMPORTED_GLOBAL) + if(NOT is_global) + set_property(TARGET ${qt_find_package_target_name} PROPERTY + IMPORTED_GLOBAL TRUE) + endif() + else() + message(FATAL_ERROR + "Error while trying to mark target '${qt_find_package_target_name}' as part" + " of the ${ARGV0} package. Provided target name does not exist.") + endif() + + endforeach() + endif() +endmacro() diff --git a/cmake/QtModuleConfig.cmake.in b/cmake/QtModuleConfig.cmake.in index e9eec7ae94..9a0f0c368c 100644 --- a/cmake/QtModuleConfig.cmake.in +++ b/cmake/QtModuleConfig.cmake.in @@ -5,6 +5,11 @@ include(CMakeFindDependencyMacro) get_filename_component(_import_prefix "${CMAKE_CURRENT_LIST_FILE}" PATH) get_filename_component(_import_prefix "${_import_prefix}" REALPATH) +# Find required dependencies, if any. +if(EXISTS "${CMAKE_CURRENT_LIST_DIR}/@INSTALL_CMAKE_NAMESPACE@@target@Dependencies.cmake") + include("${CMAKE_CURRENT_LIST_DIR}/@INSTALL_CMAKE_NAMESPACE@@target@Dependencies.cmake") +endif() + # note: target_deps example: "Qt5Core\;5.12.0;Qt5Gui\;5.12.0" set(_target_deps "@target_deps@") foreach(_target_dep ${_target_deps}) diff --git a/cmake/QtModuleDependencies.cmake.in b/cmake/QtModuleDependencies.cmake.in new file mode 100644 index 0000000000..7ff02df894 --- /dev/null +++ b/cmake/QtModuleDependencies.cmake.in @@ -0,0 +1,34 @@ +# Save old module path, and append a new path that points to the copied over Find modules +# so that find_dependency() can find the third party packages. +set(old_CMAKE_MODULE_PATH "${CMAKE_MODULE_PATH}") +list(APPEND CMAKE_MODULE_PATH "${_import_prefix}/../@INSTALL_CMAKE_NAMESPACE@") +list(APPEND CMAKE_MODULE_PATH "${_import_prefix}/../@INSTALL_CMAKE_NAMESPACE@/3rdparty/extra-cmake-modules/find-modules") + +# note: _third_party_deps example: "ICU\\;1.0\\;i18n uc data;ZLIB\\;\\;" +set(_third_party_deps "@third_party_deps@") + +foreach(_target_dep ${_third_party_deps}) + list(GET _target_dep 0 pkg) + list(GET _target_dep 1 version) + list(GET _target_dep 2 components) + set(find_package_args "${pkg}") + if(version) + list(APPEND find_package_args "${version}") + endif() + + if(components) + list(APPEND find_package_args "COMPONENTS" ${components}) + endif() + + if (NOT ${pkg}_FOUND) + find_dependency(${find_package_args}) + endif() + + if (NOT ${pkg}_FOUND) + set(@INSTALL_CMAKE_NAMESPACE@@target@_FOUND FALSE) + return() + endif() +endforeach() + +# Restore old module path. +set(CMAKE_MODULE_PATH "${old_CMAKE_MODULE_PATH}") diff --git a/cmake/QtPostProcess.cmake b/cmake/QtPostProcess.cmake index e63bcc0538..10dd5bfbb1 100644 --- a/cmake/QtPostProcess.cmake +++ b/cmake/QtPostProcess.cmake @@ -16,7 +16,10 @@ function(qt_internal_create_depends_files) message("Generating depends files for ${QT_KNOWN_MODULES}...") foreach (target ${QT_KNOWN_MODULES}) get_target_property(depends "${target}" LINK_LIBRARIES) + get_target_property(public_depends "${target}" INTERFACE_LINK_LIBRARIES) set(qtdeps "") + set(third_party_deps "") + set(third_party_deps_seen "") foreach (dep ${depends}) # Normalize module by stripping leading "Qt::" and trailing "Private" if (dep MATCHES "Qt::(.*)") @@ -32,6 +35,37 @@ function(qt_internal_create_depends_files) endif() endforeach() + # If we are doing a non-static Qt build, we only want to propagate public dependencies. + # If we are doing a static Qt build, we need to propagate all dependencies. + set(depends_var "public_depends") + if(NOT QT_BUILD_SHARED_LIBS) + set(depends_var "depends") + endif() + + foreach(dep ${${depends_var}}) + # Gather third party packages that should be found when using the Qt module. + if(TARGET ${dep}) + list(FIND third_party_deps_seen ${dep} dep_seen) + + get_target_property(package_name ${dep} INTERFACE_QT_PACKAGE_NAME) + if(dep_seen EQUAL -1 AND package_name) + list(APPEND third_party_deps_seen ${dep}) + get_target_property(package_version ${dep} INTERFACE_QT_PACKAGE_VERSION) + if(NOT package_version) + set(package_version "") + endif() + + get_target_property(package_components ${dep} INTERFACE_QT_PACKAGE_COMPONENTS) + if(NOT package_components) + set(package_components "") + endif() + + list(APPEND third_party_deps + "${package_name}\;${package_version}\;${package_components}") + endif() + endif() + endforeach() + if (DEFINED qtdeps) list(REMOVE_DUPLICATES qtdeps) endif() @@ -40,7 +74,26 @@ function(qt_internal_create_depends_files) if (${hasModuleHeaders}) qt_internal_write_depends_file("${target}" ${qtdeps}) endif() + + if(third_party_deps) + # Configure and install dependencies file. + configure_file( + "${QT_CMAKE_DIR}/QtModuleDependencies.cmake.in" + "${CMAKE_CURRENT_BINARY_DIR}/${INSTALL_CMAKE_NAMESPACE}${target}Dependencies.cmake" + @ONLY + ) + + set(config_install_dir "${INSTALL_LIBDIR}/cmake/${INSTALL_CMAKE_NAMESPACE}${target}") + + install(FILES + "${CMAKE_CURRENT_BINARY_DIR}/${INSTALL_CMAKE_NAMESPACE}${target}Dependencies.cmake" + DESTINATION "${config_install_dir}" + COMPONENT Devel + ) + endif() endforeach() + + endfunction() qt_internal_create_depends_files() |