name: CMake

on:
  push:
    branches: [ "master" ]
  pull_request:
    branches: [ "master" ]

jobs:
  build:
    runs-on: ${{ matrix.os }}

    strategy:
      matrix:
        os: [ubuntu-latest, windows-latest, macos-latest]
        build_type: [Release]
        c_compiler: [gcc, clang, cl]
        shared_libs: ['ON', 'OFF']
        include:
          - os: windows-latest
            c_compiler: cl
            cpp_compiler: cl
          - os: windows-latest
            c_compiler: gcc
            cpp_compiler: g++
          - os: windows-latest
            c_compiler: clang
            cpp_compiler: clang++
          - os: ubuntu-latest
            c_compiler: gcc
            cpp_compiler: g++
          - os: ubuntu-latest
            c_compiler: clang
            cpp_compiler: clang++
          - os: macos-latest
            c_compiler: clang
            cpp_compiler: clang++
        exclude:
          - os: ubuntu-latest
            c_compiler: cl
          - os: macos-latest
            c_compiler: cl
          - os: macos-latest
            c_compiler: gcc

    steps:
    - uses: actions/checkout@v4

    - name: Set reusable strings
      id: strings
      shell: bash
      run: |
        echo "build-output-dir=${{ github.workspace }}/build" >> "$GITHUB_OUTPUT"

    - uses: seanmiddleditch/gha-setup-ninja@master

    - uses: ilammy/msvc-dev-cmd@v1

    - uses: actions/setup-python@v5
      with:
        python-version: '3.10'

    - run: pip install numpy

    - name: Configure CMake
      run: >
        cmake -B ${{ steps.strings.outputs.build-output-dir }}
        -G Ninja
        -D CMAKE_CXX_COMPILER=${{ matrix.cpp_compiler }}
        -D CMAKE_C_COMPILER=${{ matrix.c_compiler }}
        -D CMAKE_BUILD_TYPE=${{ matrix.build_type }}
        -D BUILD_SHARED_LIBS=${{ matrix.shared_libs }}
        -D BUILD_TESTING=ON
        -S ${{ github.workspace }}

    - name: Build
      run: cmake --build ${{ steps.strings.outputs.build-output-dir }} --config ${{ matrix.build_type }}

    - name: Install (Windows)
      if: matrix.os == 'windows-latest'
      run: Start-Process -Verb RunAs -FilePath cmake "--build ${{ steps.strings.outputs.build-output-dir }} --config ${{ matrix.build_type }} --target install"

    - name: Install (sudo)
      if: matrix.os != 'windows-latest'
      run: sudo cmake --build ${{ steps.strings.outputs.build-output-dir }} --config ${{ matrix.build_type }} --target install

    - name: add DLL to test folder
      if: matrix.os == 'windows-latest'
      working-directory: ${{ steps.strings.outputs.build-output-dir }}
      run: |
        cp *apriltag.dll test/

    - name: Test
      working-directory: ${{ steps.strings.outputs.build-output-dir }}
      run: |
        ctest --build-config ${{ matrix.build_type }} --no-tests=error --output-on-failure --verbose

    - name: Test Python Module Import
      working-directory: ${{ steps.strings.outputs.build-output-dir }}
      run: |
        python3 -c "import apriltag; apriltag.apriltag(family='tag36h11')"
