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

name: Build Tremotesf for Windows

on:
  workflow_call:
    inputs:
      arch:
        description: Architecture (either x86_64 or arm64)
        type: string
        required: true
      release-tag:
        description: Release tag
        type: string
        required: false
        default: ''

jobs:
  build-vcpkg-dependencies:
    runs-on: ${{ inputs.arch == 'arm64' && 'windows-11-arm' || 'windows-2025' }}

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

    - name: Set up MSVC environment
      uses: equeim/action-setup-msvc-environment@5d989fa1a8d416d51b2089554f07a5233cb76a77
      with:
        arch: ${{ inputs.arch == 'x86_64' && 'amd64' || 'arm64' }}

    - name: Set up vcpkg
      uses: equeim/action-setup-vcpkg@b973e4a3c3a7a6e35e1686034dedc46086639be4
      with:
        vcpkg-root: ${{ github.workspace }}\vcpkg
        binary-cache-path: ${{ github.workspace }}\vcpkg_binary_cache
        save-cache: true
        cache-key-tag: ${{ inputs.arch }}

    - name: Configure CMake
      run:
        cmake -G Ninja -S . -B build --preset windows-${{ inputs.arch }}-msvc-vcpkg -D VCPKG_INSTALLED_DIR=${{ github.workspace }}/vcpkg_installed

    - name: Archive vcpkg build logs
      uses: actions/upload-artifact@330a01c490aca151604b8cf639adc76d48f6c5d4
      if: always()
      with:
        name: 'windows-${{ inputs.arch }}-vcpkg-build-logs'
        retention-days: ${{ github.event_name == 'push' && 7 || 3 }}
        path: |
          ${{ github.workspace }}\vcpkg\buildtrees\*\*.log
          ${{ github.workspace }}\vcpkg\buildtrees\*\*.txt

  build-windows-msvc:
    needs: build-vcpkg-dependencies

    strategy:
      fail-fast: false
      matrix:
        toolchain: ${{ fromJSON(inputs.release-tag == '' && '["msvc", "msvc-clang"]' || '["msvc"]') }}

    runs-on: ${{ inputs.arch == 'arm64' && 'windows-11-arm' || 'windows-2025' }}

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

    - name: Install WiX
      run: |
        dotnet tool install --global wix
        wix extension add --global WixToolset.UI.wixext

    - name: Set up MSVC environment
      uses: equeim/action-setup-msvc-environment@5d989fa1a8d416d51b2089554f07a5233cb76a77
      with:
        arch: ${{ inputs.arch == 'x86_64' && 'amd64' || 'arm64' }}

    - name: Set up vcpkg
      uses: equeim/action-setup-vcpkg@b973e4a3c3a7a6e35e1686034dedc46086639be4
      with:
        vcpkg-root: ${{ github.workspace }}\vcpkg
        binary-cache-path: ${{ github.workspace }}\vcpkg_binary_cache
        save-cache: 'false'
        cache-key-tag: ${{ inputs.arch }}

    - name: Build Tremotesf with ${{ matrix.toolchain }} toolchain for ${{ inputs.arch }} architecture
      id: build
      uses: equeim/action-cmake-build@72a6a248408085b42fe81c6045cee41bb4853396
      with:
        cmake-arguments: --preset windows-${{ inputs.arch }}-${{ matrix.toolchain }}-vcpkg -D VCPKG_INSTALLED_DIR=${{ github.workspace }}/vcpkg_installed -D VCPKG_INSTALL_OPTIONS='--disable-metrics;--only-binarycaching' -D TREMOTESF_ASAN=${{ inputs.release-tag == '' && matrix.toolchain == 'msvc' && 'ON' || 'OFF' }}
        package: true
      env:
        QT_FORCE_STDERR_LOGGING: 1

    - name: Archive packages
      if: inputs.release-tag == ''
      uses: actions/upload-artifact@330a01c490aca151604b8cf639adc76d48f6c5d4
      with:
        name: 'windows-${{ inputs.arch }}-${{ matrix.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@330a01c490aca151604b8cf639adc76d48f6c5d4
      if: always()
      with:
        name: 'windows-${{ inputs.arch }}-${{ matrix.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 }}\CMakeFiles\CMakeConfigureLog.yaml
