blob: 560ccb692bb119999c373f120e5b731f5eb1efb1 [file] [log] [blame]
MÃ¥rten Kongstad02751232018-04-27 13:16:32 +02001#!/bin/bash
2#
3# Copyright (C) 2018 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
17
18function _log()
19{
20 echo -e "$*" >&2
21}
22
23function _eval()
24{
25 local label="$1"
26 local cmd="$2"
27 local red="\e[31m"
28 local green="\e[32m"
29 local reset="\e[0m"
30
31 _log "${green}[ RUN ]${reset} ${label}"
32 local output="$(eval "$cmd")"
33 if [[ -z "${output}" ]]; then
34 _log "${green}[ OK ]${reset} ${label}"
35 return 0
36 else
37 echo "${output}"
38 _log "${red}[ FAILED ]${reset} ${label}"
39 errors=$((errors + 1))
40 return 1
41 fi
42}
43
44function _clang_format()
45{
46 local path
47 local errors=0
48
49 for path in $cpp_files; do
50 local output="$(clang-format -style=file "$path" | diff $path -)"
51 if [[ "$output" ]]; then
52 echo "$path"
53 echo "$output"
54 errors=1
55 fi
56 done
57 return $errors
58}
59
60function _bpfmt()
61{
62 local output="$(bpfmt -s -d $bp_files)"
63 if [[ "$output" ]]; then
64 echo "$output"
65 return 1
66 fi
67 return 0
68}
69
70function _cpplint()
71{
72 local cpplint="${ANDROID_BUILD_TOP}/tools/repohooks/tools/cpplint.py"
73 $cpplint --quiet $cpp_files
74}
75
76function _parse_args()
77{
78 local opts
79
80 opts="$(getopt -o cfh --long check,fix,help -- "$@")"
81 if [[ $? -ne 0 ]]; then
82 exit 1
83 fi
84 eval set -- "$opts"
85 while true; do
86 case "$1" in
87 -c|--check) opt_mode="check"; shift ;;
88 -f|--fix) opt_mode="fix"; shift ;;
89 -h|--help) opt_mode="help"; shift ;;
90 *) break ;;
91 esac
92 done
93}
94
95errors=0
96script="$(readlink -f "$BASH_SOURCE")"
97prefix="$(dirname "$script")"
98cpp_files="$(find "$prefix" -name '*.cpp' -or -name '*.h')"
99bp_files="$(find "$prefix" -name 'Android.bp')"
100opt_mode="check"
101
102_parse_args "$@"
103if [[ $opt_mode == "check" ]]; then
104 _eval "clang-format" "_clang_format"
105 _eval "bpfmt" "_bpfmt"
106 _eval "cpplint" "_cpplint"
107 exit $errors
108elif [[ $opt_mode == "fix" ]]; then
109 clang-format -style=file -i $cpp_files
110 bpfmt -s -w $bp_files
111 exit 0
112elif [[ $opt_mode == "help" ]]; then
113 echo "Run static analysis tools such as clang-format and cpplint on the idmap2"
114 echo "module. Optionally fix some of the issues found (--fix). Intended to be run"
115 echo "before merging any changes."
116 echo
117 echo "usage: $(basename $script) [--check|--fix|--help]"
118 exit 0
119else
120 exit 1
121fi