summaryrefslogtreecommitdiff
path: root/scripts/clang-tools.sh
blob: 1e739511768a569bb4447618c721cc57d5155e43 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env bash

set -e
set -o pipefail

CLANG_TIDY_PREFIX=${CLANG_TIDY_PREFIX:-$(scripts/mason.sh PREFIX clang-tidy VERSION 4.0.1)}
CLANG_TIDY=${CLANG_TIDY:-${CLANG_TIDY_PREFIX}/bin/clang-tidy}
CLANG_APPLY=${CLANG_APPLY:-${CLANG_TIDY_PREFIX}/bin/clang-apply-replacements}

CLANG_FORMAT=${CLANG_FORMAT:-$(scripts/mason.sh PREFIX clang-format VERSION 4.0.1)/bin/clang-format}

for CLANG_FILE in "${CLANG_TIDY} ${CLANG_APPLY} ${CLANG_FORMAT}"; do
    command -v ${CLANG_TIDY} > /dev/null 2>&1 || {
        echo "Can't find ${CLANG_FILE} in PATH."
        if [ -z ${CLANG_FILE} ]; then
            echo "Alternatively, you can manually set ${!CLANG_FILE@}."
        fi
        exit 1
    }
done

cd $1

export CDUP=$(git rev-parse --show-cdup)
export CLANG_TIDY CLANG_APPLY CLANG_FORMAT

function run_clang_tidy() {
    FILES=$(git ls-files "src/mbgl/*.cpp" "platform/*.cpp" "test/*.cpp")
    ${CLANG_TIDY_PREFIX}/share/run-clang-tidy.py -j ${JOBS} \
        -clang-tidy-binary ${CLANG_TIDY} \
        -clang-apply-replacements-binary ${CLANG_APPLY} \
        -fix ${FILES} 2>/dev/null || exit 1
}

function run_clang_tidy_diff() {
    OUTPUT=$(git diff origin/$2 --src-prefix=${CDUP} --dst-prefix=${CDUP} | \
                ${CLANG_TIDY_PREFIX}/share/clang-tidy-diff.py \
                    -clang-tidy-binary ${CLANG_TIDY} \
                    2>/dev/null)
    if [[ -n $OUTPUT ]] && [[ $OUTPUT != "No relevant changes found." ]]; then
        echo -e "${OUTPUT}"
        exit 1
    fi
}

function run_clang_format() {
    echo "Running clang-format on $0..."
    DIFF_FILES=$(git diff origin/$2 --name-only *cpp)
    echo "${DIFF_FILES}" | xargs -I{} -P ${JOBS} bash -c 'run_clang_format' {}
    ${CLANG_FORMAT} -i ${CDUP}/$0 || exit 1
}

export -f run_clang_tidy run_clang_tidy_diff run_clang_format

echo "Running Clang checks... (this might take a while)"

if [[ -n $3 ]] && [[ $3 == "--diff" ]]; then
    run_clang_tidy_diff $@
    # XXX disabled until we run clang-format over the entire codebase.
    #run_clang_format $@
    echo "All checks pass!"
else
    run_clang_tidy $@
fi