CTest.cmake 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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
  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. find_program(SLURM_SBATCH_COMMAND sbatch DOC
  154. "Path to the SLURM sbatch executable"
  155. )
  156. find_program(SLURM_SRUN_COMMAND srun DOC
  157. "Path to the SLURM srun executable"
  158. )
  159. set(MEMORYCHECK_SUPPRESSIONS_FILE "" CACHE FILEPATH
  160. "File that contains suppressions for the memory checker")
  161. find_program(COVERAGE_COMMAND gcov DOC
  162. "Path to the coverage program that CTest uses for performing coverage inspection"
  163. )
  164. set(COVERAGE_EXTRA_FLAGS "-l" CACHE STRING
  165. "Extra command line flags to pass to the coverage tool")
  166. # set the site name
  167. site_name(SITE)
  168. # set the build name
  169. if(NOT BUILDNAME)
  170. set(DART_COMPILER "${CMAKE_CXX_COMPILER}")
  171. if(NOT DART_COMPILER)
  172. set(DART_COMPILER "${CMAKE_C_COMPILER}")
  173. endif()
  174. if(NOT DART_COMPILER)
  175. set(DART_COMPILER "unknown")
  176. endif()
  177. if(WIN32)
  178. set(DART_NAME_COMPONENT "NAME_WE")
  179. else()
  180. set(DART_NAME_COMPONENT "NAME")
  181. endif()
  182. if(NOT BUILD_NAME_SYSTEM_NAME)
  183. set(BUILD_NAME_SYSTEM_NAME "${CMAKE_SYSTEM_NAME}")
  184. endif()
  185. if(WIN32)
  186. set(BUILD_NAME_SYSTEM_NAME "Win32")
  187. endif()
  188. if(UNIX OR BORLAND)
  189. get_filename_component(DART_COMPILER_NAME
  190. "${DART_COMPILER}" ${DART_NAME_COMPONENT})
  191. else()
  192. get_filename_component(DART_COMPILER_NAME
  193. "${CMAKE_MAKE_PROGRAM}" ${DART_NAME_COMPONENT})
  194. endif()
  195. if(DART_COMPILER_NAME MATCHES "devenv")
  196. GET_VS_VERSION_STRING("${CMAKE_GENERATOR}" DART_COMPILER_NAME)
  197. endif()
  198. set(BUILDNAME "${BUILD_NAME_SYSTEM_NAME}-${DART_COMPILER_NAME}")
  199. endif()
  200. # the build command
  201. build_command(MAKECOMMAND_DEFAULT_VALUE
  202. CONFIGURATION "\${CTEST_CONFIGURATION_TYPE}")
  203. set(MAKECOMMAND ${MAKECOMMAND_DEFAULT_VALUE}
  204. CACHE STRING "Command to build the project")
  205. # the default build configuration the ctest build handler will use
  206. # if there is no -C arg given to ctest:
  207. set(DEFAULT_CTEST_CONFIGURATION_TYPE "$ENV{CMAKE_CONFIG_TYPE}")
  208. if(DEFAULT_CTEST_CONFIGURATION_TYPE STREQUAL "")
  209. set(DEFAULT_CTEST_CONFIGURATION_TYPE "Release")
  210. endif()
  211. mark_as_advanced(
  212. BZRCOMMAND
  213. BZR_UPDATE_OPTIONS
  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. PURIFYCOMMAND
  228. SCPCOMMAND
  229. SLURM_SBATCH_COMMAND
  230. SLURM_SRUN_COMMAND
  231. SITE
  232. SVNCOMMAND
  233. SVN_UPDATE_OPTIONS
  234. )
  235. if(NOT RUN_FROM_DART)
  236. set(RUN_FROM_CTEST_OR_DART 1)
  237. include(CTestTargets)
  238. set(RUN_FROM_CTEST_OR_DART)
  239. endif()
  240. endif()