# Syntax reference https://help.github.com/en/actions/reference/workflow-syntax-for-github-actions
# Environment reference https://help.github.com/en/actions/reference/virtual-environments-for-github-hosted-runners
name: release-windows-mingw

on:
  push:
    branches:
      - 'main'
      - 'releases/**'
      - '2.*'
    tags:
      - '2.*'
  pull_request:

permissions:
  contents: read

defaults:
  run:
    shell: msys2 {0}

jobs:
  # TODO: add CMake build
  build_mingw:
    strategy:
      matrix:
        # only use the latest windows-* as the installed toolchain is identical
        os: [windows-2025]
      fail-fast: false

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

    timeout-minutes: 19 # max + 3*std of the last 7K runs

    steps:
      - uses: actions/checkout@v4
        with:
          persist-credentials: false

      - name: Set up MSYS2
        uses: msys2/setup-msys2@v2
        with:
          release: false # use pre-installed
          # TODO: install mingw-w64-x86_64-make and use mingw32.make instead - currently fails with "Windows Subsystem for Linux has no installed distributions."
          install: >-
            mingw-w64-x86_64-lld
            make
            mingw-w64-x86_64-gcc
            python

      - name: Build cppcheck
        run: |
          export PATH="/mingw64/lib/ccache/bin:$PATH"
          # set RDYNAMIC to work around broken MinGW detection
          make VERBOSE=1 RDYNAMIC=-lshlwapi -j$(nproc) CXXFLAGS=-O2 MATCHCOMPILER=yes cppcheck

      - name: Package
        run: |
          mkdir cppcheck-mingw
          cp cppcheck.exe cppcheck-mingw/
          cp -R cfg platforms cppcheck-mingw/
          cp /mingw64/bin/libgcc_s_seh-1.dll cppcheck-mingw/
          cp /mingw64/bin/libstdc*.dll cppcheck-mingw/
          cp /mingw64/bin/libwinpthread-1.dll cppcheck-mingw/

      - uses: actions/upload-artifact@v4
        with:
          name: cppcheck-mingw
          path: cppcheck-mingw
