#!/bin/sh

LLVM_SOURCE_DIR=$Y_WORKSPACE_ROOT/userspace/target/ports/meta-llvm/source/llvm-project-${Y_PORT_VERSION}.src
CMAKE_TOOLCHAIN=$Y_PORT_UTILS/toolchain-${Y_ARCH}.cmake

set -e

build_dir=$3

mkdir -p $build_dir
cd $build_dir
if [ ! -f build.ninja ]; then
    cmake \
        --toolchain=$CMAKE_TOOLCHAIN \
        -GNinja \
        -DCMAKE_BUILD_TYPE=RelWithDebInfo \
        -DLLVM_ENABLE_RUNTIMES="libcxxabi;libcxx" \
        -DLIBCXXABI_ENABLE_STATIC=ON \
        -DLIBCXXABI_ENABLE_SHARED=OFF \
        -DLIBCXXABI_USE_COMPILER_RT=ON \
        -DLIBCXXABI_ENABLE_THREADS=ON \
        -DLIBCXXABI_ENABLE_EXCEPTIONS=OFF \
        -DLIBCXXABI_USE_LLVM_UNWINDER=OFF \
        -DLIBCXX_CXX_ABI=libcxxabi \
        -DLIBCXX_STATICALLY_LINK_ABI_IN_STATIC_LIBRARY=ON \
        -DLIBCXX_STATICALLY_LINK_ABI_IN_SHARED_LIBRARY=ON \
        -DLIBCXX_ENABLE_STATIC_ABI_LIBRARY=ON \
        -DLIBCXX_ENABLE_FILESYSTEM=ON \
        -DLIBCXX_ENABLE_THREADS=ON \
        -DLIBCXX_ENABLE_EXCEPTIONS=OFF \
        -DLIBCXX_ENABLE_SHARED=ON \
        -DLIBCXX_ENABLE_STATIC=ON \
        -DLIBCXX_INCLUDE_BENCHMARKS=OFF \
        -DCMAKE_EXE_LINKER_FLAGS=-nostdlib \
        -DCMAKE_SYSROOT=$Y_SYSROOT \
        -DCMAKE_INSTALL_PREFIX=$Y_SYSROOT \
        -DLLVM_DEFAULT_TARGET_TRIPLE=$Y_TRIPLE \
        $LLVM_SOURCE_DIR/runtimes
fi

cmake --build . -j
