#!groovy

//===========================================================================================================
// Main loop of the compilation
//===========================================================================================================
node ('master'){

	boolean bIsSandbox = env.BRANCH_NAME == "jenkins-sandbox"
	def err = null
	def log_needed = false

	stage_name 		= ''

	compiler 		= null
	arch_name 		= ''
	architecture 	= ''
	
	do_alltests		= false
	do_benchmark	= false
	do_doc		= false
	do_publish		= false
	do_sendemail	= true

	currentBuild.result = "SUCCESS"

	try {
		//Wrap build to add timestamp to command line
		wrap([$class: 'TimestamperBuildWrapper']) {

			//Prevent the build from exceeding 60 minutes
			timeout(60) {

				notify_server()

				prepare_build()

				checkout()

				build()

				test()

				benchmark()

				clean()

				build_doc()

				publish()

				notify_server()
			}
		}
	}

	//If an exception is caught we need to change the status and remember to
	//attach the build log to the email
	catch (Exception caughtError) {
		//rethrow error later
		err = caughtError

		//An error has occured, the build log is relevent
		log_needed = true

		//Store the result of the build log
		currentBuild.result = "${stage_name} FAILURE".trim()
	}

	finally {
		//Send email with final results if this is not a full build
		if( do_sendemail && !bIsSandbox ) {
			echo 'Notifying users of result'
			email(currentBuild.result, log_needed)
		}

		echo 'Build Completed'

		/* Must re-throw exception to propagate error */
		if (err) {
			throw err
		}
	}
}

//===========================================================================================================
// Helper classes/variables/routines
//===========================================================================================================
//Helper routine to collect information about the git history
def collect_git_info() {

	//create the temporary output directory in case it doesn't already exist
	def out_dir = pwd tmp: true
	sh "mkdir -p ${out_dir}"

	//parse git logs to find what changed
	gitRefName = env.BRANCH_NAME
	dir("../${gitRefName}@script") {
		sh "git reflog > ${out_dir}/GIT_COMMIT"
	}
	git_reflog = readFile("${out_dir}/GIT_COMMIT")
	gitRefOldValue = (git_reflog =~ /moving from (.+) to (.+)/)[0][1]
	gitRefNewValue = (git_reflog =~ /moving from (.+) to (.+)/)[0][2]
}

def prepare_build() {
	properties ([ 													\
		[$class: 'ParametersDefinitionProperty', 								\
			parameterDefinitions: [ 									\
				[$class: 'ChoiceParameterDefinition',						\
					description: 'Which compiler to use',					\
					name: 'pCompiler',								\
					choices: 'gcc-6\ngcc-5\ngcc-4.9\nclang',					\
					defaultValue: 'gcc-6',								\
				],												\
				[$class: 'ChoiceParameterDefinition',						\
					description: 'The target architecture',					\
					name: 'pArchitecture',								\
					choices: 'x64\nx86',								\
					defaultValue: 'x64',								\
				],												\
				[$class: 'BooleanParameterDefinition',  						\
					description: 'If false, only the quick test suite is ran', 		\
					name: 'pRunAllTests', 								\
					defaultValue: false,  								\
				], 												\
				[$class: 'BooleanParameterDefinition',  						\
					description: 'If true, jenkins also runs benchmarks', 		\
					name: 'pRunBenchmark', 								\
					defaultValue: true,  								\
				], 												\
				[$class: 'BooleanParameterDefinition',  						\
					description: 'If true, jenkins also builds documentation', 		\
					name: 'pBuildDocumentation', 							\
					defaultValue: true,  								\
				],												\
				[$class: 'BooleanParameterDefinition',  						\
					description: 'If true, jenkins also publishes results', 		\
					name: 'pPublish', 								\
					defaultValue: false,  								\
				],												\
				[$class: 'BooleanParameterDefinition',  						\
					description: 'If true, jenkins will not send emails', 		\
					name: 'pSilent', 									\
					defaultValue: false,  								\
				],												\
			],
		]])

	compiler 		= compiler_from_params( pCompiler )
	arch_name		= pArchitecture
	architecture 	= architecture_from_params( arch_name )

	do_alltests		= (pRunAllTests == 'true')
	do_benchmark	= (pRunBenchmark == 'true')
	do_doc		= (pBuildDocumentation == 'true')
	do_publish		= (pPublish == 'true')
	do_sendemail	= ! (pSilent == 'true')

	echo """Compiler 		: ${compiler.cc_name} (${compiler.cpp_cc}/${compiler.cfa_cc})
Architecture		: ${arch_name}
Arc Flags		: ${architecture}
Run All Tests		: ${ pRunAllTests.toString() }
Run Benchmark		: ${ pRunBenchmark.toString() }
Build Documentation	: ${ pBuildDocumentation.toString() }
Publish		: ${ pPublish.toString() }
Silent			: ${ pSilent.toString() }
"""

	collect_git_info()
}

def build_stage(String name) {
	stage_name = name
	stage name
}

def notify_server() {
	sh 'curl --silent -X POST http://plg2:8082/jenkins/notify > /dev/null || true'
	return
}

def make_doc() {
	def err = null
	try {
		sh 'make clean > /dev/null'
		sh 'make > /dev/null 2>&1'
	} 
	catch (Exception caughtError) {
		err = caughtError //rethrow error later
		sh 'cat *.log'
	}
	finally {
		if (err) throw err // Must re-throw exception to propagate error
	}
}

