CTest.cmake 9 KB
Newer Older
1
2
3
# Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
# file Copyright.txt or https://cmake.org/licensing for details.

4
5
6
7
8
9
10
#[=======================================================================[.rst:
CTest
-----

Configure a project for testing with CTest/CDash

Include this module in the top CMakeLists.txt file of a project to
11
enable testing with CTest and dashboard submissions to CDash::
12

13
14
15
  project(MyProject)
  ...
  include(CTest)
16

17
18
19
The module automatically creates a ``BUILD_TESTING`` option that selects
whether to enable testing support (``ON`` by default).  After including
the module, use code like::
20

21
22
23
  if(BUILD_TESTING)
    # ... CMake code to create tests ...
  endif()
24
25
26

to creating tests when testing is enabled.

27
28
To enable submissions to a CDash server, create a ``CTestConfig.cmake``
file at the top of the project with content such as::
29

30
  set(CTEST_NIGHTLY_START_TIME "01:00:00 UTC")
31
  set(CTEST_SUBMIT_URL "http://my.cdash.org/submit.php?project=MyProject")
32
33

(the CDash server can provide the file to a project administrator who
34
35
36
configures ``MyProject``).  Settings in the config file are shared by
both this ``CTest`` module and the :manual:`ctest(1)` command-line
:ref:`Dashboard Client` mode (``ctest -S``).
37
38
39
40
41

While building a project for submission to CDash, CTest scans the
build output for errors and warnings and reports them with surrounding
context from the build log.  This generic approach works for all build
tools, but does not give details about the command invocation that
42
43
produced a given problem.  One may get more detailed reports by setting
the :variable:`CTEST_USE_LAUNCHERS` variable::
44

45
  set(CTEST_USE_LAUNCHERS 1)
46

47
in the ``CTestConfig.cmake`` file.
48
#]=======================================================================]
49

50
option(BUILD_TESTING "Build the testing tree." ON)
Andy Cedilnik's avatar
Andy Cedilnik committed
51

52
# function to turn generator name into a version string
53
# like vs9 or vs10
54
55
function(GET_VS_VERSION_STRING generator var)
  string(REGEX REPLACE "Visual Studio ([0-9][0-9]?)($|.*)" "\\1"
56
    NUMBER "${generator}")
57
58
    set(ver_string "vs${NUMBER}")
  set(${var} ${ver_string} PARENT_SCOPE)
59
endfunction()
60

61
62
include(CTestUseLaunchers)

63
if(BUILD_TESTING)
luzpaz's avatar
luzpaz committed
64
  # Setup some auxiliary macros
65
66
67
  macro(SET_IF_NOT_SET var val)
    if(NOT DEFINED "${var}")
      set("${var}" "${val}")
68
69
    endif()
  endmacro()
Andy Cedilnik's avatar
Andy Cedilnik committed
70

71
  macro(SET_IF_SET var val)
72
    if(NOT "${val}" STREQUAL "")
73
      set("${var}" "${val}")
74
75
    endif()
  endmacro()
Andy Cedilnik's avatar
Andy Cedilnik committed
76

77
  macro(SET_IF_SET_AND_NOT_SET var val)
78
    if(NOT "${val}" STREQUAL "")
79
      SET_IF_NOT_SET("${var}" "${val}")
80
81
    endif()
  endmacro()
82

Andy Cedilnik's avatar
Andy Cedilnik committed
83
  # Make sure testing is enabled
84
  enable_testing()
Andy Cedilnik's avatar
Andy Cedilnik committed
85

86
87
  if(EXISTS "${PROJECT_SOURCE_DIR}/CTestConfig.cmake")
    include("${PROJECT_SOURCE_DIR}/CTestConfig.cmake")
88
    SET_IF_SET_AND_NOT_SET(NIGHTLY_START_TIME "${CTEST_NIGHTLY_START_TIME}")
wahikihiki's avatar
wahikihiki committed
89
    SET_IF_SET_AND_NOT_SET(SUBMIT_URL "${CTEST_SUBMIT_URL}")
90
91
92
93
94
95
96
    SET_IF_SET_AND_NOT_SET(DROP_METHOD "${CTEST_DROP_METHOD}")
    SET_IF_SET_AND_NOT_SET(DROP_SITE "${CTEST_DROP_SITE}")
    SET_IF_SET_AND_NOT_SET(DROP_SITE_USER "${CTEST_DROP_SITE_USER}")
    SET_IF_SET_AND_NOT_SET(DROP_SITE_PASSWORD "${CTEST_DROP_SITE_PASWORD}")
    SET_IF_SET_AND_NOT_SET(DROP_SITE_MODE "${CTEST_DROP_SITE_MODE}")
    SET_IF_SET_AND_NOT_SET(DROP_LOCATION "${CTEST_DROP_LOCATION}")
    SET_IF_SET_AND_NOT_SET(TRIGGER_SITE "${CTEST_TRIGGER_SITE}")
Alexander Neundorf's avatar
   
Alexander Neundorf committed
97
    SET_IF_SET_AND_NOT_SET(UPDATE_TYPE "${CTEST_UPDATE_TYPE}")
