| 1 |
|
| 2 |
# Copyright Catch2 Authors |
| 3 |
# Distributed under the Boost Software License, Version 1.0. |
| 4 |
# (See accompanying file LICENSE.txt or copy at |
| 5 |
# https://www.boost.org/LICENSE_1_0.txt) |
| 6 |
|
| 7 |
# SPDX-License-Identifier: BSL-1.0 |
| 8 |
|
| 9 |
# Supported optional args: |
| 10 |
# * SHARD_COUNT - number of shards to split target's tests into |
| 11 |
# * REPORTER - reporter spec to use for tests |
| 12 |
# * TEST_SPEC - test spec used for filtering tests |
| 13 |
function(catch_add_sharded_tests TARGET) |
| 14 |
if (${CMAKE_VERSION} VERSION_LESS "3.10.0") |
| 15 |
message(FATAL_ERROR "add_sharded_catch_tests only supports CMake versions 3.10.0 and up") |
| 16 |
endif() |
| 17 |
|
| 18 |
cmake_parse_arguments( |
| 19 |
"" |
| 20 |
"" |
| 21 |
"SHARD_COUNT;REPORTER;TEST_SPEC" |
| 22 |
"" |
| 23 |
${ARGN} |
| 24 |
) |
| 25 |
|
| 26 |
if (NOT DEFINED _SHARD_COUNT) |
| 27 |
set(_SHARD_COUNT 2) |
| 28 |
endif() |
| 29 |
|
| 30 |
# Generate a unique name based on the extra arguments |
| 31 |
string(SHA1 args_hash "${_TEST_SPEC} ${_EXTRA_ARGS} ${_REPORTER} ${_OUTPUT_DIR} ${_OUTPUT_PREFIX} ${_OUTPUT_SUFFIX} ${_SHARD_COUNT}") |
| 32 |
string(SUBSTRING ${args_hash} 0 7 args_hash) |
| 33 |
|
| 34 |
set(ctest_include_file "${CMAKE_CURRENT_BINARY_DIR}/${TARGET}-sharded-tests-include-${args_hash}.cmake") |
| 35 |
set(ctest_tests_file "${CMAKE_CURRENT_BINARY_DIR}/${TARGET}-sharded-tests-impl-${args_hash}.cmake") |
| 36 |
|
| 37 |
file(WRITE "${ctest_include_file}" |
| 38 |
"if(EXISTS \"${ctest_tests_file}\")\n" |
| 39 |
" include(\"${ctest_tests_file}\")\n" |
| 40 |
"else()\n" |
| 41 |
" add_test(${TARGET}_NOT_BUILT-${args_hash} ${TARGET}_NOT_BUILT-${args_hash})\n" |
| 42 |
"endif()\n" |
| 43 |
) |
| 44 |
|
| 45 |
set_property(DIRECTORY |
| 46 |
APPEND PROPERTY TEST_INCLUDE_FILES "${ctest_include_file}" |
| 47 |
) |
| 48 |
|
| 49 |
set(shard_impl_script_file "${_CATCH_DISCOVER_SHARD_TESTS_IMPL_SCRIPT}") |
| 50 |
|
| 51 |
add_custom_command( |
| 52 |
TARGET ${TARGET} POST_BUILD |
| 53 |
BYPRODUCTS "${ctest_tests_file}" |
| 54 |
COMMAND "${CMAKE_COMMAND}" |
| 55 |
-D "TARGET_NAME=${TARGET}" |
| 56 |
-D "TEST_BINARY=$<TARGET_FILE:${TARGET}>" |
| 57 |
-D "CTEST_FILE=${ctest_tests_file}" |
| 58 |
-D "SHARD_COUNT=${_SHARD_COUNT}" |
| 59 |
-D "REPORTER_SPEC=${_REPORTER}" |
| 60 |
-D "TEST_SPEC=${_TEST_SPEC}" |
| 61 |
-P "${shard_impl_script_file}" |
| 62 |
VERBATIM |
| 63 |
) |
| 64 |
|
| 65 |
|
| 66 |
endfunction() |
| 67 |
|
| 68 |
|
| 69 |
############################################################################### |
| 70 |
|
| 71 |
set(_CATCH_DISCOVER_SHARD_TESTS_IMPL_SCRIPT |
| 72 |
${CMAKE_CURRENT_LIST_DIR}/CatchShardTestsImpl.cmake |
| 73 |
CACHE INTERNAL "Catch2 full path to CatchShardTestsImpl.cmake helper file" |
| 74 |
) |