- job:
    name: tcwg-backport
    project-type: freestyle
    defaults: global
    properties:
        - authorization:
            anonymous:
                - job-read
                - job-extended-read
            everyone-flat:
                - job-build
                - job-cancel
        - build-discarder:
            days-to-keep: 30
            num-to-keep: 60
    parameters:
        - string:
            name: GERRIT_PATCHSET_REVISION
            default: refs/remotes/origin/linaro/gcc-5-branch
            description: 'Patched GCC revision'
        - string:
            name: GERRIT_BRANCH
            default: linaro-local/gcc-5-integration-branch
            description: 'Base GCC branch'
        - string:
            name: gcc_orig
            default: gcc_patch_prev
            description: 'Original GCC revision'
        - string:
            name: log_server
            default: dev-01.tcwglab:/home/tcwg-buildslave/logs
            description: 'Log_Server'
        - string:
            name: target_list
            default: 'aarch64-linux-gnu armv8l-linux-gnueabihf arm-linux-gnueabi arm-linux-gnueabihf armeb-linux-gnueabihf arm-eabi aarch64-none-elf aarch64_be-none-elf tcwg-x86_64-build'
            description: 'List of targets -- aarch64-linux-gnu armv8l-linux-gnueabihf arm-linux-gnueabi arm-linux-gnueabihf armeb-linux-gnueabihf arm-eabi aarch64-none-elf aarch64_be-none-elf tcwg-x86_64-build tcwg-x86_32-build tcwg-tk1_32-build tcwg-apm_64-build tcwg-apm_32-build -- the tcwg-* targets should be a subset of slave axis'
        - string:
            name: abe_branch
            default: tested
            description: 'ABE revision to use'
        - string:
            name: scripts_branch
            default: tested
            description: 'jenkins-scripts revision to use'
        - bool:
            name: rebuild
            default: false
            description: 'Rebuild the toolchain even if results are already on logserver'
        - bool:
            name: dryrun
            default: false
            description: 'Dry-run, do nothing, just print steps'
    disabled: false
    node: tcwg-coordinator
    concurrent: true
    display-name: 'TCWG AAA Backport'
    scm:
        - git:
            url: https://git.linaro.org/toolchain/gcc.git
            refspec: +refs/changes/*:refs/remotes/changes/*
            basedir: gcc
            branches:
                - ${GERRIT_PATCHSET_REVISION}
            skip-tag: true
            wipe-workspace: false
            clean:
                before: true
            reference-repo: /home/tcwg-buildslave/snapshots-ref/gcc.git
            choosing-strategy: gerrit
        - git:
            url: https://gitlab.com/Linaro/tcwg/abe.git
            refspec: +refs/changes/*:refs/remotes/changes/*
            basedir: abe
            branches:
                - $abe_branch
            skip-tag: true
            shallow-clone: true
            wipe-workspace: true
        - git:
            url: https://gitlab.com/LinaroLtd/tcwg/jenkins-scripts.git
            refspec: +refs/heads/*:refs/remotes/origin/* +refs/changes/*:refs/changes/*
            branches:
                - $scripts_branch
            basedir: jenkins-scripts
            skip-tag: true
            shallow-clone: true
            wipe-workspace: true
    triggers:
        - gerrit:
            server-name: 'review.linaro.org'
            override-votes: true
            gerrit-build-successful-verified-value: 1
            gerrit-build-failed-verified-value: -1
            gerrit-build-successful-codereview-value: 1
            gerrit-build-failed-codereview-value: -1
            readable-message: true
            successful-message: 'Sanity check OK'
            failure-message: 'Sanity check FAIL'
            trigger-for-unreviewed-patches: true
            projects:
                - project-compare-type: 'PLAIN'
                  project-pattern: 'toolchain/gcc'
                  branches:
                    - branch-compare-type: 'ANT'
                      branch-pattern: '**'
    wrappers:
        - timeout:
            timeout: 3000
        - timestamps
        - ssh-agent-credentials:
            # tcwg-buildslave user id
            users:
                - 'e0958a95-204f-4c14-a66c-5e2be6c5d50a'
        - build-name:
            name: '#${BUILD_NUMBER}-${ENV,var="GERRIT_PATCHSET_REVISION"}'
    builders:
        - shell: |
            #!/bin/bash

            set -e
            set -x

            # Make sure we start from a clean state
            rm -f gcc_orig_parameters gcc_patch_prev compare_results_parameters

            # For git_rev_parse
            . ./jenkins-scripts/jenkins-helpers.sh

            # Split in two commands to help debug traces if needed
            commit_msg=$(git -C gcc log --pretty=%b -n1 $GERRIT_PATCHSET_REVISION)
            commit_msg=$(echo $commit_msg | grep DO_NOT_RUN_VALIDATION) || true
            if [ "x$commit_msg" != "x" ]; then
              exit 0
            fi

            abe_rev=$(git_rev_parse abe $abe_branch)
            gcc_patch_rev=$(git_rev_parse gcc $GERRIT_PATCHSET_REVISION)
            if [ x"$gcc_orig" = x"gcc_patch_prev" ]; then
              gcc_orig="$gcc_patch_rev^"
            fi
            gcc_orig_rev=$(git_rev_parse gcc $gcc_orig)

            case ${GERRIT_BRANCH} in
              *gcc-4_9*)
                toolchainconf='--extraconfigdir ../config/gcc4_9'
                ;;
              *gcc-5*)
                toolchainconf='--extraconfigdir ../config/gcc5'
                ;;
              *gcc-6*)
                toolchainconf='--extraconfigdir ../config/gcc6'
                ;;
              *gcc-7*)
                toolchainconf='--extraconfigdir ../config/gcc7'
                ;;
              *)
                toolchainconf='--extraconfigdir ../config/latest-rel'
                ;;
            esac

            cat <<EOF > gcc_orig_parameters
            override=$toolchainconf gcc=gcc.git@$gcc_orig_rev
            log_name=tcwg-backport-$gcc_orig_rev/$abe_rev/@@host@@.\$target
            rebuild=${rebuild}
            dont_fail=true
            log_server=$log_server
            target_list=$target_list
            abe_branch=$abe_rev
            scripts_branch=$scripts_branch
            displaytag=backport-ref-${GERRIT_CHANGE_NUMBER}
            dryrun=$dryrun
            EOF

            cat <<EOF > gcc_patch_parameters
            override=$toolchainconf gcc=gcc.git@$gcc_patch_rev
            log_name=tcwg-backport-$gcc_patch_rev/$abe_rev/@@host@@.\$target
            rebuild=${rebuild}
            dont_fail=true
            log_server=$log_server
            target_list=$target_list
            abe_branch=$abe_rev
            scripts_branch=$scripts_branch
            displaytag=backport-patch-${GERRIT_CHANGE_NUMBER}
            dryrun=$dryrun
            EOF

            cat <<EOF > compare_results_parameters
            ref_logs=tcwg-backport-$gcc_orig_rev/$abe_rev
            new_logs=tcwg-backport-$gcc_patch_rev/$abe_rev
            target_list=$target_list
            log_server=$log_server
            dryrun=$dryrun
            EOF
        - conditional-step:
            condition-kind: file-exists
            condition-filename: gcc_orig_parameters
            steps:
              - trigger-builds:
                  - project: tcwg-buildfarm
                    parameter-factories:
                      - factory: filebuild
                        file-pattern: gcc_*_parameters
                    block: true
        - conditional-step:
            condition-kind: file-exists
            condition-filename: gcc_orig_parameters
            steps:
              - trigger-builds:
                  - project: tcwg-compare-results
                    property-file: compare_results_parameters
                    block: true
