Imported Upstream version 2.8.10.2
[platform/upstream/cmake.git] / Modules / CTest.cmake
1 # - Configure a project for testing with CTest/CDash
2 # Include this module in the top CMakeLists.txt file of a project to
3 # enable testing with CTest and dashboard submissions to CDash:
4 #   project(MyProject)
5 #   ...
6 #   include(CTest)
7 # The module automatically creates a BUILD_TESTING option that selects
8 # whether to enable testing support (ON by default).  After including
9 # the module, use code like
10 #   if(BUILD_TESTING)
11 #     # ... CMake code to create tests ...
12 #   endif()
13 # to creating tests when testing is enabled.
14 #
15 # To enable submissions to a CDash server, create a CTestConfig.cmake
16 # file at the top of the project with content such as
17 #   set(CTEST_PROJECT_NAME "MyProject")
18 #   set(CTEST_NIGHTLY_START_TIME "01:00:00 UTC")
19 #   set(CTEST_DROP_METHOD "http")
20 #   set(CTEST_DROP_SITE "my.cdash.org")
21 #   set(CTEST_DROP_LOCATION "/submit.php?project=MyProject")
22 #   set(CTEST_DROP_SITE_CDASH TRUE)
23 # (the CDash server can provide the file to a project administrator
24 # who configures 'MyProject').
25 # Settings in the config file are shared by both this CTest module and
26 # the CTest command-line tool's dashboard script mode (ctest -S).
27 #
28 # While building a project for submission to CDash, CTest scans the
29 # build output for errors and warnings and reports them with
30 # surrounding context from the build log.  This generic approach works
31 # for all build tools, but does not give details about the command
32 # invocation that produced a given problem.  One may get more detailed
33 # reports by adding
34 #   set(CTEST_USE_LAUNCHERS 1)
35 # to the CTestConfig.cmake file.  When this option is enabled, the
36 # CTest module tells CMake's Makefile generators to invoke every
37 # command in the generated build system through a CTest launcher
38 # program.  (Currently the CTEST_USE_LAUNCHERS option is ignored on
39 # non-Makefile generators.)  During a manual build each launcher
40 # transparently runs the command it wraps.  During a CTest-driven
41 # build for submission to CDash each launcher reports detailed
42 # information when its command fails or warns.
43 # (Setting CTEST_USE_LAUNCHERS in CTestConfig.cmake is convenient, but
44 # also adds the launcher overhead even for manual builds.  One may
45 # instead set it in a CTest dashboard script and add it to the CMake
46 # cache for the build tree.)
47
48 #=============================================================================
49 # Copyright 2005-2009 Kitware, Inc.
50 #
51 # Distributed under the OSI-approved BSD License (the "License");
52 # see accompanying file Copyright.txt for details.
53 #
54 # This software is distributed WITHOUT ANY WARRANTY; without even the
55 # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
56 # See the License for more information.
57 #=============================================================================
58 # (To distribute this file outside of CMake, substitute the full
59 #  License text for the above reference.)
60
61 option(BUILD_TESTING "Build the testing tree." ON)
62
63 # function to turn generator name into a version string
64 # like vs7 vs71 vs8 vs9
65 function(GET_VS_VERSION_STRING generator var)
66   string(REGEX REPLACE "Visual Studio ([0-9][0-9]?)($|.*)" "\\1"
67     NUMBER "${generator}")
68   if("${generator}" MATCHES "Visual Studio 7 .NET 2003")
69     set(ver_string "vs71")
70   else()
71     set(ver_string "vs${NUMBER}")
72   endif()
73   set(${var} ${ver_string} PARENT_SCOPE)
74 endfunction()
75
76 include(CTestUseLaunchers)
77
78 if(BUILD_TESTING)
79   # Setup some auxilary macros
80   macro(SET_IF_NOT_SET var val)
81     if(NOT DEFINED "${var}")
82       set("${var}" "${val}")
83     endif()
84   endmacro()
85
86   macro(SET_IF_SET var val)
87     if(NOT "${val}" MATCHES "^$")
88       set("${var}" "${val}")
89     endif()
90   endmacro()
91
92   macro(SET_IF_SET_AND_NOT_SET var val)
93     if(NOT "${val}" MATCHES "^$")
94       SET_IF_NOT_SET("${var}" "${val}")
95     endif()
96   endmacro()
97
98   # Make sure testing is enabled
99   enable_testing()
100
101   if(EXISTS "${PROJECT_SOURCE_DIR}/CTestConfig.cmake")
102     include("${PROJECT_SOURCE_DIR}/CTestConfig.cmake")
103     SET_IF_SET_AND_NOT_SET(NIGHTLY_START_TIME "${CTEST_NIGHTLY_START_TIME}")
104     SET_IF_SET_AND_NOT_SET(DROP_METHOD "${CTEST_DROP_METHOD}")
105     SET_IF_SET_AND_NOT_SET(DROP_SITE "${CTEST_DROP_SITE}")
106     SET_IF_SET_AND_NOT_SET(DROP_SITE_USER "${CTEST_DROP_SITE_USER}")
107     SET_IF_SET_AND_NOT_SET(DROP_SITE_PASSWORD "${CTEST_DROP_SITE_PASWORD}")
108     SET_IF_SET_AND_NOT_SET(DROP_SITE_MODE "${CTEST_DROP_SITE_MODE}")
109     SET_IF_SET_AND_NOT_SET(DROP_LOCATION "${CTEST_DROP_LOCATION}")
110     SET_IF_SET_AND_NOT_SET(TRIGGER_SITE "${CTEST_TRIGGER_SITE}")
111     SET_IF_SET_AND_NOT_SET(UPDATE_TYPE "${CTEST_UPDATE_TYPE}")
112   endif()
113
114   # the project can have a DartConfig.cmake file
115   if(EXISTS "${PROJECT_SOURCE_DIR}/DartConfig.cmake")
116     include("${PROJECT_SOURCE_DIR}/DartConfig.cmake")
117   else()
118     # Dashboard is opened for submissions for a 24 hour period starting at
119     # the specified NIGHTLY_START_TIME. Time is specified in 24 hour format.
120     SET_IF_NOT_SET (NIGHTLY_START_TIME "00:00:00 EDT")
121     SET_IF_NOT_SET(DROP_METHOD "http")
122     SET_IF_NOT_SET (COMPRESS_SUBMISSION ON)
123   endif()
124   SET_IF_NOT_SET (NIGHTLY_START_TIME "00:00:00 EDT")
125
126   find_program(CVSCOMMAND cvs )
127   set(CVS_UPDATE_OPTIONS "-d -A -P" CACHE STRING
128     "Options passed to the cvs update command.")
129   find_program(SVNCOMMAND svn)
130   find_program(BZRCOMMAND bzr)
131   find_program(HGCOMMAND hg)
132   find_program(GITCOMMAND git)
133
134   if(NOT UPDATE_TYPE)
135     if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/CVS")
136       set(UPDATE_TYPE cvs)
137     elseif(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/.svn")
138       set(UPDATE_TYPE svn)
139     elseif(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/.bzr")
140       set(UPDATE_TYPE bzr)
141     elseif(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/.hg")
142       set(UPDATE_TYPE hg)
143     elseif(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/.git")
144       set(UPDATE_TYPE git)
145     endif()
146   endif()
147
148   string(TOLOWER "${UPDATE_TYPE}" _update_type)
149   if("${_update_type}" STREQUAL "cvs")
150     set(UPDATE_COMMAND "${CVSCOMMAND}")
151     set(UPDATE_OPTIONS "${CVS_UPDATE_OPTIONS}")
152   elseif("${_update_type}" STREQUAL "svn")
153     set(UPDATE_COMMAND "${SVNCOMMAND}")
154     set(UPDATE_OPTIONS "${SVN_UPDATE_OPTIONS}")
155   elseif("${_update_type}" STREQUAL "bzr")
156     set(UPDATE_COMMAND "${BZRCOMMAND}")
157     set(UPDATE_OPTIONS "${BZR_UPDATE_OPTIONS}")
158   elseif("${_update_type}" STREQUAL "hg")
159     set(UPDATE_COMMAND "${HGCOMMAND}")
160     set(UPDATE_OPTIONS "${HG_UPDATE_OPTIONS}")
161   elseif("${_update_type}" STREQUAL "git")
162     set(UPDATE_COMMAND "${GITCOMMAND}")
163     set(UPDATE_OPTIONS "${GIT_UPDATE_OPTIONS}")
164   endif()
165
166   set(DART_TESTING_TIMEOUT 1500 CACHE STRING
167     "Maximum time allowed before CTest will kill the test.")
168
169   set(CTEST_SUBMIT_RETRY_DELAY 5 CACHE STRING
170     "How long to wait between timed-out CTest submissions.")
171   set(CTEST_SUBMIT_RETRY_COUNT 3 CACHE STRING
172     "How many times to retry timed-out CTest submissions.")
173
174   find_program(MEMORYCHECK_COMMAND
175     NAMES purify valgrind boundscheck
176     PATHS
177     "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Rational Software\\Purify\\Setup;InstallFolder]"
178     DOC "Path to the memory checking command, used for memory error detection."
179     )
180   find_program(SLURM_SBATCH_COMMAND sbatch DOC
181     "Path to the SLURM sbatch executable"
182     )
183   find_program(SLURM_SRUN_COMMAND srun DOC
184     "Path to the SLURM srun executable"
185     )
186   set(MEMORYCHECK_SUPPRESSIONS_FILE "" CACHE FILEPATH
187     "File that contains suppressions for the memory checker")
188   find_program(SCPCOMMAND scp DOC
189     "Path to scp command, used by CTest for submitting results to a Dart server"
190     )
191   find_program(COVERAGE_COMMAND gcov DOC
192     "Path to the coverage program that CTest uses for performing coverage inspection"
193     )
194   set(COVERAGE_EXTRA_FLAGS "-l" CACHE STRING
195     "Extra command line flags to pass to the coverage tool")
196
197   # set the site name
198   site_name(SITE)
199   # set the build name
200   if(NOT BUILDNAME)
201     set(DART_COMPILER "${CMAKE_CXX_COMPILER}")
202     if(NOT DART_COMPILER)
203       set(DART_COMPILER "${CMAKE_C_COMPILER}")
204     endif()
205     if(NOT DART_COMPILER)
206       set(DART_COMPILER "unknown")
207     endif()
208     if(WIN32)
209       set(DART_NAME_COMPONENT "NAME_WE")
210     else()
211       set(DART_NAME_COMPONENT "NAME")
212     endif()
213     if(NOT BUILD_NAME_SYSTEM_NAME)
214       set(BUILD_NAME_SYSTEM_NAME "${CMAKE_SYSTEM_NAME}")
215     endif()
216     if(WIN32)
217       set(BUILD_NAME_SYSTEM_NAME "Win32")
218     endif()
219     if(UNIX OR BORLAND)
220       get_filename_component(DART_CXX_NAME
221         "${CMAKE_CXX_COMPILER}" ${DART_NAME_COMPONENT})
222     else()
223       get_filename_component(DART_CXX_NAME
224         "${CMAKE_BUILD_TOOL}" ${DART_NAME_COMPONENT})
225     endif()
226     if(DART_CXX_NAME MATCHES "msdev")
227       set(DART_CXX_NAME "vs60")
228     endif()
229     if(DART_CXX_NAME MATCHES "devenv")
230       GET_VS_VERSION_STRING("${CMAKE_GENERATOR}" DART_CXX_NAME)
231     endif()
232     set(BUILDNAME "${BUILD_NAME_SYSTEM_NAME}-${DART_CXX_NAME}")
233   endif()
234
235   # the build command
236   build_command(MAKECOMMAND_DEFAULT_VALUE
237     CONFIGURATION "\${CTEST_CONFIGURATION_TYPE}")
238   set(MAKECOMMAND ${MAKECOMMAND_DEFAULT_VALUE}
239     CACHE STRING "Command to build the project")
240
241   # the default build configuration the ctest build handler will use
242   # if there is no -C arg given to ctest:
243   set(DEFAULT_CTEST_CONFIGURATION_TYPE "$ENV{CMAKE_CONFIG_TYPE}")
244   if(DEFAULT_CTEST_CONFIGURATION_TYPE STREQUAL "")
245     set(DEFAULT_CTEST_CONFIGURATION_TYPE "Release")
246   endif()
247
248   mark_as_advanced(
249     BZRCOMMAND
250     BZR_UPDATE_OPTIONS
251     COVERAGE_COMMAND
252     COVERAGE_EXTRA_FLAGS
253     CTEST_SUBMIT_RETRY_DELAY
254     CTEST_SUBMIT_RETRY_COUNT
255     CVSCOMMAND
256     CVS_UPDATE_OPTIONS
257     DART_TESTING_TIMEOUT
258     GITCOMMAND
259     HGCOMMAND
260     MAKECOMMAND
261     MEMORYCHECK_COMMAND
262     MEMORYCHECK_SUPPRESSIONS_FILE
263     PURIFYCOMMAND
264     SCPCOMMAND
265     SLURM_SBATCH_COMMAND
266     SLURM_SRUN_COMMAND
267     SITE
268     SVNCOMMAND
269     SVN_UPDATE_OPTIONS
270     )
271   if(NOT RUN_FROM_DART)
272     set(RUN_FROM_CTEST_OR_DART 1)
273     include(CTestTargets)
274     set(RUN_FROM_CTEST_OR_DART)
275   endif()
276 endif()