name: cmake

on:
  workflow_call:

env:
  CCACHE_DIR: ${{ github.workspace }}/.cache/ccache
  SCCACHE_DIR: ${{ github.workspace }}/.cache/sccache
  CCACHE_BASEDIR: ${{ github.workspace }}
  SCCACHE_BASEDIRS: ${{ github.workspace }}
  CCACHE_COMPILERCHECK: content
  CCACHE_MAXSIZE: 2G
  SCCACHE_CACHE_SIZE: 2G

jobs:
  build:
    strategy:
      fail-fast: false
      matrix:
        os: [windows-2025, ubuntu-24.04, macos-15]
        build_type: [Debug, RelWithDebInfo]
    runs-on: ${{ matrix.os }}
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - uses: ./.github/actions/setup-pixi

      - name: Restore compiler cache
        uses: actions/cache@v4
        with:
          path: ${{ runner.os == 'Windows' && '.cache/sccache' || '.cache/ccache' }}
          key: ${{ runner.os }}-${{ matrix.build_type }}-ccache-${{ github.sha }}
          restore-keys: |
            ${{ runner.os }}-${{ matrix.build_type }}-ccache-

      - name: Zero cache stats
        run: |
          if [ "$RUNNER_OS" = "Windows" ]; then
            pixi run -- sccache --stop-server || true
            pixi run -- sccache --zero-stats || true
          else
            pixi run -- ccache --zero-stats || true
          fi
        shell: bash

      - name: Build
        run: pixi run build ${{ matrix.build_type }} ON

      - name: Unit Test
        run: pixi run unit-test ${{ matrix.build_type }}

      - name: Integration Test
        run: pixi run integration-test ${{ matrix.build_type }}

      - name: Smoke Test
        if: success() || failure()
        run: pixi run smoke-test ${{ matrix.build_type }}

      - name: Print cache stats and stop server
        if: always()
        run: |
          if [ "$RUNNER_OS" = "Windows" ]; then
            pixi run -- sccache --show-stats
            pixi run -- sccache --stop-server || true
          else
            pixi run -- ccache --show-stats
          fi
        shell: bash
