name: Windows
on:
  push:
    branches:
    - "main"
    - "core-9-0-branch"
    tags:
    - "core-*"
permissions:
  contents: read
jobs:
  plan:
    runs-on: ubuntu-latest
    outputs:
      msvc: ${{ steps.matrix.outputs.msvc }}
      gcc: ${{ steps.matrix.outputs.gcc }}
    steps:
      - name: Select build matrix based on branch name
        id: matrix
        run: |
          (
            echo msvc=$(jq -nc '{config: (if env.IsMatched == "true" then env.MSVC_FULL else env.MSVC_PARTIAL end) | fromjson }' )
            echo gcc=$(jq -nc '{config: (if env.IsMatched == "true" then env.GCC_FULL else env.GCC_PARTIAL end) | fromjson }' )
          ) | tee -a $GITHUB_OUTPUT
        env:
          IsMatched: ${{ github.ref_name == 'main' || github.ref_name == 'core-9-0-branch' }}
          # DO NOT CHANGE THESE MATRIX SPECS; IT AFFECTS OUR COST CONTROLS
          MSVC_FULL: >
            [
              "",
              "CHECKS=nodep",
              "OPTS=static",
              "OPTS=noembed",
              "OPTS=symbols",
              "OPTS=symbols STATS=compdbg,memdbg"
            ]
          MSVC_PARTIAL: >
            [
              "",
              "OPTS=symbols STATS=compdbg,memdbg"
            ]
          GCC_FULL: >
            [
              "",
              "CFLAGS=-DTCL_NO_DEPRECATED=1",
              "--disable-shared",
              "--disable-zipfs",
              "--enable-symbols",
              "--enable-symbols=mem",
              "--enable-symbols=all"
            ]
          GCC_PARTIAL: >
            [
              "",
              "--disable-shared",
              "--enable-symbols=all"
            ]
  msvc:
    runs-on: windows-2025
    needs: plan
    defaults:
      run:
        shell: powershell
        working-directory: win
    strategy:
      matrix: ${{ fromJson(needs.plan.outputs.msvc) }}
    # Using powershell means we need to explicitly stop on failure
    steps:
      - name: Checkout
        uses: actions/checkout@v7
        timeout-minutes: 5
      - name: Init MSVC
        uses: ilammy/msvc-dev-cmd@v1
        timeout-minutes: 5
      - name: Build ${{ matrix.config }}
        run: |
          &nmake -f makefile.vc ${{ matrix.config }} all
          if ($lastexitcode -ne 0) {
             throw "nmake exit code: $lastexitcode"
          }
        timeout-minutes: 5
      - name: Build Test Harness ${{ matrix.config }}
        run: |
          &nmake -f makefile.vc ${{ matrix.config }} tcltest
          if ($lastexitcode -ne 0) {
             throw "nmake exit code: $lastexitcode"
          }
        timeout-minutes: 5
      - name: Run Tests ${{ matrix.config }}
        run: |
          &nmake -f makefile.vc ${{ matrix.config }} test
          if ($lastexitcode -ne 0) {
             throw "nmake exit code: $lastexitcode"
          }
        timeout-minutes: 30
        env:
          ERROR_ON_FAILURES: 1
  gcc:
    runs-on: windows-2025
    needs: plan
    defaults:
      run:
        shell: msys2 {0}
        working-directory: win
    strategy:
      matrix: ${{ fromJson(needs.plan.outputs.gcc) }}
    steps:
      - name: Install MSYS2
        uses: msys2/setup-msys2@v2
        with:
          msystem: MINGW64
          install: git mingw-w64-x86_64-toolchain make
        timeout-minutes: 10
      - name: Checkout
        uses: actions/checkout@v7
        timeout-minutes: 5
      - name: Prepare
        run: |
          touch tclStubInit.c tclOOStubInit.c tclOOScript.h
          mkdir "${HOME}/install dir"
        working-directory: generic
      - name: Configure ${{ matrix.config }}
        run: |
          ./configure ${CFGOPT} "--prefix=$HOME/install dir" || (cat config.log && exit 1)
        env:
          CFGOPT: --enable-64bit ${{ matrix.config }}
        timeout-minutes: 5
      - name: Build
        run: make -j4 all
        timeout-minutes: 5
      - name: Build Test Harness
        run: make -j4 tcltest
        timeout-minutes: 5
      - name: Info
        run: |
          ulimit -a || echo 'get limit failed'
          make runtest SCRIPT=../.github/workflows/info.tcl || echo 'get info failed'
      - name: Run Tests
        run: make test
        timeout-minutes: 30
        env:
          ERROR_ON_FAILURES: 1
      - name: Install
        run: make install
        timeout-minutes: 5

# If you add builds with Wine, be sure to define the environment variable
# CI_USING_WINE when running them so that broken tests know not to run.
