---
name: mingw

on:
  pull_request:
    types: [opened, synchronize]
    paths-ignore:
      - '**.md'
      - '.mailmap'
      - 'ChangeLog*'
      - 'whatsnew*'
      - 'LICENSE'
  push:
    paths-ignore:
      - '**.md'
      - '.mailmap'
      - 'ChangeLog*'
      - 'whatsnew*'
      - 'LICENSE'

jobs:
  autotools:
    runs-on: windows-2019
    if: "!contains(github.event.head_commit.message, 'ci/mingw skip') && !contains(github.event.head_commit.message, 'ci/mingw/autotools skip')"
    strategy:
      fail-fast: false
      matrix:
        EVENT_MATRIX:
          - none
          - no-ssl
          - disable-openssl
          - disable-thread-support
          - disable-debug-mode
          - disable-malloc-replacement

    steps:
      - uses: actions/checkout@v2.0.0
      - name: Cache Build
        uses: actions/cache@v2
        with:
          path: build
          key: mingw-autotools-${{ matrix.EVENT_MATRIX }}-v5
      - name: Setup MSYS2
        uses: msys2/setup-msys2@v2
        with:
          msystem: MINGW64
          update: true
          install: mingw-w64-x86_64-gcc autoconf automake libtool mingw-w64-x86_64-openssl mingw-w64-x86_64-mbedtls pkg-config

      - name: Build
        shell: powershell
        run: |
          $env:EVENT_CONFIGURE_OPTIONS=""
          if ( "${{ matrix.EVENT_MATRIX }}" -eq "no-ssl" ) {
            $env:EVENT_CONFIGURE_OPTIONS="--disable-openssl --disable-mbedtls"
          }
          elseif ( "${{ matrix.EVENT_MATRIX }}" -ne "none" ) {
            $env:EVENT_CONFIGURE_OPTIONS="--${{ matrix.EVENT_MATRIX }}"
          }
          $env:EVENT_BUILD_PARALLEL=10

          $script='
          export PATH="/mingw64/bin:/usr/bin:/bin:/usr/local/bin:/opt/bin:/usr/bin/site_perl:/usr/bin/vendor_perl:/usr/bin/core_perl:$PATH"
          ./autogen.sh 2>&1 3>&1
          [[ $? -ne 0 ]] && exit 1
          mkdir -p build
          cd build
          [[ $? -ne 0 ]] && exit 1
          LDFLAGS="-L/mingw64/lib" CFLAGS="-I/mingw64/include" ../configure $EVENT_CONFIGURE_OPTIONS 2>&1
          [[ $? -ne 0 ]] && exit 1
          make -j $EVENT_BUILD_PARALLEL 2>&1
          [[ $? -ne 0 ]] && exit 1
          exit 0
          '
          D:\a\_temp\msys64\usr\bin\bash.exe -c $script

      - name: Test
        shell: powershell
        run: |
          $env:EVENT_TESTS_PARALLEL=1

          $script='
          export PATH="/mingw64/bin:/usr/bin:/bin:/usr/local/bin:/opt/bin:/usr/bin/site_perl:/usr/bin/vendor_perl:/usr/bin/core_perl:$PATH"
          cd build
          make verify -j $EVENT_TESTS_PARALLEL 2>&1 '
          D:\a\_temp\msys64\usr\bin\bash.exe -c $script

      - uses: actions/upload-artifact@v1
        if: failure()
        with:
          name: mingw-${{ matrix.EVENT_MATRIX }}-build
          path: build

  cmake:
    runs-on: windows-2019
    if: "!contains(github.event.head_commit.message, 'ci/mingw skip') && !contains(github.event.head_commit.message, 'ci/mingw/cmake skip')"
    strategy:
      fail-fast: false
      matrix:
        EVENT_MATRIX:
          - NONE
          - NO_SSL
          - DISABLE_OPENSSL
          - DISABLE_THREAD_SUPPORT
          - DISABLE_DEBUG_MODE
          - DISABLE_MM_REPLACEMENT

    steps:
      - uses: actions/checkout@v2.0.0
      - name: Cache Build
        uses: actions/cache@v2
        with:
          path: build
          key: mingw-cmake-${{ matrix.EVENT_MATRIX }}-v4

      - name: Setup MSYS2
        uses: msys2/setup-msys2@v2
        with:
          msystem: MINGW64
          update: true
          install: mingw-w64-x86_64-gcc mingw-w64-x86_64-openssl mingw-w64-x86_64-mbedtls  

      - name: Build
        shell: powershell
        run: |
          $EVENT_CONFIGURE_OPTIONS=""
          if ( "${{ matrix.EVENT_MATRIX }}" -ne "NO_SSL" ) {
            $EVENT_CONFIGURE_OPTIONS="-DEVENT__DISABLE_OPENSSL=ON -DEVENT__DISABLE_MBEDTLS=ON"
          }
          elseif ( "${{ matrix.EVENT_MATRIX }}" -ne "NONE" ) {
            $EVENT_CONFIGURE_OPTIONS="-DEVENT__${{ matrix.EVENT_MATRIX }}=ON"
          }
          $env:PATH="D:\a\_temp\msys64\mingw64\bin;D:\a\_temp\msys64\usr\bin;$env:PATH"
          mkdir build -ea 0
          cd build
          function cmake_configure($retry)
          {
            $errcode=0
            try {
              cmake .. -G "MSYS Makefiles" $EVENT_CONFIGURE_OPTIONS -DCMAKE_C_FLAGS=-w
              $errcode=$LastExitCode
            }
            catch {
              $errcode=1
            }
            finally {
              if ($errcode -ne 0) {
                if ($retry -eq 0) {
                  $host.SetShouldExit($LastExitCode)
                } else {
                  echo "Remove all entries in build directory"
                  rm -r -fo *
                  cmake_configure 0
                }
              }
            }
          }
          cmake_configure 1
          cmake --build .

      - name: Test
        shell: powershell
        run: |
          cd build
          ctest --output-on-failure

      - uses: actions/upload-artifact@v1
        if: failure()
        with:
          name: mingw-${{ matrix.EVENT_MATRIX }}-build
          path: build
