CatchShardTests.cmake 2.1 KB

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