From 066e8a4557e7cbe98403bbf97b957f5f993f89c3 Mon Sep 17 00:00:00 2001 From: Alexandru Croitor Date: Thu, 9 Apr 2020 09:59:35 +0200 Subject: [PATCH] CMake: Handle missing packages in project compile tests If a find_package() in a try_compile project doesn't find a package, and we then link against a non-existent target, the configuration failure of the compile test also fails the configuration of the project. To avoid that, separate library targets from non-targets, and make sure to only link against the targets if they exist. pro2cmake now outputs modified compile test project code which iterates over targets and non-target libraries, and links against them when needed. Change-Id: Ib0f4b5f07af13929c42d01a661df2cabdf9b926b Reviewed-by: Simon Hausmann --- cmake/QtFeature.cmake | 20 +++++++++++++++++++- util/cmake/pro2cmake.py | 8 +++++++- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/cmake/QtFeature.cmake b/cmake/QtFeature.cmake index b2d993647f3..4cf7c0f9466 100644 --- a/cmake/QtFeature.cmake +++ b/cmake/QtFeature.cmake @@ -699,7 +699,25 @@ function(qt_config_compile_test name) # Pass which libraries need to be linked against. if(arg_LIBRARIES) - list(APPEND flags "-DQT_CONFIG_COMPILE_TEST_LIBRARIES:STRING=${arg_LIBRARIES}") + set(link_flags "") + set(library_targets "") + # Separate targets from link flags or paths. This is to prevent configuration failures + # when the targets are not found due to missing packages. + foreach(lib ${arg_LIBRARIES}) + string(FIND "${lib}" "::" is_library_target) + if(is_library_target EQUAL -1) + list(APPEND link_flags "${lib}") + else() + list(APPEND library_targets "${lib}") + endif() + endforeach() + if(link_flags) + list(APPEND flags "-DQT_CONFIG_COMPILE_TEST_LIBRARIES:STRING=${link_flags}") + endif() + if(library_targets) + list(APPEND flags + "-DQT_CONFIG_COMPILE_TEST_LIBRARY_TARGETS:STRING=${library_targets}") + endif() endif() try_compile(HAVE_${name} "${CMAKE_BINARY_DIR}/config.tests/${name}" "${arg_PROJECT_PATH}" diff --git a/util/cmake/pro2cmake.py b/util/cmake/pro2cmake.py index 87e2593d00e..92b7597dcd7 100755 --- a/util/cmake/pro2cmake.py +++ b/util/cmake/pro2cmake.py @@ -3948,7 +3948,13 @@ endforeach() if(QT_CONFIG_COMPILE_TEST_LIBRARIES) link_libraries(${QT_CONFIG_COMPILE_TEST_LIBRARIES}) endif() - +if(QT_CONFIG_COMPILE_TEST_LIBRARY_TARGETS) + foreach(lib ${QT_CONFIG_COMPILE_TEST_LIBRARY_TARGETS}) + if(TARGET ${lib}) + link_libraries(${lib}) + endif() + endforeach() +endif() """ ) cm_fh.write(f"{content}\n")