name: Wheels

on:
  pull_request:
  push:
    tags:
      - "v*"
  release:
    types:
      - published

permissions:
  contents: read

jobs:

  build_sdist:
    name: Build source distribution
    runs-on: ubuntu-22.04
    steps:
      - uses: actions/checkout@v6
        with:
          fetch-depth: 0

      - uses: actions/setup-python@v6
        name: Install Python
        with:
          python-version: 3.x

      - name: Install APT packages
        if: contains(${{ matrix.os }}, 'ubuntu')
        run: |
          sudo apt update
          sudo apt install libhdf5-dev libnetcdf-dev

      - name: Build sdist
        run: >
          pip install build
          &&  python -m build --sdist . --outdir dist

      - uses: actions/upload-artifact@v6
        with:
          name: pypi-artifacts
          path: ${{ github.workspace }}/dist/*.tar.gz


  build_bdist:
    name: "Build ${{ matrix.os }} (${{ matrix.arch }}) wheels"
    runs-on: ${{ matrix.os }}
    # Prevent hanging when building from emulation like aarch64.
    timeout-minutes: 300
    strategy:
      fail-fast: false
      matrix:
        include:
          - os: ubuntu-22.04
            arch: x86_64
          - os: ubuntu-24.04-arm
            arch: aarch64

    steps:
    - uses: actions/checkout@v6
      with:
        fetch-depth: 0

    # For aarch64 support
    # https://cibuildwheel.pypa.io/en/stable/faq/#emulation
    - uses: docker/setup-qemu-action@v3
      with:
        platforms: all
      if: runner.os == 'Linux' && matrix.arch == 'aarch64'

    - name: Build oldest and newest Python
      shell: bash
      # On PRs we run only oldest and newest Python versions to reduce CI load.
      # Skips pypy and musllinux everywhere.
      # We are building 310, 311 and 314 for now.
      # (3.11 is the oldest version for which we support abi3 wheels)
      # These needs to rotate every new Python release.
      run: |
        set -x
        echo "CIBW_BUILD=cp310-* cp311-* cp314-* cp314t-*" >> $GITHUB_ENV
        set +x

      if: ${{ github.event_name }} == "pull_request"

    - name: "Building ${{ matrix.os }} (${{ matrix.arch }}) wheels"
      uses: pypa/cibuildwheel@v3.3.0
      env:
        CIBW_ARCHS: ${{ matrix.arch }}

    - uses: actions/upload-artifact@v6
      with:
        name: pypi-artifacts-${{ matrix.os }}-${{ matrix.arch }}
        path: ${{ github.workspace }}/wheelhouse/*.whl


  build_wheels_winmac:
    name: Build wheels for ${{matrix.arch}} on ${{ matrix.os }}
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        include:
          - os: windows-latest
            arch: AMD64
          - os: macos-14
            arch: arm64
          - os: macos-15-intel
            arch: x86_64

    steps:
      - uses: actions/checkout@v6
        with:
          fetch-depth: 0

      - uses: actions/setup-python@v6
        name: Install Python
        with:
          python-version: 3.x

      - name: Setup Micromamba Python ${{ matrix.python-version }}
        uses: mamba-org/setup-micromamba@v2
        with:
          environment-name: build
          init-shell: bash
          create-args: >-
            python=${{ matrix.python-version }} libnetcdf=4.9.3 --channel conda-forge

      - name: Build wheels for Windows/Mac 
        uses: pypa/cibuildwheel@v3.3.0
        env:
          CIBW_ARCHS: ${{ matrix.arch }}

      - uses: actions/upload-artifact@v6
        with:
          name: pypi-artifacts-${{ matrix.os }}-${{ matrix.arch }}
          path: ${{ github.workspace }}/wheelhouse/*.whl


  build_wheels_windows_arm:
    name: Build wheels for ARM64 on Windows
    runs-on: windows-11-arm
    steps:
      - uses: actions/checkout@v6
        with:
          fetch-depth: 0

      - uses: actions/setup-python@v6
        name: Install Python
        with:
          python-version: 3.x

      - name: Install vcpkg dependencies
        shell: pwsh
        run: |
          # Install vcpkg
          git clone https://github.com/Microsoft/vcpkg.git C:\vcpkg
          cd C:\vcpkg
          .\bootstrap-vcpkg.bat
          
          # Install netcdf and dependencies
          .\vcpkg.exe install hdf5:arm64-windows netcdf-c:arm64-windows zlib:arm64-windows
          
          # Set environment variables for build
          echo "HDF5_DIR=C:\vcpkg\installed\arm64-windows" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append
          echo "NETCDF4_DIR=C:\vcpkg\installed\arm64-windows" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append

      - name: Build wheels for Windows ARM64
        uses: pypa/cibuildwheel@v3.3.0
        env:
          CIBW_ARCHS: ARM64
          CIBW_SKIP: "cp310-*"

      - uses: actions/upload-artifact@v6
        with:
          name: pypi-artifacts-windows-11-arm-ARM64
          path: ${{ github.workspace }}/wheelhouse/*.whl


  show-artifacts:
    needs: [build_bdist, build_sdist, build_wheels_winmac, build_wheels_windows_arm]
    name: "Show artifacts"
    runs-on: ubuntu-22.04
    steps:
    - uses: actions/download-artifact@v7
      with:
        pattern: pypi-artifacts*
        path: ${{ github.workspace }}/dist
        merge-multiple: true

    - shell: bash
      run: |
        ls -lh ${{ github.workspace }}/dist


  publish-artifacts-pypi:
    needs: [build_bdist, build_sdist, build_wheels_winmac, build_wheels_windows_arm]
    name: "Publish to PyPI"
    runs-on: ubuntu-22.04
    # upload to PyPI for every tag starting with 'v'
    if: github.event_name == 'push' && startsWith(github.event.ref, 'refs/tags/v')
    steps:
    - uses: actions/download-artifact@v7
      with:
        pattern: pypi-artifacts*
        path: ${{ github.workspace }}/dist
        merge-multiple: true

    - uses: pypa/gh-action-pypi-publish@release/v1
      with:
        user: __token__
        password: ${{ secrets.PYPI_PASSWORD }}
        print_hash: true
