CMake: Handle conditions in third party find modules correctly

Previously the FindWrap modules checked for hardcoded features
when deciding whether to use a bundled library or not. This proved
not to work correctly because features were not available when
the find modules were processed.

Introduce a new CMake API call that needs to be manually called
after an add_subdirectory call which declares a bundled library.

The call will check for the existence of the bundled target, and will
then set a cache variable QT_USE_BUNDLED_Bundled<TargetName>.

The same variable is written into a FindWrapFooConfigExtra.cmake file
which will be loaded by the appropriate FindWrap module. The module
can that use that variable to decided whether to link against the
bundled library or the system library.

Change-Id: I75e9a4f4e14d88d4490916a79ad12f1ce57891e0
Reviewed-by: Simon Hausmann <simon.hausmann@qt.io>
This commit is contained in:
Alexandru Croitor 2020-03-05 13:15:34 +01:00
parent a2305f9430
commit 4f7cc72298
9 changed files with 49 additions and 37 deletions

View File

@ -1,17 +1,11 @@
include(QtFindWrapHelper NO_POLICY_SCOPE)
set(_qt_wrap_use_bundled FALSE)
if(QT_FEATURE_freetype AND NOT QT_FEATURE_system_freetype)
set(_qt_wrap_use_bundled TRUE)
endif()
qt_find_package_system_or_bundled(wrap_freetype
FRIENDLY_PACKAGE_NAME "Freetype"
WRAP_PACKAGE_TARGET "WrapFreetype::WrapFreetype"
WRAP_PACKAGE_FOUND_VAR_NAME "WrapFreetype_FOUND"
BUNDLED_PACKAGE_NAME "Qt6BundledFreetype"
BUNDLED_PACKAGE_TARGET "Qt6::BundledFreetype"
BUNDLED_PACKAGE_NAME "BundledFreetype"
BUNDLED_PACKAGE_TARGET "BundledFreetype"
SYSTEM_PACKAGE_NAME "WrapSystemFreetype"
SYSTEM_PACKAGE_TARGET "WrapSystemFreetype::WrapSystemFreetype"
USE_BUNDLED_PACKAGE "${_qt_wrap_use_bundled}"
)

View File

@ -1,17 +1,11 @@
include(QtFindWrapHelper NO_POLICY_SCOPE)
set(_qt_wrap_use_bundled FALSE)
if(QT_FEATURE_harfbuzz AND NOT QT_FEATURE_system_harfbuzz)
set(_qt_wrap_use_bundled TRUE)
endif()
qt_find_package_system_or_bundled(wrap_harfbuzz
FRIENDLY_PACKAGE_NAME "Harfbuzz"
WRAP_PACKAGE_TARGET "WrapHarfbuzz::WrapHarfbuzz"
WRAP_PACKAGE_FOUND_VAR_NAME "WrapHarfbuzz_FOUND"
BUNDLED_PACKAGE_NAME "Qt6BundledHarfbuzz"
BUNDLED_PACKAGE_TARGET "Qt6::BundledHarfbuzz"
BUNDLED_PACKAGE_NAME "BundledHarfbuzz"
BUNDLED_PACKAGE_TARGET "BundledHarfbuzz"
SYSTEM_PACKAGE_NAME "WrapSystemHarfbuzz"
SYSTEM_PACKAGE_TARGET "WrapSystemHarfbuzz::WrapSystemHarfbuzz"
USE_BUNDLED_PACKAGE "${_qt_wrap_use_bundled}"
)

View File

@ -1,17 +1,11 @@
include(QtFindWrapHelper NO_POLICY_SCOPE)
set(_qt_wrap_use_bundled FALSE)
if(QT_FEATURE_pcre2 AND NOT QT_FEATURE_system_pcre2)
set(_qt_wrap_use_bundled TRUE)
endif()
qt_find_package_system_or_bundled(wrap_pcre2
FRIENDLY_PACKAGE_NAME "PCRE2"
WRAP_PACKAGE_TARGET "WrapPCRE2::WrapPCRE2"
WRAP_PACKAGE_FOUND_VAR_NAME "WrapPCRE2_FOUND"
BUNDLED_PACKAGE_NAME "Qt6BundledPcre2"
BUNDLED_PACKAGE_TARGET "Qt6::BundledPcre2"
BUNDLED_PACKAGE_NAME "BundledPcre2"
BUNDLED_PACKAGE_TARGET "BundledPcre2"
SYSTEM_PACKAGE_NAME "WrapSystemPCRE2"
SYSTEM_PACKAGE_TARGET "WrapSystemPCRE2::WrapSystemPCRE2"
USE_BUNDLED_PACKAGE "${_qt_wrap_use_bundled}"
)

View File

@ -1,17 +1,11 @@
include(QtFindWrapHelper NO_POLICY_SCOPE)
set(_qt_wrap_use_bundled FALSE)
if(QT_FEATURE_png AND NOT QT_FEATURE_system_png)
set(_qt_wrap_use_bundled TRUE)
endif()
qt_find_package_system_or_bundled(wrap_png
FRIENDLY_PACKAGE_NAME "PNG"
WRAP_PACKAGE_TARGET "WrapPNG::WrapPNG"
WRAP_PACKAGE_FOUND_VAR_NAME "WrapPNG_FOUND"
BUNDLED_PACKAGE_NAME "Qt6BundledLibpng"
BUNDLED_PACKAGE_TARGET "Qt6::BundledLibpng"
BUNDLED_PACKAGE_NAME "BundledLibpng"
BUNDLED_PACKAGE_TARGET "BundledLibpng"
SYSTEM_PACKAGE_NAME "WrapSystemPNG"
SYSTEM_PACKAGE_TARGET "WrapSystemPNG::WrapSystemPNG"
USE_BUNDLED_PACKAGE "${_qt_wrap_use_bundled}"
)

