1#!/bin/bash
2
3set -u
4set -e
5set -o pipefail
6set -x
7
8jobs="${1:-1}"
9tools_dir="$(dirname "$0")"
10compile_db="$tools_dir/../build/compile_commands.json"
11
12if [ ! -r "$compile_db" ]
13then
14    echo "Expected compilation database to exist at '$compile_db'" >&2
15    exit 1
16fi
17
18for prog in iwyu_tool.py fix_includes.py run-clang-tidy.py
19do
20    if ! which $prog &>/dev/null
21    then
22        echo "You need '$prog' on your PATH" >&2
23        exit 1
24    fi
25done
26
27# Must use absolute paths to mapping file because IWYU changes
28# directory.
29mapping_file="$(realpath "$tools_dir/iwyu/cata.imp")"
30
31function run_iwyu {
32    pass="$1"
33    iwyu_tool.py -j "$jobs" -p "$compile_db" -- \
34        -Xiwyu --mapping_file="$mapping_file" | \
35        tee fixes."$pass" | \
36        ( fix_includes.py --nosafe_headers --reorder || true; )
37}
38
39function run_clang_tidy {
40    run-clang-tidy.py -j "$jobs" -fix -checks='-*,modernize-deprecated-headers'
41}
42
43# First IWYU pass
44run_iwyu 1
45# Make sure we didn't break the build
46make -j "$jobs"
47# Clean up old-style headers that might have been added
48run_clang_tidy
49# Second IWYU pass
50run_iwyu 2
51# Tidy up
52make astyle
53# Make sure we didn't break the build
54make -j "$jobs"
55