#include tcwg/default.yaml.inc

- job:
    name: tcwg-#{TOOLCHAIN}-builds
    project-type: multijob
    defaults: global
    properties:
      - default-properties
    parameters:
      - string:
          name: release_name
          default: test
          description: 'Name of release series'
      - string:
          name: buildnumber
          default: 0
          description: 'Build number in release series'
      - choice:
          name: version
          choices:
            - latest-rel
            - master
          description: 'Toolchain version; should match a supported ABE config'
      - bool:
          name: host_x86_64_target_aarch64
          default: true
      - bool:
          name: host_x86_64_target_armhf
          default: true
      - bool:
          name: host_aarch64_target_native
          default: true
      - bool:
          name: host_aarch64_target_armhf
          default: true
      - bool:
          name: host_armhf_target_native
          # FIXME: https://linaro.atlassian.net/browse/STG-5042
          default: false
      - choice:
          name: distro
          choices:
            - lts_1
            - lts
          description: 'Ubuntu distro to use for build environment'
      - bool:
          name: publish
          default: false
          description: 'Whether to publish to snapshots.linaro.org'
      - bool:
          name: verbose
          default: true
          description: 'Whether to be verbose'
    disabled: false
    node: tcwg-coordinator
    concurrent: true
    display-name: 'TCWG AAA GNU Builds'
    wrappers:
      - build-name:
          name: '#$BUILD_NUMBER-$version-$release_name-$buildnumber'
      - timeout:
          timeout: 600
      - timestamps
      - workspace-cleanup
    builders:
      - shell: |
          #!/bin/bash
          set -euf -o pipefail
          if $verbose; then
            set -x
          fi

          if [ x"$(ls)" != x"" ]; then
            echo "ERROR: Dirty workspace"
            exit 1
          fi
          
          cat > build-params.txt <<EOF
          release_name=$release_name
          buildnumber=$buildnumber
          version=$version
          distro=$distro
          EOF
      - multijob:
          name: Build
          projects:
            - name: tcwg-#{TOOLCHAIN}-build
              alias: "host_x86_64_target_aarch64"
              enable-condition: "$host_x86_64_target_aarch64"
              node-label-name: "builder"
              node-label: "tcwg-build && tcwg-x86_64-build"
              property-file: build-params.txt
              predefined-parameters: "target=aarch64-linux-gnu"
            - name: tcwg-#{TOOLCHAIN}-build
              alias: "host_x86_64_target_armhf"
              enable-condition: "$host_x86_64_target_armhf"
              node-label-name: "builder"
              node-label: "tcwg-build && tcwg-x86_64-build"
              property-file: build-params.txt
              predefined-parameters: "target=arm-linux-gnueabihf"
            - name: tcwg-#{TOOLCHAIN}-build
              alias: "host_aarch64_target_aarch64"
              enable-condition: "$host_aarch64_target_native"
              node-label-name: "builder"
              node-label: "tcwg-build && tcwg-armv8_64"
              property-file: build-params.txt
              predefined-parameters: "target=native"
            - name: tcwg-#{TOOLCHAIN}-build
              alias: "host_aarch64_target_armhf"
              enable-condition: "$host_aarch64_target_armhf"
              node-label-name: "builder"
              node-label: "tcwg-build && tcwg-armv8_64"
              property-file: build-params.txt
              predefined-parameters: "target=arm-linux-gnueabihf"
            - name: tcwg-#{TOOLCHAIN}-build
              alias: "host_armhf_target_native"
              enable-condition: "$host_armhf_target_native"
              node-label-name: "builder"
              node-label: "tcwg-build && tcwg-armv7"
              property-file: build-params.txt
              predefined-parameters: "target=native"
      - shell: |
          #!/bin/bash
          set -euf -o pipefail
          if $verbose; then
            set -x
          fi
          
          env | grep "TCWG_#{TOOLCHAIN2}_BUILD"
      - conditional-step:
          condition-kind: num-comp
          lhs: $TRIGGERED_BUILD_RUN_COUNT_TCWG_#{TOOLCHAIN2}_BUILD
          comparator: greater-than-equal
          rhs: 1
          steps:
            - copyartifact:
                project: tcwg-#{TOOLCHAIN}-build
                which-build: specific-build
                # Note that $TCWG_#{TOOLCHAIN2}_BUILD_BUILD_NUMBER points to the last
                # build (i.e., it's the same as $TCWG_#{TOOLCHAIN2}_BUILD_5_BUILD_NUMBER
                # in a full 5-configuration build).  At the same time,
                # $TCWG_#{TOOLCHAIN2}_BUILD_nnn_BUILD_NUMBER variables are defined only
                # when multiple builds were triggered.
                # We need to use $TCWG_#{TOOLCHAIN2}_BUILD_BUILD_NUMBER here to correctly
                # handle single-toolchain builds.
                build-number: $TCWG_#{TOOLCHAIN2}_BUILD_BUILD_NUMBER
                target: tcwg-#{TOOLCHAIN}-build-1
      - conditional-step:
          condition-kind: num-comp
          lhs: $TRIGGERED_BUILD_RUN_COUNT_TCWG_#{TOOLCHAIN2}_BUILD
          comparator: greater-than-equal
          rhs: 2
          steps:
            - copyartifact:
                project: tcwg-#{TOOLCHAIN}-build
                which-build: specific-build
                build-number: $TCWG_#{TOOLCHAIN2}_BUILD_1_BUILD_NUMBER
                target: tcwg-#{TOOLCHAIN}-build-2
      - conditional-step:
          condition-kind: num-comp
          lhs: $TRIGGERED_BUILD_RUN_COUNT_TCWG_#{TOOLCHAIN2}_BUILD
          comparator: greater-than-equal
          rhs: 3
          steps:
            - copyartifact:
                project: tcwg-#{TOOLCHAIN}-build
                which-build: specific-build
                build-number: $TCWG_#{TOOLCHAIN2}_BUILD_2_BUILD_NUMBER
                target: tcwg-#{TOOLCHAIN}-build-3
      - conditional-step:
          condition-kind: num-comp
          lhs: $TRIGGERED_BUILD_RUN_COUNT_TCWG_#{TOOLCHAIN2}_BUILD
          comparator: greater-than-equal
          rhs: 4
          steps:
            - copyartifact:
                project: tcwg-#{TOOLCHAIN}-build
                which-build: specific-build
                build-number: $TCWG_#{TOOLCHAIN2}_BUILD_3_BUILD_NUMBER
                target: tcwg-#{TOOLCHAIN}-build-4
      - conditional-step:
          condition-kind: num-comp
          lhs: $TRIGGERED_BUILD_RUN_COUNT_TCWG_#{TOOLCHAIN2}_BUILD
          comparator: greater-than-equal
          rhs: 5
          steps:
            - copyartifact:
                project: tcwg-#{TOOLCHAIN}-build
                which-build: specific-build
                build-number: $TCWG_#{TOOLCHAIN2}_BUILD_4_BUILD_NUMBER
                target: tcwg-#{TOOLCHAIN}-build-5
