# Copyright 2025-2026, Intel Corporation
# SPDX-License-Identifier: BSD-3-Clause

name: Build OpenMoonRay

permissions: read-all

on:
  pull_request:
    paths-ignore:
      - 'docs/**'
      - '*.md'
      - 'snap/**'
      - 'benchmarks/**'
      - 'tests/**'
      - 'examples/**'
      - 'tools/**'
      - 'utils/**'
  workflow_dispatch:

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

jobs:
  build-ispc:
    name: Build ISPC
    runs-on: ubuntu-22.04

    steps:
    - uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3 # v6.0.0
      with:
        submodules: true

    - name: Install ISPC build dependencies
      run: .github/workflows/scripts/install-build-deps.sh

    - name: Build ISPC
      run: .github/workflows/scripts/build-ispc.sh

    - name: Upload ISPC artifact
      uses: actions/upload-artifact@b7c566a772e6b6bfb58ed0dc250532a479d7789f # v6.0.0
      with:
        name: ispc-fresh-build
        path: build/ispc-*-linux.tar.gz

  build-openmoonray:
    name: Build OpenMoonRay AUR package
    needs: build-ispc
    runs-on: ubuntu-22.04
    container:
      image: archlinux@sha256:2fcdd55448255f87dd337ef5c0fdbd5e4fec432345db1aa5faea4bf2764b4ca8
      options: --privileged

    steps:
    - name: Setup Arch Linux
      run: |
        pacman-key --init
        pacman-key --populate archlinux
        # Update keyring first, then re-populate to trust new signing keys
        pacman -Sy --noconfirm archlinux-keyring
        pacman-key --populate archlinux
        pacman -Su --noconfirm
        pacman -S --needed --noconfirm base-devel git sudo

    - name: Create build user
      run: |
        useradd -m -G wheel -s /bin/bash builder
        echo 'builder ALL=(ALL) NOPASSWD: ALL' >> /etc/sudoers

    - name: Download fresh ISPC
      uses: actions/download-artifact@37930b1c2abaa49bbe596cd826c3c89aef350131 # v7.0.0
      with:
        name: ispc-fresh-build
        path: /tmp/ispc-build

    - name: Install fresh ISPC
      run: |
        cd /tmp/ispc-build
        ISPC_TAR=$(ls ispc-*-linux.tar.gz)
        tar -xzf "$ISPC_TAR"
        ISPC_DIR=$(basename "$ISPC_TAR" .tar.gz)
        cp -r "$ISPC_DIR"/bin/* /usr/local/bin/
        chmod +x /usr/local/bin/ispc
        ispc --version

    # Build and install the required dependency for OpenMoonRay from AUR
    - name: Build and install random123
      run: |
        su - builder -c "
          git clone --branch random123 --single-branch https://github.com/archlinux/aur.git random123
          cd random123
          makepkg -si --noconfirm
        "

    - name: Build OpenMoonRay
      run: |
        su - builder -c "
          git clone --branch openmoonray --single-branch https://github.com/archlinux/aur.git openmoonray
          cd openmoonray

          # Remove ispc dependency since we have fresh build
          sed -i '/ispc/d' PKGBUILD

          # Enable testing in CMAKE configuration
          sed -i 's/-DMOONRAY_BUILD_TESTING=OFF/-DMOONRAY_BUILD_TESTING=ON/' PKGBUILD
          sed -i 's/-DBUILD_TESTING=OFF/-DBUILD_TESTING=ON/' PKGBUILD

          # Build with automatic dependency resolution
          makepkg -s --noconfirm

          # Run tests
          cd src/build
          ctest -L unit -E 'scenerdl2_render_util_tests|scenerdl2_common_grid_util_tests' --output-on-failure
        "
