GoogleTest.cmake 26 KB

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