Skip to content

Instantly share code, notes, and snippets.

@karkhaz
Created December 1, 2016 22:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save karkhaz/27524ea8e9d85c110320951f57d75852 to your computer and use it in GitHub Desktop.
Save karkhaz/27524ea8e9d85c110320951f57d75852 to your computer and use it in GitHub Desktop.
#!/bin/sh
#
# Run clang static analyzer on magenta.
#
# Usage:
# run.sh
#
# To build a single target, set the TARGET variable:
# TARGET=build-magenta-pc-x86-64/dev/hw_rng/hw_rng.mod.o ./run.sh
set -e
export RESULT_DIR=.result-xtu
rm -rf ${RESULT_DIR}
rm -rf .xtu
# Location of toolchain
TC=~/doc/build
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"
BINUTILS=$(pwd)/prebuilt/downloads/x86_64-elf-6.2.0-Linux-x86_64/bin
for tool in objcopy nm ld ld.gold strip readelf; do
cp "$BINUTILS/x86_64-elf-$tool" "$TC/bin/$tool"
done
# strace_interceptor.py 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
fi
(>&2 echo "Using $tool from $LOC")
done
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 \
TOOLCHAIN_PREFIX=$TC/bin/ \
ARCH_x86_64_TOOLCHAIN_PREFIX=$TC/bin/ \
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
fi
# 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
./clangify.py --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)
"$XTU_SCRIPT_DIR/xtu-build.py" \
--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)
#"$XTU_SCRIPT_DIR/xtu-build-graph.py" \
# -b $(pwd)/$CLANGIFIED_COMPILE_DATABASE \
(>&2 echo Running xtu-analyze.py)
(echo Running xtu-analyze.py >> $OUT_FILE)
CCC_CC="$TC/bin/clang" \
"$XTU_SCRIPT_DIR/xtu-analyze.py" \
-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