name: Windows build
on: [push, pull_request]
concurrency:
  group: ${{ github.workflow }}-${{ github.job }}-${{ github.ref }}
  cancel-in-progress: true
jobs:
  build:
    name: 'MSVC ${{ matrix.os }}, ${{ matrix.target }} ${{ matrix.sys.set }}'
    defaults:
      run:
        shell: bash {0}
    strategy:
      matrix:
        os:
          - 2019
          - 2022
        target:
          - x86
          - x64
        sys:
          - { set: SSE, flags: "/arch:SSE2" }
          - { set: AVX, flags: "/arch:AVX" }
          - { set: AVX2, flags: "/arch:AVX2" }
          - { set: AVX512, flags: "/arch:AVX512" }
        exclude:
          # AVX on both platforms has a codegen error
          # On 2019 in _mm256_rsqrt_ps, on 2022 in _mm256_blend_p*
          - { sys: { set: AVX } }
          # On both platforms x86 + AVX512 triggers a compiler crash
          - { target: x86, sys: { set: AVX512 } }
          # /arch:SSE2 is not available on x64 platforms (SSE2 is enabled by default)
          - { target: x64, sys: { set: SSE} }
    runs-on: windows-${{ matrix.os }}
    steps:
    - name: Setup compiler
      uses: ilammy/msvc-dev-cmd@v1
      with:
        arch: ${{ matrix.target }}
    - name: Setup Ninja
      run: |
        python3 -m pip install --upgrade pip setuptools wheel
        python3 -m pip install ninja
    - name: Checkout xsimd
      uses: actions/checkout@v2
    - name: Setup
      run: |
        mkdir _build
        cd _build && cmake .. -DBUILD_TESTS=ON -DDOWNLOAD_DOCTEST=ON -DBUILD_BENCHMARK=ON -DBUILD_EXAMPLES=ON -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_FLAGS="${{ matrix.sys.flags }}" -G Ninja
    - name: Build
      run: |
        cd _build && cmake --build .
    - name: Testing xsimd
      if: ${{ !startsWith(matrix.sys.set, 'AVX512') }}
      run: |
        cd _build && ./test/test_xsimd

  build-windows-mingw:
    name: 'MSYS2 ${{ matrix.msystem }}'
    runs-on: windows-2019
    defaults:
      run:
        shell: msys2 {0}
    strategy:
      matrix:
        # Temporarily remove MINGW64 and UCRT64 builds because
        # GCC 12 gives an unexpected overflow warning for __builtin_memmove
        # see https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106199
        msystem: [ MINGW32, CLANG32, CLANG64 ]
        #msystem: [ MINGW32, MINGW64, UCRT64, CLANG32, CLANG64 ]
      fail-fast: false
    steps:
      - name: Use MinGW from MSYS2
        uses: msys2/setup-msys2@v2
        with:
          msystem: ${{ matrix.msystem }}
          update: true
          path-type: minimal
          pacboy: >-
            cc:p
            cmake:p
            ninja:p
            doctest:p
      - name: Checkout xsimd
        uses: actions/checkout@v2
      - name: Configure
        run: |
          mkdir _build
          cd _build
          cmake .. -DBUILD_TESTS=ON -DBUILD_BENCHMARK=ON -DBUILD_EXAMPLES=ON -DCMAKE_BUILD_TYPE=Release -G Ninja
      - name: Build
        run: ninja -C _build
      - name: Test
        run: |
          cd _build && ./test/test_xsimd
