---
name: Packages
on:
  - workflow_dispatch

jobs:

  build-sdist:
    name: Build sdist package
    runs-on: ubuntu-24.04
    steps:
      - name: Checkout repos
        uses: actions/checkout@v4

      - name: Build sdist
        run: python setup.py sdist

      - name: Upload artifacts
        uses: actions/upload-artifact@v4
        with:
          name: sdist
          path: ./dist/*


  build-wheel:
    runs-on: ${{matrix.os}}
    strategy:
      fail-fast: false
      matrix:
        os: [ubuntu-24.04, windows-latest, macos-latest]
        pyver: [cp38, cp39, cp310, cp311, cp312, cp313, cp313t, cp314, cp314t]

    steps:
      - name: Checkout repos
        uses: actions/checkout@v4

      - name: Build wheels
        uses: pypa/cibuildwheel@v3.1.4
        env:
          CIBW_BUILD: ${{matrix.pyver}}-*
          CIBW_ARCHS_LINUX: auto
          CIBW_ARCHS_MACOS: auto universal2
          CIBW_ARCHS_WINDOWS: auto
          CIBW_TEST_COMMAND: pytest --color=yes -m 'not embedded' {project}/tests
          # Passing a space in a param is a b*tch on windows.
          # However embedded tests are skipped anyway there.
          CIBW_TEST_COMMAND_WINDOWS: pytest --color=yes {project}/tests
          # musllinux tests fail with some pid mixup
          # cross-build macos images can't be tested on this runner.
          CIBW_TEST_SKIP: >-
            *-musllinux_*
            *-macosx_universal2:arm64

      - name: Upload artifacts
        uses: actions/upload-artifact@v4
        with:
          name: wheel-${{ matrix.pyver }}-${{ matrix.os }}
          path: ./wheelhouse/*.whl


  build-cross-wheel:
    runs-on: ubuntu-24.04
    strategy:
      fail-fast: false
      matrix:
        pyver: [cp38, cp39, cp310, cp311, cp312, cp313, cp313t, cp314, cp314t]
        arch: [aarch64, ppc64le]

    steps:
      - name: Checkout repos
        uses: actions/checkout@v4

      - name: Set up QEMU for multi-arch build
        uses: docker/setup-qemu-action@v3

      - name: Build wheels
        uses: pypa/cibuildwheel@v3.1.4
        env:
          CIBW_BUILD: ${{matrix.pyver}}-*
          CIBW_ARCHS: ${{matrix.arch}}

      # Tests mostly fail because of some confusion with the python interpreter

      - name: Upload artifacts
        uses: actions/upload-artifact@v4
        with:
          name: wheel-${{ matrix.pyver }}-${{ matrix.arch }}
          path: ./wheelhouse/*.whl


  build-wheel-pypy:
    runs-on: ${{matrix.os}}
    strategy:
      fail-fast: false
      matrix:
        os: [ubuntu-24.04, windows-latest, macos-latest]

    steps:
      - name: Checkout repos
        uses: actions/checkout@v4

      - name: Build wheels
        uses: pypa/cibuildwheel@v3.1.4
        env:
          CIBW_BUILD: pp*
          CIBW_TEST_COMMAND: pytest --color=yes -m 'not embedded' {project}/tests
          # Passing a space in a param is a b*tch on windows.
          # However embedded tests are skipped anyway there.
          CIBW_TEST_COMMAND_WINDOWS: pytest --color=yes {project}/tests
          # musllinux tests fail with some pid mixup
          # cross-build macos images can't be tested on this runner.
          CIBW_TEST_SKIP: >-
            *-musllinux_*
            pp*-macosx_*

      - name: Upload artifacts
        uses: actions/upload-artifact@v4
        with:
          name: wheels-pp-${{ matrix.os }}
          path: ./wheelhouse/*.whl


  build-cross-wheel-pypy:
    runs-on: ubuntu-24.04

    steps:
      - name: Checkout repos
        uses: actions/checkout@v4

      - name: Set up QEMU for multi-arch build
        uses: docker/setup-qemu-action@v3

      - name: Build wheels
        uses: pypa/cibuildwheel@v3.1.4
        env:
          CIBW_BUILD: pp*

      # Tests mostly fail because of some confusion with the python interpreter

      - name: Upload artifacts
        uses: actions/upload-artifact@v4
        with:
          name: wheels-pp-cross
          path: ./wheelhouse/*.whl


  merge:
    runs-on: ubuntu-latest
    needs:
      - build-sdist
      - build-wheel
      - build-cross-wheel
      - build-wheel-pypy
      - build-cross-wheel-pypy
    steps:
      - name: Merge Artifacts
        uses: actions/upload-artifact/merge@v4
        with:
          name: setproctitle-artifacts
          delete-merged: true
