name: Build code

on:
  push:
  pull_request:
    types: [opened, synchronize, reopened]

env:
  VMA_VULKAN_VERSION: "1.4.350.0"
  VMA_VULKAN_SDK_PATH: ${{ github.workspace }}/vulkan_sdk

jobs:
  windows:
    name: ${{ matrix.config.name }}
    runs-on: windows-2022
    strategy:
      fail-fast: false
      matrix:
        config:
          - {
            name: "Windows MSVC (Debug)",
            compiler: "msvc",
            cc: "cl", cxx: "cl",
            cmake_configure_options: '-G "Visual Studio 17 2022" -A x64',
            build_type: "Debug",
          }
          - {
            name: "Windows MSVC (Release)",
            compiler: "msvc",
            cc: "cl", cxx: "cl",
            cmake_configure_options: '-G "Visual Studio 17 2022" -A x64',
            build_type: "Release",
          }

    steps:
      - uses: actions/checkout@v4

      - name: Check for Vulkan SDK Cache
        id: cache-vulkan
        uses: actions/cache@v4
        with:
          path: ${{ env.VMA_VULKAN_SDK_PATH }}
          key: vulkan-sdk-${{ env.VMA_VULKAN_VERSION }}-installed

      - name: Install Vulkan SDK
        if: steps.cache-vulkan.outputs.cache-hit != 'true'
        shell: pwsh
        run: |
          Write-Host "Vulkan SDK not found in cache. Installing..."
          curl -LS -o vulkansdk.exe https://sdk.lunarg.com/sdk/download/${{ env.VMA_VULKAN_VERSION }}/windows/vulkansdk-windows-X64-${{ env.VMA_VULKAN_VERSION }}.exe
          .\vulkansdk.exe --root "${{ env.VMA_VULKAN_SDK_PATH }}" --accept-licenses --default-answer --confirm-command install copy_only=1

      - name: Configure CMake
        shell: pwsh
        run: |
          $env:CC="${{ matrix.config.cc }}"
          $env:CXX="${{ matrix.config.cxx }}"
          $env:Path += ";${{ env.VMA_VULKAN_SDK_PATH }}\Bin\"
          $env:VULKAN_SDK="${{ env.VMA_VULKAN_SDK_PATH }}"
          cmake . `
            -Bbuild `
            -DCMAKE_BUILD_TYPE=${{ matrix.config.build_type }} `
            -DVMA_BUILD_SAMPLES=ON `
            ${{ matrix.config.cmake_configure_options }}

      - name: Build
        shell: pwsh
        run: |
          cmake --build build --config ${{ matrix.config.build_type }}
