#include tcwg/default.yaml.inc

- job:
#if TOOLCHAIN_llvm && HOST_windows
    name: tcwg-llvm_woa-build
#else
    name: tcwg-#{TOOLCHAIN}-build
#endif
    project-type: freestyle
    defaults: global
    properties:
      - default-properties
    parameters:
#if TOOLCHAIN_gnu
        - string:
            name: release_name
            default: default
            description: 'Name of release series; default is "date +%Y%m%d-%H_%M_%S"'
#elif TOOLCHAIN_llvm
        - string:
            name: release
# if !HOST_windows
            default: ''
            description: 'Release number. Ex. 4.0.1'
# else
            default: 'main'
            description: 'Release build ID (13.0.0-rc1, 13.0.0) or git branch/sha1'
# endif
#endif
#if TOOLCHAIN_gnu
        - string:
            name: buildnumber
            default: 0
            description: 'Build number in release series'
        - choice:
            name: target
            choices:
              - aarch64-linux-gnu
              - aarch64-elf
              - aarch64-linux-gnu_ilp32
              - aarch64_be-elf
              - aarch64_be-linux-gnu
              - arm-eabi
              - arm-linux-gnueabi
              - arm-linux-gnueabihf
              - armeb-eabi
              - armeb-linux-gnueabi
              - armeb-linux-gnueabihf
              - armv8l-linux-gnueabihf
              - native
            description: 'Target'
        - choice:
            name: version
            choices:
              - default
              - latest-rel
              - master
              - gcc8
              - gcc7
              - gcc6
              - gcc5
              - gcc4_9
            description: 'Toolchain version; should match a supported ABE config'
#elif TOOLCHAIN_llvm
# if !HOST_windows
        - string:
            name: candidate
            default: 'git-ref=main'
            description: 'RC number. Ex. 1, 2, final'
        - 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: compiler
            default: 'clang'
            description: 'Override the default system compiler. Only used if "toolchain" is empty.'
        - bool:
            name: useninja
            default: true
            description: 'Whether or not to use ninja for the release (works for releases > 9.0.0)'
        - bool:
            name: testsuite
            default: true
            description: 'Whether or not to trigger a test-suite with this build.'
# else
        - choice:
            name: build_type
            choices:
              - test
              - release
# endif
#endif
#if TOOLCHAIN_gnu
        - choice:
            name: distro
            choices:
              - lts_1
              - lts
            description: 'Ubuntu distro to use for build environment'
#elif TOOLCHAIN_llvm && !HOST_windows
        - string:
            name: build_container_tag
            default: 'lts_1'
            description: 'Type of image to use in the container: lts_1, lts, ...'
#endif
        - label:
            name: builder
#if HOST_windows
            default: tcwg-surface
#else
            default: tcwg-x86_64-build
#endif
            description: 'Jenkins label to build on; determines host of the toolchain'
#if TOOLCHAIN_gnu
        - file:
            name: manifest.txt
            description: 'Use provided manifest file (which may override above settings)'
        - bool:
            name: verbose
            default: false
            description: 'Whether to be verbose'
#endif
        - string:
            name: scripts_branch
#if TOOLCHAIN_gnu || HOST_windows
            default: master
#elif TOOLCHAIN_llvm
            default: tested
#endif
            description: 'Scripts revision to use'
#if TOOLCHAIN_gnu
        - text:
            name: extra_options
            default:
            description: 'Extra options to pass to docker-run'
#endif
    disabled: false
    concurrent: true
    display-name: 'TCWG AAA #{TOOLCHAIN} #{HOST} Build'
#if HOST_windows
    # Save bytes in limited 250-byte windows PATHs
    workspace: ws/tdb$EXECUTOR_NUMBER
#endif
    scm:
      - jenkins-scripts
    wrappers:
        - timeout:
#if TOOLCHAIN_gnu
            timeout: 300
#else TOOLCHAIN_llvm
            timeout: 1800
#endif
        - timestamps
#if HOST_linux
        - ssh-agent-credentials:
            # tcwg-buildslave user id
            users:
                - 'e0958a95-204f-4c14-a66c-5e2be6c5d50a'
#endif
        - build-name:
#if TOOLCHAIN_gnu
            name: '#$BUILD_NUMBER-$version-$release_name-$buildnumber/$builder-$target'
#elif TOOLCHAIN_llvm && !HOST_windows
            name: '#$BUILD_NUMBER-$release-$candidate/$builder'
#elif TOOLCHAIN_llvm && HOST_windows
            name: '#$BUILD_NUMBER-$release/$builder'
#endif
        - workspace-cleanup
    builders:
#if TOOLCHAIN_gnu
        - shell: |
            #!/bin/bash
            set -ex
            abe_branch=tested
            eval $extra_options
            ./jenkins-scripts/docker-run.sh --distro "$distro" \
            -- \
            ./jenkins-scripts/tcwg-dev-build.sh \
            --abe_branch "$abe_branch" \
            --release_name "$release_name" \
            --buildnumber "$buildnumber" \
            --target "$target" \
            --version "$version" \
            --manifest "$(pwd)/manifest.txt" \
            --verbose "$verbose"
#elif TOOLCHAIN_llvm
# if HOST_linux
        - shell: |
            #!/bin/bash

            set -ex

            rm -rf artifacts
            mkdir artifacts

            # Until we figure out a way to use heavy-job with different weights
            # based on the builder, we hardcode to one.
            buildjobs=1

            # Select builder arch/type/container
            case ${builder} in
                tcwg-x86_64-build|tcwg-x86_64-cam)
                    builder_arch=amd64
                    buildjobs=8 # 1 slot is 32/4
                    ;;
                tcwg-tk1_32-build|tcwg-llvm_tk1-*)
                    builder_arch=armhf
                    buildjobs=3 # TK1s only have 4 cores and little RAM
                    ;;
                tcwg-jade*)
                    builder_arch=arm64
                    buildjobs=64 # Jades have lots of cores
                    ;;
                *) echo "ERROR: Unsupported label: $builder"; exit 1 ;;
            esac

            # 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

            # Setup job parameters and run (for now, just pretend to run)
            ret=0
            ./jenkins-scripts/docker-run.sh \
                --arch ${builder_arch} \
                --distro ${build_container_tag} \
                -- \
                bash -x ./jenkins-scripts/tcwg-llvm-release.sh \
                --workspace=${WORKSPACE} \
                --release=${release} \
                --candidate=${candidate} \
                --buildjobs=${buildjobs} \
                --toolchain=${toolchain} \
                --use-ninja=${useninja} \
                $toolchain_file_option $compiler_option \
                > artifacts/release.log 2>&1 || ret=$?

            if [ $ret -ne 0 ]; then
                touch llvm.failed
            fi

            # Chained jobs
            if ${testsuite}; then
              echo "target_list=$builder" > llvm.params.testsuite
              cat $toolchain_file >> llvm.params.testsuite
            fi

            # failure is handled by conditional step on llvm.failed
            # so that the testsuite job is always triggered
            exit 0
        - 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
        - conditional-step:
            condition-kind: file-exists
            condition-filename: llvm.failed
            steps:
                - shell: "exit 1"
# elif HOST_windows
      - batch: |
          jenkins-scripts\tcwg-llvm-release.bat %release% %build_type%
# endif
#endif
    publishers:
        - archive:
            artifacts: 'artifacts/**'
            latest-only: false
