#!/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

build_dir=$3

set -e

mkdir -p $build_dir
cd $build_dir
if [ ! -f build.ninja ]; then
    cmake \
        --toolchain=$CMAKE_TOOLCHAIN \
        -DCOMPILER_RT_BUILD_BUILTINS=ON \
        -DCOMPILER_RT_BUILD_LIBFUZZER=OFF \
        -DCOMPILER_RT_BUILD_MEMPROF=OFF \
        -DCOMPILER_RT_BUILD_PROFILE=OFF \
        -DCOMPILER_RT_BUILD_CTX_PROFILE=OFF \
        -DCOMPILER_RT_BUILD_SANITIZERS=OFF \
        -DCOMPILER_RT_BUILD_XRAY=OFF \
        -DCOMPILER_RT_BUILD_STANDALONE_LIBATOMIC=ON \
        -DCOMPILER_RT_DEFAULT_TARGET_ONLY=ON \
        -DCOMPILER_RT_BAREMETAL_BUILD=ON \
        -DCOMPILER_RT_BUILTINS_ENABLE_PIC=ON \
        -DCOMPILER_RT_BUILTINS_HIDE_SYMBOLS=OFF \
        -DDEFAULT_COMPILER_RT_USE_BUILTINS_LIBRARY=ON \
        -DCMAKE_EXE_LINKER_FLAGS=-nostdlib \
        -DCMAKE_SYSROOT=${Y_SYSROOT} \
        -DCMAKE_INSTALL_PREFIX=${Y_SYSROOT} \
        -GNinja \
        $LLVM_SOURCE_DIR/compiler-rt
fi

cmake --build . -j
