- job:
    name: tcwg-llvm-staged-build
    project-type: matrix
    defaults: global
    properties:
        - authorization:
            anonymous:
                - job-read
                - job-extended-read
            everyone-flat:
                - job-build
                - job-cancel
        - build-discarder:
            days-to-keep: 10
            num-to-keep: 10
    parameters:
        - string:
            name: revision
            default: ''
            description: 'SVN revision in the format rNNNNN. Empty string will bring trunk on all repos.'
        - string:
            name: toolchain
            default: ''
            description: 'URL of the toolchain to use. This needs to be a tarball with a /bin directory inside with either GCC or LLVM from make install'
        - string:
            name: build_type
            default: 'Release'
            description: 'Build type. Accepts: Release, RelWithDebInfo, Debug, MinSizeRel'
        - string:
            name: cmake_flags
            default: ''
            description: 'Additional CMake flags, ex. CMAKE_CXX_FLAGS, etc.'
        - string:
            name: build_container_tag
            default: 'lts'
            description: 'Type of image to use in the container: lts_1, lts, ...'
        - bool:
            name: twostage
            default: false
            description: 'Whether or not to trigger a second stage with this build.'
        - bool:
            name: testsuite
            default: false
            description: 'Whether or not to trigger a test-suite with this build.'
        - string:
            name: compiler
            default: 'clang'
            description: 'Override the default system compiler. Only used if "toolchain" is empty. Accepts: clang, gcc.'
        - string:
            name: repository
            default: 'https://git.linaro.org/toolchain/llvm'
            description: 'Repository to pull changes from.'
        - string:
            name: projects
            default: ''
            description: 'Which projects to build - llvm clang compiler-rt lld libcxx libcxxabi libunwind.'
        - string:
            name: branch
            default: 'master'
            description: 'Which branch to pull from. Need to exist in all projects above, if any selected.'
        - string:
            name: scripts_branch
            default: 'refs/remotes/origin/tested'
            description: 'Branch to pull jenkins-script from'
        - string:
            name: target_list
            default: 'tcwg-x86_64-cam'
            description: 'List of targets -- tcwg-x86_64-cam tcwg-tk1_32-build tcwg-apm_32-build tcwg-apm_64-build-- should be a subset of slave axis'
    disabled: true
    node: tcwg-coordinator
    retry-count: 3
    concurrent: true
    display-name: 'TCWG LLVM Staged Build'
    scm:
        - git:
            url: https://gitlab.com/LinaroLtd/tcwg/jenkins-scripts.git
            refspec: +refs/heads/*:refs/remotes/origin/* +refs/changes/*:refs/changes/*
            name: origin
            branches:
                - ${scripts_branch}
            skip-tag: true
            shallow-clone: true
            wipe-workspace: true
    axes:
        - axis:
            type: slave
            name: label
            values:
                - tcwg-x86_64-cam
                - tcwg-tk1_32-build
                - tcwg-apm_32-build
                - tcwg-apm_64-build
        - axis:
            type: dynamic
            name: target
            values:
                - target_list
    execution-strategy:
        combination-filter: |
            label==target
        sequential: false
    wrappers:
        - timeout:
            timeout: 300
        - timestamps
        - ssh-agent-credentials:
            # tcwg-buildslave user id
            users:
                - 'e0958a95-204f-4c14-a66c-5e2be6c5d50a'
        - build-name:
            name: '#${BUILD_NUMBER}-${GIT_REVISION,length=8}'
    builders:
        - shell: |
            #!/bin/bash
            set -ex

            . jenkins-helpers.sh

            # Select builder arch/type/container
            buildjobs=1
            case ${label} in
                tcwg-x86_64-build|tcwg-x86_64-cam)
                    builder_arch=amd64
                    buildjobs=8 # 1 slot is 32/4
                    ;;
                tcwg-tk1_32-build)
                    builder_arch=armhf
                    buildjobs=4 # TK1s only have 4 cores
                    ;;
                tcwg-apm_32-build)
                    builder_arch=armhf
                    buildjobs=8 # APMs only have 8 cores
                    ;;
                tcwg-apm_64-build)
                    builder_arch=arm64
                    buildjobs=8 # APMs only have 8 cores
                    ;;
                *) echo "ERROR: Unsupported label: $label"; exit 1 ;;
            esac
            linkjobs=${buildjobs}

            # Sets the system compiler
            compiler_option=''
            if [ "${toolchain:+set}" != "set" ] && [ "${compiler:+set}" = "set" ]; then
              compiler_option="--compiler=${compiler}"
            fi

            # Trigger toolchain name
            toolchain_file="${WORKSPACE}/llvm.params.toolchain"
            toolchain_file_option=""
            if ${twostage} || ${testsuite}; then
              toolchain_file_option="--toolchain-file=$toolchain_file"
            fi

            # Start build container
            builder=$(print_host_for_node $NODE_NAME)
            bash ${WORKSPACE}/start-container-docker.sh --arch ${builder_arch} --distro ${build_container_tag} --session-host ${builder} --weight 1 --prefix run_ > build-container.sh

            # Source container logic, set cleanup job
            . ./build-container.sh
            trap "cleanup_all_containers" EXIT

            # Setup job parameters
            RUN=(bash -x ${WORKSPACE}/tcwg-llvm-build.sh \
                --workspace=${WORKSPACE} \
                --buildid=${BUILD_ID} \
                --buildjobs=${buildjobs} \
                --linkjobs=${linkjobs} \
                --revision=${revision} \
                --toolchain=${toolchain} \
                --buildtype=${build_type} \
                --repository=${repository} \
                --branch=${branch} \
                --projects="${projects}" \
                --cmakeflags="${cmake_flags}" \
                $toolchain_file_option $compiler_option)

            # Run the job
            run_container_exec "${RUN[@]}"

            # Chained jobs
            if ${twostage}; then
              # We don't want an infinite chain, 3rd stage is off
              cat << EOF > llvm.params.twostage
              twostage=false
              target_list=${label}
            EOF
              cat $toolchain_file >> llvm.params.twostage

            # If we requested 2 stages and test-suite, only test the 2nd stage
            elif ${testsuite}; then
              echo "target_list=${label}" > llvm.params.testsuite
              cat $toolchain_file >> llvm.params.testsuite

            fi

        - conditional-step:
            condition-kind: file-exists
            condition-filename: llvm.params.twostage
            steps:
                - trigger-builds:
                    - project: 'tcwg-llvm-staged-build'
                      property-file: llvm.params.twostage
                      current-parameters: true
                      block: true

        - conditional-step:
            condition-kind: file-exists
            condition-filename: llvm.params.testsuite
            steps:
                - trigger-builds:
                    - project: 'tcwg-llvm-testsuite'
                      property-file: llvm.params.testsuite
                      current-parameters: true
                      block: true
    publishers:
        - archive:
            artifacts: '*.tar.xz'
            latest-only: false
