common_steps_template: &COMMON_STEPS_TEMPLATE
  install_prerequisites_script: ./ci/cirrusci.sh
  install_host_compiler_script: ./ci/run.sh install_host_compiler
  setup_repos_script: |
    set -uexo pipefail
    ln -s $CIRRUS_WORKING_DIR ../dmd

    if [ -z ${CIRRUS_PR+x} ] ; then
      # not a PR
      REPO_BRANCH="$CIRRUS_BRANCH"
    else
      # PR - merge with target branch first
      git fetch origin "+refs/pull/$CIRRUS_PR/merge:"
      git checkout -f FETCH_HEAD

      if [[ ! "$CIRRUS_BRANCH" =~ ^pull/ ]]; then
        # PR originating from the official dlang repo
        REPO_BRANCH="$CIRRUS_BRANCH"
      else
        # PR from a fork
        REPO_BRANCH="$CIRRUS_BASE_BRANCH"
      fi
    fi

    ./ci/run.sh setup_repos "$REPO_BRANCH"
  build_script: ./ci/run.sh build

  test_dmd_script: |
    if [ "${DMD_TEST_COVERAGE:-0}" == "1" ]
    then
      OS_NAME=$OS ENABLE_RELEASE=0 ENABLE_DEBUG=1 ENABLE_COVERAGE=1 ./ci/run.sh rebuild
      ./ci/run.sh test_dmd
      ./ci/run.sh codecov
    else
      ./ci/run.sh test_dmd
    fi

  test_druntime_script: \[ "${DMD_TEST_COVERAGE:-0}" == "1" \] || ./ci/run.sh test_druntime
  test_phobos_script: \[ "${DMD_TEST_COVERAGE:-0}" == "1" \] || ./ci/run.sh test_phobos

environment:
  CIRRUS_CLONE_DEPTH: 50
  # for ci/run.sh:
  MODEL: 64
  HOST_DMD: dmd
  N: 4
  OS_NAME: linux
  FULL_BUILD: true

coverage_environment_template: &COVERAGE_ENVIRONMENT_TEMPLATE
  DMD_TEST_COVERAGE: 1
  CODECOV_TOKEN: ENCRYPTED[cc6ce01618eaee6c9a08ebc81446a9233588df3b2c6ba4e30ffb7715ee9815734ccfd9fcec4f6abfc6a1b9dd8253110d]

# FreeBSD
freebsd13_task:
  name: FreeBSD 13.0 x64, DMD ($TASK_NAME_TYPE)
  allow_failures: true # might be out of credits
  freebsd_instance:
    image_family: freebsd-13-0
    cpu: 4
    memory: 8G
  timeout_in: 60m
  environment:
    OS_NAME: freebsd
    CI_DFLAGS: -version=TARGET_FREEBSD13
    matrix:
      - TASK_NAME_TYPE: coverage
        << : *COVERAGE_ENVIRONMENT_TEMPLATE
  install_bash_script: pkg install -y bash
  << : *COMMON_STEPS_TEMPLATE
