name: Tests
on:
  push:
    branches:
      - main
      - v*
  pull_request:

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

permissions:
  contents: read
  
env:
  CTEST_OUTPUT_ON_FAILURE: "1"

jobs:
  coverage:
    name: Coverage
    runs-on: ubuntu-latest
    strategy:
      matrix:
        std: ["14", "17", "20", "23"]
        precompile: ["ON", "OFF"]
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Get LCov
        run: |
          sudo apt-get install ca-certificates lcov
          #wget https://github.com/linux-test-project/lcov/releases/download/v1.16/lcov-1.16.tar.gz
          #tar -xzf lcov-1.16.tar.gz
          #cd lcov-1.16
          #sudo make install

      - name: Configure
        run: |
          cmake -S . -B build \
            -DCMAKE_CXX_STANDARD=${{matrix.std}} \
            -DCLI11_SINGLE_FILE_TESTS=OFF \
            -DCLI11_BUILD_EXAMPLES=OFF \
            -DCLI11_PRECOMPILED=${{matrix.precompile}} \
            -DCMAKE_BUILD_TYPE=Coverage \
            -DCLI11_ENABLE_EXTRA_VALIDATORS=ON

      - name: Build
        run: cmake --build build -j4

      - name: Test
        run: cmake --build build --target CLI11_coverage

      - name: Prepare coverage
        run: |
          lcov --ignore-errors gcov,mismatch --directory . --capture --output-file coverage.info
          lcov --remove coverage.info '*/tests/*' '/usr/*' --output-file coverage.info
          lcov --list coverage.info
        working-directory: build

      - uses: codecov/codecov-action@v5
        with:
          files: build/coverage.info

  preset:
    name: Preset
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Run CMake Workflow
        run: cmake --workflow default


  catch2-3:
    name: Catch 2 3.x
    runs-on: macos-latest
    steps:
      - uses: actions/checkout@v4

      - name: Get Catch 2
        run: brew install catch2

      - name: Configure
        run: |
          cmake -S . -B build \
            -DCMAKE_CXX_STANDARD=14 \
            -DCLI11_SINGLE_FILE_TESTS=OFF \
            -DCLI11_BUILD_EXAMPLES=OFF \
            -DCLI11_PRECOMPILED=ON

      - name: Build
        run: cmake --build build -j4

      - name: Test
        run: cmake --build build --target test


  clang-tidy:
    name: Clang-Tidy
    runs-on: ubuntu-latest
    container: silkeh/clang:20
    steps:
      - uses: actions/checkout@v4

      - name: Install requirements
        run: apt-get update && apt-get install -y ninja-build

      - name: Configure
        run: cmake --preset tidy

      - name: Build
        run: cmake --build --preset tidy

  cuda11-build:
    name: CUDA 11 build only
    runs-on: ubuntu-latest
    container: nvidia/cuda:11.8.0-devel-ubuntu22.04
    steps:
      - name: Add build tools
        run: apt-get update && apt-get install -y wget git cmake
      - uses: actions/checkout@v4
        with:
          submodules: true
      - name: Configure
        run: cmake -S . -B build -DCLI11_CUDA_TESTS=ON
      - name: Build
        run: cmake --build build -j2

  cuda13-build:
    name: CUDA 13 build only
    runs-on: ubuntu-latest
    container: nvidia/cuda:13.0.0-devel-ubuntu22.04
    steps:
      - name: Add build tools
        run: apt-get update && apt-get install -y wget git cmake
      - uses: actions/checkout@v4
        with:
          submodules: true
      - name: Configure
        run: cmake -S . -B build -DCLI11_CUDA_TESTS=ON
      - name: Build
        run: cmake --build build -j2

  boost-build:
    name: Boost build
    runs-on: ubuntu-24.04
    steps:
      - uses: actions/checkout@v4
        with:
          submodules: true
      - name: Add boost
        run: sudo apt-get update && sudo apt-get install -y libboost-dev
      # NOTE: If a boost version matching all requirements cannot be found,
      # this build step will fail
      - name: Configure
        run: cmake -S . -B build -DCLI11_BOOST=ON
      - name: Build
        run: cmake --build build -j2
      - name: Run tests
        run: ctest --output-on-failure
        working-directory: build

  sanitizer-build:
    name: sanitizer build
    runs-on: ubuntu-24.04
    steps:
      - uses: actions/checkout@v4
        with:
          submodules: true
      # this build step will fail
      - name: Configure
        run: cmake -S . -B build -DCLI11_SANITIZERS=ON
      - name: Build
        run: cmake --build build -j2
      - name: Run tests
        run: ctest --output-on-failure
        working-directory: build

  meson-build:
    name: Meson build
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Prepare commands
        run: |
          pipx install meson
          pipx install ninja

      - name: Configure
        run: meson setup build-meson . -Dtests=true

      - name: Build
        run: meson compile -C build-meson

      - name: Test
        run: meson test -C build-meson

  bazel-build:
    name: Bazel build
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Build
        run: bazel build //...

      - name: Test
        run: bazel test --test_output=errors //...

  install:
    name: install tests
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          submodules: true
      - name: Configure
        run: cmake -S . -B build -DCLI11_INSTALL_PACKAGE_TESTS=ON -DCMAKE_INSTALL_PREFIX=/home/runner/work/install
      - name: Build
        run: cmake --build build  -j2
      - name: install
        run: cmake --install build
      - name: Run tests
        run: ctest --output-on-failure -L Packaging
        working-directory: build

  install-precompiled:
    name: install tests precompiled
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          submodules: true
      - name: Configure
        run: cmake -S . -B build -DCLI11_INSTALL_PACKAGE_TESTS=ON -DCLI11_ENABLE_EXTRA_VALIDATORS=ON -DCMAKE_INSTALL_PREFIX=/home/runner/work/install -DCLI11_PRECOMPILED=ON
      - name: Build
        run: cmake --build build  -j2
      - name: install
        run: cmake --install build
      - name: Run tests
        run: ctest --output-on-failure -L Packaging
        working-directory: build

  install-precompiled-all:
    name: install tests precompiled full install
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          submodules: true
      - name: Configure
        run: cmake -S . -B build -DCLI11_INSTALL_PACKAGE_TESTS=ON -DCLI11_ENABLE_EXTRA_VALIDATORS=ON -DCMAKE_INSTALL_PREFIX=/home/runner/work/install -DCLI11_PRECOMPILED=ON -DCLI11_FULL_INSTALL=ON
      - name: Build
        run: cmake --build build  -j2
      - name: install
        run: cmake --install build
      - name: Configure2
        run: cmake -S . -B build -DCLI11_INSTALL_PACKAGE_TESTS=ON -DCMAKE_INSTALL_PREFIX=/home/runner/work/install
      - name: Run tests
        run: ctest --output-on-failure -L Packaging
        working-directory: build
  
  install-precompiled-macos:
    name: install tests precompiled macos
    runs-on: macos-15
    steps:
      - uses: actions/checkout@v4
        with:
          submodules: true
      - name: Configure
        run: cmake -S . -B build -DCLI11_INSTALL_PACKAGE_TESTS=ON -DCLI11_ENABLE_EXTRA_VALIDATORS=ON -DCMAKE_INSTALL_PREFIX=/Users/runner/work/install -DCLI11_PRECOMPILED=ON -DCMAKE_CXX_STANDARD=20
      - name: Build
        run: cmake --build build  -j2
      - name: install
        run: cmake --install build
      - name: Run tests
        run: ctest --output-on-failure -L Packaging
        working-directory: build
   
  install-precompiled-macos-no-validators:
    name: install tests precompiled macos no validators
    runs-on: macos-15
    steps:
      - uses: actions/checkout@v4
        with:
          submodules: true
      - name: Configure
        run: cmake -S . -B build -DCLI11_INSTALL_PACKAGE_TESTS=ON -DCLI11_ENABLE_EXTRA_VALIDATORS=OFF -DCMAKE_INSTALL_PREFIX=/Users/runner/work/install -DCLI11_PRECOMPILED=ON
      - name: Build
        run: cmake --build build  -j2
      - name: install
        run: cmake --install build
      - name: Run tests
        run: ctest --output-on-failure -L Packaging
        working-directory: build

  install-single_file:
    name: install tests single file
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          submodules: true
      - name: Configure
        run: cmake -S . -B build -DCLI11_INSTALL_PACKAGE_TESTS=ON -DCLI11_ENABLE_EXTRA_VALIDATORS=ON -DCMAKE_INSTALL_PREFIX=/home/runner/work/install -DCLI11_SINGLE_FILE=ON
      - name: Build
        run: cmake --build build  -j2
      - name: install
        run: cmake --install build
      - name: Run tests
        run: ctest --output-on-failure -L Packaging
        working-directory: build
        
  install-module:
    name: install module tests
    runs-on: ubuntu-latest
    container: gcc:15
    steps:
      - uses: actions/checkout@v4
        with:
          submodules: true
      - uses: ./.github/actions/quick_cmake
        with:
          cmake-version: "4.2"
      - uses: seanmiddleditch/gha-setup-ninja@master
      - name: Configure
        run: cmake -S . -B build -DCLI11_INSTALL_PACKAGE_TESTS=ON -DCLI11_MODULE_TESTS=ON -DCMAKE_INSTALL_PREFIX=/home/runner/work/install -DCMAKE_CXX_STANDARD=23
      - name: Build
        run: cmake --build build  -j2
      - name: install
        run: cmake --install build
      - name: Run tests
        run: ctest --output-on-failure -L Packaging
        working-directory: build


  cmake-config-ubuntu-2204:
    name: CMake config check (Ubuntu 22.04)
    runs-on: ubuntu-22.04
    steps:
      - uses: actions/checkout@v4

      - name: Check CMake 3.14
        uses: ./.github/actions/quick_cmake
        with:
          cmake-version: "3.14"
          args: -DCLI11_SANITIZERS=ON -DCLI11_BUILD_EXAMPLES_JSON=ON

      - name: Check CMake 3.15
        uses: ./.github/actions/quick_cmake
        with:
          cmake-version: "3.15.6"
        if: success() || failure()

      - name: Check CMake 3.16
        uses: ./.github/actions/quick_cmake
        with:
          cmake-version: "3.16.2"
        if: success() || failure()

  cmake-config-ubuntu-2404:
    name: CMake config check (Ubuntu 24.04)
    runs-on: ubuntu-24.04
    steps:
      - uses: actions/checkout@v4

      - name: Check CMake 3.17
        uses: ./.github/actions/quick_cmake
        with:
          cmake-version: "3.17"

      - name: Check CMake 3.18
        uses: ./.github/actions/quick_cmake
        with:
          cmake-version: "3.18"
        if: success() || failure()

      - name: Check CMake 3.19
        uses: ./.github/actions/quick_cmake
        with:
          cmake-version: "3.19"
        if: success() || failure()

      - name: Check CMake 3.20
        uses: ./.github/actions/quick_cmake
        with:
          cmake-version: "3.20"
        if: success() || failure()

      - name: Check CMake 3.21
        uses: ./.github/actions/quick_cmake
        with:
          cmake-version: "3.21"
        if: success() || failure()

      - name: Check CMake 3.22
        uses: ./.github/actions/quick_cmake
        with:
          cmake-version: "3.22"
        if: success() || failure()

      - name: Check CMake 3.23
        uses: ./.github/actions/quick_cmake
        with:
          cmake-version: "3.23"
        if: success() || failure()

      - name: Check CMake 3.24
        uses: ./.github/actions/quick_cmake
        with:
          cmake-version: "3.24"
        if: success() || failure()

      - name: Check CMake 3.25
        uses: ./.github/actions/quick_cmake
        with:
          cmake-version: "3.25"
        if: success() || failure()

      - name: Check CMake 3.26 (full)
        uses: ./.github/actions/quick_cmake
        with:
          cmake-version: "3.26"
          args: -DCLI11_SANITIZERS=ON -DCLI11_BUILD_EXAMPLES_JSON=ON
        if: success() || failure()

      - name: Check CMake 3.27
        uses: ./.github/actions/quick_cmake
        with:
          cmake-version: "3.27"
        if: success() || failure()

      - name: Check CMake 3.28 (full)
        uses: ./.github/actions/quick_cmake
        with:
          cmake-version: "3.28.X"
          args: -DCLI11_SANITIZERS=ON -DCLI11_BUILD_EXAMPLES_JSON=ON
        if: success() || failure()

      - name: Check CMake 3.29
        uses: ./.github/actions/quick_cmake
        with:
          cmake-version: "3.29"
        if: success() || failure()

      - name: Check CMake 3.30
        uses: ./.github/actions/quick_cmake
        with:
          cmake-version: "3.30"
        if: success() || failure()

      - name: Check CMake 3.31
        uses: ./.github/actions/quick_cmake
        with:
          cmake-version: "3.31"
          args: -DCLI11_SANITIZERS=ON -DCLI11_BUILD_EXAMPLES_JSON=ON
        if: success() || failure()

      - name: Check CMake 4.0
        uses: ./.github/actions/quick_cmake
        with:
          cmake-version: "4.0"
          args: -DCLI11_BUILD_EXAMPLES_JSON=ON
        if: success() || failure()

      - name: Check CMake 4.1
        uses: ./.github/actions/quick_cmake
        with:
          cmake-version: "4.1"
        if: success() || failure()
      
      - name: Check CMake 4.2
        uses: ./.github/actions/quick_cmake
        with:
          cmake-version: "4.2"
        if: success() || failure()