98
  endif()
99

Andy Cedilnik's avatar
Andy Cedilnik committed
100
  # the project can have a DartConfig.cmake file
101
102
  if(EXISTS "${PROJECT_SOURCE_DIR}/DartConfig.cmake")
    include("${PROJECT_SOURCE_DIR}/DartConfig.cmake")
103
  else()
Andy Cedilnik's avatar
Andy Cedilnik committed
104
105
106
    # Dashboard is opened for submissions for a 24 hour period starting at
    # the specified NIGHTLY_START_TIME. Time is specified in 24 hour format.
    SET_IF_NOT_SET (NIGHTLY_START_TIME "00:00:00 EDT")
Andy Cedilnik's avatar
Andy Cedilnik committed
107
    SET_IF_NOT_SET(DROP_METHOD "http")
Andy Cedilnik's avatar
Andy Cedilnik committed
108
    SET_IF_NOT_SET (COMPRESS_SUBMISSION ON)
109
  endif()
110
  SET_IF_NOT_SET (NIGHTLY_START_TIME "00:00:00 EDT")
Andy Cedilnik's avatar
Andy Cedilnik committed
111

wahikihiki's avatar
wahikihiki committed
112
113
114
115
116
117
118
119
120
  if(NOT SUBMIT_URL)
    set(SUBMIT_URL "${DROP_METHOD}://")
    if(DROP_SITE_USER)
      string(APPEND SUBMIT_URL "${DROP_SITE_USER}")
      if(DROP_SITE_PASSWORD)
        string(APPEND SUBMIT_URL ":${DROP_SITE_PASSWORD}")
      endif()
      string(APPEND SUBMIT_URL "@")
    endif()
wahikihiki's avatar
wahikihiki committed
121
    string(APPEND SUBMIT_URL "${DROP_SITE}${DROP_LOCATION}")
wahikihiki's avatar
wahikihiki committed
122
123
  endif()