View File

@ -230,6 +230,7 @@ qt_copy_or_install(FILES
cmake/QtCompilerOptimization.cmake
cmake/QtFeature.cmake
cmake/QtFindWrapHelper.cmake
cmake/QtFindWrapConfigExtra.cmake.in
cmake/QtPlatformSupport.cmake
cmake/QtPlatformAndroid.cmake
cmake/QtPostProcess.cmake

View File

@ -3163,6 +3163,28 @@ function(qt_add_3rdparty_library target)
endif()
endfunction()
function(qt_install_3rdparty_library_wrap_config_extra_file target)
if(TARGET "${target}")
set(use_bundled "ON")
else()
set(use_bundled "OFF")
endif()
set(QT_USE_BUNDLED_${target} "${use_bundled}" CACHE BOOL "" FORCE)
set(extra_cmake_code "set(QT_USE_BUNDLED_${target} ${use_bundled} CACHE BOOL \"\" FORCE)")
configure_file(
"${QT_CMAKE_DIR}/QtFindWrapConfigExtra.cmake.in"
"${QT_CONFIG_BUILD_DIR}/${INSTALL_CMAKE_NAMESPACE}/FindWrap${target}ConfigExtra.cmake"
@ONLY
)
qt_install(FILES
"${QT_CONFIG_BUILD_DIR}/${INSTALL_CMAKE_NAMESPACE}/FindWrap${target}ConfigExtra.cmake"
DESTINATION "${QT_CONFIG_INSTALL_DIR}/${INSTALL_CMAKE_NAMESPACE}"
COMPONENT Devel
)
endfunction()
function(qt_get_tool_cmake_configuration out_var)
qt_get_main_cmake_configuration("${out_var}")
string(TOUPPER "${${out_var}}" upper_config)

View File

@ -0,0 +1 @@
@extra_cmake_code@

View File

@ -19,7 +19,6 @@ macro(qt_find_package_system_or_bundled _unique_prefix)
BUNDLED_PACKAGE_TARGET
SYSTEM_PACKAGE_NAME
SYSTEM_PACKAGE_TARGET
USE_BUNDLED_PACKAGE
)
set(_multioptions "")
@ -36,11 +35,20 @@ macro(qt_find_package_system_or_bundled _unique_prefix)
set(${_qfwrap_${_unique_prefix}_WRAP_PACKAGE_FOUND_VAR_NAME} OFF)
if(_qfwrap_${_unique_prefix}_USE_BUNDLED_PACKAGE)
include("FindWrap${_qfwrap_${_unique_prefix}_BUNDLED_PACKAGE_TARGET}ConfigExtra" OPTIONAL)
if(NOT DEFINED "QT_USE_BUNDLED_${_qfwrap_${_unique_prefix}_BUNDLED_PACKAGE_TARGET}")
message(FATAL_ERROR
"Can't find cache variable "
"QT_USE_BUNDLED_${_qfwrap_${_unique_prefix}_BUNDLED_PACKAGE_TARGET} "
"to decide whether to use bundled or system library.")
endif()
if("${QT_USE_BUNDLED_${_qfwrap_${_unique_prefix}_BUNDLED_PACKAGE_TARGET}}")
set(${_unique_prefix}_qt_package_name_to_use
"${_qfwrap_${_unique_prefix}_BUNDLED_PACKAGE_NAME}")
"Qt6${_qfwrap_${_unique_prefix}_BUNDLED_PACKAGE_NAME}")
set(${_unique_prefix}_qt_package_target_to_use
"${_qfwrap_${_unique_prefix}_BUNDLED_PACKAGE_TARGET}")
"Qt6::${_qfwrap_${_unique_prefix}_BUNDLED_PACKAGE_TARGET}")
set(${_unique_prefix}_qt_package_success_message
"Using Qt bundled ${_qfwrap_${_unique_prefix}_FRIENDLY_PACKAGE_NAME}.")
set(${_unique_prefix}_qt_package_type "bundled")
@ -69,6 +77,6 @@ macro(qt_find_package_system_or_bundled _unique_prefix)
INTERFACE_QT_3RD_PARTY_PACKAGE_TYPE
"${${_unique_prefix}_qt_package_type}")
elseif(${_unique_prefix}_qt_package_type STREQUAL "bundled")
message(FATAL_ERROR "Can't find ${_qfwrap_${_unique_prefix}_BUNDLED_PACKAGE_TARGET}.")
message(FATAL_ERROR "Can't find ${${_unique_prefix}_qt_package_target_to_use}.")
endif()
endmacro()

View File

@ -6,18 +6,22 @@ add_subdirectory(double-conversion)
if(QT_FEATURE_png AND NOT QT_FEATURE_system_png)
add_subdirectory(libpng)
endif()
qt_install_3rdparty_library_wrap_config_extra_file(BundledLibpng)
if(QT_FEATURE_freetype AND NOT QT_FEATURE_system_freetype)
add_subdirectory(freetype)
endif()
qt_install_3rdparty_library_wrap_config_extra_file(BundledFreetype)
if(QT_FEATURE_harfbuzz AND NOT QT_FEATURE_system_harfbuzz)
add_subdirectory(harfbuzz-ng)
endif()
qt_install_3rdparty_library_wrap_config_extra_file(BundledHarfbuzz)
if(QT_FEATURE_regularexpression AND NOT QT_FEATURE_system_pcre2)
add_subdirectory(pcre2)
endif()
qt_install_3rdparty_library_wrap_config_extra_file(BundledPcre2)
if (ANDROID)
add_subdirectory(gradle)