name: windows port

on:
  push:
  pull_request:
    paths:
      - '.github/workflows/*.yml'
      - 'tools/**'
      - 'py/**'
      - 'extmod/**'
      - 'shared/**'
      - 'lib/**'
      - 'ports/unix/**'
      - 'ports/windows/**'

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

jobs:
  build-vs:
    strategy:
      fail-fast: false
      matrix:
        platform: [x86, x64]
        configuration: [Debug, Release]
        variant: [dev, standard]
        visualstudio: ['2019', '2022']
        include:
        - visualstudio: '2019'
          # The v142 toolset (VS 2019 compiler) is pre-installed on
          # windows-2022 as a component of VS 2022.  Use VS 2022's
          # MSBuild and select the v142 toolset via PlatformToolset.
          vs_version: '[17, 18)'
          platform_toolset: v142
        - visualstudio: '2022'
          vs_version: '[17, 18)'
          platform_toolset: v143
        # trim down the number of jobs in the matrix
        exclude:
        - variant: standard
          configuration: Debug
        - visualstudio: '2019'
          configuration: Debug
    runs-on: windows-2022
    env:
      CI_BUILD_CONFIGURATION: ${{ matrix.configuration }}
    steps:
    - name: Install Python 3.11
      # As of 20260112 the default Python version in Windows image is 3.12, which breaks settrace tests
      # Use 3.11 for now
      uses: actions/setup-python@v7
      with:
        python-version: '3.11'
    - uses: microsoft/setup-msbuild@v3
      with:
        vs-version: ${{ matrix.vs_version }}
    - uses: actions/checkout@v7
    - name: Build mpy-cross.exe
      run: msbuild mpy-cross\mpy-cross.vcxproj -maxcpucount -property:Configuration=${{ matrix.configuration }} -property:Platform=${{ matrix.platform }} -property:PlatformToolset=${{ matrix.platform_toolset }}
    - name: Update submodules
      run: git submodule update --init lib/micropython-lib
    - name: Build micropython.exe
      run: msbuild ports\windows\micropython.vcxproj -maxcpucount -property:Configuration=${{ matrix.configuration }} -property:Platform=${{ matrix.platform }} -property:PyVariant=${{ matrix.variant }} -property:PlatformToolset=${{ matrix.platform_toolset }}
    - name: Get micropython.exe path
      id: get_path
      run: |
        $exePath="$(msbuild ports\windows\micropython.vcxproj -nologo -v:m -t:ShowTargetPath -property:Configuration=${{ matrix.configuration }} -property:Platform=${{ matrix.platform }} -property:PyVariant=${{ matrix.variant }} -property:PlatformToolset=${{ matrix.platform_toolset }})"
        echo ("micropython=" + $exePath.Trim()) >> $env:GITHUB_OUTPUT
    - name: Run tests
      id: test
      env:
        MICROPY_MICROPYTHON: ${{ steps.get_path.outputs.micropython }}
      working-directory: tests
      run: python run-tests.py
    - name: Print failures
      if: failure() && steps.test.conclusion == 'failure'
      working-directory: tests
      run: python run-tests.py --print-failures
    - name: Run mpy tests
      id: test_mpy
      env:
        MICROPY_MICROPYTHON: ${{ steps.get_path.outputs.micropython }}
      working-directory: tests
      run: python run-tests.py --via-mpy -d basics float micropython
    - name: Print mpy failures
      if: failure() && steps.test_mpy.conclusion == 'failure'
      working-directory: tests
      run: python run-tests.py --print-failures

  build-mingw:
    strategy:
      fail-fast: false
      matrix:
        variant: [dev, standard]
        sys: [mingw32, mingw64]
        include:
          - sys: mingw32
            env: i686
          - sys: mingw64
            env: x86_64
    runs-on: windows-latest
    env:
      CHERE_INVOKING: enabled_from_arguments
    defaults:
      run:
        shell: msys2 {0}
    steps:
    - uses: actions/setup-python@v7
      # note: can go back to installing mingw-w64-${{ matrix.env }}-python after
      # MSYS2 updates to Python >3.12 (due to settrace compatibility issue)
      with:
        python-version: '3.11'
    - uses: msys2/setup-msys2@v2
      with:
        msystem: ${{ matrix.sys }}
        update: true
        install: >-
          make
          mingw-w64-${{ matrix.env }}-gcc
          pkg-config
          git
          diffutils
        path-type: inherit  # Remove when setup-python is removed
    - uses: actions/checkout@v7
    - name: Build mpy-cross.exe
      run: make -C mpy-cross -j2
    - name: Update submodules
      run: make -C ports/windows VARIANT=${{ matrix.variant }} submodules
    - name: Build micropython.exe
      run: make -C ports/windows -j2 VARIANT=${{ matrix.variant }}
    - name: Run tests
      id: test
      run: make -C ports/windows test_full VARIANT=${{ matrix.variant }}
    - name: Print failures
      if: failure() && steps.test.conclusion == 'failure'
      working-directory: tests
      run: python run-tests.py --print-failures

  cross-build-on-linux:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v7
    - name: Install packages
      run: tools/ci.sh windows_setup
    - name: Build
      run: tools/ci.sh windows_build
