Debian Codegraph @

scripts/test/cmake-sanitizers.sh

raw
1 #!/bin/sh
2 #
3 # Copyright (c) 2026 Enrico M. Crisostomo
4 #
5 # This program is free software; you can redistribute it and/or modify it under
6 # the terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 3, or (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful, but WITHOUT
10 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12 # details.
13 #
14 # You should have received a copy of the GNU General Public License along with
15 # this program. If not, see <http://www.gnu.org/licenses/>.
16
17 set -eu
18
19 BUILD_DIR=${BUILD_DIR:-build-sanitize}
20 JOBS=${JOBS:-2}
21 SANITIZERS=${SANITIZERS:-address,undefined}
22 SANITIZER_FLAGS="-g -O1 -fsanitize=${SANITIZERS} -fno-omit-frame-pointer"
23 CXX=${CXX:-c++}
24 export CCACHE_DISABLE=${CCACHE_DISABLE:-1}
25 export ASAN_OPTIONS=${ASAN_OPTIONS:-detect_leaks=1:abort_on_error=1}
26 export UBSAN_OPTIONS=${UBSAN_OPTIONS:-print_stacktrace=1:halt_on_error=1}
27
28 TMPDIR=${TMPDIR:-/tmp}
29 CHECK_DIR=$(mktemp -d "${TMPDIR%/}/fswatch-sanitizer-check.XXXXXX")
30 trap 'rm -rf "${CHECK_DIR}"' EXIT INT TERM
31
32 printf 'int main() { return 0; }\n' > "${CHECK_DIR}/sanitizer-check.cpp"
33 if ! "${CXX}" ${SANITIZER_FLAGS} "${CHECK_DIR}/sanitizer-check.cpp" \
34 -o "${CHECK_DIR}/sanitizer-check" >/dev/null 2>"${CHECK_DIR}/sanitizer-check.err"; then
35 echo "sanitizer compiler/link check failed for: ${SANITIZERS}" >&2
36 sed -n '1,120p' "${CHECK_DIR}/sanitizer-check.err" >&2
37 exit 1
38 fi
39
40 cmake -S . -B "${BUILD_DIR}" \
41 -DBUILD_TESTING=ON \
42 -DCMAKE_BUILD_TYPE=Debug \
43 -DCMAKE_C_FLAGS_DEBUG="${SANITIZER_FLAGS}" \
44 -DCMAKE_CXX_FLAGS_DEBUG="${SANITIZER_FLAGS}" \
45 -DCMAKE_EXE_LINKER_FLAGS_DEBUG="-fsanitize=${SANITIZERS}" \
46 -DCMAKE_SHARED_LINKER_FLAGS_DEBUG="-fsanitize=${SANITIZERS}"
47
48 cmake --build "${BUILD_DIR}" -j "${JOBS}"
49 ctest --test-dir "${BUILD_DIR}" --output-on-failure