/* groovylint-disable DuplicateStringLiteral */
/* groovylint-disable LineLength */
/* groovylint-disable NestedBlockDepth */

pipeline {
    agent none
    options {
        timestamps()
    }
    stages {
        stage('Build') {
            matrix {
                axes {
                    axis {
                        name 'DISTRO'
                        values 'alpine', 'ubuntu'
                    }
                }
                agent {
                    dockerfile {
                        dir "build/jenkins/${DISTRO}"
                        additionalBuildArgs '--load'
                    }
                }
                stages {
                    stage('Bootstrap') {
                        steps {
                            sh 'autoreconf --install'
                            sh 'rm resip/stack/gen/*'
                        }
                    }
                    stage('Configure') {
                        steps {
                            sh "./configure --enable-assert-syslog \
                                            --enable-dtls \
                                            --enable-ipv6 \
                                            --enable-repro-plugins \
                                            --with-apps \
                                            --with-c-ares \
                                            --with-mysql \
                                            --with-netsnmp \
                                            --with-popt \
                                            --with-postgresql \
                                            --with-python \
                                            --with-qpid-proton \
                                            --with-radcli \
                                            --with-recon \
                                            --with-rend \
                                            --with-repro \
                                            --with-return \
                                            --with-ssl \
                                            --with-telepathy \
                                            --with-tfm \
                                            CPPFLAGS=\"-D__pingtel_on_posix__ -D_FILE_OFFS -D_REENTRANT -D_linux_ -DDEFAULT_BRIDGE_MAX_IN_OUTPUTS=20 -DRESIP_DIGEST_LOGGING -I${WORKSPACE}/contrib/cajun/include -I/usr/include/mysql -I/usr/include/sipxtapi -I/usr/include/soci `net-snmp-config --base-cflags`\" \
                                            CXXFLAGS=\"-Wformat -Werror=format-security -fpermissive\" \
                                            DEPS_PYTHON_CFLAGS=\"`/usr/bin/python3.8-config --cflags`\" \
                                            DEPS_PYTHON_LIBS=\"`/usr/bin/python3.8-config --ldflags`\" \
                                            PYCXX_SRCDIR=/usr/share/python3.8/CXX/Python3"
                        }
                    }
                    stage('Build') {
                        steps {
                            sh 'make -j'
                        }
                    }
                    stage('Static Analysis') {
                        when {
                            expression { DISTRO == 'alpine' }
                        }
                        steps {
                            sh 'compiledb -n make'
                            sh 'clang-tidy --list-checks'
                            sh '/usr/share/clang/run-clang-tidy.py -j8 2>/dev/null > clang-tidy-results.txt || true'
                            archiveArtifacts artifacts: 'clang-tidy-results.txt'
                            recordIssues(
                                tools: [
                                    clangTidy(pattern: 'clang-tidy-results.txt')
                                ]
                            )
                        }
                    }
                    stage('Test') {
                        steps {
                            sh 'make -j check TESTS='
                            sh 'make check'
                            archiveArtifacts artifacts: '**/test-suite.log', allowEmptyArchive: true
                        }
                    }
                }
                post {
                    always {
                        recordIssues (
                            filters: [
                                excludeFile('/root/*')
                            ],
                            tools: [
                                gcc(
                                    id: "${DISTRO}-gcc",
                                    name: "${DISTRO} - GNU C Compiler (gcc)"
                                )
                            ]
                        )
                    }
                }
            }
        }
    }
}
