name: cmake

on:
  push:
    paths:
      - src/**
      - CMakeLists.txt
  pull_request:
    paths:
      - src/**
      - CMakeLists.txt
  workflow_dispatch:

concurrency:
  group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
  cancel-in-progress: true

jobs:
  linux:
    runs-on: ubuntu-latest
    steps:
      - name: 'Checkout'
        uses: actions/checkout@v6
        with:
          fetch-depth: 0
          fetch-tags: true

      - name: 'Install dependencies'
        run: |
          sudo apt-get update
          sudo apt-get install libleptonica-dev -y
          sudo apt-get install ninja-build -y
          cmake --version

      - name: 'Configure build'
        run: |
          cmake -S . -B build -DCMAKE_BUILD_TYPE=Debug

      - name: 'Build'
        run: cmake --build build

      - name: 'Run tests'
        run: |
          cd build
          ctest --output-on-failure

  linux-memcheck:
    runs-on: ubuntu-latest
    needs: linux
    steps:
      - name: 'Checkout'
        uses: actions/checkout@v6
        with:
          fetch-depth: 0
          fetch-tags: true

      - name: 'Install dependencies'
        run: |
          sudo apt-get update
          sudo apt-get install -y libleptonica-dev

      - name: 'Configure build'
        run: |
          cmake -S . -B build -DCMAKE_BUILD_TYPE=Debug -DCMAKE_CXX_FLAGS="-g -fsanitize=address -fno-omit-frame-pointer"

      - name: 'Build'
        run: cmake --build build

      - name: 'Run tests with AddressSanitizer'
        run: |
          cd build
          export ASAN_OPTIONS=detect_leaks=1:abort_on_error=1:print_stats=1
          ctest --output-on-failure

  windows:
    runs-on: windows-latest
    steps:
      - name: 'Checkout'
        uses: actions/checkout@v6
        with:
          fetch-depth: 0
          fetch-tags: true

      - name: 'Cache vcpkg'
        uses: actions/cache@v5
        with:
          path: C:\vcpkg\installed
          key: vcpkg-leptonica-${{ runner.os }}-${{ hashFiles('**/CMakeLists.txt') }}
          restore-keys: vcpkg-leptonica-${{ runner.os }}-

      - name: 'Install leptonica via vcpkg'
        run: |
          vcpkg install leptonica:x64-windows
          vcpkg integrate install

      - name: 'Configure build'
        run: |
          cmake -S . -B build -DCMAKE_BUILD_TYPE=Debug -DCMAKE_TOOLCHAIN_FILE="C:\vcpkg\scripts\buildsystems\vcpkg.cmake" -DVCPKG_TARGET_TRIPLET=x64-windows

      - name: 'Build'
        run: cmake --build build --config Debug

      - name: 'Run tests'
        run: |
          cd build
          ctest -C Debug --output-on-failure

  windows-memcheck:
    runs-on: windows-latest
    needs: windows
    steps:
      - name: 'Checkout'
        uses: actions/checkout@v6
        with:
          fetch-depth: 0
          fetch-tags: true

      - name: 'Cache vcpkg'
        uses: actions/cache@v5
        with:
          path: C:\vcpkg\installed
          key: vcpkg-leptonica-${{ runner.os }}-${{ hashFiles('**/CMakeLists.txt') }}
          restore-keys: vcpkg-leptonica-${{ runner.os }}-

      - name: 'Install leptonica via vcpkg'
        run: |
          vcpkg install leptonica:x64-windows
          vcpkg integrate install

      - name: 'Configure build'
        run: |
          cmake -S . -B build -DCMAKE_BUILD_TYPE=Debug -DCMAKE_TOOLCHAIN_FILE="C:\vcpkg\scripts\buildsystems\vcpkg.cmake" -DVCPKG_TARGET_TRIPLET=x64-windows -DCMAKE_CXX_FLAGS="-fsanitize=address -fno-omit-frame-pointer"

      - name: 'Build'
        run: cmake --build build --config Debug

      - name: 'Run tests with AddressSanitizer'
        run: |
          cd build
          $env:ASAN_OPTIONS="detect_leaks=1:abort_on_error=1:print_stats=1"
          ctest -C Debug --output-on-failure

  macos:
    runs-on: macos-latest
    steps:
      - name: 'Checkout'
        uses: actions/checkout@v6
        with:
          fetch-depth: 0
          fetch-tags: true

      - name: 'Install dependencies'
        run: |
          brew update
          brew install leptonica

      - name: 'Configure build'
        run: |
          cmake -S . -B build -DCMAKE_BUILD_TYPE=Debug

      - name: 'Build'
        run: cmake --build build

      - name: 'Run tests'
        run: |
          cd build
          ctest --output-on-failure

  macos-memcheck:
    runs-on: macos-latest
    needs: macos
    steps:
      - name: 'Checkout'
        uses: actions/checkout@v6
        with:
          fetch-depth: 0
          fetch-tags: true

      - name: 'Install dependencies'
        run: |
          brew update
          brew install leptonica

      - name: 'Configure build'
        run: |
          cmake -S . -B build -DCMAKE_BUILD_TYPE=Debug -DCMAKE_CXX_FLAGS="-g -fsanitize=address -fno-omit-frame-pointer"

      - name: 'Build'
        run: cmake --build build

      - name: 'Run tests with AddressSanitizer'
        run: |
          cd build
          export ASAN_OPTIONS=detect_leaks=1:abort_on_error=1:print_stats=1
          ctest --output-on-failure