1 | #!groovy |
---|
2 | |
---|
3 | //=========================================================================================================== |
---|
4 | // Main compilation routine |
---|
5 | //=========================================================================================================== |
---|
6 | //Compilation script is done here but environnement set-up and error handling is done in main loop |
---|
7 | def cfa_build(boolean full_build) { |
---|
8 | build_stage 'Checkout' |
---|
9 | def install_dir = pwd tmp: true |
---|
10 | //checkout the source code and clean the repo |
---|
11 | checkout scm |
---|
12 | |
---|
13 | //Clean all temporary files to make sure no artifacts of the previous build remain |
---|
14 | sh 'git clean -fdqx' |
---|
15 | |
---|
16 | //Reset the git repo so no local changes persist |
---|
17 | sh 'git reset --hard' |
---|
18 | |
---|
19 | build_stage 'Build' |
---|
20 | |
---|
21 | //Configure the conpilation (Output is not relevant) |
---|
22 | //Use the current directory as the installation target so nothing |
---|
23 | //escapes the sandbox |
---|
24 | //Also specify the compiler by hand |
---|
25 | sh "./configure CXX=${currentCC.cpp_cc} --with-backend-compiler=${currentCC.cfa_backend_cc} --prefix=${install_dir} --enable-silent-rules --quiet" |
---|
26 | |
---|
27 | //Compile the project |
---|
28 | sh 'make -j 8 --no-print-directory V=0 install' |
---|
29 | |
---|
30 | build_stage 'Test' |
---|
31 | |
---|
32 | //Run the tests from the tests directory |
---|
33 | dir ('src/tests') { |
---|
34 | if (full_build) { |
---|
35 | sh 'make all-tests' |
---|
36 | } |
---|
37 | else { |
---|
38 | sh 'make' |
---|
39 | } |
---|
40 | } |
---|
41 | |
---|
42 | build_stage 'Cleanup' |
---|
43 | |
---|
44 | //do a maintainer-clean to make sure we need to remake from scratch |
---|
45 | sh 'make maintainer-clean > /dev/null' |
---|
46 | } |
---|
47 | |
---|
48 | def make_doc() { |
---|
49 | def err = null |
---|
50 | |
---|
51 | try { |
---|
52 | sh 'make clean > /dev/null' |
---|
53 | sh 'make > /dev/null 2>&1' |
---|
54 | } |
---|
55 | |
---|
56 | catch (Exception caughtError) { |
---|
57 | //rethrow error later |
---|
58 | err = caughtError |
---|
59 | |
---|
60 | sh 'cat *.log' |
---|
61 | } |
---|
62 | |
---|
63 | finally { |
---|
64 | /* Must re-throw exception to propagate error */ |
---|
65 | if (err) { |
---|
66 | throw err |
---|
67 | } |
---|
68 | } |
---|
69 | } |
---|
70 | |
---|
71 | def doc_build() { |
---|
72 | stage 'Documentation' |
---|
73 | |
---|
74 | status_prefix = 'Documentation' |
---|
75 | |
---|
76 | dir ('doc/user') { |
---|
77 | make_doc() |
---|
78 | } |
---|
79 | |
---|
80 | dir ('doc/refrat') { |
---|
81 | make_doc() |
---|
82 | } |
---|
83 | } |
---|
84 | |
---|
85 | def push_build() { |
---|
86 | //Don't use the build_stage function which outputs the compiler |
---|
87 | stage 'Push' |
---|
88 | |
---|
89 | status_prefix = 'Push' |
---|
90 | |
---|
91 | def out_dir = pwd tmp: true |
---|
92 | sh "mkdir -p ${out_dir}" |
---|
93 | |
---|
94 | //parse git logs to find what changed |
---|
95 | sh "git remote > ${out_dir}/GIT_REMOTE" |
---|
96 | git_remote = readFile("${out_dir}/GIT_REMOTE") |
---|
97 | remoteDoLangExists = git_remote.contains("DoLang") |
---|
98 | |
---|
99 | if( !remoteDoLangExists ) { |
---|
100 | sh 'git remote add DoLang git@gitlab.do-lang.org:internal/cfa-cc.git' |
---|
101 | } |
---|
102 | |
---|
103 | sh "git push DoLang ${gitRefNewValue}:master" |
---|
104 | } |
---|
105 | |
---|
106 | //=========================================================================================================== |
---|
107 | // Helper classes/variables/routines to make the status and stage name easier to use |
---|
108 | //=========================================================================================================== |
---|
109 | //Description of a compiler (Must be serializable since pipelines are persistent) |
---|
110 | class CC_Desc implements Serializable { |
---|
111 | public String cc_name |
---|
112 | public String cpp_cc |
---|
113 | public String cfa_backend_cc |
---|
114 | |
---|
115 | CC_Desc(String cc_name, String cpp_cc, String cfa_backend_cc) { |
---|
116 | this.cc_name = cc_name |
---|
117 | this.cpp_cc = cpp_cc |
---|
118 | this.cfa_backend_cc = cfa_backend_cc |
---|
119 | } |
---|
120 | } |
---|
121 | |
---|
122 | //Global Variables defining the compiler and at which point in the build we are |
---|
123 | // These variables are used but can't be declared before hand because of wierd scripting rules |
---|
124 | // @Field String currentCC |
---|
125 | // @Field String status_prefix |
---|
126 | |
---|
127 | //Wrapper to sync stage name and status name |
---|
128 | def build_stage(String name) { |
---|
129 | def stage_name = "${currentCC.cc_name} ${name}".trim() |
---|
130 | stage stage_name |
---|
131 | |
---|
132 | status_prefix = stage_name |
---|
133 | } |
---|
134 | |
---|
135 | //Helper routine to collect information about the git history |
---|
136 | def collect_git_info() { |
---|
137 | |
---|
138 | //create the temporary output directory in case it doesn't already exist |
---|
139 | def out_dir = pwd tmp: true |
---|
140 | sh "mkdir -p ${out_dir}" |
---|
141 | |
---|
142 | //parse git logs to find what changed |
---|
143 | gitRefName = env.BRANCH_NAME |
---|
144 | dir("../${gitRefName}@script") { |
---|
145 | sh "git reflog > ${out_dir}/GIT_COMMIT" |
---|
146 | } |
---|
147 | git_reflog = readFile("${out_dir}/GIT_COMMIT") |
---|
148 | gitRefOldValue = (git_reflog =~ /moving from (.+) to (.+)/)[0][1] |
---|
149 | gitRefNewValue = (git_reflog =~ /moving from (.+) to (.+)/)[0][2] |
---|
150 | } |
---|
151 | |
---|
152 | //=========================================================================================================== |
---|
153 | // Main loop of the compilation |
---|
154 | //=========================================================================================================== |
---|
155 | node ('master'){ |
---|
156 | |
---|
157 | boolean doPromoteBuild2DoLang |
---|
158 | buildArchitecture = '' |
---|
159 | def err = null |
---|
160 | def log_needed = false |
---|
161 | currentBuild.result = "SUCCESS" |
---|
162 | status_prefix = '' |
---|
163 | |
---|
164 | try { |
---|
165 | //Prevent the build from exceeding 30 minutes |
---|
166 | timeout(60) { |
---|
167 | |
---|
168 | //Wrap build to add timestamp to command line |
---|
169 | wrap([$class: 'TimestamperBuildWrapper']) { |
---|
170 | |
---|
171 | collect_git_info() |
---|
172 | |
---|
173 | properties ([ \ |
---|
174 | [$class: 'ParametersDefinitionProperty', \ |
---|
175 | parameterDefinitions: [ \ |
---|
176 | [$class: 'BooleanParameterDefinition', \ |
---|
177 | defaultValue: false, \ |
---|
178 | description: 'If true, the build will be promoted to the do-lang git repository (on successful builds only)', \ |
---|
179 | name: 'promoteBuild2DoLang' \ |
---|
180 | ]] \ |
---|
181 | ], \ |
---|
182 | [$class: 'ParametersDefinitionProperty', \ |
---|
183 | parameterDefinitons: [ \ |
---|
184 | [$class: 'StringParameterDefinition', \ |
---|
185 | defaultValue: '64bit', \ |
---|
186 | description: 'The architecture to use for compilation', \ |
---|
187 | name: 'buildArchitecture' \ |
---|
188 | ]] \ |
---|
189 | ]]) |
---|
190 | |
---|
191 | doPromoteBuild2DoLang = promoteBuild2DoLang == 'true' |
---|
192 | |
---|
193 | |
---|
194 | echo "FULL BUILD = ${doPromoteBuild2DoLang}" |
---|
195 | |
---|
196 | architectureFlag = buildArchitecture == '64bit' ? '-m64' : (buildArchitecture == '32bit' ? '-m32' : 'ERROR') |
---|
197 | echo "Architecture = ${buildArchitecture} (flag ${architectureFlag})" |
---|
198 | echo |
---|
199 | |
---|
200 | //Compile using gcc-4.9 |
---|
201 | currentCC = new CC_Desc('gcc-4.9', 'g++-4.9', 'gcc-4.9') |
---|
202 | cfa_build(doPromoteBuild2DoLang) |
---|
203 | |
---|
204 | //Compile latex documentation |
---|
205 | doc_build() |
---|
206 | |
---|
207 | if( doPromoteBuild2DoLang ) { |
---|
208 | //Compile using gcc-5 |
---|
209 | currentCC = new CC_Desc('gcc-5', 'g++-5', 'gcc-5') |
---|
210 | cfa_build(true) |
---|
211 | |
---|
212 | //Compile using gcc-4.9 |
---|
213 | currentCC = new CC_Desc('gcc-6', 'g++-6', 'gcc-6') |
---|
214 | cfa_build(true) |
---|
215 | |
---|
216 | //Push latest changes to do-lang repo |
---|
217 | push_build() |
---|
218 | } |
---|
219 | } |
---|
220 | } |
---|
221 | } |
---|
222 | |
---|
223 | //If an exception is caught we need to change the status and remember to |
---|
224 | //attach the build log to the email |
---|
225 | catch (Exception caughtError) { |
---|
226 | //rethrow error later |
---|
227 | err = caughtError |
---|
228 | |
---|
229 | //An error has occured, the build log is relevent |
---|
230 | log_needed = true |
---|
231 | |
---|
232 | //Store the result of the build log |
---|
233 | currentBuild.result = "${status_prefix} FAILURE".trim() |
---|
234 | } |
---|
235 | |
---|
236 | finally { |
---|
237 | //Send email with final results |
---|
238 | //notify_result(doPromoteBuild2DoLang, err, currentBuild.result, log_needed) |
---|
239 | |
---|
240 | /* Must re-throw exception to propagate error */ |
---|
241 | if (err) { |
---|
242 | throw err |
---|
243 | } |
---|
244 | } |
---|
245 | } |
---|
246 | |
---|
247 | //=========================================================================================================== |
---|
248 | //Routine responsible of sending the email notification once the build is completed |
---|
249 | //=========================================================================================================== |
---|
250 | def notify_result(boolean promote, Exception err, String status, boolean log) { |
---|
251 | echo 'Build completed, sending result notification' |
---|
252 | if(promote) { |
---|
253 | if( err ) { |
---|
254 | promote_email(status) |
---|
255 | } |
---|
256 | } |
---|
257 | else { |
---|
258 | email(status, log) |
---|
259 | } |
---|
260 | } |
---|
261 | |
---|
262 | //Email notification on a full build failure |
---|
263 | def promote_email(String status) { |
---|
264 | //Since tokenizer doesn't work, figure stuff out from the environnement variables and command line |
---|
265 | //Configurations for email format |
---|
266 | def email_subject = "[cforall git][PROMOTE - FAILURE]" |
---|
267 | def email_body = """This is an automated email from the Jenkins build machine. It was |
---|
268 | generated because of a git hooks/post-receive script following |
---|
269 | a ref change was pushed to the repository containing |
---|
270 | the project "UNNAMED PROJECT". |
---|
271 | |
---|
272 | Check console output at ${env.BUILD_URL} to view the results. |
---|
273 | |
---|
274 | - Status -------------------------------------------------------------- |
---|
275 | |
---|
276 | PROMOTE FAILURE - ${status} |
---|
277 | """ |
---|
278 | |
---|
279 | def email_to = "pabuhr@uwaterloo.ca, rschlunt@uwaterloo.ca, a3moss@uwaterloo.ca, tdelisle@uwaterloo.ca, brice.dobry@huawei.com" |
---|
280 | |
---|
281 | //send email notification |
---|
282 | emailext body: email_body, subject: email_subject, to: email_to, attachLog: true |
---|
283 | } |
---|
284 | |
---|
285 | //Standard build email notification |
---|
286 | def email(String status, boolean log) { |
---|
287 | //Since tokenizer doesn't work, figure stuff out from the environnement variables and command line |
---|
288 | //Configurations for email format |
---|
289 | def project_name = (env.JOB_NAME =~ /(.+)\/.+/)[0][1].toLowerCase() |
---|
290 | |
---|
291 | def gitLog = 'Error retrieving git logs' |
---|
292 | def gitDiff = 'Error retrieving git diff' |
---|
293 | |
---|
294 | try { |
---|
295 | |
---|
296 | sh "git rev-list --format=short ${gitRefOldValue}...${gitRefNewValue} > GIT_LOG" |
---|
297 | gitLog = readFile('GIT_LOG') |
---|
298 | |
---|
299 | sh "git diff --stat ${gitRefNewValue} ${gitRefOldValue} > GIT_DIFF" |
---|
300 | gitDiff = readFile('GIT_DIFF') |
---|
301 | } |
---|
302 | catch (Exception error) {} |
---|
303 | |
---|
304 | def email_subject = "[${project_name} git][BUILD# ${env.BUILD_NUMBER} - ${status}] - branch ${env.BRANCH_NAME}" |
---|
305 | def email_body = """This is an automated email from the Jenkins build machine. It was |
---|
306 | generated because of a git hooks/post-receive script following |
---|
307 | a ref change was pushed to the repository containing |
---|
308 | the project "UNNAMED PROJECT". |
---|
309 | |
---|
310 | The branch ${env.BRANCH_NAME} has been updated. |
---|
311 | via ${gitRefOldValue} (commit) |
---|
312 | from ${gitRefNewValue} (commit) |
---|
313 | |
---|
314 | Check console output at ${env.BUILD_URL} to view the results. |
---|
315 | |
---|
316 | - Status -------------------------------------------------------------- |
---|
317 | |
---|
318 | BUILD# ${env.BUILD_NUMBER} - ${status} |
---|
319 | |
---|
320 | - Log ----------------------------------------------------------------- |
---|
321 | ${gitLog} |
---|
322 | ----------------------------------------------------------------------- |
---|
323 | Summary of changes: |
---|
324 | ${gitDiff} |
---|
325 | """ |
---|
326 | |
---|
327 | def email_to = "pabuhr@uwaterloo.ca, rschlunt@uwaterloo.ca, a3moss@uwaterloo.ca, tdelisle@uwaterloo.ca, brice.dobry@huawei.com" |
---|
328 | |
---|
329 | //send email notification |
---|
330 | emailext body: email_body, subject: email_subject, to: email_to, attachLog: log |
---|
331 | } |
---|