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