CTest.cmake 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. CTest
  5. -----
  6. Configure a project for testing with CTest/CDash
  7. Include this module in the top CMakeLists.txt file of a project to
  8. enable testing with CTest and dashboard submissions to CDash::
  9. project(MyProject)
  10. ...
  11. include(CTest)
  12. The module automatically creates a ``BUILD_TESTING`` option that selects
  13. whether to enable testing support (``ON`` by default). After including
  14. the module, use code like::
  15. if(BUILD_TESTING)
  16. # ... CMake code to create tests ...
  17. endif()
  18. to creating tests when testing is enabled.
  19. To enable submissions to a CDash server, create a ``CTestConfig.cmake``
  20. file at the top of the project with content such as::
  21. set(CTEST_NIGHTLY_START_TIME "01:00:00 UTC")
  22. set(CTEST_SUBMIT_URL "http://my.cdash.org/submit.php?project=MyProject")
  23. (the CDash server can provide the file to a project administrator who
  24. configures ``MyProject``). Settings in the config file are shared by
  25. both this ``CTest`` module and the :manual:`ctest(1)` command-line
  26. :ref:`Dashboard Client` mode (``ctest -S``).
  27. While building a project for submission to CDash, CTest scans the
  28. build output for errors and warnings and reports them with surrounding
  29. context from the build log. This generic approach works for all build
  30. tools, but does not give details about the command invocation that
  31. produced a given problem. One may get more detailed reports by setting
  32. the :variable:`CTEST_USE_LAUNCHERS` variable::
  33. set(CTEST_USE_LAUNCHERS 1)
  34. in the ``CTestConfig.cmake`` file.
  35. #]=======================================================================]
  36. option(BUILD_TESTING "Build the testing tree." ON)
  37. # function to turn generator name into a version string
  38. # like vs9 or vs10
  39. function(GET_VS_VERSION_STRING generator var)
  40. string(REGEX REPLACE "Visual Studio ([0-9][0-9]?)($|.*)" "\\1"
  41. NUMBER "${generator}")
  42. set(ver_string "vs${NUMBER}")
  43. set(${var} ${ver_string} PARENT_SCOPE)
  44. endfunction()
  45. include(CTestUseLaunchers)
  46. if(BUILD_TESTING)
  47. # Setup some auxiliary macros
  48. macro(SET_IF_NOT_SET var val)
  49. if(NOT DEFINED "${var}")
  50. set("${var}" "${val}")
  51. endif()
  52. endmacro()
  53. macro(SET_IF_SET var val)
  54. if(NOT "${val}" STREQUAL "")
  55. set("${var}" "${val}")
  56. endif()
  57. endmacro()
  58. macro(SET_IF_SET_AND_NOT_SET var val)
  59. if(NOT "${val}" STREQUAL "")
  60. SET_IF_NOT_SET("${var}" "${val}")
  61. endif()
  62. endmacro()
  63. # Make sure testing is enabled
  64. enable_testing()
  65. if(EXISTS "${PROJECT_SOURCE_DIR}/CTestConfig.cmake")
  66. include("${PROJECT_SOURCE_DIR}/CTestConfig.cmake")
  67. SET_IF_SET_AND_NOT_SET(NIGHTLY_START_TIME "${CTEST_NIGHTLY_START_TIME}")
  68. SET_IF_SET_AND_NOT_SET(SUBMIT_URL "${CTEST_SUBMIT_URL}")
  69. SET_IF_SET_AND_NOT_SET(DROP_METHOD "${CTEST_DROP_METHOD}")
  70. SET_IF_SET_AND_NOT_SET(DROP_SITE "${CTEST_DROP_SITE}")
  71. SET_IF_SET_AND_NOT_SET(DROP_SITE_USER "${CTEST_DROP_SITE_USER}")
  72. SET_IF_SET_AND_NOT_SET(DROP_SITE_PASSWORD "${CTEST_DROP_SITE_PASWORD}")
  73. SET_IF_SET_AND_NOT_SET(DROP_SITE_MODE "${CTEST_DROP_SITE_MODE}")
  74. SET_IF_SET_AND_NOT_SET(DROP_LOCATION "${CTEST_DROP_LOCATION}")
  75. SET_IF_SET_AND_NOT_SET(TRIGGER_SITE "${CTEST_TRIGGER_SITE}")
  76. SET_IF_SET_AND_NOT_SET(UPDATE_TYPE "${CTEST_UPDATE_TYPE}")
  77. endif()
  78. # the project can have a DartConfig.cmake file
  79. if(EXISTS "${PROJECT_SOURCE_DIR}/DartConfig.cmake")
  80. include("${PROJECT_SOURCE_DIR}/DartConfig.cmake")
  81. else()
  82. # Dashboard is opened for submissions for a 24 hour period starting at
  83. # the specified NIGHTLY_START_TIME. Time is specified in 24 hour format.
  84. SET_IF_NOT_SET (NIGHTLY_START_TIME "00:00:00 EDT")
  85. SET_IF_NOT_SET(DROP_METHOD "http")
  86. SET_IF_NOT_SET (COMPRESS_SUBMISSION ON)
  87. endif()
  88. SET_IF_NOT_SET (NIGHTLY_START_TIME "00:00:00 EDT")
  89. if(NOT SUBMIT_URL)
  90. set(SUBMIT_URL "${DROP_METHOD}://")
  91. if(DROP_SITE_USER)
  92. string(APPEND SUBMIT_URL "${DROP_SITE_USER}")
  93. if(DROP_SITE_PASSWORD)
  94. string(APPEND SUBMIT_URL ":${DROP_SITE_PASSWORD}")
  95. endif()
  96. string(APPEND SUBMIT_URL "@")
  97. endif()
  98. string(APPEND SUBMIT_URL "${DROP_SITE}${DROP_LOCATION}")
  99. endif()
  100. find_program(CVSCOMMAND cvs )
  101. set(CVS_UPDATE_OPTIONS "-d -A -P" CACHE STRING
  102. "Options passed to the cvs update command.")
  103. find_program(SVNCOMMAND svn)
  104. find_program(BZRCOMMAND bzr)
  105. find_program(HGCOMMAND hg)
  106. find_program(GITCOMMAND git)
  107. find_program(P4COMMAND p4)
  108. if(NOT UPDATE_TYPE)
  109. if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/CVS")
  110. set(UPDATE_TYPE cvs)
  111. elseif(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/.svn")
  112. set(UPDATE_TYPE svn)
  113. elseif(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/.bzr")
  114. set(UPDATE_TYPE bzr)
  115. elseif(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/.hg")
  116. set(UPDATE_TYPE hg)
  117. elseif(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/.git")
  118. set(UPDATE_TYPE git)
  119. endif()
  120. endif()
  121. string(TOLOWER "${UPDATE_TYPE}" _update_type)
  122. if("${_update_type}" STREQUAL "cvs")
  123. set(UPDATE_COMMAND "${CVSCOMMAND}")
  124. set(UPDATE_OPTIONS "${CVS_UPDATE_OPTIONS}")
  125. elseif("${_update_type}" STREQUAL "svn")
  126. set(UPDATE_COMMAND "${SVNCOMMAND}")
  127. set(UPDATE_OPTIONS "${SVN_UPDATE_OPTIONS}")
  128. elseif("${_update_type}" STREQUAL "bzr")
  129. set(UPDATE_COMMAND "${BZRCOMMAND}")
  130. set(UPDATE_OPTIONS "${BZR_UPDATE_OPTIONS}")
  131. elseif("${_update_type}" STREQUAL "hg")
  132. set(UPDATE_COMMAND "${HGCOMMAND}")
  133. set(UPDATE_OPTIONS "${HG_UPDATE_OPTIONS}")
  134. elseif("${_update_type}" STREQUAL "git")
  135. set(UPDATE_COMMAND "${GITCOMMAND}")
  136. set(UPDATE_OPTIONS "${GIT_UPDATE_OPTIONS}")
  137. elseif("${_update_type}" STREQUAL "p4")
  138. set(UPDATE_COMMAND "${P4COMMAND}")
  139. set(UPDATE_OPTIONS "${P4_UPDATE_OPTIONS}")
  140. endif()
  141. set(DART_TESTING_TIMEOUT 1500 CACHE STRING
  142. "Maximum time allowed before CTest will kill the test.")
  143. set(CTEST_SUBMIT_RETRY_DELAY 5 CACHE STRING
  144. "How long to wait between timed-out CTest submissions.")
  145. set(CTEST_SUBMIT_RETRY_COUNT 3 CACHE STRING
  146. "How many times to retry timed-out CTest submissions.")
  147. find_program(MEMORYCHECK_COMMAND
  148. NAMES purify valgrind boundscheck drmemory cuda-memcheck compute-sanitizer
  149. PATHS
  150. "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Rational Software\\Purify\\Setup;InstallFolder]"
  151. DOC "Path to the memory checking command, used for memory error detection."
  152. )
  153. set(MEMORYCHECK_SUPPRESSIONS_FILE "" CACHE FILEPATH
  154. "File that contains suppressions for the memory checker")
  155. find_program(COVERAGE_COMMAND gcov DOC
  156. "Path to the coverage program that CTest uses for performing coverage inspection"
  157. )
  158. set(COVERAGE_EXTRA_FLAGS "-l" CACHE STRING
  159. "Extra command line flags to pass to the coverage tool")
  160. # set the site name
  161. if(COMMAND cmake_host_system_information)
  162. cmake_host_system_information(RESULT _ctest_hostname QUERY HOSTNAME)
  163. set(SITE "${_ctest_hostname}" CACHE STRING "Name of the computer/site where compile is being run")
  164. unset(_ctest_hostname)
  165. else()
  166. # This code path is needed for CMake itself during bootstrap.
  167. site_name(SITE)
  168. endif()
  169. # set the build name
  170. if(NOT BUILDNAME)
  171. set(DART_COMPILER "${CMAKE_CXX_COMPILER}")
  172. if(NOT DART_COMPILER)
  173. set(DART_COMPILER "${CMAKE_C_COMPILER}")
  174. endif()
  175. if(NOT DART_COMPILER)
  176. set(DART_COMPILER "unknown")
  177. endif()
  178. if(WIN32)
  179. set(DART_NAME_COMPONENT "NAME_WE")
  180. else()
  181. set(DART_NAME_COMPONENT "NAME")
  182. endif()
  183. if(NOT BUILD_NAME_SYSTEM_NAME)
  184. set(BUILD_NAME_SYSTEM_NAME "${CMAKE_SYSTEM_NAME}")
  185. endif()
  186. if(WIN32)
  187. set(BUILD_NAME_SYSTEM_NAME "Win32")
  188. endif()
  189. if(UNIX OR BORLAND)
  190. get_filename_component(DART_COMPILER_NAME
  191. "${DART_COMPILER}" ${DART_NAME_COMPONENT})
  192. else()
  193. get_filename_component(DART_COMPILER_NAME
  194. "${CMAKE_MAKE_PROGRAM}" ${DART_NAME_COMPONENT})
  195. endif()
  196. if(DART_COMPILER_NAME MATCHES "devenv")
  197. GET_VS_VERSION_STRING("${CMAKE_GENERATOR}" DART_COMPILER_NAME)
  198. endif()
  199. set(BUILDNAME "${BUILD_NAME_SYSTEM_NAME}-${DART_COMPILER_NAME}")
  200. endif()
  201. # the build command
  202. build_command(MAKECOMMAND_DEFAULT_VALUE
  203. CONFIGURATION "\${CTEST_CONFIGURATION_TYPE}")
  204. set(MAKECOMMAND ${MAKECOMMAND_DEFAULT_VALUE}
  205. CACHE STRING "Command to build the project")
  206. # the default build configuration the ctest build handler will use
  207. # if there is no -C arg given to ctest:
  208. set(DEFAULT_CTEST_CONFIGURATION_TYPE "$ENV{CMAKE_CONFIG_TYPE}")
  209. if(DEFAULT_CTEST_CONFIGURATION_TYPE STREQUAL "")
  210. set(DEFAULT_CTEST_CONFIGURATION_TYPE "Release")
  211. endif()
  212. mark_as_advanced(
  213. BZRCOMMAND
  214. COVERAGE_COMMAND
  215. COVERAGE_EXTRA_FLAGS
  216. CTEST_SUBMIT_RETRY_DELAY
  217. CTEST_SUBMIT_RETRY_COUNT
  218. CVSCOMMAND
  219. CVS_UPDATE_OPTIONS
  220. DART_TESTING_TIMEOUT
  221. GITCOMMAND
  222. P4COMMAND
  223. HGCOMMAND
  224. MAKECOMMAND
  225. MEMORYCHECK_COMMAND
  226. MEMORYCHECK_SUPPRESSIONS_FILE
  227. SITE
  228. SVNCOMMAND
  229. )
  230. if(NOT RUN_FROM_DART)
  231. set(RUN_FROM_CTEST_OR_DART 1)
  232. include(CTestTargets)
  233. set(RUN_FROM_CTEST_OR_DART)
  234. endif()
  235. endif()