build_and_store_wheels: &BUILD_AND_STORE_WHEELS
  install_cibuildwheel_script:
    - python -m pip install cibuildwheel
  cibuildwheel_script:
    - cibuildwheel
  wheels_artifacts:
    path: "wheelhouse/*"

######################################################################
# Build linux_aarch64 natively
######################################################################

linux_aarch64_task:
  use_compute_credits: $CIRRUS_USER_COLLABORATOR == 'true'
  env:
    CIRRUS_CLONE_SUBMODULES: true
  compute_engine_instance:
    image_project: cirrus-images
    image: family/docker-builder-arm64
    architecture: arm64
    platform: linux
    cpu: 1
    memory: 4G
  matrix:
    # build in a matrix because building and testing all four wheels in a
    # single task takes longer than 60 mins (the default time limit for a
    # cirrus-ci task).
    - env:
        CIBW_BUILD: cp310-*
    - env:
        CIBW_BUILD: cp311-*
    - env:
        CIBW_BUILD: cp312-*
    - env:
        CIBW_BUILD: cp313-*
    - env:
        CIBW_BUILD: cp313t-*
        CIBW_FREE_THREADED_SUPPORT: 1
        CIBW_BUILD_FRONTEND: "pip; args: --no-build-isolation"

  initial_setup_script: |
    apt update
    apt install -y python3-venv python-is-python3 gfortran libatlas-base-dev libgfortran5 eatmydata
    git fetch origin
    bash ./tools/wheels/cibw_before_build.sh ${PWD}
    which python
    echo $CIRRUS_CHANGE_MESSAGE
  <<: *BUILD_AND_STORE_WHEELS


######################################################################
# Build macosx_arm64 natively
#
# macosx_arm64 for macos >= 14 used to be built here, but are now
# built on GHA.
######################################################################

macosx_arm64_task:
  use_compute_credits: $CIRRUS_USER_COLLABORATOR == 'true'
  env:
      CIRRUS_CLONE_SUBMODULES: true
  macos_instance:
    matrix:
      image: ghcr.io/cirruslabs/macos-monterey-xcode

  matrix:
    - env:
        CIBW_BUILD: cp310-* cp311-*
    - env:
        CIBW_BUILD: cp312-* cp313-*
    - env:
        CIBW_BUILD: cp313t-*
        CIBW_FREE_THREADED_SUPPORT: 1
        CIBW_BUILD_FRONTEND: "pip; args: --no-build-isolation"
  env:
    PATH: /usr/local/lib:/usr/local/include:$PATH
    CIBW_ARCHS: arm64

  build_script: |
    brew install micromamba gfortran
    micromamba shell init -s bash --root-prefix ~/micromamba
    source ~/.bash_profile
    
    micromamba create -n numpydev
    micromamba activate numpydev
    micromamba install -y -c conda-forge python=3.11 2>/dev/null
    
    # Use scipy-openblas wheels
    export INSTALL_OPENBLAS=true
    export CIBW_ENVIRONMENT_MACOS="MACOSX_DEPLOYMENT_TARGET='11.0' INSTALL_OPENBLAS=true RUNNER_OS=macOS PKG_CONFIG_PATH=$PWD/.openblas"

    # needed for submodules
    git submodule update --init
    # need to obtain all the tags so setup.py can determine FULLVERSION
    git fetch origin
    uname -m
    python -c "import platform;print(platform.python_version());print(platform.system());print(platform.machine())"
    clang --version

    python -m pip install cibuildwheel
    cibuildwheel

  wheels_artifacts:
    path: "wheelhouse/*"

######################################################################
# Upload all wheels
######################################################################

wheels_upload_task:
  use_compute_credits: $CIRRUS_USER_COLLABORATOR == 'true'
  # Artifacts don't seem to be persistent from task to task.
  # Rather than upload wheels at the end of each cibuildwheel run we do a
  # final upload here. This is because a run may be on different OS for
  # which bash, etc, may not be present.
  depends_on:
    - linux_aarch64
    - macosx_arm64
  compute_engine_instance:
    image_project: cirrus-images
    image: family/docker-builder
    platform: linux
    cpu: 1

  env:
    NUMPY_STAGING_UPLOAD_TOKEN: ENCRYPTED[!5a69522ae0c2af9edb2bc1cdfeaca6292fb3666d9ecd82dca0615921834a6ce3b702352835d8bde4ea2a9ed5ef8424ac!]
    NUMPY_NIGHTLY_UPLOAD_TOKEN: ENCRYPTED[4376691390321cd5e76613ec21de8456cc0af0164971dd9542f985a017dc30ccb4d40e60f59184618e2d55afd63e93b7]

  upload_script: |
    apt-get update
    apt-get install -y curl wget
    export IS_SCHEDULE_DISPATCH="false"
    export IS_PUSH="false"

    # cron job
    if [[ "$CIRRUS_CRON" == "nightly" ]]; then
      export IS_SCHEDULE_DISPATCH="true"
    fi

    # a manual build was started
    if [[ "$CIRRUS_BUILD_SOURCE" == "api" && "$CIRRUS_COMMIT_MESSAGE" == "API build for null" ]]; then
      export IS_SCHEDULE_DISPATCH="true"
    fi

    # only upload wheels to staging if it's a tag beginning with 'v' and you're
    # on a maintenance branch
    if [[ "$CIRRUS_TAG" == v* ]] && [[ $CIRRUS_TAG != *"dev0"* ]]; then
      export IS_PUSH="true"
    fi

    if [[ $IS_PUSH == "true" ]] || [[ $IS_SCHEDULE_DISPATCH == "true" ]]; then
        # install miniconda in the home directory. For some reason HOME isn't set by Cirrus
        export HOME=$PWD

        # install miniconda for uploading to anaconda
        wget -q https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -O miniconda.sh
        bash miniconda.sh -b -p $HOME/miniconda3
        $HOME/miniconda3/bin/conda init bash
        source $HOME/miniconda3/bin/activate
        conda install -y anaconda-client

        # The name of the zip file is derived from the `wheels_artifact` line.
        # If you change the artifact line to `myfile_artifact` then it would be
        # called myfile.zip

        curl https://api.cirrus-ci.com/v1/artifact/build/$CIRRUS_BUILD_ID/wheels.zip --output wheels.zip
        unzip wheels.zip

        source ./tools/wheels/upload_wheels.sh
        # IS_PUSH takes precedence over IS_SCHEDULE_DISPATCH
        set_upload_vars

        # Will be skipped if not a push/tag/scheduled build
        upload_wheels
    fi
