diff options
author | Alexandru Croitor <alexandru.croitor@qt.io> | 2020-04-02 10:33:04 +0200 |
---|---|---|
committer | Alexandru Croitor <alexandru.croitor@qt.io> | 2020-04-03 16:05:19 +0200 |
commit | d2931a2626eb67b8b39e25fc62501c01901ab1d3 (patch) | |
tree | f2245dbe7e98d0b56ca816a5a845938234b500fd /cmake | |
parent | de78425ca0c508eb02aaac794ecbcda5ff09a5eb (diff) |
CMake: Handle standalone config.tests in configure libraries section
Some library entries in configure.json have a test entry.
An example is assimp in qtquick3d.
qmake tries to find the library via the sources section, and then tries
to compile the test found in config.tests/assimp/assimp.pro while
automagically passing it the include and link flags it found for assimp.
We didn't handle that in CMake, and now we kind of do.
configurejson2cmake will now create a corresponding
qt_config_compile_test call where it will pass a list of packages and
libraries to find and link against.
pro2cmake will in turn generate new code for the standalone
config.test project. This code will iterate over packages that need to
be found (like WrapAssimp) and then link against a list of passed-in
targets.
In this way the config.test/assimp/main.cpp file can successfully
use assimp code (due to propagated include headers).
qt_config_compile_test is augmented to take a new PACKAGES argument,
with an example as follows
PACKAGES PACKAGE Foo 6 COMPONENTS Bar
PACKAGE Baz REQUIRED
The arguments will be parsed and passed to the try_compile project,
to call find_package() on them.
We also need to pass the C/C++ standard values to the try_compile
project, as well as other try_compile specific flags, like the
toolchain, as given by qt_get_platform_try_compile_vars().
Change-Id: I4a3f76c75309c70c78e580b80114b33870b2cf79
Reviewed-by: Leander Beernaert <leander.beernaert@qt.io>
Reviewed-by: Alexandru Croitor <alexandru.croitor@qt.io>
Diffstat (limited to 'cmake')
-rw-r--r-- | cmake/QtFeature.cmake | 70 |
1 files changed, 68 insertions, 2 deletions
diff --git a/cmake/QtFeature.cmake b/cmake/QtFeature.cmake index b957253fea..9e88ceac91 100644 --- a/cmake/QtFeature.cmake +++ b/cmake/QtFeature.cmake @@ -641,12 +641,74 @@ function(qt_config_compile_test name) endif() cmake_parse_arguments(arg "" "LABEL;PROJECT_PATH;C_STANDARD;CXX_STANDARD" - "COMPILE_OPTIONS;LIBRARIES;CODE" ${ARGN}) + "COMPILE_OPTIONS;LIBRARIES;CODE;PACKAGES" ${ARGN}) if(arg_PROJECT_PATH) message(STATUS "Performing Test ${arg_LABEL}") + + set(flags "") + qt_get_platform_try_compile_vars(platform_try_compile_vars) + list(APPEND flags ${platform_try_compile_vars}) + + # If the repo has its own cmake modules, include those in the module path, so that various + # find_package calls work. + if(EXISTS "${PROJECT_SOURCE_DIR}/cmake") + list(APPEND flags "-DCMAKE_MODULE_PATH:STRING=${PROJECT_SOURCE_DIR}/cmake") + endif() + + # Pass which packages need to be found. + # Very scary / ugly escaping incoming. + if(arg_PACKAGES) + set(packages_list "") + + # Parse the package names, version, etc. An example would be: + # PACKAGE Foo 6 REQUIRED + # PACKAGE Bar 2 COMPONENTS Baz + foreach(p ${arg_PACKAGES}) + if(p STREQUAL PACKAGE) + if(package_entry) + # Use 6 backslashes + ; which will be collapsed when doing variable + # expansion at multiple stages. + list(JOIN package_entry "\\\\\\;" package_entry_string) + list(APPEND packages_list "${package_entry_string}") + endif() + + set(package_entry "") + else() + list(APPEND package_entry "${p}") + endif() + endforeach() + # Parse final entry. + if(package_entry) + list(JOIN package_entry "\\\\\\;" package_entry_string) + list(APPEND packages_list "${package_entry_string}") + endif() + + # Before the join, packages_list has 3 backslashes + ; for each package part + # (name, component) if you display them. + # After the join, packages_list has 2 backslashes + ; for each package part, and a + # '\;' to separate package entries. + list(JOIN packages_list "\;" packages_list) + + # Finally when appending the joined string to the flags, the flags are separated by + # ';', the package entries by '\;', and the packages parts of an entry by '\\;'. + # Example: + # WrapFoo\\;6\\;COMPONENTS\\;bar\;WrapBaz\\;5 + list(APPEND flags "-DQT_CONFIG_COMPILE_TEST_PACKAGES:STRING=${packages_list}") + + # Inside the project, the value of QT_CONFIG_COMPILE_TEST_PACKAGES is used in a foreach + # loop that calls find_package() for each package entry, and thus the variable expansion + # ends up calling something like find_package(WrapFoo;6;COMPONENTS;bar) aka + # find_package(WrapFoo 6 COMPONENTS bar). + endif() + + # Pass which libraries need to be linked against. + if(arg_LIBRARIES) + list(APPEND flags "-DQT_CONFIG_COMPILE_TEST_LIBRARIES:STRING=${arg_LIBRARIES}") + endif() + try_compile(HAVE_${name} "${CMAKE_BINARY_DIR}/config.tests/${name}" "${arg_PROJECT_PATH}" - "${name}") + "${name}" CMAKE_FLAGS ${flags}) if(${HAVE_${name}}) set(status_label "Success") @@ -721,6 +783,10 @@ function(qt_get_platform_try_compile_vars out_var) list(APPEND flags "VCPKG_CHAINLOAD_TOOLCHAIN_FILE") endif() + # Pass language standard flags. + list(APPEND flags "CMAKE_C_STANDARD") + list(APPEND flags "CMAKE_CXX_STANDARD") + # Assemble the list with regular options. set(flags_cmd_line "") foreach(flag ${flags}) |