Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Catch.cmake: Support CMake multi-config with PRE_TEST discovery mode #2739

Merged
merged 2 commits into from
Sep 25, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 28 additions & 3 deletions extras/Catch.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,10 @@ function(catch_discover_tests TARGET)
string(SUBSTRING ${args_hash} 0 7 args_hash)

# Define rule to generate test list for aforementioned test executable
set(ctest_include_file "${CMAKE_CURRENT_BINARY_DIR}/${TARGET}_include-${args_hash}.cmake")
set(ctest_tests_file "${CMAKE_CURRENT_BINARY_DIR}/${TARGET}_tests-${args_hash}.cmake")
set(ctest_file_base "${CMAKE_CURRENT_BINARY_DIR}/${TARGET}-${args_hash}")
set(ctest_include_file "${ctest_file_base}_include.cmake")
set(ctest_tests_file "${ctest_file_base}_tests.cmake")

get_property(crosscompiling_emulator
TARGET ${TARGET}
PROPERTY CROSSCOMPILING_EMULATOR
Expand Down Expand Up @@ -218,6 +220,14 @@ function(catch_discover_tests TARGET)

elseif(_DISCOVERY_MODE STREQUAL "PRE_TEST")

get_property(GENERATOR_IS_MULTI_CONFIG GLOBAL
PROPERTY GENERATOR_IS_MULTI_CONFIG
)

if(GENERATOR_IS_MULTI_CONFIG)
set(ctest_tests_file "${ctest_file_base}_tests-$<CONFIG>.cmake")
endif()

string(CONCAT ctest_include_content
"if(EXISTS \"$<TARGET_FILE:${TARGET}>\")" "\n"
" if(NOT EXISTS \"${ctest_tests_file}\" OR" "\n"
Expand Down Expand Up @@ -249,7 +259,22 @@ function(catch_discover_tests TARGET)
"endif()" "\n"
)

file(GENERATE OUTPUT "${ctest_include_file}" CONTENT "${ctest_include_content}")
if(GENERATOR_IS_MULTI_CONFIG)
foreach(_config ${CMAKE_CONFIGURATION_TYPES})
file(GENERATE OUTPUT "${ctest_file_base}_include-${_config}.cmake" CONTENT "${ctest_include_content}" CONDITION $<CONFIG:${_config}>)
endforeach()
string(CONCAT ctest_include_multi_content
"if(NOT CTEST_CONFIGURATION_TYPE)" "\n"
" message(\"No configuration for testing specified, use '-C <cfg>'.\")" "\n"
"else()" "\n"
" include(\"${ctest_file_base}_include-\${CTEST_CONFIGURATION_TYPE}.cmake\")" "\n"
"endif()" "\n"
Comment on lines +267 to +271
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is fine to merge already, but I still have a question: is there a reason to do this like this (outside of consistency with the surrounding code), rather than either making the newlines part of the line-strings, or using multiline strings?

IIRC something like this would work as well and be simpler to read

set(ctest_include_multi_content [[
if(NOT CTEST_CONFIGURATION_TYPE)
  message("No configuration for testing specified, use '-C <cfg>'.")
else()
  include("C:/ubuntu/temp/discover-tests-tests/msvc-sln-1/tests-b12d07c_include-${CTEST_CONFIGURATION_TYPE}.cmake")
endif()
]])

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, the issue is that we want to interpret some of the variables, while [[]] does not do any interpretation. nevermind then

)
file(GENERATE OUTPUT "${ctest_include_file}" CONTENT "${ctest_include_multi_content}")
else()
file(GENERATE OUTPUT "${ctest_file_base}_include.cmake" CONTENT "${ctest_include_content}")
file(WRITE "${ctest_include_file}" "include(\"${ctest_file_base}_include.cmake\")")
endif()
endif()

if(NOT ${CMAKE_VERSION} VERSION_LESS "3.10.0")
Expand Down