modified_clone: &MODIFIED_CLONE
  # makes sure that for a PR the CI runs against a merged main
  clone_script: |
    if [ -z "$CIRRUS_PR" ]; then
      # if you're not in a PR then clone against the branch name that was pushed to.
      git clone --recursive --branch=$CIRRUS_BRANCH https://x-access-token:${CIRRUS_REPO_CLONE_TOKEN}@github.com/${CIRRUS_REPO_FULL_NAME}.git $CIRRUS_WORKING_DIR
      git reset --hard $CIRRUS_CHANGE_IN_REPO
    else
      # it's a PR so clone the main branch then merge the changes from the PR
      git clone https://x-access-token:${CIRRUS_REPO_CLONE_TOKEN}@github.com/${CIRRUS_REPO_FULL_NAME}.git $CIRRUS_WORKING_DIR
      git fetch origin pull/$CIRRUS_PR/head:pull/$CIRRUS_PR
    
      # CIRRUS_BASE_BRANCH will probably be `main` for the majority of the time
      # However, if you do a PR against a maintenance branch we will want to
      # merge the PR into the maintenance branch, not main
      git checkout $CIRRUS_BASE_BRANCH

      # alpine git package needs default user.name and user.email to be set before a merge
      git -c user.email="you@example.com" merge --no-commit pull/$CIRRUS_PR
      git submodule update --init --recursive
    fi


linux_aarch64_test_task:
  use_compute_credits: $CIRRUS_USER_COLLABORATOR == 'true'
  compute_engine_instance:
    image_project: cirrus-images
    image: family/docker-builder-arm64
    architecture: arm64
    platform: linux
    cpu: 1
    memory: 4G

  <<: *MODIFIED_CLONE

  ccache_cache:
    folder: .ccache
    populate_script:
      - mkdir -p .ccache
    fingerprint_key: ccache-linux_aarch64

  prepare_env_script: |
    apt-get update
    apt-get install -y --no-install-recommends software-properties-common gcc g++ gfortran pkg-config ccache
    apt-get install -y --no-install-recommends python3.10 python3.10-venv libopenblas-dev libatlas-base-dev liblapack-dev

    # python3.10 -m ensurepip --default-pip --user
    ln -s $(which python3.10) python

    # put ccache and python on PATH
    export PATH=/usr/lib/ccache:$PWD:$PATH
    echo "PATH=$PATH" >> $CIRRUS_ENV
    echo "CCACHE_DIR=$PWD/.ccache" >> $CIRRUS_ENV

    pip install -r requirements/build_requirements.txt
    pip install -r requirements/test_requirements.txt

  build_script: |
    spin build -- -Dallow-noblas=true

  test_script: |
    spin test -j 1
    ccache -s


freebsd_test_task:
  use_compute_credits: $CIRRUS_USER_COLLABORATOR == 'true'
  compute_engine_instance:
    image_project: freebsd-org-cloud-dev
    image: family/freebsd-14-2
    platform: freebsd
    cpu: 1
    memory: 4G

  install_devtools_script: |
    pkg install -y git bash ninja ccache blas cblas lapack pkgconf
    pkg install -y python311

  <<: *MODIFIED_CLONE

  ccache_cache:
    folder: .ccache
    populate_script:
      - mkdir -p .ccache
    fingerprint_key: ccache-freebsd

  prepare_env_script: |
    # Create a venv (the `source` command needs bash, not the default sh shell)
    chsh -s /usr/local/bin/bash
    python3.11 -m venv .venv
    source .venv/bin/activate
    # Minimal build and test requirements
    python3.11 -m pip install -U pip
    python3.11 -m pip install meson-python Cython pytest hypothesis

  build_script: |
    chsh -s /usr/local/bin/bash
    source .venv/bin/activate
    python3.11 -m pip install . --no-build-isolation -v -Csetup-args="-Dallow-noblas=false"

  test_script: |
    chsh -s /usr/local/bin/bash
    source .venv/bin/activate
    cd tools
    python3.11 -m pytest --pyargs numpy -m "not slow"
    ccache -s

  on_failure:
    debug_script: |
      cat build/meson-logs/meson-log.txt
