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

name: Build opensource projects

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 (${{ matrix.os }})
    runs-on: ${{ matrix.os }}

    strategy:
      fail-fast: false
      matrix:
        include:
          - os: ubuntu-22.04
            install_deps_args: ""
            artifact_name: ispc-archive
            artifact_path: build/ispc-*-linux.tar.gz
          - os: macos-14
            install_deps_args: macos-14
            artifact_name: ispc-macos-arm
            artifact_path: build/ispc-*-macos.tar.gz

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

    - name: Install ISPC build dependencies
      run: .github/workflows/scripts/install-build-deps.sh ${{ matrix.install_deps_args }}

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

    - name: Upload ISPC artifact
      uses: actions/upload-artifact@b7c566a772e6b6bfb58ed0dc250532a479d7789f # v6.0.0
      with:
        name: ${{ matrix.artifact_name }}
        path: ${{ matrix.artifact_path }}

  build-projects:
    name: Build ${{ matrix.project.name }}
    needs: build-ispc
    runs-on: ubuntu-22.04
    container:
      image: archlinux@sha256:2fcdd55448255f87dd337ef5c0fdbd5e4fec432345db1aa5faea4bf2764b4ca8

    strategy:
      fail-fast: false
      matrix:
        project:
          - name: hfisher/nbody
            extra_packages: mercurial glfw
            build_cmd: |
              hg clone https://hg.sr.ht/~hfisher/nbody
              cd nbody
              sed -i 's/BUILD_ISPC 0/BUILD_ISPC 1/' CMakeLists.txt
              hg diff
              cmake -S ./ -B build -G Ninja
              cmake --build build

          - name: ISPCTextureCompressor
            build_cmd: |
              git clone https://github.com/GameTechDev/ISPCTextureCompressor.git
              cd ISPCTextureCompressor/
              make -f Makefile.linux ISPC_ARCH=x86_64 ISPC_TARGETS=sse2,avx2,avx512skx-x16 ISPC=/usr/local/bin/ispc

          - name: GPSnoopy/RayTracingInOneWeekend
            build_cmd: |
              git clone https://github.com/GPSnoopy/RayTracingInOneWeekend.git
              cmake -G Ninja -B build -S RayTracingInOneWeekend/ispc
              cmake --build build --verbose

          - name: ispc-upwind
            build_cmd: |
              git clone https://gitlab.com/rossinelli/ispc-upwind.git
              cd ispc-upwind/
              make TARGET=sse4,avx2,avx512skx-x16

          - name: FlashAttentionGPTonCPU
            build_cmd: |
              git clone https://github.com/ZipXuan/FlashAttentionGPTonCPU.git
              cd FlashAttentionGPTonCPU/
              ispc -O3 --target=avx2-i32x8 --arch=x86-64 --pic module.ispc -h module_ispc.h -o module_ispc.o

          - name: merle
            extra_packages: libx11 libxext
            build_cmd: |
              # Pinned to commit before VCPKG migration (fbfbdb7f12fb79a3280c45ea12ea81fa824a682f)
              # VCPKG builds are too complex/fragile for CI (deep dependency chains, libsystemd build failures)
              # This version uses git submodules and builds reliably
              git clone --recursive https://github.com/chinmaygarde/merle.git
              cd merle/
              git checkout 3eecb311ac8862c41f0c53a5d9b360be923142bb
              git submodule update --init --recursive
              sed -i '/add_custom_command(/,/DEPENDS src\/texture.ispc/ s/--werror/--werror\n --pic/' CMakeLists.txt
              git diff
              cmake -B build -S ./ -G Ninja
              cmake --build build

    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 cmake ninja ${{ matrix.project.extra_packages }}

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

    - name: Install 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"/* /usr/local/
        chmod +x /usr/local/bin/ispc
        ispc --version

    - name: Build ${{ matrix.project.name }}
      run: ${{ matrix.project.build_cmd }}

  build-projects-with-builder:
    name: Build ${{ matrix.project.name }}
    needs: build-ispc
    runs-on: ubuntu-22.04
    container:
      image: archlinux@sha256:2fcdd55448255f87dd337ef5c0fdbd5e4fec432345db1aa5faea4bf2764b4ca8

    strategy:
      fail-fast: false
      matrix:
        project:
          - name: Leela Chess lc0
            extra_packages: gtest wget
            build_cmd: |
              su - builder -c '
                git clone --branch lc0 --single-branch https://github.com/archlinux/aur.git lc0
                cd lc0
                # Temporary fix to use unofficial download location for weights
                export SRCDEST=$HOME/pkg_src

                # Make the directory if needed
                mkdir -p "$SRCDEST"

                # Download the weights file to SRCDEST if not present
                # Filename must match _weights variable in PKGBUILD (weights_hanse-69722-vf2.gz)
                weights="weights_hanse-69722-vf2.gz"
                url="https://derr.pro/lc0/hanse-69722-vf2.gz"
                if [ ! -f "$SRCDEST/$weights" ]; then
                  wget -O "$SRCDEST/$weights" "$url"
                fi
                makepkg -s --noconfirm
              '

          # Open3D disabled because open-source default runners don't have enough disk space
          # - name: Open3D
          #   extra_packages: minizip
          #   build_cmd: |
          #     # Build and install the required dependency for Open3D from AUR
          #     su - builder -c "
          #       git clone https://aur.archlinux.org/python-dash.git
          #       cd python-dash/
          #       makepkg -si --noconfirm
          #
          #       git clone https://aur.archlinux.org/nanoflann.git
          #       cd nanoflann
          #       makepkg -si --noconfirm
          #
          #       git clone https://aur.archlinux.org/open3d.git
          #       cd open3d
          #       sed -i 's|-DUSE_SYSTEM_VTK=ON|-DUSE_SYSTEM_VTK=OFF -DISPC_USE_LEGACY_EMULATION=ON -DBUILD_ISPC_MODULE=ON -DCMAKE_ISPC_COMPILER=/usr/local/bin/ispc -DCMAKE_ISPC_INSTRUCTION_SETS=host|' PKGBUILD
          #       git diff
          #       makepkg -s --noconfirm
          #     "

    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 cmake ninja ${{ matrix.project.extra_packages }}

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

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

    - name: Install 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"/* /usr/local/
        chmod +x /usr/local/bin/ispc
        ispc --version

    - name: Build ${{ matrix.project.name }}
      run: ${{ matrix.project.build_cmd }}

  build-ispc-downsampler:
    name: Build ispc-downsampler on macOS ARM
    needs: build-ispc
    runs-on: macos-14

    steps:
    - name: Install Rust
      uses: dtolnay/rust-toolchain@4be9e76fd7c4901c61fb841f559994984270fce7 # stable

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

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

    - name: Checkout ispc-downsampler
      uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3 # v6.0.0
      with:
        repository: Traverse-Research/ispc-downsampler
        path: ispc-downsampler

    - name: Build and benchmark ispc-downsampler
      run: |
        source "$HOME/.cargo/env"
        cd ispc-downsampler
        cargo clean
        cargo build --features ispc
        cargo bench
