name: Build wheel and deploy on PyPI

on:
  workflow_dispatch:
  release:
    types:
      - published

jobs:
  build_sdist:
    name: Build source distribution
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-python@v5
        with:
          python-version: "3.12"
          cache: "pip"
      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install build twine pillow
      - name: Build sdist
        run: python -m build --sdist
      - name: Check the package
        run: python -m twine check dist/*
      - uses: actions/upload-artifact@v4
        with:
          name: cibw-sdist
          path: dist/*.tar.gz

  test_sdist:
    needs: [build_sdist]
    name: Test source distribution
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-python@v5
        with:
          python-version: "3.12"
          cache: "pip"
      - uses: actions/download-artifact@v4
        with:
          name: cibw-sdist
          path: dist
      - name: Install from sdist
        run: pip install "$(ls dist/freesas-*.tar.gz)"
      - name: Run tests
        env:
          WITH_QT_TEST: "False"  # skip GUI tests
          PYFAI_OPENCL: "False"  # skip GPU tests
          PYFAI_LOW_MEM: "True"  # skip all tests >100Mb
          WITH_GL_TEST: "False"  # disable tests using OpenGL
        run: python -c "import freesas.test, sys; sys.exit(freesas.test.run_tests())"

  build_doc:
    name: Build documentation
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-python@v5
        with:
          python-version: "3.12"
          cache: "pip"
      - name: Install pandoc&graphviz
        run: sudo apt-get install pandoc graphviz
      - name: Install freesas
        run: pip install .
      - name: Install documentation dependencies
        run: pip install -r ci/requirements_rtd.txt
      - name: Build doc
        env:
          READTHEDOCS: "True"  # To skip checking that freesas is installed locally
        run: |
          export FREESAS_VERSION="$(python -c 'import freesas; print(freesas.strictversion)')"
          sphinx-build doc/source/ "freesas-${FREESAS_VERSION}_documentation/"
          zip -r "freesas-${FREESAS_VERSION}_documentation.zip" "freesas-${FREESAS_VERSION}_documentation/"
      - uses: actions/upload-artifact@v4
        with:
          name: documentation
          path: freesas-*_documentation.zip

  build_wheels:
    name: Build wheels on ${{ matrix.os }}-${{ matrix.cibw_archs }}
    runs-on: ${{ matrix.os }}
    strategy:
      # Ensure that a wheel builder finishes even if another fails
      fail-fast: false
      matrix:
        include:
          - os: ubuntu-20.04
            cibw_archs: "auto64"
          # - os: ubuntu-20.04
          #   cibw_archs: "aarch64"
          # - os: ubuntu-20.04
          #   cibw_archs: "ppc64le"
          - os: windows-2022
            cibw_archs: "auto64"
          - os: macos-12
            cibw_archs: "x86_64"
            macos_target: "10.9"
          - os: macos-14
            cibw_archs: "arm64"
            macos_target: "11.0"

    steps:
      - uses: actions/checkout@v4
      - uses: docker/setup-qemu-action@v3
        if: runner.os == 'Linux'
        with:
          platforms: all
      - uses: ilammy/msvc-dev-cmd@v1
      # - name: Install Ubuntu dependencies
      #   run: |
      #     sudo apt-get update
      #     sudo apt-get install -y libopenblas-dev libatlas-base-dev liblapack-dev gfortran libgmp-dev libmpfr-dev libhdf5-dev ccache
      - uses: pypa/cibuildwheel@v2.16.5
        env:
          # Use silx wheelhouse: needed for ppc64le
          CIBW_ENVIRONMENT_LINUX: "PIP_FIND_LINKS=https://www.silx.org/pub/wheelhouse/ PIP_TRUSTED_HOST=www.silx.org"
          #CIBW_ENVIRONMENT_WINDOWS: "PYFAI_WITH_OPENMP=False"
          CIBW_BUILD_FRONTEND_WINDOWS: "build; args: -C setup-args=-Duse_openmp=disabled"
          #CIBW_BEFORE_BUILD_WINDOWS: '"C:\Program Files\Microsoft Visual Studio\2022\Enterprise\Common7\Tools\VsDevCmd.bat"'
          CIBW_BUILD_VERBOSITY: 1
          CIBW_BUILD: cp39-* cp310-* cp311-* cp312-*
          # Do not build for pypy and muslinux
          CIBW_SKIP: pp* *-musllinux_*
          CIBW_ARCHS: ${{ matrix.cibw_archs }}

          MACOSX_DEPLOYMENT_TARGET: "${{ matrix.macos_target }}"

          # Install test dependencies
          CIBW_TEST_COMMAND: python -c "import freesas.test, sys; sys.exit(freesas.test.run_tests())"
          # Skip tests for emulated architectures
          CIBW_TEST_SKIP: "*-*linux_{aarch64,ppc64le,s390x} cp312-win_amd64"
          WITH_QT_TEST: "False"  # skip GUI tests
          PYFAI_OPENCL: "False"  # skip GPU tests
          PYFAI_LOW_MEM: "True"  # skip all tests >100Mb
          WITH_GL_TEST: "False"  # disable tests using OpenGL
      - uses: actions/upload-artifact@v4
        with:
          name: cibw-wheels-${{ matrix.os }}-${{ strategy.job-index }}
          path: ./wheelhouse/*.whl

  pypi-publish:
    needs: [build_doc, build_sdist, build_wheels, test_sdist]
    name: Upload release to PyPI
    runs-on: ubuntu-latest
    environment:
      name: pypi
    permissions:
      id-token: write
    if: github.event_name == 'release' && github.event.action == 'published'
    # or, alternatively, upload to PyPI on every tag starting with 'v' (remove on: release above to use this)
    # if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/v')
    steps:
      - uses: actions/download-artifact@v4
        with:
          pattern: cibw-*
          path: dist
          merge-multiple: true
      - uses: pypa/gh-action-pypi-publish@release/v1
