name: Build wheels for NVTX

on:
  workflow_dispatch:
    inputs:
      branchOrTag:
        description: 'Branch or tag to checkout'
        required: true
        default: 'release-v3'
        type: string
      prNumber:
        description: 'PR number (overrides branch/tag if > 0)'
        required: false
        type: integer
        default: 0

jobs:
  build:
    name: Build wheels on ${{ matrix.os }} ${{ matrix.name }}
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        include:
        - os: 'ubuntu-latest'
          cibw_archs_linux: "x86_64"
          docker_py_container: "python"
          name: 'ubuntu-x86_64'
        - os: 'ubuntu-latest'
          cibw_archs_linux: "aarch64"
          docker_py_container: "arm64v8/python"
          name: 'ubuntu-qemu-aarch64'
          qemu: true
    steps:
    - uses: actions/checkout@v2
      with:
        fetch-depth: 0  # unshallow fetch for setuptools-scm
        ref: ${{ inputs.branchOrTag }}

    - name: Check out PR code if prNumber is specified
      if: inputs.prNumber > 0
      uses: dawidd6/action-checkout-pr@v1
      with:
        pr: ${{ inputs.prNumber }}

    - name: Set up QEMU
      if: matrix.qemu
      uses: docker/setup-qemu-action@v2
      with:
        platforms: arm64

    - name: Build wheels
      uses: pypa/cibuildwheel@v2.4.0
      env:
        CIBW_SKIP: "*musllinux*"
        CIBW_BUILD: "cp37-* cp38-* cp39-* cp310-*"
        CIBW_ARCHS_LINUX: ${{ matrix.cibw_archs_linux }}
        CIBW_MANYLINUX_X86_64_IMAGE: manylinux2014
        CIBW_MANYLINUX_AARCH64_IMAGE: manylinux2014
        CIBW_TEST_COMMAND: "pytest {package}/nvtx/tests"
        CIBW_TEST_REQUIRES: "pytest"
        CIBW_REPAIR_WHEEL_COMMAND_LINUX: 'auditwheel repair -w {dest_dir} {wheel}'
        CIBW_ENVIRONMENT: "C_INCLUDE_PATH=$(pwd)/c/include"
      with:
        output-dir: dist
        package-dir: python

    - name: Run docker smoke tests for all python versions
      run: |
        for pyver in 3.7 3.8 3.9 3.10; do
          docker run --rm \
            -v $(pwd)/dist:/wheels \
            ${{ matrix.docker_py_container}}:$pyver-bullseye \
            sh -c \
            "python3 -m pip install --verbose --find-links=file:///wheels --no-index nvtx && python3 -m pip check && python3 -c 'import nvtx; print(nvtx.annotate())';" ;\
        done

    - name: Upload distributions
      uses: actions/upload-artifact@v2
      with:
        path: dist
        name: nvtx-wheels
