|
| 1 | +cmake_policy(PUSH) |
| 2 | +cmake_policy(SET CMP0057 NEW) |
| 3 | + |
| 4 | +# Automatically add tests with CTest by querying the compiled test executable |
| 5 | +# for available tests. |
| 6 | +# |
| 7 | +# xctest_discover_tests(target |
| 8 | +# [COMMAND command] |
| 9 | +# [WORKING_DIRECTORY dir] |
| 10 | +# [PROPERTIES name1 value1...] |
| 11 | +# [DISCOVERY_TIMEOUT seconds] |
| 12 | +# ) |
| 13 | +# |
| 14 | +# `xctest_discover_tests` sets up a post-build command on the test executable |
| 15 | +# that generates the list of tests by parsing the output from running the test |
| 16 | +# with the `--list-tests` argument. |
| 17 | +# |
| 18 | +# The options are: |
| 19 | +# |
| 20 | +# `target` |
| 21 | +# Specifies the XCTest executable, which must be a known CMake target. CMake |
| 22 | +# will substitute the location of the built executable when running the test. |
| 23 | +# |
| 24 | +# `COMMAND command` |
| 25 | +# Override the command used for the test executable. If you executable is not |
| 26 | +# created with CMake add_executable, you will have to provide a command path. |
| 27 | +# If this option is not provided, the target file of the target is used. |
| 28 | +# |
| 29 | +# `WORKING_DIRECTORY dir` |
| 30 | +# Specifies the directory in which to run the discovered test cases. If this |
| 31 | +# option is not provided, the current binary directory is used. |
| 32 | +# |
| 33 | +# `PROPERTIES name1 value1...` |
| 34 | +# Specifies additional properties to be set on all tests discovered by this |
| 35 | +# invocation of `xctest_discover_tests`. |
| 36 | +# |
| 37 | +# `DISCOVERY_TIMEOUT seconds` |
| 38 | +# Specifies how long (in seconds) CMake will wait for the test to enumerate |
| 39 | +# available tests. If the test takes longer than this, discovery (and your |
| 40 | +# build) will fail. The default is 5 seconds. |
| 41 | +# |
| 42 | +# The inspiration for this is CMake `gtest_discover_tests`. The official |
| 43 | +# documentation might be useful for using this function. Many details of that |
| 44 | +# function has been dropped in the name of simplicity, and others have been |
| 45 | +# improved. |
| 46 | +function(xctest_discover_tests TARGET) |
| 47 | + cmake_parse_arguments( |
| 48 | + "" |
| 49 | + "" |
| 50 | + "COMMAND;WORKING_DIRECTORY;DISCOVERY_TIMEOUT" |
| 51 | + "PROPERTIES" |
| 52 | + ${ARGN} |
| 53 | + ) |
| 54 | + |
| 55 | + if(NOT _COMMAND) |
| 56 | + set(_COMMAND "$<TARGET_FILE:${TARGET}>") |
| 57 | + endif() |
| 58 | + if(NOT _WORKING_DIRECTORY) |
| 59 | + set(_WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) |
| 60 | + endif() |
| 61 | + if(NOT _DISCOVERY_TIMEOUT) |
| 62 | + set(_DISCOVERY_TIMEOUT 5) |
| 63 | + endif() |
| 64 | + |
| 65 | + set(ctest_file_base ${CMAKE_CURRENT_BINARY_DIR}/${TARGET}) |
| 66 | + set(ctest_include_file "${ctest_file_base}_include.cmake") |
| 67 | + set(ctest_tests_file "${ctest_file_base}_tests.cmake") |
| 68 | + |
| 69 | + add_custom_command( |
| 70 | + TARGET ${TARGET} POST_BUILD |
| 71 | + BYPRODUCTS "${ctest_tests_file}" |
| 72 | + COMMAND "${CMAKE_COMMAND}" |
| 73 | + -D "TEST_TARGET=${TARGET}" |
| 74 | + -D "TEST_EXECUTABLE=${_COMMAND}" |
| 75 | + -D "TEST_WORKING_DIR=${_WORKING_DIRECTORY}" |
| 76 | + -D "TEST_PROPERTIES=${_PROPERTIES}" |
| 77 | + -D "CTEST_FILE=${ctest_tests_file}" |
| 78 | + -D "TEST_DISCOVERY_TIMEOUT=${_DISCOVERY_TIMEOUT}" |
| 79 | + -P "${_XCTEST_DISCOVER_TESTS_SCRIPT}" |
| 80 | + VERBATIM |
| 81 | + ) |
| 82 | + |
| 83 | + file(WRITE "${ctest_include_file}" |
| 84 | + "if(EXISTS \"${ctest_tests_file}\")\n" |
| 85 | + " include(\"${ctest_tests_file}\")\n" |
| 86 | + "else()\n" |
| 87 | + " add_test(${TARGET}_NOT_BUILT ${TARGET}_NOT_BUILT)\n" |
| 88 | + "endif()\n" |
| 89 | + ) |
| 90 | + |
| 91 | + set_property(DIRECTORY |
| 92 | + APPEND PROPERTY TEST_INCLUDE_FILES "${ctest_include_file}" |
| 93 | + ) |
| 94 | +endfunction() |
| 95 | + |
| 96 | +set(_XCTEST_DISCOVER_TESTS_SCRIPT |
| 97 | + ${CMAKE_CURRENT_LIST_DIR}/XCTestAddTests.cmake |
| 98 | +) |
| 99 | + |
| 100 | +cmake_policy(POP) |
0 commit comments