name: Python package
on:
  push:
    tags:
        - 'v*'

jobs:
  prepare-sqlite:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v4
      with:
        submodules: true

    - if: steps.sqlite-amalgamation.outputs.cache-hit != 'true'
      run: |
        cd sqlcipher/
        LIBS="-lm" ./configure --disable-tcl --with-tempstore=yes
        make sqlite3.c

    - uses: actions/upload-artifact@v4
      with:
        name: sqlite-amalgamation
        path: |
          sqlcipher/sqlite3.c
          sqlcipher/sqlite3.h

  build-sdist:
    needs: prepare-sqlite
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v4
      with:
        submodules: true

    - uses: actions/setup-python@v5
      with:
        python-version: 3.8

    - uses: actions/download-artifact@v4
      with:
        name: sqlite-amalgamation
        path: ./src/sqlcipher

    - name: Build sdist
      run: pipx run build --sdist

    - name: Test sdist
      run: |
        sudo apt install -y perl
        python -m pip install dist/sqlcipher3_wheels-*.tar.gz
        mv sqlcipher3 sqlcipher3_ &&
        mv sqlcipher3_ sqlcipher3

    - uses: actions/upload-artifact@v4
      with:
        name: sdist
        path: dist/sqlcipher3_wheels-*.tar.gz

  build-wheels:
    needs: prepare-sqlite
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        include:
          - os: ubuntu-latest
            cibw_archs: x86_64
            cibw_build: "cp*-manylinux_*"
            cibw_skip: ""
            compile_target: x86_64
            out: x86_64-manylinux
          - os: ubuntu-latest
            cibw_archs: x86_64
            cibw_build: "cp*-musllinux_*"
            cibw_skip: ""
            compile_target: x86_64
            out: x86_64-musllinux
          - os: ubuntu-24.04-arm
            cibw_archs: aarch64
            cibw_build: "cp*-manylinux_*"
            cibw_skip: ""
            compile_target: armv8
            out: armv8-manylinux
          - os: ubuntu-24.04-arm
            cibw_archs: aarch64
            cibw_build: "cp*-musllinux_*"
            cibw_skip: ""
            compile_target: armv8
            out: armv8-musllinux
          - os: ubuntu-latest
            cibw_archs: i686
            cibw_build: "cp*-manylinux_*"
            cibw_skip: ""
            compile_target: x86
            out: x86-manylinux
          - os: ubuntu-latest
            cibw_archs: i686
            cibw_build: "cp*-musllinux_*"
            cibw_skip: ""
            compile_target: x86
            out: x86-musllinux
          - os: ubuntu-latest
            cibw_archs: ppc64le
            cibw_build: "cp38-manylinux_* cp39-manylinux_* cp310-manylinux_* cp311-manylinux_*"
            cibw_skip: ""
            compile_target: ppc64le
            out: pp64le-manylinux-1
          - os: ubuntu-latest
            cibw_archs: ppc64le
            cibw_build: "cp*-manylinux_*"
            cibw_skip: "cp38-manylinux_* cp39-manylinux_* cp310-manylinux_* cp311-manylinux_*"
            compile_target: ppc64le
            out: pp64le-manylinux-2
          - os: ubuntu-latest
            cibw_archs: ppc64le
            cibw_build: "cp38-musllinux_* cp39-musllinux_* cp310-musllinux_* cp311-musllinux_*"
            cibw_skip: ""
            compile_target: ppc64le
            out: ppc64le-musllinux-1
          - os: ubuntu-latest
            cibw_archs: ppc64le
            cibw_build: "cp*-musllinux_*"
            cibw_skip: "cp38-musllinux_* cp39-musllinux_* cp310-musllinux_* cp311-musllinux_*"
            compile_target: ppc64le
            out: ppc64le-musllinux-2
          - os: ubuntu-latest
            cibw_archs: s390x
            cibw_build: "cp38-manylinux_* cp39-manylinux_* cp310-manylinux_* cp311-manylinux_*"
            cibw_skip: ""
            compile_target: s390x
            out: s390x-manylinux-1
          - os: ubuntu-latest
            cibw_archs: s390x
            cibw_build: "cp*-manylinux_*"
            cibw_skip: "cp38-manylinux_* cp39-manylinux_* cp310-manylinux_* cp311-manylinux_*"
            compile_target: s390x
            out: s390x-manylinux-2
          - os: ubuntu-latest
            cibw_archs: s390x
            cibw_build: "cp38-musllinux_* cp39-musllinux_* cp310-musllinux_* cp311-musllinux_*"
            cibw_skip: ""
            compile_target: s390x
            out: s390x-musllinux-1
          - os: ubuntu-latest
            cibw_archs: s390x
            cibw_build: "cp*-musllinux_*"
            cibw_skip: "cp38-musllinux_* cp39-musllinux_* cp310-musllinux_* cp311-musllinux_*"
            compile_target: s390x
            out: s390x-musllinux-2
          - os: windows-latest
            cibw_archs: AMD64
            cibw_build: "cp*"
            cibw_skip: ""
            compile_target: x86_64
            out: x86_64-windows
          - os: windows-latest
            cibw_archs: x86
            cibw_build: "cp*"
            cibw_skip: ""
            compile_target: x86
            out: x86-windows
          - os: windows-latest
            cibw_archs: ARM64
            cibw_build: "cp*"
            cibw_skip: ""
            compile_target: armv8
            out: armv8-windows
          - os: macos-13
            cibw_archs: x86_64
            cibw_build: "cp*"
            cibw_skip: ""
            compile_target: x86_64
            out: x86_64-macos
          - os: macos-latest
            cibw_archs: arm64
            cibw_build: "cp*"
            cibw_skip: ""
            compile_target: armv8
            out: armv8-macos
          - os: macos-latest
            cibw_archs: universal2
            cibw_build: "cp*"
            cibw_skip: ""
            compile_target: universal2
            out: universal2-macos

    steps:
    - uses: actions/checkout@v4
      with:
        submodules: true

    - uses: actions/setup-python@v5
      with:
        python-version: 3.11

    - name: Set up QEMU
      if: runner.os == 'Linux'
      uses: docker/setup-qemu-action@v3
      with:
        platforms: all
        # This should be temporary
        # xref https://github.com/docker/setup-qemu-action/issues/188
        # xref https://github.com/tonistiigi/binfmt/issues/215
        image: tonistiigi/binfmt:qemu-v8.1.5

    - uses: actions/download-artifact@v4
      with:
        name: sqlite-amalgamation
        path: ./src/sqlcipher

    - name: Build wheels for ${{ matrix.os }} ${{ matrix.cibw_archs }} ${{ matrix.cibw_build }}
      uses: pypa/cibuildwheel@v3.3.0
      env:
        CIBW_BUILD_FRONTEND: build
        CIBW_BUILD: ${{ matrix.cibw_build }}
        CIBW_SKIP: ${{ matrix.cibw_skip }}
        CIBW_ARCHS: ${{ matrix.cibw_archs }}
        CIBW_ENVIRONMENT: SQLCIPHER3_COMPILE_TARGET=${{ matrix.compile_target }}
        CIBW_BEFORE_ALL_LINUX: command -v yum >/dev/null && yum -y install perl-core || command -v apk >/dev/null && apk add perl || true
        CIBW_TEST_COMMAND: >
          mv {project}/sqlcipher3 {project}/sqlcipher3_ &&
          python {project}/tests/ &&
          mv {project}/sqlcipher3_ {project}/sqlcipher3

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

  upload-pypi:
    needs: [build-wheels, build-sdist]
    runs-on: ubuntu-latest

    steps:
      - uses: actions/download-artifact@v4
        with:
          pattern: wheels-*
          merge-multiple: true
          path: dist
      - uses: actions/download-artifact@v4
        with:
          name: sdist
          merge-multiple: true
          path: dist

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