This repository has been archived by the owner on Jan 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
ci_utils.cmake
314 lines (286 loc) · 14.4 KB
/
ci_utils.cmake
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#==================================================================================================#
# #
# Copyright 2012 MaidSafe.net limited #
# #
# This MaidSafe Software is licensed to you under (1) the MaidSafe.net Commercial License, #
# version 1.0 or later, or (2) The General Public License (GPL), version 3, depending on which #
# licence you accepted on initial access to the Software (the "Licences"). #
# #
# By contributing code to the MaidSafe Software, or to this project generally, you agree to be #
# bound by the terms of the MaidSafe Contributor Agreement, version 1.0, found in the root #
# directory of this project at LICENSE, COPYING and CONTRIBUTOR respectively and also available #
# at: http://www.maidsafe.net/licenses #
# #
# Unless required by applicable law or agreed to in writing, the MaidSafe Software distributed #
# under the GPL Licence is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF #
# ANY KIND, either express or implied. #
# #
# See the Licences for the specific language governing permissions and limitations relating to #
# use of the MaidSafe Software. #
# #
#==================================================================================================#
# #
# Helper module used in running CI on all submodules of MaidSafe/MaidSafe #
# #
#==================================================================================================#
set(SelfGitFetchCheck ON)
set(FetchFailed OFF)
function(checkout_to_branch SourceDir Branch)
execute_process(COMMAND ${CTEST_GIT_COMMAND} checkout ${Branch}
WORKING_DIRECTORY ${SourceDir}
RESULT_VARIABLE ResultVar
OUTPUT_VARIABLE OutputVar
ERROR_VARIABLE ErrorVar)
if(NOT ${ResultVar} EQUAL 0)
message(FATAL_ERROR "Failed to switch to '${Branch}' in ${SourceDir}:\n\n${ErrorVar}")
endif()
endfunction()
macro(get_tag)
file(STRINGS ${CTEST_BINARY_DIRECTORY}/Testing/TAG TagId LIMIT_COUNT 1)
endmacro()
function(get_git_log SourceDir CommitHash CommitMessage Author)
execute_process(COMMAND ${CTEST_GIT_COMMAND} rev-parse --verify HEAD
WORKING_DIRECTORY ${${SourceDir}}
RESULT_VARIABLE ResultVar
OUTPUT_VARIABLE ${CommitHash}
OUTPUT_STRIP_TRAILING_WHITESPACE)
if(${ResultVar} EQUAL 0)
execute_process(COMMAND ${CTEST_GIT_COMMAND} log -1 --format="Hash: %H%nAuthor: %an%nCommitter: %cn%nCommit Message: %s"
WORKING_DIRECTORY ${${SourceDir}}
RESULT_VARIABLE ResultVar
OUTPUT_VARIABLE ${CommitMessage})
if(${ResultVar} EQUAL 0)
string(REPLACE "\"" "" ${CommitMessage} ${${CommitMessage}})
string(REGEX MATCH "Author: ([^\n]+)" FoundVar ${${CommitMessage}})
set(${Author} ${CMAKE_MATCH_1})
else()
set(${CommitMessage} "N/A")
set(${Author} "N/A")
endif()
else()
set(${${CommitHash}} "unknown")
endif()
set(${CommitHash} ${${CommitHash}} PARENT_SCOPE)
set(${CommitMessage} ${${CommitMessage}} PARENT_SCOPE)
set(${Author} ${${Author}} PARENT_SCOPE)
endfunction()
# Calls git fetch via execute_process to check error code
# Current workaround to the ctest_update = 0 regardless of git fetch cmd failure
# Relate CMake Bugs: http://www.cmake.org/Bug/view.php?id=8262 && http://www.cmake.org/Bug/view.php?id=8277
function(check_fetch_failure)
execute_process(COMMAND ${CTEST_GIT_COMMAND} fetch --dry-run
WORKING_DIRECTORY ${CTEST_SOURCE_DIRECTORY}
RESULT_VARIABLE FetchResult
OUTPUT_VARIABLE OutputVar
ERROR_VARIABLE ErrorVar)
if(FetchResult EQUAL 0)
set(FetchFailed OFF PARENT_SCOPE)
else()
set(FetchFailed ON PARENT_SCOPE)
endif()
endfunction()
function(update_super_project)
math(EXPR UpdateAttempts ${UpdateAttempts}+1)
set(UpdateAttempts ${UpdateAttempts} PARENT_SCOPE)
message("Updating super project, attempt ${UpdateAttempts} of ${MaxUpdateAttempts}")
if(DashboardModel STREQUAL "Continuous")
get_git_log(CTEST_SOURCE_DIRECTORY SuperOldCommit SuperOldCommitLogMsg SuperOldCommitLogAuthor)
execute_process(COMMAND ${CTEST_GIT_COMMAND} pull
WORKING_DIRECTORY ${CTEST_SOURCE_DIRECTORY}
RESULT_VARIABLE ResultVar
OUTPUT_VARIABLE OutputVar
ERROR_VARIABLE ErrorVar)
set(UpdateSuperResult ${ResultVar} PARENT_SCOPE)
if(NOT ${ResultVar} EQUAL 0)
message(WARNING "Super project failed to update. Sleeping for 30 seconds.")
ctest_sleep(30)
return()
#message(WARNING "Failed to pull super project:\n\n${ErrorVar}")
endif()
get_git_log(CTEST_SOURCE_DIRECTORY SuperCurrentCommit SuperCurrentCommitLogMsg SuperCurrentCommitLogAuthor)
if(${SuperCurrentCommit} STREQUAL ${SuperOldCommit})
set(RunAll OFF PARENT_SCOPE)
endif()
elseif(DashboardModel STREQUAL "Nightly" OR DashboardModel STREQUAL "Weekly")
message("Updating super project")
ctest_update(RETURN_VALUE UpdatedCount)
if(UpdatedCount LESS 0)
set(UpdateSuperResult ${UpdatedCount} PARENT_SCOPE)
message(WARNING "Super project failed to update. Sleeping for 30 seconds.")
ctest_sleep(30)
return()
elseif(UpdatedCount EQUAL 0)
if(SelfGitFetchCheck)
check_fetch_failure()
if(FetchFailed)
set(UpdateSuperResult -99 PARENT_SCOPE)
message(WARNING "Super project failed to update. Sleeping for 30 seconds.")
ctest_sleep(30)
return()
else()
set(UpdateSuperResult 0 PARENT_SCOPE)
endif()
endif()
#message(FATAL_ERROR "Failed to update the super project.")
endif()
ctest_submit()
set(UpdateSuperResult 0 PARENT_SCOPE)
endif()
get_git_log(ThirdPartySourceDirectory ThirdPartyCurrentCommit ThirdPartyCurrentCommitLogMsg ThirdPartyCurrentCommitLogAuthor)
set(ThirdPartyCurrentCommit ${ThirdPartyCurrentCommit} PARENT_SCOPE)
set(ThirdPartyCurrentCommitLogMsg ${ThirdPartyCurrentCommitLogMsg} PARENT_SCOPE)
set(ThirdPartyCurrentCommitLogAuthor ${ThirdPartyCurrentCommitLogAuthor} PARENT_SCOPE)
endfunction()
macro(update_sub_project SubProject)
math(EXPR UpdateAttempts ${UpdateAttempts}+1)
message("Updating ${SubProject}, attempt ${UpdateAttempts} of ${MaxUpdateAttempts}")
if(DashboardModel STREQUAL "Continuous")
get_git_log(${SubProject}SourceDirectory ${SubProject}OldCommit ${SubProject}OldCommitLogMsg ${SubProject}OldCommitLogAuthor)
ctest_update(SOURCE ${${SubProject}SourceDirectory} RETURN_VALUE UpdatedCount)
if(UpdatedCount LESS 0)
set(UpdateResult ${UpdatedCount})
message(WARNING "${SubProject} failed to update. Sleeping for 30 seconds.")
ctest_sleep(30)
set(${SubProject}ShouldRun OFF)
unset(${SubProject}UpdateXmlName)
elseif(UpdatedCount GREATER 0)
set(UpdateResult 0)
set(${SubProject}ShouldRun ON)
# temporarily rename the Update.xml so it can be found and submitted along with the other subproject's xml files after testing
set(${SubProject}UpdateXmlName ${CTEST_BINARY_DIRECTORY}/Testing/${TagId}/Update-${SubProject}.xml)
file(RENAME ${CTEST_BINARY_DIRECTORY}/Testing/${TagId}/Update.xml ${${SubProject}UpdateXmlName})
else()
# Look at project's dependencies
set(${SubProject}ShouldRun OFF)
unset(${SubProject}UpdateXmlName)
if(SelfGitFetchCheck)
check_fetch_failure()
if(FetchFailed)
set(UpdateResult -99)
message(WARNING "${SubProject} failed to update. Sleeping for 30 seconds.")
ctest_sleep(30)
else()
set(UpdateResult 0)
endif()
endif()
endif()
elseif(DashboardModel STREQUAL "Nightly" OR DashboardModel STREQUAL "Weekly")
ctest_update(SOURCE ${${SubProject}SourceDirectory} RETURN_VALUE UpdatedCount)
set(${SubProject}ShouldRun ON)
if(SelfGitFetchCheck)
check_fetch_failure()
if(FetchFailed)
set(UpdateResult -99)
message(WARNING "${SubProject} failed to update. Sleeping for 30 seconds.")
ctest_sleep(30)
else()
set(UpdateResult 0)
endif()
endif()
endif()
get_git_log(${SubProject}SourceDirectory ${SubProject}CurrentCommit ${SubProject}CurrentCommitLogMsg ${SubProject}CurrentCommitLogAuthor)
endmacro()
function(fix_xml_files_platform_entries_for_x64)
if(NOT DashboardModel STREQUAL "Experimental" AND WIN32 AND MachineBuildType STREQUAL "x64")
foreach(XML_File "Configure.xml" "Build.xml" "Test.xml")
file(READ ${CTEST_BINARY_DIRECTORY}/Testing/${TagId}/${XML_File} ModFileContents)
string(REPLACE "OSPlatform=\"x86\"" "OSPlatform=\"x64\"" ModFileContents "${ModFileContents}")
file(WRITE ${CTEST_BINARY_DIRECTORY}/Testing/${TagId}/${XML_File} "${ModFileContents}")
endforeach()
endif()
endfunction()
function(write_git_update_details_to_file)
set(GitDetailsFile "${CTEST_BINARY_DIRECTORY}/Testing/${TagId}/GitDetails.txt")
if(DashboardModel STREQUAL "Continuous")
execute_process(COMMAND ${CTEST_GIT_COMMAND} diff --stat ${${SubProject}OldCommit} ${${SubProject}CurrentCommit}
WORKING_DIRECTORY ${${SubProject}SourceDirectory}
RESULT_VARIABLE ResultVar
OUTPUT_VARIABLE ChangedFiles)
if(ResultVar EQUAL 0)
file(WRITE "${GitDetailsFile}" "Old Commit Details:\n${${SubProject}OldCommitLogMsg}\nNew Commit Details:\n${${SubProject}CurrentCommitLogMsg}\nFiles Changed:\n${ChangedFiles}")
set(CTEST_NOTES_FILES "${GitDetailsFile}" PARENT_SCOPE)
endif()
elseif(DashboardModel STREQUAL "Nightly" OR DashboardModel STREQUAL "Weekly")
file(WRITE "${GitDetailsFile}" "Latest Commit Details:\n${${SubProject}CurrentCommitLogMsg}")
set(CTEST_NOTES_FILES "${GitDetailsFile}" PARENT_SCOPE)
endif()
endfunction()
function(report_build_result Result)
if(${Result} STREQUAL "false")
message(WARNING "\n#################################### ${SubProject} failed during build ####################################\n")
endif()
# execute_process(COMMAND ${CTEST_PYTHON_EXECUTABLE} ci_build_reporter.py "${MachineType}" "k${MachineBuildType}" "${Result}" "${SubProject}" "${${SubProject}CurrentCommitLogAuthor}"
# WORKING_DIRECTORY "${CTEST_SOURCE_DIRECTORY}/tools"
# RESULT_VARIABLE ResultVar
# OUTPUT_VARIABLE OutputVar)
# if(NOT ${ResultVar} EQUAL 0)
# message(WARNING "${SubProject} failed running \"${CTEST_PYTHON_EXECUTABLE} ci_build_reporter.py \"${MachineType}\" \"k${MachineBuildType}\" \"${Result}\" \"${SubProject}\" \"${${SubProject}CurrentCommitLogAuthor}\"\"\n\n${OutputVar}")
# endif()
endfunction()
function(build_and_run SubProject RunAll)
if(NOT ${SubProject}ShouldRun AND NOT RunAll)
message("Not building or running tests in ${SubProject}")
if(${SubProject} STREQUAL ${FinalSubProject})
report_build_result("true")
endif()
return()
endif()
# add coverage flags
if(DashboardModel STREQUAL "Experimental" OR DashboardModel STREQUAL "Continuous" AND NOT WIN32)
set(ExtraConfigureArgs "${ExtraConfigureArgs};-DCOVERAGE=ON")
set(CTEST_COVERAGE_COMMAND /usr/bin/gcov)
endif()
ctest_configure(OPTIONS "${ExtraConfigureArgs};-DCMAKE_C_COMPILER=${CMAKE_C_COMPILER};-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}")
ctest_read_custom_files(${CMAKE_CURRENT_BINARY_DIR})
# build
message("Building ${SubProject}")
set(CTEST_BUILD_TARGET "All${SubProject}")
string(REGEX REPLACE "-" "" CTEST_BUILD_TARGET ${CTEST_BUILD_TARGET})
if(PathOfMake)
set(CTEST_BUILD_COMMAND "${PathOfMake} -k ${CTEST_BUILD_TARGET}")
endif()
ctest_build(RETURN_VALUE BuildResult)
if(BuildResult EQUAL 0)
set(RecurringBuildFailureCount 0)
else()
if(${SubProject}RecurringBuildFailureCount)
math(EXPR RecurringBuildFailureCount ${${SubProject}RecurringBuildFailureCount}+1)
else()
set(RecurringBuildFailureCount 1)
endif()
message(WARNING "Recurring build failure count of ${RecurringBuildFailureCount} for ${SubProject}.")
if(SelfGitFetchCheck)
check_fetch_failure()
endif()
endif()
set(${SubProject}RecurringBuildFailureCount ${RecurringBuildFailureCount} PARENT_SCOPE)
if(RecurringBuildFailureCount EQUAL 0)
# runs only tests that have a LABELS property matching "${SubProject}"
message("Testing ${SubProject}")
ctest_test(INCLUDE_LABEL "${SubProject}$")
endif()
if(COVERAGE)
ctest_coverage()
endif()
fix_xml_files_platform_entries_for_x64()
write_git_update_details_to_file()
# Rename Update-${SubProject}.xml back to Update.xml before submitting
if(${SubProject}UpdateXmlName)
file(RENAME ${${SubProject}UpdateXmlName} ${CTEST_BINARY_DIRECTORY}/Testing/${TagId}/Update.xml)
endif()
# Avoid clogging dashboard with repeated build failures
if(RecurringBuildFailureCount LESS 3)
ctest_submit(RETURN_VALUE ReturnVar)
endif()
if(${ReturnVar} EQUAL 0)
message("Submitted results for ${SubProject}\n-----------------------------------")
else()
message(WARNING "Failed to submit results for ${SubProject}\n-------------------------------------------")
endif()
if(NOT ${BuildResult} EQUAL 0)
report_build_result("false")
message(FATAL_ERROR "Build of ${SubProject} has resulted in a fatal error. Reported to Dash.")
elseif(FinalSubmodule)
report_build_result("true")
endif()
endfunction()