name: cmake-tests
# CMake-specific build and test workflows
# This workflow validates zstd builds across different CMake configurations,
# platforms, and edge cases to ensure broad compatibility.

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

on:
  pull_request:
    branches: [ dev, release, actionsTest ]

permissions: read-all

env:
  # Centralized test timeouts for consistency
  QUICK_TEST_TIME: "30s"
  STANDARD_TEST_TIME: "1mn"
  # Common CMake flags
  COMMON_CMAKE_FLAGS: "-DCMAKE_COMPILE_WARNING_AS_ERROR=ON -DZSTD_BUILD_TESTS=ON"

jobs:
  # Ubuntu-based cmake build using make wrapper
  # This test uses the make-driven cmake build to ensure compatibility
  # with the existing build system integration
  cmake-ubuntu-basic:
    name: "CMake Ubuntu Basic Build"
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # tag=v5.0.0
    - name: Install dependencies
      run: |
        sudo apt install liblzma-dev  # Required for compression algorithms
    - name: CMake build and test via make
      run: |
        # Use make wrapper for cmake build with quick test timeouts
        FUZZERTEST=-T${{ env.STANDARD_TEST_TIME }} ZSTREAM_TESTTIME=-T${{ env.STANDARD_TEST_TIME }} make cmakebuild V=1

  # Cross-platform cmake build with edge case: source paths containing spaces
  # This test ensures cmake handles filesystem paths with spaces correctly
  # across different operating systems and build generators
  cmake-cross-platform-spaces:
    name: "CMake Cross-Platform (Spaces in Path)"
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        include:
          - os: ubuntu-latest
            generator: "Unix Makefiles"
            name: "Linux"
          - os: windows-latest
            generator: "NMake Makefiles"
            name: "Windows NMake"
          - os: macos-latest
            generator: "Unix Makefiles"
            name: "macOS"
    env:
      SRC_DIR: "source directory with spaces"
    steps:
    - uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # tag=v5.0.0
      with:
        path: "${{ env.SRC_DIR }}"
    - uses: ilammy/msvc-dev-cmd@0b201ec74fa43914dc39ae48a89fd1d8cb592756 # v1.13.0
      if: ${{ matrix.generator == 'NMake Makefiles' }}
    - name: "CMake build and install (${{ matrix.name }})"
      run: |
        # Test Release build with installation to verify packaging
        cmake -S "${{ env.SRC_DIR }}/build/cmake" -B build -DBUILD_TESTING=ON -G "${{ matrix.generator }}" -DCMAKE_BUILD_TYPE=Release --install-prefix "${{ runner.temp }}/install"
        cmake --build build --config Release
        cmake --install build --config Release

  # Windows-specific cmake testing with Visual Studio 2022
  # Tests multiple generators and toolchains to ensure broad Windows compatibility
  # including MSVC (x64, Win32, ARM64), MinGW, and Clang-CL with various architectures and optimizations
  cmake-windows-comprehensive:
    name: "CMake Windows VS2022 (${{ matrix.name }})"
    runs-on: ${{ matrix.runner }}
    strategy:
      fail-fast: false
      matrix:
        include:
          - generator: "Visual Studio 17 2022"
            flags: "-A x64"
            name: "MSVC x64"
            runner: "windows-2022"
            cmake_extra_flags: "-DCMAKE_COMPILE_WARNING_AS_ERROR=ON -DZSTD_BUILD_TESTS=ON"
          - generator: "Visual Studio 17 2022"
            flags: "-A Win32"
            name: "MSVC Win32"
            runner: "windows-2022"
            cmake_extra_flags: "-DCMAKE_COMPILE_WARNING_AS_ERROR=ON -DZSTD_BUILD_TESTS=ON"
          - generator: "Visual Studio 17 2022"
            flags: "-A x64"
            name: "MSVC x64 (No ZSTD_BUILD_TESTS)"
            runner: "windows-2022"
            # Intentionally omit ZSTD_BUILD_TESTS to reproduce the CXX language configuration bug
            cmake_extra_flags: "-DCMAKE_COMPILE_WARNING_AS_ERROR=ON"
          # - generator: "Visual Studio 17 2022"
          #   flags: "-A ARM64"
          #   name: "MSVC ARM64"
          #   runner: "windows-2022-arm64"  # Disabled due to very long queue times
          - generator: "MinGW Makefiles"
            flags: ""
            name: "MinGW"
            runner: "windows-2022"
            cmake_extra_flags: "-DCMAKE_COMPILE_WARNING_AS_ERROR=ON -DZSTD_BUILD_TESTS=ON"
          - generator: "Visual Studio 17 2022"
            flags: "-T ClangCL"
            name: "Clang-CL"
            runner: "windows-2022"
            cmake_extra_flags: "-DCMAKE_COMPILE_WARNING_AS_ERROR=ON -DZSTD_BUILD_TESTS=ON"
          - generator: "Visual Studio 17 2022"
            flags: "-T ClangCL -A x64 -DCMAKE_C_FLAGS=/arch:AVX2"
            name: "Clang-CL AVX2"
            runner: "windows-2022"
            cmake_extra_flags: "-DCMAKE_COMPILE_WARNING_AS_ERROR=ON -DZSTD_BUILD_TESTS=ON"
    steps:
    - uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # tag=v5.0.0
    - name: Add MSBuild to PATH
      uses: microsoft/setup-msbuild@6fb02220983dee41ce7ae257b6f4d8f9bf5ed4ce # tag=v2.0.0
    - name: "Configure CMake (${{ matrix.name }})"
      run: |
        cd build\cmake
        mkdir build
        cd build
        cmake.exe -G "${{matrix.generator}}" ${{matrix.flags}} -DCMAKE_BUILD_TYPE=Debug ${{ matrix.cmake_extra_flags }} -DZSTD_ZSTREAM_FLAGS=-T${{ env.QUICK_TEST_TIME }} -DZSTD_FUZZER_FLAGS=-T${{ env.QUICK_TEST_TIME }} -DZSTD_FULLBENCH_FLAGS=-i0 ..
    - name: "Build (${{ matrix.name }})"
      run: |
        cd build\cmake\build
        cmake.exe --build .
    - name: "Test (${{ matrix.name }})"
      run: |
        cd build\cmake\build
        ctest.exe -V -C Debug

  # macOS ARM64 (Apple Silicon) specific cmake testing
  # Validates zstd builds and runs correctly on Apple Silicon architecture
  # Uses native ARM64 hardware for optimal performance and compatibility testing
  cmake-macos-arm64:
    name: "CMake macOS ARM64 (Apple Silicon)"
    runs-on: macos-14  # ARM64 runner
    steps:
    - uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # tag=v5.0.0
    - name: "CMake build and test (ARM64)"
      run: |
        # Configure and build with ARM64-specific optimizations
        cd build/cmake
        mkdir build
        cd build
        cmake -DCMAKE_BUILD_TYPE=Release ${{ env.COMMON_CMAKE_FLAGS }} -DZSTD_ZSTREAM_FLAGS=-T${{ env.QUICK_TEST_TIME }} -DZSTD_FUZZER_FLAGS=-T${{ env.QUICK_TEST_TIME }} -DZSTD_FULLBENCH_FLAGS=-i1 ..
        make -j$(sysctl -n hw.ncpu)
        ctest -V
