name: CI

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

env:
  EM_VERSION: 2.0.13
  EM_CACHE_DIR: 'emsdk-cache'

jobs:
  clang_format_check:
    runs-on: ubuntu-20.04
    steps:
    - uses: actions/checkout@v3
    - uses: DoozyX/clang-format-lint-action@v0.12
      with:
        source: 'benchmark src test'
        extensions: 'hpp,cpp,ipp'
        clangFormatVersion: 11
  build_wasm:
    runs-on: ubuntu-20.04
    steps:
    - uses: actions/checkout@v3
      with:
        submodules: recursive
    - name: Setup cache for Emscripten
      id: cache-system-libraries
      uses: actions/cache@v3
      with:
        path: ${{env.EM_CACHE_DIR}}
        key: ${{env.EM_VERSION}}-${{ runner.os }}
    - uses: mymindstorm/setup-emsdk@v11
      with:
        version: ${{env.EM_VERSION}}
        actions-cache-folder: ${{env.EM_CACHE_DIR}}
    - name: Setup cache for game data
      id: cache-game-data
      uses: actions/cache@v3
      with:
        path: DUKE2
        key: duke2-shareware
    - name: Fetch game data
      run: bash -c '[ -d DUKE2 ] || (wget https://archive.org/download/msdos_DUKE2_shareware/DUKE2.zip && unzip DUKE2.zip)'
    - name: Run CMake
      run: emcmake cmake -H. -Bbuild_wasm -DCMAKE_BUILD_TYPE=Release -DWARNINGS_AS_ERRORS=ON -DWEBASSEMBLY_GAME_PATH=$(pwd)/DUKE2
    - name: Build
      run: make -j2 -Cbuild_wasm
    - uses: actions/upload-artifact@v3
      with:
        name: wasm_build
        path: build_wasm/src/RigelEngine.*
  build_linux:
    runs-on: ubuntu-20.04
    steps:
    - uses: actions/checkout@v3
      with:
        submodules: recursive
    - name: Install dependencies
      run: sudo ./docker/ubuntu-deps.sh
    - name: Run CMake (debug)
      run: CC=gcc-8 CXX=g++-8 cmake -H. -Bbuild_dbg -DCMAKE_BUILD_TYPE=Debug -DWARNINGS_AS_ERRORS=ON -DBUILD_TESTS=ON
    - name: Run CMake (release)
      run: CC=gcc-8 CXX=g++-8 cmake -H. -Bbuild -DCMAKE_BUILD_TYPE=Release -DWARNINGS_AS_ERRORS=ON -DBUILD_TESTS=ON -DBUILD_BENCHMARKS=ON
    - name: Build (debug)
      run: cd build_dbg && make -j2
    - name: Build (release)
      run: cd build && make -j2
    - name: Test (debug)
      run: cd build_dbg && ctest
    - name: Test (release)
      run: cd build && ctest
  build_osx:
    runs-on: macos-latest
    steps:
    - uses: actions/checkout@v3
      with:
        submodules: recursive
    - name: Install dependencies
      run: brew install sdl2 sdl2_mixer
    - name: Run CMake (debug)
      run: cmake -H. -Bbuild_dbg -DCMAKE_BUILD_TYPE=Debug -DWARNINGS_AS_ERRORS=ON -DBUILD_TESTS=ON
    - name: Run CMake (release)
      run: cmake -H. -Bbuild -DCMAKE_BUILD_TYPE=Release -DWARNINGS_AS_ERRORS=ON -DBUILD_TESTS=ON -DBUILD_BENCHMARKS=ON
    - name: Build (debug)
      run: cd build_dbg && make -j2
    - name: Build (release)
      run: cd build && make -j2
    - name: Test (debug)
      run: cd build_dbg && ctest
    - name: Test (release)
      run: cd build_dbg && ctest
