Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
# Run clang static analyzer on magenta.
# Usage:
# To build a single target, set the TARGET variable:
# TARGET=build-magenta-pc-x86-64/dev/hw_rng/hw_rng.mod.o ./
set -e
export RESULT_DIR=.result-xtu
rm -rf ${RESULT_DIR}
rm -rf .xtu
# Location of toolchain
export JOBS="${JOBS:-$(grep processor < /proc/cpuinfo | wc -l)}"
export PATH="$TC/bin:$PATH"
export PATH="$TC/libexec:$PATH"
export OUT_FILE="xtu-out/$(date -Iseconds).log"
for tool in objcopy nm ld strip readelf; do
cp "$BINUTILS/x86_64-elf-$tool" "$TC/bin/$tool"
# and other xtu-* tools need this
export CLANG_PATH="$TC/bin"
# ccc-analyzer needs this
export CCC_CC="$TC/bin/clang"
export CCC_CXX="$TC/bin/clang"
export XTU_SCRIPT_DIR=~/doc/llvm/tools/clang/tools/xtu-build-new
export CLANGIFIED_COMPILE_DATABASE=clang_compile_commands.json
# Sanity checks
for tool in clang ld ccc-analyzer; do
LOC=`which "$tool"`
if [ "$?" != "0" ]; then
(>&2 echo "Unable to find path to $tool");
exit 1
(>&2 echo "Using $tool from $LOC")
echo >$OUT_FILE
export TARGET="${TARGET:-magenta-pc-x86-64}"
echo "Building target $TARGET"
# Initially, run the build process as usual, but wrapped up in bear to
# generate a compile_commands.json file.
bear make \
USE_CLANG=true \
LIBGCC=$TC/lib/clang/4.0.0/lib/linux/libclang_rt.builtins-x86_64.a \
V=1 \
-j ${JOBS} $TARGET 2>&1 | tee --append $OUT_FILE
if [ "$?" != "0" ]; then
(>&2 echo Build of magenta failed)
(echo Build of magenta failed >> $OUT_FILE)
exit 1
# Bear is seeing compiler invocations as invocations to "cc" rather than
# "clang" for some reason. This is bad, since if we re-play those
# invocations we'll end up being redirected to GCC. Replace occurrences
# of "cc" with the path to clang in compile_commands.json
./ --clang-path "$TC/bin/clang" > $CLANGIFIED_COMPILE_DATABASE
(>&2 echo Running xtu-build with compilation database)
(echo Running xtu-build with compilation database >> $OUT_FILE)
--clang-path "$TC/bin" -v -b $CLANGIFIED_COMPILE_DATABASE \
2>&1 | tee --append $OUT_FILE
#(>&2 echo Running xtu-build-graph with compilation database)
#(echo Running xtu-build-graph with compilation database >> $OUT_FILE)
(>&2 echo Running
(echo Running >> $OUT_FILE)
CCC_CC="$TC/bin/clang" \
-b $CLANGIFIED_COMPILE_DATABASE --clang-path="$TC/bin" \
--analyze-cc-path ~/doc/llvm/tools/clang/tools/scan-build-py/bin \
--output-format html \
--reanalyze-xtu-visited \
--timeout 90 \
-o ${RESULT_DIR} \
-j ${JOBS} \
2>&1 | tee --append $OUT_FILE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment