def CleanBuildAndTest(String cmakeArgs) {
    sh """
    export CCACHE_DIR=/ccache
    rm -rf build
    mkdir build
    cd build
    cmake .. -G Ninja -DCMAKE_CXX_COMPILER_LAUNCHER=ccache ${cmakeArgs} 
    ninja
    ccache -s
    """
    catchError {
        sh '''
        cd build
        ctest --no-compress-output --test-output-size-passed 100000 --test-output-size-failed 100000 -T Test -j `nproc` --timeout 90
        zstd Testing/*/Test.xml
        '''
    }
    xunit tools: [CTest(pattern: 'build/Testing/*/Test.xml')], reduceLog: false, skipPublishingChecks: false
    minio bucket: 'jenkins', credentialsId: 'jenkins-minio', excludes: '', host: 'minio.weaselab.dev', includes: 'build/Testing/*/Test.xml.zst', targetFolder: '${JOB_NAME}/${BUILD_NUMBER}/${STAGE_NAME}/'
}

pipeline {
    agent any
    stages {
        stage('Pre-commit') {
            agent {
                dockerfile {
                    args '-v /home/jenkins/ccache:/ccache'
                    reuseNode true
                }
            }
            steps {
                script {
                    env.HOME = env.WORKSPACE
                }
                sh 'pre-commit run --all-files --show-diff-on-failure'
            }
        }
        stage('Clang') {
            agent {
                dockerfile {
                    args '-v /home/jenkins/ccache:/ccache'
                    reuseNode true
                }
            }
            steps {
                CleanBuildAndTest("")
                recordIssues(tools: [clang()])
            }
        }
        stage('64 bit versions') {
            agent {
                dockerfile {
                    args '-v /home/jenkins/ccache:/ccache'
                    reuseNode true
                }
            }
            steps {
                CleanBuildAndTest("-DCMAKE_CXX_FLAGS=-DUSE_64_BIT=1")
            }
        }
        stage('Debug') {
            agent {
                dockerfile {
                    args '-v /home/jenkins/ccache:/ccache'
                    reuseNode true
                }
            }
            steps {
                CleanBuildAndTest("-DCMAKE_BUILD_TYPE=Debug")
            }
        }
        stage('SIMD fallback') {
            agent {
                dockerfile {
                    args '-v /home/jenkins/ccache:/ccache'
                    reuseNode true
                }
            }
            steps {
                CleanBuildAndTest("-DUSE_SIMD_FALLBACK=ON")
            }
        }
        stage('Release [gcc]') {
            agent {
                dockerfile {
                    args '-v /home/jenkins/ccache:/ccache'
                    reuseNode true
                }
            }
            steps {
                CleanBuildAndTest("-DCMAKE_C_COMPILER=gcc -DCMAKE_CXX_COMPILER=g++ -DCMAKE_CXX_FLAGS=-DNVALGRIND")
                recordIssues(tools: [gcc()])
                sh '''
                cd build
                cpack -G DEB
                cpack -G RPM
                '''
                sh '''
                cd paper
                make
                '''
                minio bucket: 'jenkins', credentialsId: 'jenkins-minio', excludes: '', host: 'minio.weaselab.dev', includes: 'build/*.deb,build/*.rpm,paper/*.pdf', targetFolder: '${JOB_NAME}/${BUILD_NUMBER}/${STAGE_NAME}/'
            }
        }
        stage('Release [gcc,aarch64]') {
            agent {
                dockerfile {
                    args '-v /home/jenkins/ccache:/ccache'
                    reuseNode true
                }
            }
            steps {
                CleanBuildAndTest("-DCMAKE_TOOLCHAIN_FILE=../aarch64-toolchain.cmake -DCMAKE_CXX_FLAGS=-DNVALGRIND")
                sh '''
                cd build
                cpack -G DEB
                cpack -G RPM
                '''
                minio bucket: 'jenkins', credentialsId: 'jenkins-minio', excludes: '', host: 'minio.weaselab.dev', includes: 'build/*.deb,build/*.rpm', targetFolder: '${JOB_NAME}/${BUILD_NUMBER}/${STAGE_NAME}'
            }
        }
        stage('Coverage') {
            agent {
                dockerfile {
                    args '-v /home/jenkins/ccache:/ccache'
                    reuseNode true
                }
            }
            steps {
                script {
                    filter_args = "-f ConflictSet.cpp -f LongestCommonPrefix.h -f Metrics.h"
                }
                CleanBuildAndTest("-DCMAKE_C_COMPILER=gcc -DCMAKE_CXX_COMPILER=g++ -DCMAKE_C_FLAGS=--coverage -DCMAKE_CXX_FLAGS=--coverage -DCMAKE_BUILD_TYPE=Debug -DDISABLE_TSAN=ON")
                sh """
                gcovr ${filter_args} --cobertura > build/coverage.xml
                """
                recordCoverage qualityGates: [[criticality: 'NOTE', metric: 'MODULE']], tools: [[parser: 'COBERTURA', pattern: 'build/coverage.xml']]
                sh """
                gcovr ${filter_args}
                gcovr ${filter_args} --fail-under-line 100 > /dev/null
                """
            }
        }
    }
    post {
        always {
            emailext mimeType: 'text/html', body: '${SCRIPT, template="groovy-html.template"}', subject: "${env.JOB_NAME} - Build# ${env.BUILD_NUMBER} - ${currentBuild.currentResult}", to: 'andrew@weaselab.dev'
        }
    }
}
