Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / scripts / helpers / shellcheck_tree.sh
1 #!/usr/bin/env bash
2
3 #
4 # Copyright (c) 2020 Project CHIP Authors
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 #     http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 #
18
19 me=$(basename "$0")
20
21 # Run shellcheck on all the shell-ish files in the tree
22 patterns=('*.bash*' 'bash*'
23     '*.ksh*' 'ksh*'
24     '*.zsh*' 'zsh*'
25     '.zlogin*' 'zlogin*'
26     '.zlogout*' 'zlogout*'
27     '.zprofile*' 'zprofile*'
28     '*.sh' '.shlib*' 'shlib*'
29     '.profile*' 'profile*'
30     'suid_profile')
31
32 # excluding these directories (e.g. third_party/* )
33 excludes=()
34
35 # shellcheck disable=SC1091
36 [[ -f .shellcheck_tree ]] && . .shellcheck_tree
37
38 [[ ${*/--help//} != "${*}" ]] && {
39     echo "Usage: $me <OPTIONS>
40
41   Shellcheck all scripts in the tree.
42
43   Options:
44    --git      shellcheck via git ls-files, default is 'find .'
45    --help     get this message
46
47 "
48     exit 0
49 }
50
51 if [[ ${*/--git//} != "${*}" ]]; then
52     #
53     #  To run on git-files only
54     #
55     git ls-files -- "${patterns[@]}" | while read -r file; do
56         for exclude in "${excludes[@]}"; do
57             [[ $file =~ $exclude ]] && continue 2
58         done
59         shellcheck -f gcc "$file"
60     done
61 else
62     #
63     # use find
64     #
65     exclude_args=()
66     for exclude in "${excludes[@]}"; do
67         exclude_args+=('!' -path "./$exclude" -a)
68     done
69
70     pattern_args=()
71     for pattern in "${patterns[@]}"; do
72         pattern_args+=(-o -name "$pattern")
73     done
74
75     find . "${exclude_args[@]}" '(' "${pattern_args[@]:1}" ')' -type f -exec shellcheck -f gcc {} +
76 fi