name: Windows
on:
  push:
    branches:
    - "main"
    - "core-9-0-branch"
    tags:
    - "core-*"
permissions:
  contents: read
env:
  ERROR_ON_FAILURES: 1
jobs:
  msvc:
    runs-on: windows-2022
    defaults:
      run:
        shell: powershell
        working-directory: tk/win
    # Using powershell means we need to explicitly stop on failure
    strategy:
      matrix:
        config:
          - ""
          - "OPTS=symbols"
          - "OPTS=static"
    steps:
      - name: Checkout Tk
        uses: actions/checkout@v5
        with:
          path: tk
      - name: Checkout Tcl 9.0
        uses: actions/checkout@v5
        with:
          repository: tcltk/tcl
          ref: core-9-0-branch
          path: tcl
      - name: Init MSVC
        uses: ilammy/msvc-dev-cmd@v1
      - name: Make Install Location
        working-directory: tcl
        run: |
          echo "TCLDIR=`pwd`" >> $GITHUB_ENV
          cd ..
          mkdir install
          cd install
          echo "INSTALLDIR=`pwd`" >> $GITHUB_ENV
      - name: Build Tcl (${{ matrix.config }})
        run: |
          &nmake -f makefile.vc release install ${{ matrix.config }}
          if ($lastexitcode -ne 0) {
             throw "nmake exit code: $lastexitcode"
          }
        working-directory: tcl/win
      - name: Build Tk (${{ matrix.config }})
        run: |
          &nmake -f makefile.vc all ${{ matrix.config }}
          if ($lastexitcode -ne 0) {
             throw "nmake exit code: $lastexitcode"
          }
      - name: Build Test Harness (${{ matrix.config }})
        run: |
          &nmake -f makefile.vc tktest ${{ matrix.config }}
          if ($lastexitcode -ne 0) {
             throw "nmake exit code: $lastexitcode"
          }
      - name: Run Tk Tests (${{ matrix.config }})
        run: |
          nmake -f makefile.vc test-classic ${{ matrix.config }} | tee out-classic.txt || {
            echo "::error::Failure during Test"
            exit 1
          }
          nmake -f makefile.vc test-ttk ${{ matrix.config }} | tee out-ttk.txt || {
            echo "::error::Failure during Test"
            exit 1
          }
          grep -q "Failed	0" out-classic.txt || {
            echo "::error::Failure during Test"
            exit 1
          }
          grep -q "Failed	0" out-ttk.txt || {
            echo "::error::Failure during Test"
            exit 1
          }
        env:
          CI_BUILD_WITH_MSVC: 1
        shell: bash
        timeout-minutes: 10
      - name: Build Help (${{ matrix.config }})
        run: |
          &nmake -f makefile.vc htmlhelp ${{ matrix.config }}
          if ($lastexitcode -ne 0) {
             throw "nmake exit code: $lastexitcode"
          }
      - name: Install (${{ matrix.config }})
        run: |
          &nmake -f makefile.vc install ${{ matrix.config }}
          if ($lastexitcode -ne 0) {
             throw "nmake exit code: $lastexitcode"
          }
  gcc:
    runs-on: windows-2022
    defaults:
      run:
        shell: msys2 {0}
        working-directory: win
    strategy:
      matrix:
        config:
          - ""
          - "--enable-symbols=mem"
          - "--enable-symbols=all"
          - "--disable-shared"
    steps:
      - name: Install MSYS2
        uses: msys2/setup-msys2@v2
        with:
          msystem: MINGW64
          install: git mingw-w64-x86_64-toolchain make zip
      - name: Checkout Tk
        uses: actions/checkout@v5
      - name: Checkout Tcl 9.0
        uses: actions/checkout@v5
        with:
          repository: tcltk/tcl
          ref: core-9-0-branch
          path: tcl
      - name: Prepare
        run: |
          touch tkStubInit.c
          touch "${HOME}/forWinDialog-5.12.7"
          mkdir "${HOME}/install_dir"
          echo "INSTALL_DIR=${HOME}/install_dir" >> $GITHUB_ENV
        working-directory: generic
      - name: Configure and Build Tcl (${{ matrix.config }})
        run: |
          ./configure $CFGOPT "--prefix=$INSTALL_DIR" || {
            cat config.log
            echo "::warning::Failure during Tcl Configure"
            exit 1
          }
          make all install  || {
            echo "::warning::Failure during Tcl Build"
            exit 1
          }
          echo "TCL_CONFIG_PATH=`pwd`" >> $GITHUB_ENV
        env:
          CFGOPT: --enable-64bit ${{ matrix.config }}
        working-directory: tcl/win
      - name: Configure Tk (${{ matrix.config }})
        run: |
          ./configure $CFGOPT "--prefix=$HOME/INSTALL_DIR" "--with-tcl=$TCL_CONFIG_PATH" || {
            cat config.log
            echo "::error::Failure during Configure"
            exit 1
          }
        env:
          CFGOPT: --enable-64bit ${{ matrix.config }}
      - name: Build Tk
        run: |
          make all install tktest || {
            echo "::error::Failure during Build"
            exit 1
          }
      - name: Run Tk Tests
        run: |
          make test-classic | tee out-classic.txt
          make test-ttk | tee out-ttk.txt
          grep -q "Failed	0" out-classic.txt || {
            echo "::error::Failure during Test"
            exit 1
          }
          grep -q "Failed	0" out-ttk.txt || {
            echo "::error::Failure during Test"
            exit 1
          }
        timeout-minutes: 10