124
125
126
127
128
129
130
131
132
133
134
135
  if(NOT UPDATE_TYPE)
    if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/CVS")
      set(UPDATE_TYPE cvs)
    elseif(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/.svn")
      set(UPDATE_TYPE svn)
    elseif(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/.bzr")
      set(UPDATE_TYPE bzr)
    elseif(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/.hg")
      set(UPDATE_TYPE hg)
    elseif(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/.git")
      set(UPDATE_TYPE git)
    endif()
136
  endif()
Andy Cedilnik's avatar
Andy Cedilnik committed
137

138
139
  string(TOLOWER "${UPDATE_TYPE}" _update_type)
  if("${_update_type}" STREQUAL "cvs")
140
141
142
    find_program(CVSCOMMAND cvs )
    set(CVS_UPDATE_OPTIONS "-d -A -P" CACHE STRING
      "Options passed to the cvs update command.")
143
144
145
    set(UPDATE_COMMAND "${CVSCOMMAND}")
    set(UPDATE_OPTIONS "${CVS_UPDATE_OPTIONS}")
  elseif("${_update_type}" STREQUAL "svn")
146
    find_program(SVNCOMMAND svn)
147
148
149
    set(UPDATE_COMMAND "${SVNCOMMAND}")
    set(UPDATE_OPTIONS "${SVN_UPDATE_OPTIONS}")
  elseif("${_update_type}" STREQUAL "bzr")
150
    find_program(BZRCOMMAND bzr)
151
152
153
    set(UPDATE_COMMAND "${BZRCOMMAND}")
    set(UPDATE_OPTIONS "${BZR_UPDATE_OPTIONS}")
  elseif("${_update_type}" STREQUAL "hg")
154
    find_program(HGCOMMAND hg)
155
156
157
    set(UPDATE_COMMAND "${HGCOMMAND}")
    set(UPDATE_OPTIONS "${HG_UPDATE_OPTIONS}")
  elseif("${_update_type}" STREQUAL "git")
158
    find_program(GITCOMMAND git)
159
160
    set(UPDATE_COMMAND "${GITCOMMAND}")
    set(UPDATE_OPTIONS "${GIT_UPDATE_OPTIONS}")
161
  elseif("${_update_type}" STREQUAL "p4")
162
    find_program(P4COMMAND p4)
163
164
    set(UPDATE_COMMAND "${P4COMMAND}")
    set(UPDATE_OPTIONS "${P4_UPDATE_OPTIONS}")
165
  endif()
Andy Cedilnik's avatar
Andy Cedilnik committed
166

167
  set(DART_TESTING_TIMEOUT 1500 CACHE STRING
Ken Martin's avatar
Ken Martin committed
168
    "Maximum time allowed before CTest will kill the test.")
Andy Cedilnik's avatar
Andy Cedilnik committed
169

170
  set(CTEST_SUBMIT_RETRY_DELAY 5 CACHE STRING
171
    "How long to wait between timed-out CTest submissions.")
172
  set(CTEST_SUBMIT_RETRY_COUNT 3 CACHE STRING
173
174
    "How many times to retry timed-out CTest submissions.")

175
  find_program(MEMORYCHECK_COMMAND
176
    NAMES purify valgrind boundscheck drmemory cuda-memcheck compute-sanitizer
Andy Cedilnik's avatar
Andy Cedilnik committed
177
178
179
180
    PATHS
    "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Rational Software\\Purify\\Setup;InstallFolder]"
    DOC "Path to the memory checking command, used for memory error detection."
    )
181
  set(MEMORYCHECK_SUPPRESSIONS_FILE "" CACHE FILEPATH
Ken Martin's avatar
Ken Martin committed
182
    "File that contains suppressions for the memory checker")
183
  find_program(COVERAGE_COMMAND gcov DOC
Ken Martin's avatar
Ken Martin committed
184
185
    "Path to the coverage program that CTest uses for performing coverage inspection"
    )
186
  set(COVERAGE_EXTRA_FLAGS "-l" CACHE STRING
187
    "Extra command line flags to pass to the coverage tool")
Andy Cedilnik's avatar
Andy Cedilnik committed
188
189

  # set the site name
190
191
192
193
194
195
196
197
  if(COMMAND cmake_host_system_information)
    cmake_host_system_information(RESULT _ctest_hostname QUERY HOSTNAME)
    set(SITE "${_ctest_hostname}" CACHE STRING "Name of the computer/site where compile is being run")
    unset(_ctest_hostname)
  else()
    # This code path is needed for CMake itself during bootstrap.
    site_name(SITE)
  endif()
Andy Cedilnik's avatar
Andy Cedilnik committed
198
  # set the build name
199
200
201
202
  if(NOT BUILDNAME)
    set(DART_COMPILER "${CMAKE_CXX_COMPILER}")
    if(NOT DART_COMPILER)
      set(DART_COMPILER "${CMAKE_C_COMPILER}")
203
    endif()
204
205
    if(NOT DART_COMPILER)
      set(DART_COMPILER "unknown")
206
    endif()
207
208
    if(WIN32)
      set(DART_NAME_COMPONENT "NAME_WE")
209
    else()
210
      set(DART_NAME_COMPONENT "NAME")
211
    endif()
212
213
    if(NOT BUILD_NAME_SYSTEM_NAME)
      set(BUILD_NAME_SYSTEM_NAME "${CMAKE_SYSTEM_NAME}")
214
    endif()
215
216
    if(WIN32)
      set(BUILD_NAME_SYSTEM_NAME "Win32")
217
    endif()
218
    if(UNIX OR BORLAND)
219
220
      get_filename_component(DART_COMPILER_NAME
        "${DART_COMPILER}" ${DART_NAME_COMPONENT})
221
    else()
222
      get_filename_component(DART_COMPILER_NAME
223
        "${CMAKE_MAKE_PROGRAM}" ${DART_NAME_COMPONENT})
224
    endif()
225
226
    if(DART_COMPILER_NAME MATCHES "devenv")
      GET_VS_VERSION_STRING("${CMAKE_GENERATOR}" DART_COMPILER_NAME)
227
    endif()
228
    set(BUILDNAME "${BUILD_NAME_SYSTEM_NAME}-${DART_COMPILER_NAME}")
229
  endif()
230
231

  # the build command
232
  build_command(MAKECOMMAND_DEFAULT_VALUE
233
    CONFIGURATION "\${CTEST_CONFIGURATION_TYPE}")
234
  set(MAKECOMMAND ${MAKECOMMAND_DEFAULT_VALUE}
235
    CACHE STRING "Command to build the project")
236
237
238

  # the default build configuration the ctest build handler will use
  # if there is no -C arg given to ctest:
239
240
241
  set(DEFAULT_CTEST_CONFIGURATION_TYPE "$ENV{CMAKE_CONFIG_TYPE}")
  if(DEFAULT_CTEST_CONFIGURATION_TYPE STREQUAL "")
    set(DEFAULT_CTEST_CONFIGURATION_TYPE "Release")
242
  endif()
Andy Cedilnik's avatar
Andy Cedilnik committed
243

244
  mark_as_advanced(
245
    BZRCOMMAND
Andy Cedilnik's avatar
Andy Cedilnik committed
246
    COVERAGE_COMMAND
247
    COVERAGE_EXTRA_FLAGS
248
249
    CTEST_SUBMIT_RETRY_DELAY
    CTEST_SUBMIT_RETRY_COUNT
Andy Cedilnik's avatar
Andy Cedilnik committed
250
251
    CVSCOMMAND
    CVS_UPDATE_OPTIONS
252
253
    DART_TESTING_TIMEOUT
    GITCOMMAND
254
    P4COMMAND
255
    HGCOMMAND
256
    MAKECOMMAND
Andy Cedilnik's avatar
Andy Cedilnik committed
257
258
    MEMORYCHECK_COMMAND
    MEMORYCHECK_SUPPRESSIONS_FILE
259
    SITE
260
    SVNCOMMAND
Andy Cedilnik's avatar
Andy Cedilnik committed
261
    )
262
263
264
265
  if(NOT RUN_FROM_DART)
    set(RUN_FROM_CTEST_OR_DART 1)
    include(CTestTargets)
    set(RUN_FROM_CTEST_OR_DART)
266
267
  endif()
endif()