[a63ad80] | 1 | #!groovy |
---|
| 2 | |
---|
[29f4fe62] | 3 | //=========================================================================================================== |
---|
[9beae23] | 4 | // Main loop of the compilation |
---|
[29f4fe62] | 5 | //=========================================================================================================== |
---|
[56a9ce6] | 6 | |
---|
[5307c33] | 7 | node('master') { |
---|
[5b8413b4] | 8 | // Globals |
---|
[4c55047] | 9 | BuildDir = pwd tmp: true |
---|
| 10 | SrcDir = pwd tmp: false |
---|
[5b8413b4] | 11 | Settings = null |
---|
| 12 | StageName = '' |
---|
| 13 | |
---|
| 14 | // Local variables |
---|
[9beae23] | 15 | def err = null |
---|
| 16 | def log_needed = false |
---|
[0ef06b6] | 17 | |
---|
| 18 | currentBuild.result = "SUCCESS" |
---|
| 19 | |
---|
[9beae23] | 20 | try { |
---|
[95fdb0a] | 21 | //Wrap build to add timestamp to command line |
---|
| 22 | wrap([$class: 'TimestamperBuildWrapper']) { |
---|
[23a14d86] | 23 | |
---|
[8c700c1] | 24 | notify_server(0) |
---|
[95fdb0a] | 25 | |
---|
[c431138] | 26 | Settings = prepare_build() |
---|
[7aebc62] | 27 | |
---|
[5307c33] | 28 | node(Settings.Architecture.node) { |
---|
[bf9d323] | 29 | BuildDir = pwd tmp: true |
---|
| 30 | SrcDir = pwd tmp: false |
---|
| 31 | |
---|
[5307c33] | 32 | clean() |
---|
[0dc3ac3] | 33 | |
---|
[5307c33] | 34 | checkout() |
---|
[fde808df] | 35 | |
---|
[5307c33] | 36 | notify_server(0) |
---|
[8c700c1] | 37 | |
---|
[5307c33] | 38 | build() |
---|
[95fdb0a] | 39 | |
---|
[5307c33] | 40 | test() |
---|
[95fdb0a] | 41 | |
---|
[5307c33] | 42 | benchmark() |
---|
[95fdb0a] | 43 | |
---|
[5307c33] | 44 | build_doc() |
---|
[7359098] | 45 | |
---|
[5307c33] | 46 | publish() |
---|
| 47 | } |
---|
[9beae23] | 48 | |
---|
[4c55047] | 49 | // Update the build directories when exiting the node |
---|
| 50 | BuildDir = pwd tmp: true |
---|
| 51 | SrcDir = pwd tmp: false |
---|
| 52 | |
---|
[65f9dec] | 53 | notify_server(45) |
---|
[9beae23] | 54 | } |
---|
[738cf8f] | 55 | } |
---|
| 56 | |
---|
[9beae23] | 57 | //If an exception is caught we need to change the status and remember to |
---|
| 58 | //attach the build log to the email |
---|
[738cf8f] | 59 | catch (Exception caughtError) { |
---|
| 60 | //rethrow error later |
---|
| 61 | err = caughtError |
---|
| 62 | |
---|
[e966ec0] | 63 | echo err.toString() |
---|
| 64 | |
---|
[9beae23] | 65 | //An error has occured, the build log is relevent |
---|
| 66 | log_needed = true |
---|
| 67 | |
---|
| 68 | //Store the result of the build log |
---|
[5b8413b4] | 69 | currentBuild.result = "${StageName} FAILURE".trim() |
---|
[738cf8f] | 70 | } |
---|
| 71 | |
---|
| 72 | finally { |
---|
[9beae23] | 73 | //Send email with final results if this is not a full build |
---|
[e966ec0] | 74 | if( Settings && !Settings.Silent ) { |
---|
[e57ebb5] | 75 | email(log_needed, Settings.IsSandbox) |
---|
[9beae23] | 76 | } |
---|
| 77 | |
---|
[734891d] | 78 | echo 'Build Completed' |
---|
| 79 | |
---|
[738cf8f] | 80 | /* Must re-throw exception to propagate error */ |
---|
| 81 | if (err) { |
---|
| 82 | throw err |
---|
| 83 | } |
---|
| 84 | } |
---|
| 85 | } |
---|
[29f4fe62] | 86 | //=========================================================================================================== |
---|
[9beae23] | 87 | // Main compilation routines |
---|
[29f4fe62] | 88 | //=========================================================================================================== |
---|
[0dc3ac3] | 89 | def clean() { |
---|
| 90 | build_stage('Cleanup') { |
---|
| 91 | // clean the build by wipping the build directory |
---|
[5b8413b4] | 92 | dir(BuildDir) { |
---|
[d4cd491] | 93 | deleteDir() |
---|
[ece8a80] | 94 | } |
---|
[620dd2b] | 95 | } |
---|
[95fdb0a] | 96 | } |
---|
[29f4fe62] | 97 | |
---|
[0dc3ac3] | 98 | //Compilation script is done here but environnement set-up and error handling is done in main loop |
---|
| 99 | def checkout() { |
---|
| 100 | build_stage('Checkout') { |
---|
| 101 | //checkout the source code and clean the repo |
---|
[a336d46] | 102 | final scmVars = checkout scm |
---|
| 103 | Settings.GitNewRef = scmVars.GIT_COMMIT |
---|
| 104 | Settings.GitOldRef = scmVars.GIT_PREVIOUS_COMMIT |
---|
[d3a4564a] | 105 | |
---|
[a336d46] | 106 | echo GitLogMessage() |
---|
[0dc3ac3] | 107 | } |
---|
| 108 | } |
---|
| 109 | |
---|
[95fdb0a] | 110 | def build() { |
---|
[620dd2b] | 111 | build_stage('Build') { |
---|
[50f2cfc] | 112 | // Build outside of the src tree to ease cleaning |
---|
[5b8413b4] | 113 | dir (BuildDir) { |
---|
[50f2cfc] | 114 | //Configure the conpilation (Output is not relevant) |
---|
| 115 | //Use the current directory as the installation target so nothing escapes the sandbox |
---|
| 116 | //Also specify the compiler by hand |
---|
[3fc5f010] | 117 | targets="" |
---|
[5afeab9] | 118 | if( Settings.RunAllTests ) { |
---|
[6bde81d] | 119 | targets="--with-target-hosts='host:debug,host:nodebug'" |
---|
[3fc5f010] | 120 | } else { |
---|
[6bde81d] | 121 | targets="--with-target-hosts='host:debug'" |
---|
[3fc5f010] | 122 | } |
---|
| 123 | |
---|
[93fe3154] | 124 | sh "${SrcDir}/configure CXX=${Settings.Compiler.CXX} CC=${Settings.Compiler.CC} ${Settings.Architecture.flags} ${targets} --quiet" |
---|
[1752d0e] | 125 | |
---|
[50f2cfc] | 126 | //Compile the project |
---|
| 127 | sh 'make -j 8 --no-print-directory' |
---|
| 128 | } |
---|
[620dd2b] | 129 | } |
---|
[95fdb0a] | 130 | } |
---|
[24eecab] | 131 | |
---|
[95fdb0a] | 132 | def test() { |
---|
[620dd2b] | 133 | build_stage('Test') { |
---|
[9e5f409] | 134 | |
---|
[5b8413b4] | 135 | dir (BuildDir) { |
---|
[0dc3ac3] | 136 | //Run the tests from the tests directory |
---|
[5afeab9] | 137 | if ( Settings.RunAllTests ) { |
---|
[b90aace] | 138 | sh 'make --no-print-directory -C tests timeouts="--timeout=600" all-tests debug=yes' |
---|
| 139 | sh 'make --no-print-directory -C tests timeouts="--timeout=600" all-tests debug=no ' |
---|
[0dc3ac3] | 140 | } |
---|
| 141 | else { |
---|
[7428ad9] | 142 | sh 'make --no-print-directory -C tests' |
---|
[0dc3ac3] | 143 | } |
---|
[9beae23] | 144 | } |
---|
[620dd2b] | 145 | } |
---|
[95fdb0a] | 146 | } |
---|
| 147 | |
---|
| 148 | def benchmark() { |
---|
[620dd2b] | 149 | build_stage('Benchmark') { |
---|
[29f4fe62] | 150 | |
---|
[5afeab9] | 151 | if( !Settings.RunBenchmark ) return |
---|
[ae28ee2] | 152 | |
---|
[5b8413b4] | 153 | dir (BuildDir) { |
---|
[0dc3ac3] | 154 | //Append bench results |
---|
[ede87c6] | 155 | sh "${SrcDir}/benchmark/jenkins.sh ${Settings.GitNewRef} ${Settings.Architecture} ${BuildDir}/bench.json" |
---|
[0dc3ac3] | 156 | } |
---|
[620dd2b] | 157 | } |
---|
[9beae23] | 158 | } |
---|
[efd60d67] | 159 | |
---|
[95fdb0a] | 160 | def build_doc() { |
---|
[620dd2b] | 161 | build_stage('Documentation') { |
---|
[9beae23] | 162 | |
---|
[5afeab9] | 163 | if( !Settings.BuildDocumentation ) return |
---|
[9beae23] | 164 | |
---|
| 165 | dir ('doc/user') { |
---|
| 166 | make_doc() |
---|
| 167 | } |
---|
| 168 | |
---|
| 169 | dir ('doc/refrat') { |
---|
| 170 | make_doc() |
---|
| 171 | } |
---|
[620dd2b] | 172 | } |
---|
[9beae23] | 173 | } |
---|
| 174 | |
---|
[95fdb0a] | 175 | def publish() { |
---|
[620dd2b] | 176 | build_stage('Publish') { |
---|
[95fdb0a] | 177 | |
---|
[5afeab9] | 178 | if( !Settings.Publish ) return |
---|
[95fdb0a] | 179 | |
---|
| 180 | //Then publish the results |
---|
[8f99233] | 181 | sh 'curl --silent --show-error -H \'Content-Type: application/json\' --data @${BuildDir}/bench.json https://cforall.uwaterloo.ca:8082/jenkins/publish > /dev/null || true' |
---|
[620dd2b] | 182 | } |
---|
[95fdb0a] | 183 | } |
---|
| 184 | |
---|
[29f4fe62] | 185 | //=========================================================================================================== |
---|
| 186 | //Routine responsible of sending the email notification once the build is completed |
---|
| 187 | //=========================================================================================================== |
---|
[a336d46] | 188 | def GitLogMessage() { |
---|
| 189 | if (!Settings || !Settings.GitOldRef || !Settings.GitNewRef) return "\nERROR retrieveing git information!\n" |
---|
[e8a22a7] | 190 | |
---|
[9f5bb817] | 191 | sh "${SrcDir}/tools/PrettyGitLogs.sh ${SrcDir} ${BuildDir} ${Settings.GitOldRef} ${Settings.GitNewRef}" |
---|
[6e31c43] | 192 | |
---|
[eda8175] | 193 | def gitUpdate = readFile("${BuildDir}/GIT_UPDATE") |
---|
| 194 | def gitLog = readFile("${BuildDir}/GIT_LOG") |
---|
| 195 | def gitDiff = readFile("${BuildDir}/GIT_DIFF") |
---|
[7a927ed0] | 196 | |
---|
[a336d46] | 197 | return """ |
---|
[848fb00] | 198 | The branch ${env.BRANCH_NAME} has been updated. |
---|
[7a927ed0] | 199 | ${gitUpdate} |
---|
[7b1a604] | 200 | |
---|
| 201 | Check console output at ${env.BUILD_URL} to view the results. |
---|
| 202 | |
---|
| 203 | - Status -------------------------------------------------------------- |
---|
| 204 | |
---|
[e57ebb5] | 205 | BUILD# ${env.BUILD_NUMBER} - ${currentBuild.result} |
---|
[e8a22a7] | 206 | |
---|
[7b1a604] | 207 | - Log ----------------------------------------------------------------- |
---|
[7a927ed0] | 208 | ${gitLog} |
---|
[7b1a604] | 209 | ----------------------------------------------------------------------- |
---|
| 210 | Summary of changes: |
---|
[7a927ed0] | 211 | ${gitDiff} |
---|
[7b1a604] | 212 | """ |
---|
[a336d46] | 213 | } |
---|
| 214 | |
---|
| 215 | //Standard build email notification |
---|
| 216 | def email(boolean log, boolean bIsSandbox) { |
---|
| 217 | //Since tokenizer doesn't work, figure stuff out from the environnement variables and command line |
---|
| 218 | //Configurations for email format |
---|
| 219 | echo 'Notifying users of result' |
---|
| 220 | |
---|
| 221 | def project_name = (env.JOB_NAME =~ /(.+)\/.+/)[0][1].toLowerCase() |
---|
| 222 | def email_subject = "[${project_name} git][BUILD# ${env.BUILD_NUMBER} - ${currentBuild.result}] - branch ${env.BRANCH_NAME}" |
---|
| 223 | def email_body = """This is an automated email from the Jenkins build machine. It was |
---|
| 224 | generated because of a git hooks/post-receive script following |
---|
| 225 | a ref change which was pushed to the Cforall repository. |
---|
| 226 | """ + GitLogMessage() |
---|
[e8a22a7] | 227 | |
---|
[e39647e] | 228 | def email_to = "cforall@lists.uwaterloo.ca" |
---|
[e8a22a7] | 229 | |
---|
[e0549dba] | 230 | if( Settings && !Settings.IsSandbox ) { |
---|
[094a42c] | 231 | //send email notification |
---|
| 232 | emailext body: email_body, subject: email_subject, to: email_to, attachLog: log |
---|
| 233 | } else { |
---|
| 234 | echo "Would send email to: ${email_to}" |
---|
| 235 | echo "With title: ${email_subject}" |
---|
| 236 | echo "Content: \n${email_body}" |
---|
| 237 | } |
---|
[e8a22a7] | 238 | } |
---|
[5b8413b4] | 239 | |
---|
| 240 | //=========================================================================================================== |
---|
| 241 | // Helper classes/variables/routines |
---|
| 242 | //=========================================================================================================== |
---|
| 243 | //Description of a compiler (Must be serializable since pipelines are persistent) |
---|
| 244 | class CC_Desc implements Serializable { |
---|
[93fe3154] | 245 | public String name |
---|
| 246 | public String CXX |
---|
[6ebc13f] | 247 | public String CC |
---|
[93fe3154] | 248 | |
---|
[6ebc13f] | 249 | CC_Desc(String name, String CXX, String CC) { |
---|
[93fe3154] | 250 | this.name = name |
---|
| 251 | this.CXX = CXX |
---|
[6ebc13f] | 252 | this.CC = CC |
---|
[5b8413b4] | 253 | } |
---|
| 254 | } |
---|
| 255 | |
---|
| 256 | //Description of an architecture (Must be serializable since pipelines are persistent) |
---|
| 257 | class Arch_Desc implements Serializable { |
---|
| 258 | public String name |
---|
| 259 | public String flags |
---|
[5307c33] | 260 | public String node |
---|
[5b8413b4] | 261 | |
---|
[5307c33] | 262 | Arch_Desc(String name, String flags, String node) { |
---|
[5b8413b4] | 263 | this.name = name |
---|
| 264 | this.flags = flags |
---|
[5307c33] | 265 | this.node = node |
---|
[5b8413b4] | 266 | } |
---|
| 267 | } |
---|
| 268 | |
---|
| 269 | class BuildSettings implements Serializable { |
---|
| 270 | public final CC_Desc Compiler |
---|
| 271 | public final Arch_Desc Architecture |
---|
| 272 | public final Boolean RunAllTests |
---|
| 273 | public final Boolean RunBenchmark |
---|
| 274 | public final Boolean BuildDocumentation |
---|
| 275 | public final Boolean Publish |
---|
| 276 | public final Boolean Silent |
---|
| 277 | public final Boolean IsSandbox |
---|
| 278 | public final String DescLong |
---|
| 279 | public final String DescShort |
---|
| 280 | |
---|
[a336d46] | 281 | public String GitNewRef |
---|
| 282 | public String GitOldRef |
---|
| 283 | |
---|
[5b8413b4] | 284 | BuildSettings(java.util.Collections$UnmodifiableMap param, String branch) { |
---|
| 285 | switch( param.Compiler ) { |
---|
| 286 | case 'gcc-6': |
---|
| 287 | this.Compiler = new CC_Desc('gcc-6', 'g++-6', 'gcc-6') |
---|
| 288 | break |
---|
| 289 | case 'gcc-5': |
---|
| 290 | this.Compiler = new CC_Desc('gcc-5', 'g++-5', 'gcc-5') |
---|
| 291 | break |
---|
| 292 | case 'gcc-4.9': |
---|
| 293 | this.Compiler = new CC_Desc('gcc-4.9', 'g++-4.9', 'gcc-4.9') |
---|
| 294 | break |
---|
| 295 | case 'clang': |
---|
| 296 | this.Compiler = new CC_Desc('clang', 'clang++', 'gcc-6') |
---|
| 297 | break |
---|
| 298 | default : |
---|
| 299 | error "Unhandled compiler : ${cc}" |
---|
| 300 | } |
---|
| 301 | |
---|
| 302 | switch( param.Architecture ) { |
---|
| 303 | case 'x64': |
---|
[a3e8281] | 304 | this.Architecture = new Arch_Desc('x64', '--host=x86_64', 'x64') |
---|
[5b8413b4] | 305 | break |
---|
| 306 | case 'x86': |
---|
[a3e8281] | 307 | this.Architecture = new Arch_Desc('x86', '--host=i386', 'x86') |
---|
[5b8413b4] | 308 | break |
---|
| 309 | default : |
---|
| 310 | error "Unhandled architecture : ${arch}" |
---|
| 311 | } |
---|
| 312 | |
---|
| 313 | this.RunAllTests = param.RunAllTests |
---|
| 314 | this.RunBenchmark = param.RunBenchmark |
---|
| 315 | this.BuildDocumentation = param.BuildDocumentation |
---|
| 316 | this.Publish = param.Publish |
---|
| 317 | this.Silent = param.Silent |
---|
| 318 | this.IsSandbox = (branch == "jenkins-sandbox") |
---|
| 319 | |
---|
| 320 | def full = param.RunAllTests ? " (Full)" : "" |
---|
[93fe3154] | 321 | this.DescShort = "${ this.Compiler.name }:${ this.Architecture.name }${full}" |
---|
[5b8413b4] | 322 | |
---|
[93fe3154] | 323 | this.DescLong = """Compiler : ${ this.Compiler.name } (${ this.Compiler.CXX }/${ this.Compiler.CC }) |
---|
[5b8413b4] | 324 | Architecture : ${ this.Architecture.name } |
---|
| 325 | Arc Flags : ${ this.Architecture.flags } |
---|
| 326 | Run All Tests : ${ this.RunAllTests.toString() } |
---|
| 327 | Run Benchmark : ${ this.RunBenchmark.toString() } |
---|
| 328 | Build Documentation : ${ this.BuildDocumentation.toString() } |
---|
| 329 | Publish : ${ this.Publish.toString() } |
---|
| 330 | Silent : ${ this.Silent.toString() } |
---|
| 331 | """ |
---|
[a336d46] | 332 | |
---|
| 333 | this.GitNewRef = '' |
---|
| 334 | this.GitOldRef = '' |
---|
[5b8413b4] | 335 | } |
---|
| 336 | } |
---|
| 337 | |
---|
| 338 | def prepare_build() { |
---|
| 339 | // prepare the properties |
---|
| 340 | properties ([ \ |
---|
| 341 | [$class: 'ParametersDefinitionProperty', \ |
---|
| 342 | parameterDefinitions: [ \ |
---|
| 343 | [$class: 'ChoiceParameterDefinition', \ |
---|
| 344 | description: 'Which compiler to use', \ |
---|
| 345 | name: 'Compiler', \ |
---|
| 346 | choices: 'gcc-6\ngcc-5\ngcc-4.9\nclang', \ |
---|
| 347 | defaultValue: 'gcc-6', \ |
---|
| 348 | ], \ |
---|
| 349 | [$class: 'ChoiceParameterDefinition', \ |
---|
| 350 | description: 'The target architecture', \ |
---|
| 351 | name: 'Architecture', \ |
---|
| 352 | choices: 'x64\nx86', \ |
---|
| 353 | defaultValue: 'x64', \ |
---|
| 354 | ], \ |
---|
| 355 | [$class: 'BooleanParameterDefinition', \ |
---|
| 356 | description: 'If false, only the quick test suite is ran', \ |
---|
| 357 | name: 'RunAllTests', \ |
---|
| 358 | defaultValue: false, \ |
---|
| 359 | ], \ |
---|
| 360 | [$class: 'BooleanParameterDefinition', \ |
---|
| 361 | description: 'If true, jenkins also runs benchmarks', \ |
---|
| 362 | name: 'RunBenchmark', \ |
---|
| 363 | defaultValue: false, \ |
---|
| 364 | ], \ |
---|
| 365 | [$class: 'BooleanParameterDefinition', \ |
---|
| 366 | description: 'If true, jenkins also builds documentation', \ |
---|
| 367 | name: 'BuildDocumentation', \ |
---|
| 368 | defaultValue: true, \ |
---|
| 369 | ], \ |
---|
| 370 | [$class: 'BooleanParameterDefinition', \ |
---|
| 371 | description: 'If true, jenkins also publishes results', \ |
---|
| 372 | name: 'Publish', \ |
---|
| 373 | defaultValue: false, \ |
---|
| 374 | ], \ |
---|
| 375 | [$class: 'BooleanParameterDefinition', \ |
---|
| 376 | description: 'If true, jenkins will not send emails', \ |
---|
| 377 | name: 'Silent', \ |
---|
| 378 | defaultValue: false, \ |
---|
| 379 | ], \ |
---|
| 380 | ], |
---|
| 381 | ]]) |
---|
| 382 | |
---|
[4c55047] | 383 | // It's unfortunate but it looks like we need to checkout the entire repo just to get the pretty git printer |
---|
| 384 | checkout scm |
---|
[2407853] | 385 | |
---|
[5b8413b4] | 386 | final settings = new BuildSettings(params, env.BRANCH_NAME) |
---|
| 387 | |
---|
| 388 | currentBuild.description = settings.DescShort |
---|
| 389 | echo settings.DescLong |
---|
| 390 | |
---|
| 391 | return settings |
---|
| 392 | } |
---|
| 393 | |
---|
| 394 | def build_stage(String name, Closure block ) { |
---|
| 395 | StageName = name |
---|
| 396 | echo " -------- ${StageName} -------- " |
---|
[939fd39] | 397 | stage(name, block) |
---|
[5b8413b4] | 398 | } |
---|
| 399 | |
---|
| 400 | def notify_server(int wait) { |
---|
| 401 | sh """curl --silent --show-error --data "wait=${wait}" -X POST https://cforall.uwaterloo.ca:8082/jenkins/notify > /dev/null || true""" |
---|
| 402 | return |
---|
| 403 | } |
---|
| 404 | |
---|
| 405 | def make_doc() { |
---|
| 406 | def err = null |
---|
| 407 | try { |
---|
| 408 | sh 'make clean > /dev/null' |
---|
| 409 | sh 'make > /dev/null 2>&1' |
---|
| 410 | } |
---|
| 411 | catch (Exception caughtError) { |
---|
| 412 | err = caughtError //rethrow error later |
---|
[65f4a51] | 413 | sh 'cat build/*.log' |
---|
[5b8413b4] | 414 | } |
---|
| 415 | finally { |
---|
| 416 | if (err) throw err // Must re-throw exception to propagate error |
---|
| 417 | } |
---|
| 418 | } |
---|