name: Build wheels

#on: [push, pull_request]
on:
  workflow_dispatch:
    inputs:
      sdist:
        type: boolean
      wheels:
        type: boolean
      wheels_linux_aarch64:
        type: boolean

jobs:

  build_sdist:
    if: ${{ inputs.sdist }}
    name: Build sdist
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2
      #with:
      #  fetch-depth: 0  # Optional, use if you use setuptools_scm
      #  submodules: true  # Optional, use if you have submodules

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

    - uses: actions/upload-artifact@v2
      with:
        path: dist/*.tar.gz    


  build_wheels:
    if: ${{ inputs.wheels }}
    name: Build wheels on ${{ matrix.os }}
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-20.04, windows-2019, macos-10.15]
        #os: [ubuntu-20.04]
        #os: [windows-2019]
        #os: [macos-10.15]

    steps:

      - uses: actions/checkout@v2

      # Get Python for running cibuildwheel. This also ensures that 'python'
      # works on MacOS, where it seems only 'python3' is available by default.
      #
      - uses: actions/setup-python@v2

      # On Linux, get qemu so we can build for aarch64.
      #
      - name: Set up QEMU
        if: runner.os == 'Linux'
        uses: docker/setup-qemu-action@v1
        with:
          platforms: all

      # Get cibuildwheel.
      #
      - name: Build wheels
        uses: pypa/cibuildwheel@v2.11.2
               
        # Set extra cibuildwheel options using environmental variables.
        #
        env:
            # These exclusions are copied from PyMuPDF-1.19.
            #
            CIBW_SKIP: "pp* *i686 *-musllinux_* cp36*"
            
            # On Linux, tell cibuildwheel to build native archs ('auto'), and
            # aarch64.
            #
            #CIBW_ARCHS_LINUX: auto aarch64
            
            # https://github.community/t/possible-to-use-conditional-in-the-env-section-of-a-job/135170
            # Note that it seems that there must not be a space after the ':' in the following, i.e.
            # ok:   {"false":"auto", "true":"auto aarch64"}
            # bad:  {"false": "auto", "true": "auto aarch64"}
            #
            # This is useful: https://yamlchecker.com/
            #
            CIBW_ARCHS_LINUX: ${{ fromJSON('{"false":"auto", "true":"auto aarch64"}')[inputs.wheels_linux_aarch64] }}

            # For testing, build for single python version.
            #
            #CIBW_BUILD: "cp39*"

            # Get cibuildwheel to run pytest with each wheel.
            #
            # Setting verbosity here sometimes seems to result in SEGV's when
            # running pytest.
            #
            CIBW_TEST_REQUIRES: "fontTools pytest"
            CIBW_TEST_COMMAND: "pytest -s {project}/tests"
            CIBW_BUILD_VERBOSITY: 3

      # Upload generated wheels, to be accessible from github Actions page.
      #
      - uses: actions/upload-artifact@v2
        with:
          path: ./wheelhouse/*.whl