# linaro-publish-token has not been updated in a while.
# Disable publishing support until we need it.
#      - linaro-publish-token
      - shell: |
          #!/bin/bash
          set -euf -o pipefail
          if $verbose; then
            set -x
          fi

          mkdir out

          i="$TRIGGERED_BUILD_RUN_COUNT_TCWG_#{TOOLCHAIN2}_BUILD"
          while [ $i -gt 0 ]; do
            rsync -a tcwg-#{TOOLCHAIN}-build-$i/artifacts/binaries/ out/
            i=$(($i-1))
          done

          if [ x"$(ls out/ | wc -l)" != x"1" ]; then
            echo "ERROR: not a single directory in builds artifacts:"
            ls -l out/
            exit 1
          fi
          out_release=$(set +f; echo out/*-$release_name-$buildnumber)
          if ! [ -d "$out_release" ]; then
            echo "ERROR: no $out_release directory in builds artifacts:"
            ls -l out/
            exit 1
          fi

          cat > HEADER-latest-rel.textile <<EOF
          h4. Linaro Toolchain Upstream Integration Builds - Stable $release_name-$buildnumber
          
          These builds are made from latest release branches of GNU toolchain
           components.
           See included manifests for details.

          These builds are not for production environments.
           We will do our best to investigate problems reported via
           "Linaro Bugzilla":https://bugs.linaro.org/ and issue trackers of
           upstream projects.
          EOF

          cat > HEADER-master.textile <<EOF
          h4. Linaro Toolchain Upstream Integration Builds - *UNSTABLE* $release_name-$buildnumber
          
          These builds are made from whatever happened to be in master branches
           of GNU toolchain components at the moment.
           See included manifests for details.

          These builds are for experimental environments only.  They *will*
           break your stuff and drink all your beer.  You have been warned.
           We will do our best to investigate problems reported via
           "Linaro Bugzilla":https://bugs.linaro.org/ and issue trackers of
           upstream projects.
          EOF

          cp HEADER-$version.textile HEADER.textile
          cp HEADER.textile "$out_release/"

          wget https://git.linaro.org/ci/publishing-api.git/plain/linaro-cp.py -O ./linaro-cp.py
          linaro_cp_opts=""
          if ! $publish; then
            linaro_cp_opts="--dry-run"
          fi
          time python3 ./linaro-cp.py --api_version 3 $linaro_cp_opts \
            "$out_release/" "gnu-toolchain/$(basename "$out_release")"

- job:
    name: tcwg-#{TOOLCHAIN}-builds-monthly
    project-type: freestyle
    defaults: global
    properties:
      - default-properties
    disabled: false
    node: tcwg-coordinator
    concurrent: false
    display-name: 'TCWG AAA GNU Builds Monthly Trigger'
    triggers:
      - timed: '@weekly'
    wrappers:
      - timeout:
          timeout: 300
      - timestamps
    builders:
      - shell: |
          #!/bin/bash
          set -euf -o pipefail
          set -x

          # Release name is Year.Month-Week_of_month
          buildnumber=$((($(date +%e)+6)/7))
          publish=false
          if [ x"$buildnumber" = x"1" ]; then
            # Disable publishing until linaro-publish-token is updated.
            #publish=true
            publish=flase
          fi
          cat > build-params.txt <<EOF
          release_name=$(date +%Y.%m)
          buildnumber=$buildnumber
          publish=$publish
          EOF
      - trigger-builds:
          - project: tcwg-#{TOOLCHAIN}-builds
            property-file: build-params.txt
            predefined-parameters: |
              version=latest-rel
              distro=lts_1
          - project: tcwg-#{TOOLCHAIN}-builds
            property-file: build-params.txt
            predefined-parameters: |
              version=master
              distro=lts
