GoogleTest.cmake 30 KB

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