name: Build (Haiku @QEMU)

on:
  # push:  # Disabled ci on push because of long build times
  pull_request:
    paths:
      - '**/haiku/*'
      - '.github/workflows/haiku.yml'
  workflow_dispatch:

jobs:
  haiku:
    runs-on: ubuntu-latest
    name: Haiku
    container: docker.io/hectorm/qemu-haiku:latest
    steps:
    - name: Wait until the VM is ready
      run: |
        container-init & timeout 600 vmshell exit 0
    - name: Setup Haiku dependencies
      run: |
        vmshell pkgman install -y cmd:cmake cmd:gcc cmd:ld cmd:ninja cmd:pkg_config haiku_devel devel:libgl devel:libglu
    - name: Restart VM
      run: |
        sv force-restart qemu || true
        timeout 600 vmshell exit 0
    - uses: actions/checkout@v3
    - name: Copy project to VM
      run: |
        vmshell mkdir ./src/
        tar -cf - ./ | vmshell tar -xf - -C ./src/
    - name: Configure (CMake)
      run: |
        vmshell cmake -S src -B build -GNinja \
          -Wdeprecated -Wdev -Werror \
          -DSDL_SHARED=ON \
          -DSDL_STATIC=ON \
          -DSDL_TESTS=ON \
          -DSDL_WERROR=ON \
          -DSDL_INSTALL_TESTS=ON \
          -DSDL_VENDOR_INFO=Github_Workflow \
          -DCMAKE_INSTALL_PREFIX=cmake_prefix \
          -DCMAKE_BUILD_TYPE=Release \
          -DCMAKE_DISABLE_PRECOMPILE_HEADERS=ON \
          -DSDL_TESTS_TIMEOUT_MULTIPLIER=10
    - name: Build (CMake)
      run: |
        vmshell cmake --build ./build/ --config Release --verbose --parallel
    - name: Run build-time tests (CMake)
      run: |
        vmshell ctest -VV --test-dir build/
    - name: Install (CMake)
      run: |
        vmshell cmake --install ./build/ --config Release
    - name: Package (CPack)
      run: |
        vmshell cmake --build build/ --config Release --target package
    - name: Verify CMake configuration files
      run: |
        vmshell cmake -S ./src/cmake/test -B cmake_config_build -G Ninja \
          -DCMAKE_BUILD_TYPE=Release \
          -DCMAKE_PREFIX_PATH=\$PWD/cmake_prefix
        vmshell cmake --build ./cmake_config_build --verbose
    - name: Verify sdl3.pc
      run: |
        vmshell CC=c++ PKG_CONFIG_PATH=\$PWD/cmake_prefix/lib/pkgconfig src/cmake/test/test_pkgconfig.sh
    - name: Copy package from VM
      run: |
        vmshell tar -cf - build/dist/ | tar -xf - -C ./
    - uses: actions/upload-artifact@v3
      with:
        if-no-files-found: error
        name: SDL-haiku
        path: build/dist/SDL3*
