cmake: Make find_package(Qt5 ...) work properly

Introduce a new cached variable INSTALL_CMAKE_NAMESPACE for defining the
prefix used for CMake config files (c.f. "${PREFIX}Core/${PREFIX}CoreConfig.cmake")

Also make sure to `find_dependency(...)` the required packages inside
the individual CMake config files. I.e. in Qt5WidgetsConfig.cmake,
search for Qt5Core, etc. pp..

Change-Id: Idc027925fe9d5323091c4853803ad5ce44b1afc6
Reviewed-by: Jean-Michaël Celerier <jean-michael.celerier@kdab.com>
Reviewed-by: Tobias Hunger <tobias.hunger@qt.io>
This commit is contained in:
Kevin Funk 2019-02-11 11:34:35 +01:00
parent c5697c8597
commit c5e2838b64
4 changed files with 72 additions and 45 deletions

View File

@ -1,34 +1,29 @@
# Where QtBuild.cmake can find QtModuleConfig.cmake.in
set(Qt${PROJECT_VERSION_MAJOR}_DIR "${PROJECT_SOURCE_DIR}/cmake")
## QtPlatform Target:
set(name "Qt")
add_library("${name}" INTERFACE)
add_library("Qt::Platform" ALIAS "${name}")
target_include_directories("${name}"
add_library(Platform INTERFACE)
add_library(Qt::Platform ALIAS Platform)
target_include_directories(Platform
INTERFACE
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/${QT_PLATFORM_DEFINITION_DIR}>
$<BUILD_INTERFACE:${PROJECT_BINARY_DIR}/include>
$<INSTALL_INTERFACE:${INSTALL_DATADIR}/${QT_PLATFORM_DEFINITION_DIR}>
$<INSTALL_INTERFACE:${INSTALL_INCLUDEDIR}>
)
target_compile_definitions("${name}" INTERFACE ${QT_PLATFORM_DEFINITIONS})
set(config_install_dir "${INSTALL_LIBDIR}/cmake/${name}${PROJECT_VERSION_MAJOR}")
target_compile_definitions(Platform INTERFACE ${QT_PLATFORM_DEFINITIONS})
set(config_install_dir "${INSTALL_LIBDIR}/cmake/${INSTALL_CMAKE_NAMESPACE}")
configure_package_config_file(
"${PROJECT_SOURCE_DIR}/cmake/QtConfig.cmake.in"
"${CMAKE_CURRENT_BINARY_DIR}/Qt${PROJECT_VERSION_MAJOR}Config.cmake"
"${CMAKE_CURRENT_BINARY_DIR}/${INSTALL_CMAKE_NAMESPACE}Config.cmake"
INSTALL_DESTINATION "${config_install_dir}"
)
write_basic_package_version_file(
${CMAKE_CURRENT_BINARY_DIR}/Qt${PROJECT_VERSION_MAJOR}ConfigVersion.cmake
${CMAKE_CURRENT_BINARY_DIR}/${INSTALL_CMAKE_NAMESPACE}ConfigVersion.cmake
VERSION ${PROJECT_VERSION}
COMPATIBILITY AnyNewerVersion
)
install(FILES
"${CMAKE_CURRENT_BINARY_DIR}/Qt${PROJECT_VERSION_MAJOR}Config.cmake"
"${CMAKE_CURRENT_BINARY_DIR}/Qt${PROJECT_VERSION_MAJOR}ConfigVersion.cmake"
"${CMAKE_CURRENT_BINARY_DIR}/${INSTALL_CMAKE_NAMESPACE}Config.cmake"
"${CMAKE_CURRENT_BINARY_DIR}/${INSTALL_CMAKE_NAMESPACE}ConfigVersion.cmake"
DESTINATION "${config_install_dir}"
COMPONENT Devel
)
@ -63,8 +58,8 @@ target_include_directories(GlobalConfigPrivate INTERFACE
)
add_library(Qt::GlobalConfigPrivate ALIAS GlobalConfigPrivate)
install(TARGETS "${name}" GlobalConfig GlobalConfigPrivate EXPORT "${name}${PROJECT_VERSION_MAJOR}Targets")
install(EXPORT "${name}${PROJECT_VERSION_MAJOR}Targets" NAMESPACE Qt:: DESTINATION "${config_install_dir}")
install(TARGETS Platform GlobalConfig GlobalConfigPrivate EXPORT "${INSTALL_CMAKE_NAMESPACE}Targets")
install(EXPORT "${INSTALL_CMAKE_NAMESPACE}Targets" NAMESPACE Qt:: DESTINATION "${config_install_dir}")
## Install some QtBase specific CMake files:
install(FILES

View File

@ -36,6 +36,8 @@ set(INSTALL_SYSCONFDIR "etc/xdg" CACHE PATH
set(INSTALL_EXAMPLESDIR "examples" CACHE PATH "Examples [PREFIX/examples]")
set(INSTALL_TESTSDIR "tests" CACHE PATH "Tests [PREFIX/tests]")
set(INSTALL_CMAKE_NAMESPACE "Qt${PROJECT_VERSION_MAJOR}" CACHE STRING "CMake namespace [Qt${PROJECT_VERSION_MAJOR}]")
# Platform define path, etc.
if(WIN32)
set(QT_DEFAULT_PLATFORM_DEFINITIONS UNICODE _UNICODE WIN32 _ENABLE_EXTENDED_ALIGNED_STORAGE)
@ -520,28 +522,37 @@ function(add_qt_module target)
PRIVATE_HEADER DESTINATION ${INSTALL_INCLUDEDIR}/${module}/${PROJECT_VERSION}/${module}/private
)
set(config_install_dir "${INSTALL_LIBDIR}/cmake/${module_versioned}")
set(config_install_dir "${INSTALL_LIBDIR}/cmake/${INSTALL_CMAKE_NAMESPACE}${target}")
install(EXPORT "${module_versioned}Targets" NAMESPACE Qt:: DESTINATION ${config_install_dir})
set(target_deps)
foreach(lib ${arg_PUBLIC_LIBRARIES})
if ("${lib}" MATCHES "^Qt::(Platform|GlobalConfig)")
list(APPEND target_deps "Qt5\;${PROJECT_VERSION}")
elseif ("${lib}" MATCHES "^Qt::")
string(REGEX REPLACE "^Qt::" "${INSTALL_CMAKE_NAMESPACE}" dep "${lib}")
list(APPEND target_deps "${dep}\;${PROJECT_VERSION}")
endif()
endforeach()
configure_package_config_file(
"${Qt${PROJECT_VERSION_MAJOR}_DIR}/QtModuleConfig.cmake.in"
"${CMAKE_CURRENT_BINARY_DIR}/${module_versioned}Config.cmake"
"${PROJECT_SOURCE_DIR}/cmake/QtModuleConfig.cmake.in"
"${CMAKE_CURRENT_BINARY_DIR}/${INSTALL_CMAKE_NAMESPACE}${target}Config.cmake"
INSTALL_DESTINATION "${config_install_dir}"
)
write_basic_package_version_file(
${CMAKE_CURRENT_BINARY_DIR}/${module_versioned}ConfigVersion.cmake
${CMAKE_CURRENT_BINARY_DIR}/${INSTALL_CMAKE_NAMESPACE}${target}ConfigVersion.cmake
VERSION ${PROJECT_VERSION}
COMPATIBILITY AnyNewerVersion
)
set(extra_cmake_files)
if (EXISTS "${CMAKE_CURRENT_LIST_DIR}/${module_versioned}Macros.cmake")
list(APPEND extra_cmake_files "${CMAKE_CURRENT_LIST_DIR}/${module_versioned}Macros.cmake")
if (EXISTS "${CMAKE_CURRENT_LIST_DIR}/${INSTALL_CMAKE_NAMESPACE}${target}Macros.cmake")
list(APPEND extra_cmake_files "${CMAKE_CURRENT_LIST_DIR}/${INSTALL_CMAKE_NAMESPACE}${target}Macros.cmake")
endif()
install(FILES
"${CMAKE_CURRENT_BINARY_DIR}/${module_versioned}Config.cmake"
"${CMAKE_CURRENT_BINARY_DIR}/${module_versioned}ConfigVersion.cmake"
"${CMAKE_CURRENT_BINARY_DIR}/${INSTALL_CMAKE_NAMESPACE}${target}Config.cmake"
"${CMAKE_CURRENT_BINARY_DIR}/${INSTALL_CMAKE_NAMESPACE}${target}ConfigVersion.cmake"
${extra_cmake_files}
DESTINATION "${config_install_dir}"
COMPONENT Devel

View File

@ -5,27 +5,27 @@ if (CMAKE_VERSION VERSION_LESS 3.1.0)
message(FATAL_ERROR "Qt requires at least CMake version 3.1.0")
endif()
get_filename_component(_qt5_install_prefix "${CMAKE_CURRENT_LIST_DIR}/.." ABSOLUTE)
get_filename_component(_qt_cmake_dir "${CMAKE_CURRENT_LIST_DIR}/.." ABSOLUTE)
include("${CMAKE_CURRENT_LIST_DIR}/Qt5Targets.cmake")
include("${CMAKE_CURRENT_LIST_DIR}/Qt5ToolsTargets.cmake")
include("${CMAKE_CURRENT_LIST_DIR}/@INSTALL_CMAKE_NAMESPACE@Targets.cmake")
include("${CMAKE_CURRENT_LIST_DIR}/@INSTALL_CMAKE_NAMESPACE@ToolsTargets.cmake")
# if (NOT Qt_FIND_COMPONENTS)
# set(Qt_NOT_FOUND_MESSAGE "The Qt package requires at least one component")
# set(Qt_FOUND False)
# if (NOT @INSTALL_CMAKE_NAMESPACE@_FIND_COMPONENTS)
# set(@INSTALL_CMAKE_NAMESPACE@_NOT_FOUND_MESSAGE "The Qt package requires at least one component")
# set(@INSTALL_CMAKE_NAMESPACE@_FOUND False)
# return()
# endif()
foreach(module ${Qt_FIND_COMPONENTS})
find_package(Qt${module}
${_Qt_FIND_PARTS_QUIET}
${_Qt_FIND_PARTS_REQUIRED}
PATHS ${_qt_install_prefix} NO_DEFAULT_PATH
foreach(module ${@INSTALL_CMAKE_NAMESPACE@_FIND_COMPONENTS})
find_package(@INSTALL_CMAKE_NAMESPACE@${module}
${_@INSTALL_CMAKE_NAMESPACE@_FIND_PARTS_QUIET}
${_@INSTALL_CMAKE_NAMESPACE@_FIND_PARTS_REQUIRED}
PATHS ${_qt_cmake_dir} NO_DEFAULT_PATH
)
if (NOT Qt${module}_FOUND)
if (NOT @INSTALL_CMAKE_NAMESPACE@${module}_FOUND)
string(CONFIGURE ${_qt5_module_location_template} _expected_module_location @ONLY)
if (Qt_FIND_REQUIRED_${module})
if (@INSTALL_CMAKE_NAMESPACE@_FIND_REQUIRED_${module})
set(_Qt_NOTFOUND_MESSAGE "${_Qt_NOTFOUND_MESSAGE}Failed to find Qt component \"${module}\" config file at \"${_expected_module_location}\"\n")
elseif(NOT Qt_FIND_QUIETLY)
message(WARNING "Failed to find Qt component \"${module}\" config file at \"${_expected_module_location}\"")
@ -38,6 +38,6 @@ endforeach()
set(QT_BUILD_SHARED_LIBS @BUILD_SHARED_LIBS@)
if (_Qt_NOTFOUND_MESSAGE)
set(Qt_NOT_FOUND_MESSAGE "${_Qt_NOTFOUND_MESSAGE}")
set(Qt_FOUND False)
set(@INSTALL_CMAKE_NAMESPACE@_NOT_FOUND_MESSAGE "${_Qt_NOTFOUND_MESSAGE}")
set(@INSTALL_CMAKE_NAMESPACE@_FOUND False)
endif()

View File

@ -1,13 +1,34 @@
@PACKAGE_INIT@
find_package(Qt@PROJECT_VERSION_MAJOR@ QUIET)
include(CMakeFindDependencyMacro)
include("${CMAKE_CURRENT_LIST_DIR}/@module_versioned@Targets.cmake")
get_filename_component(_import_prefix "${CMAKE_CURRENT_LIST_FILE}" PATH)
get_filename_component(_import_prefix "${_import_prefix}" REALPATH)
if (EXISTS "${CMAKE_CURRENT_LIST_DIR}/@module_versioned@Macros.cmake")
include("${CMAKE_CURRENT_LIST_DIR}/@module_versioned@Macros.cmake")
# note: target_deps example: "Qt5Core\;5.12.0;Qt5Gui\;5.12.0"
set(_target_deps "@target_deps@")
foreach(_target_dep ${_target_deps})
list(GET _target_dep 0 pkg)
list(GET _target_dep 1 version)
if (NOT ${pkg}_FOUND)
find_dependency(${pkg} ${version}
PATHS "${CMAKE_CURRENT_LIST_DIR}/.." NO_DEFAULT_PATH
)
endif()
if (NOT ${pkg}_FOUND)
set(@target@_FOUND FALSE)
return()
endif()
endforeach()
include("${CMAKE_CURRENT_LIST_DIR}/@INSTALL_CMAKE_NAMESPACE@@target@Targets.cmake")
if (EXISTS "${CMAKE_CURRENT_LIST_DIR}/@INSTALL_CMAKE_NAMESPACE@@target@Macros.cmake")
include("${CMAKE_CURRENT_LIST_DIR}/@INSTALL_CMAKE_NAMESPACE@@target@Macros.cmake")
endif()
include QtFeature
include(${_import_prefix}/../@INSTALL_CMAKE_NAMESPACE@/QtFeature.cmake)
qt_make_features_available(Qt::@module@)
qt_make_features_available(Qt::@target@)