name: Build+Upload macOS Python Wheels
on:
  push:
    branches:
      - master
    tags:
      - '*'
  pull_request:
    branches:
      - master

jobs:
  build_macos_wheels:
    name: Build macOS wheels
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [macos-15-intel, macos-14]
        python_version: ['3.7', '3.8', '3.9', '3.10', '3.11', '3.12', '3.13', '3.14']
        exclude:
          - os: macos-14
            python_version: '3.7'
          - os: macos-14
            python_version: '3.8'
          - os: macos-14
            python_version: '3.9'
    outputs:
      perform_pypi_upload: ${{ steps.perform_pypi_upload_check.outputs.perform_pypi_upload }}
    steps:
      - name: Checkout repo
        uses: actions/checkout@v3
        with:
          submodules: 'recursive'
      - name: Set up Python
        uses: actions/setup-python@v4
        with:
          python-version: ${{ matrix.python_version }}
      - name: Install dependencies
        run: |
          brew install gcc
      - name: Setup compiler
        run: |
          if [[ "$(uname -m)" == "arm64" ]]; then
            echo "Building for Apple Silicon (arm64)"
          else
            echo "Building for Intel (x86_64)"
          fi
      - name: Build wheel
        run: |
          cd python/
          pip install setuptools wheel
          pip wheel . --no-deps -w dist/
          ls dist/
      - name: Run unit tests
        run: |
          cd python/
          pip install dist/*.whl
          python -m unittest discover -s tests -v
      - name: Show dynamic library dependencies
        run: |
          find $(python -c "import zmat, os; print(os.path.dirname(zmat.__file__))") -name "*.so" -exec otool -L {} \;
      - name: Check If the Build Version Exists on PyPI
        id: perform_pypi_upload_check
        run: |
          bash $GITHUB_WORKSPACE/.github/check-pypi-upload.sh
      - name: Prepare Wheels for Upload
        uses: actions/upload-artifact@v4
        with:
          name: macos-wheels-${{ matrix.os }}-${{ matrix.python_version }}
          path: python/dist/

  upload_macos_intel_wheels:
    needs: build_macos_wheels
    runs-on: ubuntu-latest
    if: ${{ github.repository_owner == 'NeuroJSON' && needs.build_macos_wheels.outputs.perform_pypi_upload == 1 && github.event_name != 'pull_request'}}
    permissions:
      id-token: write
    steps:
      - name: Download Wheels from Build Job
        uses: actions/download-artifact@v4
        with:
          pattern: macos-wheels-macos-15-intel-*
          merge-multiple: true
          path: dist/
      - name: Upload packages to PyPI
        uses: pypa/gh-action-pypi-publish@release/v1
        with:
          password: ${{ secrets.PYPI_API_TOKEN }}
          verify_metadata: false
          verbose: true
          skip_existing: true

  upload_macos_arm64_wheels:
    needs: build_macos_wheels
    runs-on: ubuntu-latest
    if: ${{ github.repository_owner == 'NeuroJSON' && needs.build_macos_wheels.outputs.perform_pypi_upload == 1 && github.event_name != 'pull_request'}}
    permissions:
      id-token: write
    steps:
      - name: Download Wheels from Build Job
        uses: actions/download-artifact@v4
        with:
          pattern: macos-wheels-macos-14-*
          merge-multiple: true
          path: dist/
      - name: Upload packages to PyPI
        uses: pypa/gh-action-pypi-publish@release/v1
        with:
          password: ${{ secrets.PYPI_API_TOKEN }}
          verify_metadata: false
          verbose: true
          skip_existing: true