//Description of a compiler (Must be serializable since pipelines are persistent)
class CC_Desc implements Serializable {
	public String cc_name
	public String cpp_cc
	public String cfa_cc

	CC_Desc(String cc_name, String cpp_cc, String cfa_cc) {
		this.cc_name = cc_name
		this.cpp_cc = cpp_cc
		this.cfa_cc = cfa_cc
	}
}

def compiler_from_params(cc) {
	switch( cc ) {
		case 'gcc-6':
			return new CC_Desc('gcc-6', 'g++-6', 'gcc-6')
		break
		case 'gcc-5':
			return new CC_Desc('gcc-5', 'g++-5', 'gcc-5')
		break
		case 'gcc-4.9':
			return new CC_Desc('gcc-4.9', 'g++-4.9', 'gcc-4.9')
		break
		case 'clang':
			return new CC_Desc('clang', 'clang++', 'gcc-6')
		break
		default :
			error "Unhandled compiler : ${cc}"
	}
}

def architecture_from_params( arch ) {
	switch( arch ) {
		case 'x64':
			return '--host=x86_64'
		break
		case 'x86':
			return '--host=i386'
		break
		default :
			error "Unhandled architecture : ${arch}"
	}
}

//===========================================================================================================
// Main compilation routines
//===========================================================================================================
//Compilation script is done here but environnement set-up and error handling is done in main loop
def checkout() {
	build_stage'Checkout'
		//checkout the source code and clean the repo
		checkout scm

		//Clean all temporary files to make sure no artifacts of the previous build remain
		sh 'git clean -fdqx'

		//Reset the git repo so no local changes persist
		sh 'git reset --hard'
}

def build() {
	build_stage'Build'
	
		def install_dir = pwd tmp: true
		
		//Configure the conpilation (Output is not relevant)
		//Use the current directory as the installation target so nothing
		//escapes the sandbox
		//Also specify the compiler by hand
		sh "./configure CXX=${compiler.cpp_cc} ${architecture} --with-backend-compiler=${compiler.cfa_cc} --prefix=${install_dir} --enable-silent-rules --quiet"

		//Compile the project
		sh 'make -j 8 --no-print-directory V=0 install'
}

def test() {
	build_stage'Test'

		//Run the tests from the tests directory
		if ( do_alltests ) {
			sh 'make -C src/tests all-tests debug=yes'
			sh 'make -C src/tests all-tests debug=no'
		}
		else {
			sh 'make -C src/tests'
		}
}

def benchmark() {
	build_stage'Benchmark'

		if( !do_benchmark ) return

		//Write the commit id to Benchmark
		writeFile  file: 'bench.csv', text:'data=' + gitRefNewValue + ',' + arch_name + ','
 
		//Append bench results
		sh 'make -C src/benchmark --no-print-directory csv-data >> bench.csv'
}

def clean() {
	build_stage'Cleanup'

		//do a maintainer-clean to make sure we need to remake from scratch
		sh 'make maintainer-clean > /dev/null'
}

def build_doc() {
	build_stage'Documentation'

		if( !do_doc ) return

		dir ('doc/user') {
			make_doc()
		}

		dir ('doc/refrat') {
			make_doc()
		}
}

def publish() {
	build_stage'Publish'

		if( !do_publish ) return

		//Then publish the results
		sh 'curl --silent --data @bench.csv http://plg2:8082/jenkins/publish > /dev/null || true'
}

//===========================================================================================================
//Routine responsible of sending the email notification once the build is completed
//===========================================================================================================
//Standard build email notification
def email(String status, boolean log) {
	//Since tokenizer doesn't work, figure stuff out from the environnement variables and command line
	//Configurations for email format
	def project_name = (env.JOB_NAME =~ /(.+)\/.+/)[0][1].toLowerCase()

	def gitLog = 'Error retrieving git logs'
	def gitDiff = 'Error retrieving git diff'

	try {

		sh "git rev-list --format=short ${gitRefOldValue}...${gitRefNewValue} > GIT_LOG"
		gitLog = readFile('GIT_LOG')

		sh "git diff --stat ${gitRefNewValue} ${gitRefOldValue} > GIT_DIFF"
		gitDiff = readFile('GIT_DIFF')
	}
	catch (Exception error) {}

	def email_subject = "[${project_name} git][BUILD# ${env.BUILD_NUMBER} - ${status}] - branch ${env.BRANCH_NAME}"
	def email_body = """This is an automated email from the Jenkins build machine. It was
generated because of a git hooks/post-receive script following
a ref change was pushed to the repository containing
the project "UNNAMED PROJECT".

The branch ${env.BRANCH_NAME} has been updated.
   via  ${gitRefOldValue} (commit)
  from  ${gitRefNewValue} (commit)

Check console output at ${env.BUILD_URL} to view the results.

- Status --------------------------------------------------------------

BUILD# ${env.BUILD_NUMBER} - ${status}

- Log -----------------------------------------------------------------
${gitLog}
-----------------------------------------------------------------------
Summary of changes:
${gitDiff}
"""

	def email_to = "pabuhr@uwaterloo.ca, rschlunt@uwaterloo.ca, a3moss@uwaterloo.ca, tdelisle@uwaterloo.ca, brice.dobry@huawei.com"

	//send email notification
	emailext body: email_body, subject: email_subject, to: email_to, attachLog: log
}
