diff --git a/tests/auto/cmake/RunCMake/CMakeLists.txt b/tests/auto/cmake/RunCMake/CMakeLists.txt index e6fc346707c..2ed86266d89 100644 --- a/tests/auto/cmake/RunCMake/CMakeLists.txt +++ b/tests/auto/cmake/RunCMake/CMakeLists.txt @@ -1 +1,3 @@ # Add RunCMake tests using `add_RunCMake_test()` + +add_RunCMake_test(QtFlagHandlingHelpers "-DQt6_DIR=${Qt6_DIR}") diff --git a/tests/auto/cmake/RunCMake/QtFlagHandlingHelpers/CMakeLists.txt b/tests/auto/cmake/RunCMake/QtFlagHandlingHelpers/CMakeLists.txt new file mode 100644 index 00000000000..dd0eae3fda8 --- /dev/null +++ b/tests/auto/cmake/RunCMake/QtFlagHandlingHelpers/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.16) +project(${RunCMake_TEST} LANGUAGES CXX) + +find_package(Qt6 COMPONENTS BuildInternals) + +include(${RunCMake_TEST}.cmake) diff --git a/tests/auto/cmake/RunCMake/QtFlagHandlingHelpers/RunCMakeTest.cmake b/tests/auto/cmake/RunCMake/QtFlagHandlingHelpers/RunCMakeTest.cmake new file mode 100644 index 00000000000..a71fda4ccfd --- /dev/null +++ b/tests/auto/cmake/RunCMake/QtFlagHandlingHelpers/RunCMakeTest.cmake @@ -0,0 +1,27 @@ +include(RunCMake) + +set(find_package_opts "-DQt6_DIR=${Qt6_DIR}") + +run_cmake_with_options(qt_internal_add_compiler_dependent_flags_pass ${find_package_opts}) +# Failing cases +set(RunCMake_TEST_EXPECT_RESULT 1) +# Missing OPTIONS keyword +foreach(case IN ITEMS + missing_OPTIONS + missing_COMPILERS + empty_COMPILERS + invalid_CONDITIONS +) + set(RunCMake_TEST_EXPECT_stdout "case: ${case}") + if(case STREQUAL "missing_OPTIONS") + set(RunCMake_TEST_EXPECT_stderr "OPTIONS keyword was not passed") + elseif(case STREQUAL "missing_COMPILERS") + set(RunCMake_TEST_EXPECT_stderr "COMPILERS keyword was not passed") + elseif(case STREQUAL "empty_COMPILERS") + set(RunCMake_TEST_EXPECT_stderr "COMPILERS set cannot be empty") + elseif(case STREQUAL "invalid_CONDITIONS") + set(RunCMake_TEST_EXPECT_stderr "Unrecognized condition form: not_a_genex") + endif() + run_cmake_with_options(qt_internal_add_compiler_dependent_flags_fail + -Dcase=${case} ${find_package_opts}) +endforeach() diff --git a/tests/auto/cmake/RunCMake/QtFlagHandlingHelpers/qt_internal_add_compiler_dependent_flags_fail.cmake b/tests/auto/cmake/RunCMake/QtFlagHandlingHelpers/qt_internal_add_compiler_dependent_flags_fail.cmake new file mode 100644 index 00000000000..2b122db3004 --- /dev/null +++ b/tests/auto/cmake/RunCMake/QtFlagHandlingHelpers/qt_internal_add_compiler_dependent_flags_fail.cmake @@ -0,0 +1,25 @@ +add_library(dummy INTERFACE) +message(STATUS "case: ${case}") +if(case STREQUAL "missing_OPTIONS") + qt_internal_add_compiler_dependent_flags(dummy INTERFACE + COMPILERS ALL + ) +elseif(case STREQUAL "missing_COMPILERS") + qt_internal_add_compiler_dependent_flags(dummy INTERFACE + CONDITIONS $ + OPTIONS -foo + ) +elseif(case STREQUAL "empty_COMPILERS") + qt_internal_add_compiler_dependent_flags(dummy INTERFACE + COMPILERS + OPTIONS -foo + ) +elseif(case STREQUAL "invalid_CONDITIONS") + qt_internal_add_compiler_dependent_flags(dummy INTERFACE + COMPILERS ALL + CONDITIONS not_a_genex + OPTIONS -foo + ) +else() + message(FATAL_ERROR "Unrecognized case: ${case}") +endif() diff --git a/tests/auto/cmake/RunCMake/QtFlagHandlingHelpers/qt_internal_add_compiler_dependent_flags_pass.cmake b/tests/auto/cmake/RunCMake/QtFlagHandlingHelpers/qt_internal_add_compiler_dependent_flags_pass.cmake new file mode 100644 index 00000000000..b89bcf3c2c6 --- /dev/null +++ b/tests/auto/cmake/RunCMake/QtFlagHandlingHelpers/qt_internal_add_compiler_dependent_flags_pass.cmake @@ -0,0 +1,122 @@ +## Test cases +# All test cases (libraries) are picked up and evaluated at the end of this file. +# Each test case is a dummy interface library. +# We don't really need the compiler options to be valid. +# Make sure to define a _reference variable to compare with + +add_library(basic INTERFACE) +qt_internal_add_compiler_dependent_flags(basic INTERFACE + COMPILERS ALL + OPTIONS -opt1 + COMPILERS GNU + OPTIONS -opt2 -opt3 +) +set(basic_reference + "$<$>:-opt1>" + "$<$,$>:-opt2;-opt3>" +) + +add_library(conditions INTERFACE) +qt_internal_add_compiler_dependent_flags(conditions INTERFACE + COMPILERS ALL + # No conditions + OPTIONS -opt + # Multiple conditions + CONDITIONS VERSION_LESS 42 AND $ + OPTIONS -opt + # A new condition loop + CONDITIONS $ + OPTIONS -opt + COMPILERS GNU + # New compiler set must reset to no conditions + OPTIONS -opt +) +set(conditions_reference + "$<$>:-opt>" + "$<$,$,42>,$>>:-opt>" + "$<$,$>:-opt>" + "$<$,$>:-opt>" +) + +add_library(conditions_stack INTERFACE) +qt_internal_add_compiler_dependent_flags(conditions_stack INTERFACE + COMPILERS ALL + # Conditions follow shortcircuit order + CONDITIONS $ AND $ OR $ + OPTIONS -opt + # Check parenthesis + CONDITIONS $ AND ($ OR $) + OPTIONS -opt + # Consecutive AND are combined + CONDITIONS $ AND $ AND $ OR $ + OPTIONS -opt +) +set(conditions_stack_reference + "$<$,$,$>,$>>:-opt>" + "$<$,$,$,$>>>:-opt>" + "$<$,$,$,$>,$>>:-opt>" +) + +add_library(one_language INTERFACE) +qt_internal_add_compiler_dependent_flags(one_language INTERFACE + COMPILERS ALL + CONDITIONS VERSION_LESS 42 + OPTIONS -opt + LANGUAGES C +) +set(one_language_reference + "$<$,$,42>>:-opt>" +) + +add_library(multiple_language INTERFACE) +qt_internal_add_compiler_dependent_flags(multiple_language INTERFACE + COMPILERS ALL + CONDITIONS VERSION_LESS 42 + OPTIONS -opt + LANGUAGES C OBJCXX +) +set(multiple_language_reference + # Right now we always use CXX_COMPILER_VERSION even if not in LANGUAGES + # Change this test case if we have a better interface + "$<$,$,42>>:-opt>" +) + +add_library(common_condition INTERFACE) +qt_internal_add_compiler_dependent_flags(common_condition INTERFACE + COMPILERS ALL + OPTIONS -opt + CONDITIONS $ + OPTIONS -opt + COMPILERS GNU + OPTIONS -opt + COMMON_CONDITIONS + $ + 1 +) +set(common_condition_reference + "$<$,1,$>:-opt>" + "$<$,1,$,$>:-opt>" + "$<$,1,$,$>:-opt>" +) + +## Check test cases +function(check_options target) + if(NOT ${target}_reference) + message(FATAL_ERROR + "Test case is missing *_reference variable: Case: ${target}" + ) + endif() + get_target_property(options ${target} INTERFACE_COMPILE_OPTIONS) + if(NOT options STREQUAL ${target}_reference) + message(SEND_ERROR + "Invalid compile options. Case: ${target}\n" + "expected-options> ${${target}_reference}\n" + "actual-options> ${options}" + ) + endif() +endfunction() + +get_directory_property(all_test_cases BUILDSYSTEM_TARGETS) +foreach(case IN LISTS all_test_cases) + check_options(${case}) +endforeach()