#!/bin/sh

set -e

source_dir=$2

src=$source_dir/llvm-project-${Y_PORT_VERSION}.src
build_dir=${Y_WORKSPACE_ROOT}/userspace/target/ports/meta-llvm

mkdir -p $build_dir
cd $build_dir

# Configure
if command -v ccache >/dev/null; then
    EXTRA_OPTS="-DLLVM_CCACHE_BUILD=true"
fi

if [ ! -f build.ninja ]; then
    cmake \
        -DCMAKE_BUILD_TYPE=Release \
        -DCMAKE_C_COMPILER=/usr/bin/clang \
        -DCMAKE_CXX_COMPILER=/usr/bin/clang++ \
        -DBUILD_SHARED_LIBS=false \
        -DLLVM_ENABLE_PROJECTS="clang;lld;compiler-rt" \
        -DLLVM_INCLUDE_BENCHMARKS=false \
        -DLLVM_INCLUDE_TESTS=false \
        -DLLVM_USE_LINKER=lld \
        -DLLVM_BUILD_TOOLS=true \
        -DLLVM_TARGETS_TO_BUILD="X86;AArch64;RISCV" \
        -DCMAKE_INSTALL_PREFIX=$Y_HOST_PREFIX \
        $EXTRA_OPTS \
        -GNinja \
        $src/llvm
fi

cmake --build . -j
