GoogleTest.cmake 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file Copyright.txt or https://cmake.org/licensing for details.
  3. #[=======================================================================[.rst:
  4. GoogleTest
  5. ----------
  6. This module defines functions to help use the Google Test infrastructure. Two
  7. mechanisms for adding tests are provided. :command:`gtest_add_tests` has been
  8. around for some time, originally via ``find_package(GTest)``.
  9. :command:`gtest_discover_tests` was introduced in CMake 3.10.
  10. The (older) :command:`gtest_add_tests` scans source files to identify tests.
  11. This is usually effective, with some caveats, including in cross-compiling
  12. environments, and makes setting additional properties on tests more convenient.
  13. However, its handling of parameterized tests is less comprehensive, and it
  14. requires re-running CMake to detect changes to the list of tests.
  15. The (newer) :command:`gtest_discover_tests` discovers tests by asking the
  16. compiled test executable to enumerate its tests. This is more robust and
  17. provides better handling of parameterized tests, and does not require CMake
  18. to be re-run when tests change. However, it may not work in a cross-compiling
  19. environment, and setting test properties is less convenient.
  20. More details can be found in the documentation of the respective functions.
  21. Both commands are intended to replace use of :command:`add_test` to register
  22. tests, and will create a separate CTest test for each Google Test test case.
  23. Note that this is in some cases less efficient, as common set-up and tear-down
  24. logic cannot be shared by multiple test cases executing in the same instance.
  25. However, it provides more fine-grained pass/fail information to CTest, which is
  26. usually considered as more beneficial. By default, the CTest test name is the
  27. same as the Google Test name (i.e. ``suite.testcase``); see also
  28. ``TEST_PREFIX`` and ``TEST_SUFFIX``.
  29. .. command:: gtest_add_tests
  30. Automatically add tests with CTest by scanning source code for Google Test
  31. macros::
  32. gtest_add_tests(TARGET target
  33. [SOURCES src1...]
  34. [EXTRA_ARGS arg1...]
  35. [WORKING_DIRECTORY dir]
  36. [TEST_PREFIX prefix]
  37. [TEST_SUFFIX suffix]
  38. [SKIP_DEPENDENCY]
  39. [TEST_LIST outVar]
  40. )
  41. ``gtest_add_tests`` attempts to identify tests by scanning source files.
  42. Although this is generally effective, it uses only a basic regular expression
  43. match, which can be defeated by atypical test declarations, and is unable to
  44. fully "split" parameterized tests. Additionally, it requires that CMake be
  45. re-run to discover any newly added, removed or renamed tests (by default,
  46. this means that CMake is re-run when any test source file is changed, but see
  47. ``SKIP_DEPENDENCY``). However, it has the advantage of declaring tests at
  48. CMake time, which somewhat simplifies setting additional properties on tests,
  49. and always works in a cross-compiling environment.
  50. The options are:
  51. ``TARGET target``
  52. Specifies the Google Test executable, which must be a known CMake
  53. executable target. CMake will substitute the location of the built
  54. executable when running the test.
  55. ``SOURCES src1...``
  56. When provided, only the listed files will be scanned for test cases. If
  57. this option is not given, the :prop_tgt:`SOURCES` property of the
  58. specified ``target`` will be used to obtain the list of sources.
  59. ``EXTRA_ARGS arg1...``
  60. Any extra arguments to pass on the command line to each test case.
  61. ``WORKING_DIRECTORY dir``
  62. Specifies the directory in which to run the discovered test cases. If this
  63. option is not provided, the current binary directory is used.
  64. ``TEST_PREFIX prefix``
  65. Specifies a ``prefix`` to be prepended to the name of each discovered test
  66. case. This can be useful when the same source files are being used in
  67. multiple calls to ``gtest_add_test()`` but with different ``EXTRA_ARGS``.
  68. ``TEST_SUFFIX suffix``
  69. Similar to ``TEST_PREFIX`` except the ``suffix`` is appended to the name of
  70. every discovered test case. Both ``TEST_PREFIX`` and ``TEST_SUFFIX`` may
  71. be specified.
  72. ``SKIP_DEPENDENCY``
  73. Normally, the function creates a dependency which will cause CMake to be
  74. re-run if any of the sources being scanned are changed. This is to ensure
  75. that the list of discovered tests is updated. If this behavior is not
  76. desired (as may be the case while actually writing the test cases), this
  77. option can be used to prevent the dependency from being added.
  78. ``TEST_LIST outVar``
  79. The variable named by ``outVar`` will be populated in the calling scope
  80. with the list of discovered test cases. This allows the caller to do
  81. things like manipulate test properties of the discovered tests.
  82. .. code-block:: cmake
  83. include(GoogleTest)
  84. add_executable(FooTest FooUnitTest.cxx)
  85. gtest_add_tests(TARGET FooTest
  86. TEST_SUFFIX .noArgs
  87. TEST_LIST noArgsTests
  88. )
  89. gtest_add_tests(TARGET FooTest
  90. EXTRA_ARGS --someArg someValue
  91. TEST_SUFFIX .withArgs
  92. TEST_LIST withArgsTests
  93. )
  94. set_tests_properties(${noArgsTests} PROPERTIES TIMEOUT 10)
  95. set_tests_properties(${withArgsTests} PROPERTIES TIMEOUT 20)
  96. For backward compatibility, the following form is also supported::
  97. gtest_add_tests(exe args files...)
  98. ``exe``
  99. The path to the test executable or the name of a CMake target.
  100. ``args``
  101. A ;-list of extra arguments to be passed to executable. The entire
  102. list must be passed as a single argument. Enclose it in quotes,
  103. or pass ``""`` for no arguments.
  104. ``files...``
  105. A list of source files to search for tests and test fixtures.
  106. Alternatively, use ``AUTO`` to specify that ``exe`` is the name
  107. of a CMake executable target whose sources should be scanned.
  108. .. code-block:: cmake
  109. include(GoogleTest)
  110. set(FooTestArgs --foo 1 --bar 2)
  111. add_executable(FooTest FooUnitTest.cxx)
  112. gtest_add_tests(FooTest "${FooTestArgs}" AUTO)
  113. .. command:: gtest_discover_tests
  114. Automatically add tests with CTest by querying the compiled test executable
  115. for available tests::
  116. gtest_discover_tests(target
  117. [EXTRA_ARGS arg1...]
  118. [WORKING_DIRECTORY dir]
  119. [TEST_PREFIX prefix]
  120. [TEST_SUFFIX suffix]
  121. [NO_PRETTY_TYPES] [NO_PRETTY_VALUES]
  122. [PROPERTIES name1 value1...]
  123. [TEST_LIST var]
  124. [DISCOVERY_TIMEOUT seconds]
  125. [XML_OUTPUT_DIR dir]
  126. [DISCOVERY_MODE <POST_BUILD|PRE_TEST>]
  127. )
  128. ``gtest_discover_tests`` sets up a post-build command on the test executable
  129. that generates the list of tests by parsing the output from running the test
  130. with the ``--gtest_list_tests`` argument. Compared to the source parsing
  131. approach of :command:`gtest_add_tests`, this ensures that the full list of
  132. tests, including instantiations of parameterized tests, is obtained. Since
  133. test discovery occurs at build time, it is not necessary to re-run CMake when
  134. the list of tests changes.
  135. However, it requires that :prop_tgt:`CROSSCOMPILING_EMULATOR` is properly set
  136. in order to function in a cross-compiling environment.
  137. Additionally, setting properties on tests is somewhat less convenient, since
  138. the tests are not available at CMake time. Additional test properties may be
  139. assigned to the set of tests as a whole using the ``PROPERTIES`` option. If
  140. more fine-grained test control is needed, custom content may be provided
  141. through an external CTest script using the :prop_dir:`TEST_INCLUDE_FILES`
  142. directory property. The set of discovered tests is made accessible to such a
  143. script via the ``<target>_TESTS`` variable.
  144. The options are:
  145. ``target``
  146. Specifies the Google Test executable, which must be a known CMake
  147. executable target. CMake will substitute the location of the built
  148. executable when running the test.
  149. ``EXTRA_ARGS arg1...``
  150. Any extra arguments to pass on the command line to each test case.
  151. ``WORKING_DIRECTORY dir``
  152. Specifies the directory in which to run the discovered test cases. If this
  153. option is not provided, the current binary directory is used.
  154. ``TEST_PREFIX prefix``
  155. Specifies a ``prefix`` to be prepended to the name of each discovered test
  156. case. This can be useful when the same test executable is being used in
  157. multiple calls to ``gtest_discover_tests()`` but with different
  158. ``EXTRA_ARGS``.
  159. ``TEST_SUFFIX suffix``
  160. Similar to ``TEST_PREFIX`` except the ``suffix`` is appended to the name of
  161. every discovered test case. Both ``TEST_PREFIX`` and ``TEST_SUFFIX`` may
  162. be specified.
  163. ``NO_PRETTY_TYPES``
  164. By default, the type index of type-parameterized tests is replaced by the
  165. actual type name in the CTest test name. If this behavior is undesirable
  166. (e.g. because the type names are unwieldy), this option will suppress this
  167. behavior.
  168. ``NO_PRETTY_VALUES``
  169. By default, the value index of value-parameterized tests is replaced by the
  170. actual value in the CTest test name. If this behavior is undesirable
  171. (e.g. because the value strings are unwieldy), this option will suppress
  172. this behavior.
  173. ``PROPERTIES name1 value1...``
  174. Specifies additional properties to be set on all tests discovered by this
  175. invocation of ``gtest_discover_tests``.
  176. ``TEST_LIST var``
  177. Make the list of tests available in the variable ``var``, rather than the
  178. default ``<target>_TESTS``. This can be useful when the same test
  179. executable is being used in multiple calls to ``gtest_discover_tests()``.
  180. Note that this variable is only available in CTest.
  181. ``DISCOVERY_TIMEOUT num``
  182. Specifies how long (in seconds) CMake will wait for the test to enumerate
  183. available tests. If the test takes longer than this, discovery (and your
  184. build) will fail. Most test executables will enumerate their tests very
  185. quickly, but under some exceptional circumstances, a test may require a
  186. longer timeout. The default is 5. See also the ``TIMEOUT`` option of
  187. :command:`execute_process`.
  188. .. note::
  189. In CMake versions 3.10.1 and 3.10.2, this option was called ``TIMEOUT``.
  190. This clashed with the ``TIMEOUT`` test property, which is one of the
  191. common properties that would be set with the ``PROPERTIES`` keyword,
  192. usually leading to legal but unintended behavior. The keyword was
  193. changed to ``DISCOVERY_TIMEOUT`` in CMake 3.10.3 to address this
  194. problem. The ambiguous behavior of the ``TIMEOUT`` keyword in 3.10.1
  195. and 3.10.2 has not been preserved.
  196. ``XML_OUTPUT_DIR dir``
  197. If specified, the parameter is passed along with ``--gtest_output=xml:``
  198. to test executable. The actual file name is the same as the test target,
  199. including prefix and suffix. This should be used instead of
  200. ``EXTRA_ARGS --gtest_output=xml`` to avoid race conditions writing the
  201. XML result output when using parallel test execution.
  202. ``DISCOVERY_MODE``
  203. Provides greater control over when ``gtest_discover_tests``performs test
  204. discovery. By default, ``POST_BUILD`` sets up a post-build command
  205. to perform test discovery at build time. In certain scenarios, like
  206. cross-compiling, this ``POST_BUILD`` behavior is not desirable.
  207. By contrast, ``PRE_TEST`` delays test discovery until just prior to test
  208. execution. This way test discovery occurs in the target environment
  209. where the test has a better chance at finding appropriate runtime
  210. dependencies.
  211. ``DISCOVERY_MODE`` defaults to the value of the
  212. ``CMAKE_GTEST_DISCOVER_TESTS_DISCOVERY_MODE`` variable if it is not
  213. passed when calling ``gtest_discover_tests``. This provides a mechanism
  214. for globally selecting a preferred test discovery behavior without having
  215. to modify each call site.
  216. #]=======================================================================]
  217. # Save project's policies
  218. cmake_policy(PUSH)
  219. cmake_policy(SET CMP0057 NEW) # if IN_LIST
  220. #------------------------------------------------------------------------------
  221. function(gtest_add_tests)
  222. if (ARGC LESS 1)
  223. message(FATAL_ERROR "No arguments supplied to gtest_add_tests()")
  224. endif()
  225. set(options
  226. SKIP_DEPENDENCY
  227. )
  228. set(oneValueArgs
  229. TARGET
  230. WORKING_DIRECTORY
  231. TEST_PREFIX
  232. TEST_SUFFIX
  233. TEST_LIST
  234. )
  235. set(multiValueArgs
  236. SOURCES
  237. EXTRA_ARGS
  238. )
  239. set(allKeywords ${options} ${oneValueArgs} ${multiValueArgs})
  240. unset(sources)
  241. if("${ARGV0}" IN_LIST allKeywords)
  242. cmake_parse_arguments(ARGS "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
  243. set(autoAddSources YES)
  244. else()
  245. # Non-keyword syntax, convert to keyword form
  246. if (ARGC LESS 3)
  247. message(FATAL_ERROR "gtest_add_tests() without keyword options requires at least 3 arguments")
  248. endif()
  249. set(ARGS_TARGET "${ARGV0}")
  250. set(ARGS_EXTRA_ARGS "${ARGV1}")
  251. if(NOT "${ARGV2}" STREQUAL "AUTO")
  252. set(ARGS_SOURCES "${ARGV}")
  253. list(REMOVE_AT ARGS_SOURCES 0 1)
  254. endif()
  255. endif()
  256. # The non-keyword syntax allows the first argument to be an arbitrary
  257. # executable rather than a target if source files are also provided. In all
  258. # other cases, both forms require a target.
  259. if(NOT TARGET "${ARGS_TARGET}" AND NOT ARGS_SOURCES)
  260. message(FATAL_ERROR "${ARGS_TARGET} does not define an existing CMake target")
  261. endif()
  262. if(NOT ARGS_WORKING_DIRECTORY)
  263. unset(workDir)
  264. else()
  265. set(workDir WORKING_DIRECTORY "${ARGS_WORKING_DIRECTORY}")
  266. endif()
  267. if(NOT ARGS_SOURCES)
  268. get_property(ARGS_SOURCES TARGET ${ARGS_TARGET} PROPERTY SOURCES)
  269. endif()
  270. unset(testList)
  271. set(gtest_case_name_regex ".*\\( *([A-Za-z_0-9]+) *, *([A-Za-z_0-9]+) *\\).*")
  272. set(gtest_test_type_regex "(TYPED_TEST|TEST_?[FP]?)")
  273. foreach(source IN LISTS ARGS_SOURCES)
  274. if(NOT ARGS_SKIP_DEPENDENCY)
  275. set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${source})
  276. endif()
  277. file(READ "${source}" contents)
  278. string(REGEX MATCHALL "${gtest_test_type_regex} *\\(([A-Za-z_0-9 ,]+)\\)" found_tests "${contents}")
  279. foreach(hit ${found_tests})
  280. string(REGEX MATCH "${gtest_test_type_regex}" test_type ${hit})
  281. # Parameterized tests have a different signature for the filter
  282. if("x${test_type}" STREQUAL "xTEST_P")
  283. string(REGEX REPLACE ${gtest_case_name_regex} "*/\\1.\\2/*" gtest_test_name ${hit})
  284. elseif("x${test_type}" STREQUAL "xTEST_F" OR "x${test_type}" STREQUAL "xTEST")
  285. string(REGEX REPLACE ${gtest_case_name_regex} "\\1.\\2" gtest_test_name ${hit})
  286. elseif("x${test_type}" STREQUAL "xTYPED_TEST")
  287. string(REGEX REPLACE ${gtest_case_name_regex} "\\1/*.\\2" gtest_test_name ${hit})
  288. else()
  289. message(WARNING "Could not parse GTest ${hit} for adding to CTest.")
  290. continue()
  291. endif()
  292. # Make sure tests disabled in GTest get disabled in CTest
  293. if(gtest_test_name MATCHES "(^|\\.)DISABLED_")
  294. # Add the disabled test if CMake is new enough
  295. # Note that this check is to allow backwards compatibility so this
  296. # module can be copied locally in projects to use with older CMake
  297. # versions
  298. if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.8.20170401)
  299. string(REGEX REPLACE
  300. "(^|\\.)DISABLED_" "\\1"
  301. orig_test_name "${gtest_test_name}"
  302. )
  303. set(ctest_test_name
  304. ${ARGS_TEST_PREFIX}${orig_test_name}${ARGS_TEST_SUFFIX}
  305. )
  306. add_test(NAME ${ctest_test_name}
  307. ${workDir}
  308. COMMAND ${ARGS_TARGET}
  309. --gtest_also_run_disabled_tests
  310. --gtest_filter=${gtest_test_name}
  311. ${ARGS_EXTRA_ARGS}
  312. )
  313. set_tests_properties(${ctest_test_name} PROPERTIES DISABLED TRUE)
  314. list(APPEND testList ${ctest_test_name})
  315. endif()
  316. else()
  317. set(ctest_test_name ${ARGS_TEST_PREFIX}${gtest_test_name}${ARGS_TEST_SUFFIX})
  318. add_test(NAME ${ctest_test_name}
  319. ${workDir}
  320. COMMAND ${ARGS_TARGET}
  321. --gtest_filter=${gtest_test_name}
  322. ${ARGS_EXTRA_ARGS}
  323. )
  324. list(APPEND testList ${ctest_test_name})
  325. endif()
  326. endforeach()
  327. endforeach()
  328. if(ARGS_TEST_LIST)
  329. set(${ARGS_TEST_LIST} ${testList} PARENT_SCOPE)
  330. endif()
  331. endfunction()
  332. #------------------------------------------------------------------------------
  333. function(gtest_discover_tests TARGET)
  334. cmake_parse_arguments(
  335. ""
  336. "NO_PRETTY_TYPES;NO_PRETTY_VALUES"
  337. "TEST_PREFIX;TEST_SUFFIX;WORKING_DIRECTORY;TEST_LIST;DISCOVERY_TIMEOUT;XML_OUTPUT_DIR;DISCOVERY_MODE"
  338. "EXTRA_ARGS;PROPERTIES"
  339. ${ARGN}
  340. )
  341. if(NOT _WORKING_DIRECTORY)
  342. set(_WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}")
  343. endif()
  344. if(NOT _TEST_LIST)
  345. set(_TEST_LIST ${TARGET}_TESTS)
  346. endif()
  347. if(NOT _DISCOVERY_TIMEOUT)
  348. set(_DISCOVERY_TIMEOUT 5)
  349. endif()
  350. if(NOT _DISCOVERY_MODE)
  351. if(NOT CMAKE_GTEST_DISCOVER_TESTS_DISCOVERY_MODE)
  352. set(CMAKE_GTEST_DISCOVER_TESTS_DISCOVERY_MODE "POST_BUILD")
  353. endif()
  354. set(_DISCOVERY_MODE ${CMAKE_GTEST_DISCOVER_TESTS_DISCOVERY_MODE})
  355. endif()
  356. get_property(
  357. has_counter
  358. TARGET ${TARGET}
  359. PROPERTY CTEST_DISCOVERED_TEST_COUNTER
  360. SET
  361. )
  362. if(has_counter)
  363. get_property(
  364. counter
  365. TARGET ${TARGET}
  366. PROPERTY CTEST_DISCOVERED_TEST_COUNTER
  367. )
  368. math(EXPR counter "${counter} + 1")
  369. else()
  370. set(counter 1)
  371. endif()
  372. set_property(
  373. TARGET ${TARGET}
  374. PROPERTY CTEST_DISCOVERED_TEST_COUNTER
  375. ${counter}
  376. )
  377. # Define rule to generate test list for aforementioned test executable
  378. set(ctest_file_base "${CMAKE_CURRENT_BINARY_DIR}/${TARGET}[${counter}]")
  379. set(ctest_include_file "${ctest_file_base}_include.cmake")
  380. set(ctest_tests_file "${ctest_file_base}_tests.cmake")
  381. get_property(crosscompiling_emulator
  382. TARGET ${TARGET}
  383. PROPERTY CROSSCOMPILING_EMULATOR
  384. )
  385. if(_DISCOVERY_MODE STREQUAL "POST_BUILD")
  386. add_custom_command(
  387. TARGET ${TARGET} POST_BUILD
  388. BYPRODUCTS "${ctest_tests_file}"
  389. COMMAND "${CMAKE_COMMAND}"
  390. -D "TEST_TARGET=${TARGET}"
  391. -D "TEST_EXECUTABLE=$<TARGET_FILE:${TARGET}>"
  392. -D "TEST_EXECUTOR=${crosscompiling_emulator}"
  393. -D "TEST_WORKING_DIR=${_WORKING_DIRECTORY}"
  394. -D "TEST_EXTRA_ARGS=${_EXTRA_ARGS}"
  395. -D "TEST_PROPERTIES=${_PROPERTIES}"
  396. -D "TEST_PREFIX=${_TEST_PREFIX}"
  397. -D "TEST_SUFFIX=${_TEST_SUFFIX}"
  398. -D "NO_PRETTY_TYPES=${_NO_PRETTY_TYPES}"
  399. -D "NO_PRETTY_VALUES=${_NO_PRETTY_VALUES}"
  400. -D "TEST_LIST=${_TEST_LIST}"
  401. -D "CTEST_FILE=${ctest_tests_file}"
  402. -D "TEST_DISCOVERY_TIMEOUT=${_DISCOVERY_TIMEOUT}"
  403. -D "TEST_XML_OUTPUT_DIR=${_XML_OUTPUT_DIR}"
  404. -P "${_GOOGLETEST_DISCOVER_TESTS_SCRIPT}"
  405. VERBATIM
  406. )
  407. file(WRITE "${ctest_include_file}"
  408. "if(EXISTS \"${ctest_tests_file}\")\n"
  409. " include(\"${ctest_tests_file}\")\n"
  410. "else()\n"
  411. " add_test(${TARGET}_NOT_BUILT ${TARGET}_NOT_BUILT)\n"
  412. "endif()\n"
  413. )
  414. elseif(_DISCOVERY_MODE STREQUAL "PRE_TEST")
  415. get_property(GENERATOR_IS_MULTI_CONFIG GLOBAL
  416. PROPERTY GENERATOR_IS_MULTI_CONFIG
  417. )
  418. if(GENERATOR_IS_MULTI_CONFIG)
  419. set(ctest_tests_file "${ctest_file_base}_tests-$<CONFIG>.cmake")
  420. endif()
  421. string(CONCAT ctest_include_content
  422. "if(EXISTS \"$<TARGET_FILE:${TARGET}>\")" "\n"
  423. " if(\"$<TARGET_FILE:${TARGET}>\" IS_NEWER_THAN \"${ctest_tests_file}\")" "\n"
  424. " include(GoogleTestAddTests)" "\n"
  425. " gtest_discover_tests_impl(" "\n"
  426. " TEST_EXECUTABLE" " [==[" "$<TARGET_FILE:${TARGET}>" "]==]" "\n"
  427. " TEST_EXECUTOR" " [==[" "${crosscompiling_emulator}" "]==]" "\n"
  428. " TEST_WORKING_DIR" " [==[" "${_WORKING_DIRECTORY}" "]==]" "\n"
  429. " TEST_EXTRA_ARGS" " [==[" "${_EXTRA_ARGS}" "]==]" "\n"
  430. " TEST_PROPERTIES" " [==[" "${_PROPERTIES}" "]==]" "\n"
  431. " TEST_PREFIX" " [==[" "${_TEST_PREFIX}" "]==]" "\n"
  432. " TEST_SUFFIX" " [==[" "${_TEST_SUFFIX}" "]==]" "\n"
  433. " NO_PRETTY_TYPES" " [==[" "${_NO_PRETTY_TYPES}" "]==]" "\n"
  434. " NO_PRETTY_VALUES" " [==[" "${_NO_PRETTY_VALUES}" "]==]" "\n"
  435. " TEST_LIST" " [==[" "${_TEST_LIST}" "]==]" "\n"
  436. " CTEST_FILE" " [==[" "${ctest_tests_file}" "]==]" "\n"
  437. " TEST_DISCOVERY_TIMEOUT" " [==[" "${_DISCOVERY_TIMEOUT}" "]==]" "\n"
  438. " TEST_XML_OUTPUT_DIR" " [==[" "${_XML_OUTPUT_DIR}" "]==]" "\n"
  439. " )" "\n"
  440. " endif()" "\n"
  441. " include(\"${ctest_tests_file}\")" "\n"
  442. "else()" "\n"
  443. " add_test(${TARGET}_NOT_BUILT ${TARGET}_NOT_BUILT)" "\n"
  444. "endif()" "\n"
  445. )
  446. if(GENERATOR_IS_MULTI_CONFIG)
  447. foreach(_config ${CMAKE_CONFIGURATION_TYPES})
  448. file(GENERATE OUTPUT "${ctest_file_base}_include-${_config}.cmake" CONTENT "${ctest_include_content}" CONDITION $<CONFIG:${_config}>)
  449. endforeach()
  450. file(WRITE "${ctest_include_file}" "include(\"${ctest_file_base}_include-\${CTEST_CONFIGURATION_TYPE}.cmake\")")
  451. else()
  452. file(GENERATE OUTPUT "${ctest_file_base}_include.cmake" CONTENT "${ctest_include_content}")
  453. file(WRITE "${ctest_include_file}" "include(\"${ctest_file_base}_include.cmake\")")
  454. endif()
  455. else()
  456. message(SEND_ERROR "Unknown DISCOVERY_MODE: ${_DISCOVERY_MODE}")
  457. endif()
  458. # Add discovered tests to directory TEST_INCLUDE_FILES
  459. set_property(DIRECTORY
  460. APPEND PROPERTY TEST_INCLUDE_FILES "${ctest_include_file}"
  461. )
  462. endfunction()
  463. ###############################################################################
  464. set(_GOOGLETEST_DISCOVER_TESTS_SCRIPT
  465. ${CMAKE_CURRENT_LIST_DIR}/GoogleTestAddTests.cmake
  466. )
  467. # Restore project's policies
  468. cmake_policy(POP)