CTest.cmake 8.7 KB

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