name: Build

on: [push, pull_request]

permissions: {}

jobs:

  build_sdist:
    runs-on: ubuntu-latest

    permissions:
      contents: read

    steps:
      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
        with:
          persist-credentials: false

      - name: Set up Python
        uses: actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97 # v7.0.0
        with:
          python-version: '3.11'

      - name: Install dependencies
        run: |
          sudo apt-get update
          sudo apt-get install -y libcairo2-dev pkg-config
          python -m pip install --upgrade build

      - name: Build sdist
        run: python -m build --sdist

      - uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
        with:
          name: sdist
          path: ./dist/*.tar.gz

  build_wheels:
    name: Build wheels on ${{ matrix.os }} (${{ matrix.platform_id }})
    runs-on: ${{ matrix.os }}
    permissions:
      contents: read
    strategy:
      fail-fast: false
      matrix:
        include:
          - os: windows-2022
            platform_id: win_amd64
            arch: x64
          - os: windows-2022
            platform_id: win32
            arch: x86
          - os: windows-11-arm
            platform_id: win_arm64
            arch: arm64

    steps:
      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
        with:
          persist-credentials: false

      - name: Setup MSVC
        uses: ilammy/msvc-dev-cmd@0b201ec74fa43914dc39ae48a89fd1d8cb592756 # v1.13.0
        with:
          arch : ${{ matrix.arch }}

      - name: Build wheels
        uses: pypa/cibuildwheel@1828c10ab37f080699c7b81cea34097c684a7074 # v4.2.0
        env:
          CFLAGS: "-DCAIRO_WIN32_STATIC_BUILD=1"
          CIBW_BEFORE_BUILD: "python {package}/.ci/download-cairo-win32.py ${{ matrix.arch }}"
          CIBW_BUILD: cp310-${{ matrix.platform_id }} cp311-${{ matrix.platform_id }} cp312-${{ matrix.platform_id }} cp313-${{ matrix.platform_id }} cp314-${{ matrix.platform_id }} cp314t-${{ matrix.platform_id }} cp315-${{ matrix.platform_id }} cp315t-${{ matrix.platform_id }}
          CIBW_SKIP: cp314t-win32 cp315-win32 cp315t-win32
          CIBW_TEST_REQUIRES: pytest==8.3.5
          CIBW_TEST_COMMAND: bash {package}/.ci/test-wheels.sh {package}
          CIBW_ENVIRONMENT_WINDOWS: PKG_CONFIG_PATH='${{ github.workspace }}/cairo-prebuild/lib/pkgconfig' PKG_CONFIG='${{ github.workspace }}/cairo-prebuild/bin/pkgconf.exe'

      - uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
        with:
          name: "wheel-${{ matrix.arch }}"
          path: ./wheelhouse/*.whl
