1#!/bin/sh
2# Run clang's static analyzer (scan-build) and record its output in output-scan-build/
3
4# Ensure the current directory is where this script is
5cd "$(dirname -- "$0")" || exit $?
6
7OUTPUTDIR="$(pwd)/output-scan-build"
8
9# Display the commands which are run, and make sure they succeed
10set -x -e
11
12# Use a temporary directory as an installation directory, if $DESTDIR is not set
13if [ -z "$DESTDIR" ] ; then
14    DESTDIR="$(mktemp --tmpdir -d scan-build-destdir-XXXXXXXXXX)"
15fi
16
17# Make sure to use the newly-installed libraries when running tests
18export LD_LIBRARY_PATH="$DESTDIR/usr/lib:$DESTDIR/lib"
19export PATH="$DESTDIR/usr/sbin:$DESTDIR/usr/bin:$DESTDIR/sbin:$DESTDIR/bin:$PATH"
20export PYTHONPATH="$DESTDIR$(${PYTHON:-python3} -c "from distutils.sysconfig import *;print(get_python_lib(prefix='/usr'))")"
21export RUBYLIB="$DESTDIR/$(${RUBY:-ruby} -e 'puts RbConfig::CONFIG["vendorlibdir"]'):$DESTDIR/$(${RUBY:-ruby} -e 'puts RbConfig::CONFIG["vendorarchdir"]')"
22
23# Build and analyze
24make -C .. CC=clang clean distclean -j"$(nproc)"
25scan-build -analyze-headers -o "$OUTPUTDIR" make -C .. \
26    CC=clang \
27    DESTDIR="$DESTDIR" \
28    CFLAGS="-O2 -Wall -D__CHECKER__ -I$DESTDIR/usr/include" \
29    install install-pywrap install-rubywrap all test
30
31# Reduce the verbosity in order to keep the message from scan-build saying
32# "scan-build: Run 'scan-view /.../output-scan-build/2018-...' to examine bug reports.
33set +x
34
35# Remove the destination directory without using "rm -rf"
36chmod u+w "$DESTDIR/usr/bin/newrole"
37rm -r "$DESTDIR"
38