name: Build on Linux using cross-compiler
on:
  workflow_dispatch:
  workflow_call:

jobs:
  ubuntu-latest-riscv64-cpu-cross:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v4
      - name: Setup Riscv
        run: |
          sudo dpkg --add-architecture riscv64
          sudo sed -i 's|http://azure.archive.ubuntu.com/ubuntu|http://ports.ubuntu.com/ubuntu-ports|g' \
                 /etc/apt/sources.list /etc/apt/apt-mirrors.txt
          sudo apt-get clean
          sudo apt-get update
          sudo apt-get install -y --no-install-recommends \
                  build-essential \
                  gcc-14-riscv64-linux-gnu \
                  g++-14-riscv64-linux-gnu \
                  libcurl4-openssl-dev:riscv64

      - name: Build
        run: |
          cmake -B build -DCMAKE_BUILD_TYPE=Release \
                         -DGGML_OPENMP=OFF \
                         -DLLAMA_BUILD_EXAMPLES=ON \
                         -DLLAMA_BUILD_TESTS=OFF \
                         -DCMAKE_SYSTEM_NAME=Linux \
                         -DCMAKE_SYSTEM_PROCESSOR=riscv64 \
                         -DCMAKE_C_COMPILER=riscv64-linux-gnu-gcc-14 \
                         -DCMAKE_CXX_COMPILER=riscv64-linux-gnu-g++-14 \
                         -DCMAKE_POSITION_INDEPENDENT_CODE=ON \
                         -DCMAKE_FIND_ROOT_PATH=/usr/lib/riscv64-linux-gnu \
                         -DCMAKE_FIND_ROOT_PATH_MODE_PROGRAM=NEVER \
                         -DCMAKE_FIND_ROOT_PATH_MODE_LIBRARY=ONLY \
                         -DCMAKE_FIND_ROOT_PATH_MODE_INCLUDE=BOTH

          cmake --build build --config Release -j $(nproc)

  ubuntu-latest-riscv64-vulkan-cross:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Setup Riscv
        run: |
          sudo dpkg --add-architecture riscv64
          sudo sed -i 's|http://azure.archive.ubuntu.com/ubuntu|http://ports.ubuntu.com/ubuntu-ports|g' \
                 /etc/apt/sources.list /etc/apt/apt-mirrors.txt
          sudo apt-get clean
          sudo apt-get update
          sudo apt-get install -y --no-install-recommends \
                  build-essential \
                  glslc \
                  gcc-14-riscv64-linux-gnu \
                  g++-14-riscv64-linux-gnu \
                  libvulkan-dev:riscv64 \
                  libcurl4-openssl-dev:riscv64

      - name: Build
        run: |
          cmake -B build -DCMAKE_BUILD_TYPE=Release \
                         -DGGML_VULKAN=ON \
                         -DGGML_OPENMP=OFF \
                         -DLLAMA_BUILD_EXAMPLES=ON \
                         -DLLAMA_BUILD_TESTS=OFF \
                         -DCMAKE_SYSTEM_NAME=Linux \
                         -DCMAKE_SYSTEM_PROCESSOR=riscv64 \
                         -DCMAKE_C_COMPILER=riscv64-linux-gnu-gcc-14 \
                         -DCMAKE_CXX_COMPILER=riscv64-linux-gnu-g++-14 \
                         -DCMAKE_POSITION_INDEPENDENT_CODE=ON \
                         -DCMAKE_FIND_ROOT_PATH=/usr/lib/riscv64-linux-gnu \
                         -DCMAKE_FIND_ROOT_PATH_MODE_PROGRAM=NEVER \
                         -DCMAKE_FIND_ROOT_PATH_MODE_LIBRARY=ONLY \
                         -DCMAKE_FIND_ROOT_PATH_MODE_INCLUDE=BOTH

          cmake --build build --config Release -j $(nproc)

  ubuntu-latest-arm64-vulkan-cross:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Setup Arm64
        run: |
          sudo dpkg --add-architecture arm64
          sudo sed -i 's|http://azure.archive.ubuntu.com/ubuntu|http://ports.ubuntu.com/ubuntu-ports|g' \
                 /etc/apt/sources.list /etc/apt/apt-mirrors.txt
          sudo apt-get clean
          sudo apt-get update
          sudo apt-get install -y --no-install-recommends \
                  build-essential \
                  glslc \
                  crossbuild-essential-arm64 \
                  libvulkan-dev:arm64 \
                  libcurl4-openssl-dev:arm64

      - name: Build
        run: |
          cmake -B build -DCMAKE_BUILD_TYPE=Release \
                         -DGGML_VULKAN=ON \
                         -DGGML_OPENMP=OFF \
                         -DLLAMA_BUILD_EXAMPLES=ON \
                         -DLLAMA_BUILD_TESTS=OFF \
                         -DCMAKE_SYSTEM_NAME=Linux \
                         -DCMAKE_SYSTEM_PROCESSOR=aarch64 \
                         -DCMAKE_C_COMPILER=aarch64-linux-gnu-gcc \
                         -DCMAKE_CXX_COMPILER=aarch64-linux-gnu-g++ \
                         -DCMAKE_POSITION_INDEPENDENT_CODE=ON \
                         -DCMAKE_FIND_ROOT_PATH=/usr/lib/aarch64-linux-gnu \
                         -DCMAKE_FIND_ROOT_PATH_MODE_PROGRAM=NEVER \
                         -DCMAKE_FIND_ROOT_PATH_MODE_LIBRARY=ONLY \
                         -DCMAKE_FIND_ROOT_PATH_MODE_INCLUDE=BOTH

          cmake --build build --config Release -j $(nproc)
