# SPDX-FileCopyrightText: 2015-2024 Alexey Rochev
#
# SPDX-License-Identifier: CC0-1.0

name: Build Tremotesf for Windows

on:
  workflow_call:
    inputs:
      toolchain:
        description: Either 'msvc' or 'msvc-clang'
        type: string
        required: false
        default: 'msvc'
      release-tag:
        description: Release tag
        type: string
        required: false
        default: ''

jobs:
  build-windows-msvc:
    strategy:
      fail-fast: false
      matrix:
        arch:
          - msvc-arch: amd64
            cmake-preset-arch: x86_64
          - msvc-arch: amd64_arm64
            cmake-preset-arch: arm64

    runs-on: windows-latest

    steps:
    - name: Checkout
      uses: actions/checkout@v4
      with:
        submodules: 'recursive'

    - name: Set up MSVC environment
      uses: equeim/action-setup-msvc-environment@v1
      with:
        arch: ${{ matrix.arch.msvc-arch }}

    - name: Upgrade LLVM
      if: inputs.toolchain == 'msvc-clang'
      run: |
        choco upgrade llvm

    - name: Set up vcpkg
      uses: equeim/action-setup-vcpkg@v6
      with:
        vcpkg-root: ${{ github.workspace }}\vcpkg
        binary-cache-path: ${{ github.workspace }}\vcpkg_binary_cache
        save-cache: ${{ github.event_name != 'pull_request' && inputs.toolchain == 'msvc' }}
        cache-key-tag: ${{ matrix.arch.cmake-preset-arch }}

    - name: Build Tremotesf with ${{ inputs.toolchain }} toolchain for ${{ matrix.arch.cmake-preset-arch }} architecture
      id: build
      uses: equeim/action-cmake-build@v10
      with:
        cmake-arguments: --preset windows-${{ matrix.arch.cmake-preset-arch }}-${{ inputs.toolchain }}-vcpkg -D VCPKG_INSTALLED_DIR=${{ github.workspace }}/vcpkg_installed -D TREMOTESF_ASAN=${{ inputs.release-tag == '' && inputs.toolchain == 'msvc' && 'ON' || 'OFF' }}
        package: true
        test: ${{ matrix.arch.cmake-preset-arch == 'x86_64' }}

    - name: Archive packages
      if: inputs.release-tag == ''
      uses: actions/upload-artifact@v4
      with:
        name: 'windows-${{ matrix.arch.cmake-preset-arch }}-${{ inputs.toolchain }}-packages'
        retention-days: ${{ github.event_name == 'push' && 7 || 3 }}
        path: |
          ${{ steps.build.outputs.build-directory }}\packaging\windows\*.zip
          ${{ steps.build.outputs.build-directory }}\packaging\windows\*.msi

    - name: Upload packages to release
      if: inputs.release-tag != ''
      run: |
        $packages = Get-ChildItem ${{ steps.build.outputs.build-directory }}\packaging\windows\*.zip, ${{ steps.build.outputs.build-directory }}\packaging\windows\*.msi
        echo "Uploading packages $packages"
        gh release upload '${{ inputs.release-tag }}' $packages
      env:
        GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

    - name: Archive build logs
      uses: actions/upload-artifact@v4
      if: always()
      with:
        name: 'windows-${{ matrix.arch.cmake-preset-arch }}-${{ inputs.toolchain }}-build-logs'
        retention-days: ${{ github.event_name == 'push' && 7 || 3 }}
        path: |
          ${{ steps.build.outputs.build-directory }}\CMakeCache.txt
          ${{ steps.build.outputs.build-directory }}\compile_commands.json
          ${{ steps.build.outputs.build-directory }}\vcpkg-manifest-install.log
          ${{ steps.build.outputs.build-directory }}\CMakeFiles\CMakeConfigureLog.yaml
          ${{ env.VCPKG_ROOT }}\buildtrees\*\*.log
