name: "Build OpenVulkano"

on:
  push:
    branches: [ "master" ]
  pull_request:
    branches: [ "master" ]

env:
  BUILD_TYPE: Release

jobs:
  build:
    name: Build desktop
    runs-on: ${{matrix.os}}
    strategy:
      fail-fast: false
      matrix:
        os: [ windows_x64, ubuntu-act_cpp-24.04, mac_arm ]

    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          submodules: true
      - name: Get cmake checks from cache
        uses: actions/cache@v4
        id: cache
        with:
          path: |
              ${{github.workspace}}/build/cmake_checks_cache.txt
              ${{github.workspace}}/build/CMakeFiles/**/CMake*.cmake
          key: ${{runner.os}}-cmake-checks
      
      - name: Configure CMake
        shell: bash
        run: |
          if [ "${{ steps.cache.outputs.cache-hit }}" != "true" ]; then
            cmake -B "${{github.workspace}}/build" -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} -DDEPENDENCY_MIRROR_FILE="${{github.workspace}}/.gitea/workflows/DependencyMirrors.txt"
          else
            cmake -B "${{github.workspace}}/build" -C ${{github.workspace}}/build/cmake_checks_cache.txt -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} -DDEPENDENCY_MIRROR_FILE="${{github.workspace}}/.gitea/workflows/DependencyMirrors.txt"
          fi
      - name: Build
        shell: bash
        run: cmake --build "${{github.workspace}}/build" --config ${{env.BUILD_TYPE}}
      - name: Test
        working-directory: ${{github.workspace}}/build
        run: ctest --verbose -C ${{env.BUILD_TYPE}}
      - name: Archive artifacts Windows
        if: matrix.os == 'windows_x64'
        uses: actions/upload-artifact@v3
        with:
          name: 'Artifacts-windows'
          path: |
                ${{github.workspace}}/build/examples/${{env.BUILD_TYPE}}
                ${{github.workspace}}/build/tests/${{env.BUILD_TYPE}}
          if-no-files-found: error
          retention-days: 30
          overwrite: true
      - name: Archive artifacts
        if: matrix.os != 'windows_x64'
        uses: actions/upload-artifact@v3
        with:
          name: 'Artifacts-${{matrix.os}}'
          path: |
                ${{github.workspace}}/build/examples
                ${{github.workspace}}/build/tests
                !${{github.workspace}}/build/**/CMakeFiles
                !${{github.workspace}}/build/**/*.cmake
                !${{github.workspace}}/build/**/Makefile
          if-no-files-found: error
          retention-days: 30
          overwrite: true
        
        
  build_iOS:
    name: Build iOS
    runs-on: mac_arm
    
    steps:
    - name: Checkout
      uses: actions/checkout@v4
      with:
        submodules: true
    - name: Configure CMake
      run: mkdir cmake-build && mkdir cmake-build/iOS && cd cmake-build/iOS && cmake ../.. -G Xcode -DPLATFORM=OS64 -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}}
    - name: Build & Archive
      run: xcodebuild -project ${{github.workspace}}/cmake-build/iOS/OpenVulkano.xcodeproj -derivedDataPath ${{github.workspace}}/cmake-build/iOS/dd -scheme OpenVulkano_Examples -configuration ${{env.BUILD_TYPE}} -archivePath ${{github.workspace}}/cmake-build/iOS/app.xcarchive -sdk iphoneos archive