name: windows-cmake

on:
  push:
    paths-ignore:
      - 'CHANGES*'
      - 'Doc/**'
      - 'appveyor.yml'
  pull_request:
    branches: master
    paths-ignore:
      - 'CHANGES*'
      - 'Doc/**'
      - 'appveyor.yml'

permissions:
  contents: read

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

    name: CMake VS ${{ matrix.VER }}

    strategy:
      matrix:
        include:
        - VER: '17 2022'
          os: 'windows-2025'

    env:
      BUILD_SYS: Visual Studio ${{ matrix.VER }}

    steps:
    - name: Machine Info
      shell: powershell
      run: |
          systeminfo | findstr /B /C:"OS Name" /B /C:"OS Version"

    - name: Checkout
      uses: actions/checkout@v4
      with:
        show-progress: false

    - name: Install CCache
      uses: hendrikmuhs/ccache-action@v1
      with:
        key: ${{ matrix.os }}

    - name: Install NuGet Packages
      shell: powershell
      run: |
          nuget install PCRE2 -OutputDirectory C:\Tools
          nuget install Bison -OutputDirectory C:\Tools

    # https://cygwin.com/cygwin-ug-net/cygpath.html
    - name: Prepare Environment
      shell: bash
      run: |
          cat << EOF >> $GITHUB_ENV
          PCRE2_PATH=$(cygpath -w "$(ls -d /C/Tools/PCRE2*)")
          EOF
          BISON_PATH=$(cygpath -w "$(ls -d /C/Tools/Bison*)/bin")
          echo "$BISON_PATH" >> $GITHUB_PATH

    - name: Configure
      shell: powershell
      run: |
          cmake --version
          cmake -G "$env:BUILD_SYS" -A x64 `
          -DCMAKE_INSTALL_PREFIX="C:\Tools\swig" `
          -DCMAKE_C_FLAGS="/W3 /EHsc /DPCRE2_STATIC" `
          -DCMAKE_CXX_FLAGS="/W3 /EHsc /DPCRE2_STATIC" `
          -DPCRE2_INCLUDE_DIR="$env:PCRE2_PATH\include" `
          -DPCRE2_LIBRARY="$env:PCRE2_PATH\lib\pcre2-8-static.lib" `
          -DLINK_FLAGS="/NODEFAULTLIB:MSVCRT" -S . -B .

    - name: Build
      shell: powershell
      run: |
          cmake --build . --config Release

    - name: Test
      shell: powershell
      run: |
          ctest --output-on-failure -V -C Release

    - name: Install
      shell: powershell
      run: |
          cmake --install .
          C:\Tools\swig\bin\swig.exe -version
