# This config file is generated by ./scripts/gen_gh_actions.py.
# Do not edit by hand.

name: Windows CI

on:
  push:
    branches: [ dev, ci_travis ]
  pull_request:
    branches: [ dev ]

jobs:
  test-windows:
    runs-on: windows-latest
    strategy:
      fail-fast: false
      matrix:
        include:
          - env:
              CC: gcc
              CXX: g++
              EXTRA_CFLAGS: -fcommon
          - env:
              CC: gcc
              CXX: g++
              CONFIGURE_FLAGS: --enable-debug
              EXTRA_CFLAGS: -fcommon
          - env:
              CC: cl.exe
              CXX: cl.exe
          - env:
              CC: gcc
              CXX: g++
              CROSS_COMPILE_32BIT: yes
              EXTRA_CFLAGS: -fcommon
          - env:
              CC: cl.exe
              CXX: cl.exe
              CONFIGURE_FLAGS: --enable-debug
          - env:
              CC: gcc
              CXX: g++
              CROSS_COMPILE_32BIT: yes
              CONFIGURE_FLAGS: --enable-debug
              EXTRA_CFLAGS: -fcommon
          - env:
              CC: cl.exe
              CXX: cl.exe
              CROSS_COMPILE_32BIT: yes
          - env:
              CC: cl.exe
              CXX: cl.exe
              CROSS_COMPILE_32BIT: yes
              CONFIGURE_FLAGS: --enable-debug

    steps:
    - uses: actions/checkout@v4

    - name: Show OS version
      shell: cmd
      run: |
        echo === Windows Version ===
        systeminfo | findstr /B /C:"OS Name" /C:"OS Version"
        ver
        echo.
        echo === Architecture ===
        echo PROCESSOR_ARCHITECTURE=%PROCESSOR_ARCHITECTURE%
        echo.

    - name: Setup MSYS2
      uses: msys2/setup-msys2@v2
      with:
        msystem: ${{ matrix.env.CROSS_COMPILE_32BIT == 'yes' && 'MINGW32' || 'MINGW64' }}
        update: true
        install: >-
          autotools
          git
        pacboy: >-
          make:p
          gcc:p
          binutils:p

    - name: Build and test (MinGW-GCC)
      if: matrix.env.CC != 'cl.exe'
      shell: msys2 {0}
      env:
        CC: ${{ matrix.env.CC || 'gcc' }}
        CXX: ${{ matrix.env.CXX || 'g++' }}
        COMPILER_FLAGS: ${{ matrix.env.COMPILER_FLAGS }}
        CONFIGURE_FLAGS: ${{ matrix.env.CONFIGURE_FLAGS }}
        EXTRA_CFLAGS: ${{ matrix.env.EXTRA_CFLAGS }}
      run: |
        # Run autoconf
        autoconf

        # Configure with flags
        if [ -n "$COMPILER_FLAGS" ]; then
          ./configure CC="${CC} ${COMPILER_FLAGS}" CXX="${CXX} ${COMPILER_FLAGS}" $CONFIGURE_FLAGS
        else
          ./configure $CONFIGURE_FLAGS
        fi

        # Build (mingw32-make is the "make" command in MSYS2)
        mingw32-make -j3
        mingw32-make tests

        # Run tests
        mingw32-make -k check

    - name: Setup MSVC environment
      if: matrix.env.CC == 'cl.exe'
      uses: ilammy/msvc-dev-cmd@v1
      with:
        arch: ${{ matrix.env.CROSS_COMPILE_32BIT == 'yes' && 'x86' || 'x64' }}

    - name: Build and test (MSVC)
      if: matrix.env.CC == 'cl.exe'
      shell: msys2 {0}
      env:
        CONFIGURE_FLAGS: ${{ matrix.env.CONFIGURE_FLAGS }}
        MSYS2_PATH_TYPE: inherit
      run: |
        # Export MSVC environment variables for configure
        export CC=cl.exe
        export CXX=cl.exe
        export AR=lib.exe
        export NM=dumpbin.exe
        export RANLIB=:

        # Verify cl.exe is accessible (should be in PATH via inherit)
        if ! which cl.exe > /dev/null 2>&1; then
          echo "cl.exe not found, trying to locate MSVC..."
          # Find and add MSVC bin directory to PATH
          MSVC_BIN=$(cmd.exe /c "echo %VCToolsInstallDir%" | tr -d '\\r' | sed 's/\\\\\\\\/\//g' | sed 's/C:/\\/c/g')
          if [ -n "$MSVC_BIN" ]; then
            export PATH="$PATH:$MSVC_BIN/bin/Hostx64/x64:$MSVC_BIN/bin/Hostx86/x86"
          fi
        fi

        # Run autoconf
        autoconf

        # Configure with MSVC
        ./configure CC=cl.exe CXX=cl.exe AR=lib.exe $CONFIGURE_FLAGS

        # Build (mingw32-make is the "make" command in MSYS2)
        mingw32-make -j3
        # Build tests sequentially due to PDB file issues
        mingw32-make tests

        # Run tests
        mingw32-make -k check



