#                          __  __            _
#                       ___\ \/ /_ __   __ _| |_
#                      / _ \\  /| '_ \ / _` | __|
#                     |  __//  \| |_) | (_| | |_
#                      \___/_/\_\ .__/ \__,_|\__|
#                               |_| XML parser
#
# Copyright (c) 2025 Sebastian Pipping <sebastian@pipping.org>
# Licensed under the MIT license:
#
# Permission is  hereby granted,  free of charge,  to any  person obtaining
# a  copy  of  this  software   and  associated  documentation  files  (the
# "Software"),  to  deal in  the  Software  without restriction,  including
# without  limitation the  rights  to use,  copy,  modify, merge,  publish,
# distribute, sublicense, and/or sell copies of the Software, and to permit
# persons  to whom  the Software  is  furnished to  do so,  subject to  the
# following conditions:
#
# The above copyright  notice and this permission notice  shall be included
# in all copies or substantial portions of the Software.
#
# THE  SOFTWARE  IS  PROVIDED  "AS  IS",  WITHOUT  WARRANTY  OF  ANY  KIND,
# EXPRESS  OR IMPLIED,  INCLUDING  BUT  NOT LIMITED  TO  THE WARRANTIES  OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
# NO EVENT SHALL THE AUTHORS OR  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
# DAMAGES OR  OTHER LIABILITY, WHETHER  IN AN  ACTION OF CONTRACT,  TORT OR
# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
# USE OR OTHER DEALINGS IN THE SOFTWARE.

name: Build on Windows

on:
  pull_request:
  push:
  schedule:
    - cron: '0 2 * * 5'  # Every Friday at 2am
  workflow_dispatch:

permissions:
  contents: read

jobs:
  windows_build:
    name: Build on Windows (${{ matrix.runs-on }}, ${{ matrix.cmake_platform }}, ${{ matrix.expat_char_type }})
    strategy:
      fail-fast: false
      matrix:
        include:
          - runs-on: windows-2025
            cmake_build_type: Debug
            cmake_generator: Visual Studio 17 2022
            cmake_platform: Win32
            expat_char_type: char
            expat_dll: libexpatd.dll
          - runs-on: windows-2022
            cmake_build_type: Debug
            cmake_generator: Visual Studio 17 2022
            cmake_platform: x64
            expat_char_type: wchar_t
            expat_dll: libexpatwd.dll
    defaults:
      run:
        shell: bash
    runs-on: "${{ matrix.runs-on }}"
    steps:
    - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd  # v6.0.2

    - uses: microsoft/setup-msbuild@30375c66a4eea26614e0d39710365f22f8b0af57  # v3.0.0

    - name: Configure
      env:
        cmake_build_type: ${{ matrix.cmake_build_type }}
        cmake_generator: ${{ matrix.cmake_generator }}
        cmake_platform: ${{ matrix.cmake_platform }}
        expat_char_type: ${{ matrix.expat_char_type }}
      run: |-
        cmake_args=(
          -A "${cmake_platform}"
          -G "${cmake_generator}"
          -DCMAKE_BUILD_TYPE="${cmake_build_type}"
          -DEXPAT_CHAR_TYPE="${expat_char_type}"
          -DEXPAT_WARNINGS_AS_ERRORS=ON
          -Wdev
          -Wdeprecated
        )
        set -x
        cd expat
        mkdir build
        cmake -S . -B build "${cmake_args[@]}"

    - name: Build
      env:
        cmake_build_type: ${{ matrix.cmake_build_type }}
      run: |-
        msbuild_args=(
          -m
          -property:Configuration="${cmake_build_type}"
        )
        set -x
        cd expat/build
        MSBuild.exe "${msbuild_args[@]}" expat.sln

    - name: Run tests
      env:
        cmake_build_type: ${{ matrix.cmake_build_type }}
        expat_dll: ${{ matrix.expat_dll }}
      run: |-
        ctest_args=(
          --build-config "${cmake_build_type}"
          --output-on-failure
          --parallel 2
        )
        set -x
        cd expat/build
        cp -v "${cmake_build_type}/${expat_dll}" "tests/${cmake_build_type}/"
        ctest "${ctest_args[@]}"
