4c75f9e
#!/bin/bash
4c75f9e
set -e
4c75f9e
4c75f9e
# file:///usr/share/doc/clang/html/UsersManual.html#controlling-code-generation
4c75f9e
sanitize=${sanitize:-memory}
4c75f9e
sanitize_other="bounds,bool,enum,null"
4c75f9e
compiler_flags=""
4c75f9e
4c75f9e
build_dir=build-clang-$sanitize
4c75f9e
utils_dir=$(dirname $(readlink -f "$0"))
4c75f9e
src_dir=$utils_dir/..
4c75f9e
steps=(
4c75f9e
    clean
4c75f9e
    cmake
4c75f9e
    make
4c75f9e
    run
4c75f9e
    )
4c75f9e
4c75f9e
if [ -n "$CLANG_ROOT" ]; then
4c75f9e
    export PATH=$CLANG_ROOT:$PATH
4c75f9e
fi
4c75f9e
CXX=${CXX:-clang++}
4c75f9e
4c75f9e
if [ "$sanitize" == "address" ]; then
4c75f9e
    sanitize_other="$sanitize_other,address-full,init-order"
4c75f9e
4c75f9e
    export ASAN_SYMBOLIZER_PATH=${ASAN_SYMBOLIZER_PATH:-$(which llvm-symbolizer)}
4c75f9e
    export ASAN_OPTIONS=${ASAN_OPTIONS:-"detect_leaks=1 detect_stack_use_after_return=1 print_stats=1"}
4c75f9e
elif [ "$sanitize" == "memory" ]; then
4c75f9e
    compiler_flags="$compiler_flags -fsanitize-memory-track-origins"
4c75f9e
4c75f9e
    # from https://llvm.org/svn/llvm-project/compiler-rt/trunk/lib/msan/msan.cc
4c75f9e
    export MSAN_SYMBOLIZER_PATH=${MSAN_SYMBOLIZER_PATH:-$(which llvm-symbolizer)}
4c75f9e
    export MSAN_OPTIONS=${MSAN_OPTIONS:-"keep_going=1 halt_on_error=0 report_umrs=0"}
4c75f9e
elif [ "$sanitize" == "thread" ]; then
4c75f9e
    echo
4c75f9e
fi
4c75f9e
4c75f9e
echo "=== Using $sanitize sanitizer (directory \"$build_dir\") ==="
4c75f9e
4c75f9e
step=2
4c75f9e
if [ -d "$build_dir" ]; then
4c75f9e
    echo "Select starting point:"
4c75f9e
    select step in "${steps[@]}"; do step=$REPLY; break; done
4c75f9e
fi
4c75f9e
4c75f9e
has_step () {
4c75f9e
    [[ $step -le $1 ]]
4c75f9e
}
4c75f9e
4c75f9e
has_step "${#steps[*]}"
4c75f9e
4c75f9e
if has_step 1; then
4c75f9e
    rm -r "$build_dir" || exit 1
4c75f9e
fi
4c75f9e
4c75f9e
mkdir -p "$build_dir"
4c75f9e
cd "$build_dir"
4c75f9e
4c75f9e
if has_step 2; then
4c75f9e
    cmake \
4c75f9e
        -DCMAKE_INSTALL_PREFIX="$PWD/install" \
4c75f9e
        -DCMAKE_CXX_COMPILER="$CXX" \
4c75f9e
        -DCMAKE_CXX_FLAGS="-fsanitize=$sanitize,$sanitize_other $compiler_flags -fno-omit-frame-pointer" \
4c75f9e
        -DCMAKE_BUILD_TYPE=Debug \
4c75f9e
        "$src_dir"
4c75f9e
fi
4c75f9e
4c75f9e
if has_step 3; then
4c75f9e
    make -j4 install
4c75f9e
fi
4c75f9e
4c75f9e
if has_step 4; then
4c75f9e
    ./copyq -s test1 "$@"
4c75f9e
fi
4c75f9e