• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/bash
2#
3#  Copyright (c) 2019, The OpenThread Authors.
4#  All rights reserved.
5#
6#  Redistribution and use in source and binary forms, with or without
7#  modification, are permitted provided that the following conditions are met:
8#  1. Redistributions of source code must retain the above copyright
9#     notice, this list of conditions and the following disclaimer.
10#  2. Redistributions in binary form must reproduce the above copyright
11#     notice, this list of conditions and the following disclaimer in the
12#     documentation and/or other materials provided with the distribution.
13#  3. Neither the name of the copyright holder nor the
14#     names of its contributors may be used to endorse or promote products
15#     derived from this software without specific prior written permission.
16#
17#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18#  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19#  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20#  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21#  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22#  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23#  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24#  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25#  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26#  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27#  POSSIBILITY OF SUCH DAMAGE.
28#
29
30#
31# The script to check or format source code of OpenThread.
32#
33# Format c/c++, markdown, python, and shell:
34#
35#     script/make-pretty
36#
37# Format c/c++ only:
38#
39#     script/make-pretty clang
40#     script/make-pretty clang-format
41#     script/make-pretty clang-tidy
42#
43# Format markdown only:
44#
45#     script/make-pretty markdown
46#
47# Format python only:
48#
49#     script/make-pretty python
50#
51# Format shell only:
52#
53#     script/make-pretty shell
54#
55# Check only:
56#
57#     script/make-pretty check clang
58#     script/make-pretty check clang-format
59#     script/make-pretty check clang-tidy
60#     script/make-pretty check markdown
61#     script/make-pretty check python
62#     script/make-pretty check shell
63#
64
65set -euo pipefail
66
67readonly OT_BUILD_JOBS=$(getconf _NPROCESSORS_ONLN)
68readonly OT_EXCLUDE_DIRS=(third_party doc/site)
69
70readonly OT_CLANG_SOURCES=('*.c' '*.cc' '*.cpp' '*.h' '*.hpp')
71readonly OT_MARKDOWN_SOURCES=('*.md')
72readonly OT_PYTHON_SOURCES=('*.py')
73
74readonly OT_CLANG_TIDY_FIX_DIRS=('examples' 'include' 'src' 'tests')
75readonly OT_CLANG_TIDY_BUILD_OPTS=(
76    '-DCMAKE_EXPORT_COMPILE_COMMANDS=ON'
77    '-DOT_ANYCAST_LOCATOR=ON'
78    '-DOT_APP_RCP=OFF'
79    '-DOT_MTD=OFF'
80    '-DOT_RCP=OFF'
81    '-DOT_PLATFORM=simulation'
82    '-DOT_BACKBONE_ROUTER=ON'
83    '-DOT_BORDER_AGENT=ON'
84    '-DOT_BORDER_ROUTER=ON'
85    '-DOT_BORDER_ROUTING=ON'
86    '-DOT_BORDER_ROUTING_NAT64=ON'
87    '-DOT_CHANNEL_MANAGER=ON'
88    '-DOT_CHANNEL_MONITOR=ON'
89    '-DOT_CHILD_SUPERVISION=ON'
90    '-DOT_COAP=ON'
91    '-DOT_COAP_BLOCK=ON'
92    '-DOT_COAP_OBSERVE=ON'
93    '-DOT_COAPS=ON'
94    '-DOT_COMMISSIONER=ON'
95    '-DOT_CSL_RECEIVER=ON'
96    '-DOT_DATASET_UPDATER=ON'
97    '-DOT_DHCP6_CLIENT=ON'
98    '-DOT_DHCP6_SERVER=ON'
99    '-DOT_DIAGNOSTIC=ON'
100    '-DOT_DNS_CLIENT=ON'
101    '-DOT_DNS_DSO=ON'
102    '-DOT_DNSSD_SERVER=ON'
103    '-DOT_DUA=ON'
104    '-DOT_MLR=ON'
105    '-DOT_ECDSA=ON'
106    '-DOT_HISTORY_TRACKER=ON'
107    '-DOT_IP6_FRAGM=ON'
108    '-DOT_JAM_DETECTION=ON'
109    '-DOT_JOINER=ON'
110    '-DOT_LEGACY=ON'
111    '-DOT_LINK_RAW=ON'
112    '-DOT_LINK_METRICS_INITIATOR=ON'
113    '-DOT_LINK_METRICS_SUBJECT=ON'
114    '-DOT_MAC_FILTER=ON'
115    '-DOT_MTD_NETDIAG=ON'
116    '-DOT_NETDATA_PUBLISHER=ON'
117    '-DOT_PING_SENDER=ON'
118    '-DOT_REFERENCE_DEVICE=ON'
119    '-DOT_SERVICE=ON'
120    '-DOT_SLAAC=ON'
121    '-DOT_SNTP_CLIENT=ON'
122    '-DOT_SRP_CLIENT=ON'
123    '-DOT_SRP_SERVER=ON'
124    '-DOT_THREAD_VERSION=1.3'
125    '-DOT_TREL=ON'
126    '-DOT_COVERAGE=ON'
127    '-DOT_LOG_LEVEL_DYNAMIC=ON'
128    '-DOT_COMPILE_WARNING_AS_ERROR=ON'
129    '-DOT_UPTIME=ON'
130)
131
132readonly OT_CLANG_TIDY_CHECKS="\
133-*,\
134google-explicit-constructor,\
135google-readability-casting,\
136misc-unused-using-decls,\
137modernize-use-bool-literals,\
138modernize-use-equals-default,\
139modernize-use-equals-delete,\
140modernize-use-nullptr,\
141readability-avoid-const-params-in-decls,\
142readability-else-after-return,\
143readability-inconsistent-declaration-parameter-name,\
144readability-make-member-function-const,\
145readability-redundant-member-init,\
146readability-simplify-boolean-expr,\
147readability-static-accessed-through-instance,\
148"
149
150#performance-for-range-copy\
151
152do_clang_format()
153{
154    echo -e '========================================'
155    echo -e '     format c/c++ (clang-format)'
156    echo -e '========================================'
157
158    git ls-files "${OT_CLANG_SOURCES[@]}" | grep -v -E "^($(echo "${OT_EXCLUDE_DIRS[@]}" | tr ' ' '|'))" \
159        | xargs -n3 -P"$OT_BUILD_JOBS" script/clang-format -style=file -i -verbose
160}
161
162do_clang_format_check()
163{
164    echo -e '========================================'
165    echo -e '     check c/c++ (clang-format)'
166    echo -e '========================================'
167
168    git ls-files "${OT_CLANG_SOURCES[@]}" | grep -v -E "^($(echo "${OT_EXCLUDE_DIRS[@]}" | tr ' ' '|'))" \
169        | xargs -n3 -P"$OT_BUILD_JOBS" script/clang-format-check
170}
171
172do_clang_tidy_fix()
173{
174    echo -e '========================================'
175    echo -e '     format c/c++ (clang-tidy)'
176    echo -e '========================================'
177
178    (mkdir -p ./build/cmake-tidy \
179        && cd ./build/cmake-tidy \
180        && THREAD_VERSION=1.3 cmake "${OT_CLANG_TIDY_BUILD_OPTS[@]}" ../.. \
181        && ../../script/clang-tidy -header-filter='.*' -checks="${OT_CLANG_TIDY_CHECKS}" -j"$OT_BUILD_JOBS" "${OT_CLANG_TIDY_FIX_DIRS[@]}" -fix)
182}
183
184do_clang_tidy_check()
185{
186    echo -e '========================================'
187    echo -e '     check c/c++ (clang-tidy)'
188    echo -e '========================================'
189
190    (
191        mkdir -p ./build/cmake-tidy \
192            && cd ./build/cmake-tidy \
193            && THREAD_VERSION=1.3 cmake "${OT_CLANG_TIDY_BUILD_OPTS[@]}" ../.. \
194            && ../../script/clang-tidy -header-filter='.*' -checks="${OT_CLANG_TIDY_CHECKS}" -j"$OT_BUILD_JOBS" "${OT_CLANG_TIDY_FIX_DIRS[@]}" \
195            | grep -v -E "third_party" >output.txt
196        if grep -q "warning: \|error: " output.txt; then
197            echo "You must pass the clang tidy checks before submitting a pull request"
198            echo ""
199            grep --color -E 'warning: |error: ' -A 5 output.txt
200            exit 1
201        fi
202    )
203}
204
205do_markdown_format()
206{
207    echo -e '========================================'
208    echo -e '     format markdown'
209    echo -e '========================================'
210
211    git ls-files "${OT_MARKDOWN_SOURCES[@]}" | grep -v -E "^($(echo "${OT_EXCLUDE_DIRS[@]}" | tr ' ' '|'))" \
212        | xargs -n10 -P"$OT_BUILD_JOBS" npx prettier@2.0.4 --write
213}
214
215do_markdown_check()
216{
217    echo -e '========================================'
218    echo -e '     check markdown'
219    echo -e '========================================'
220
221    git ls-files "${OT_MARKDOWN_SOURCES[@]}" | grep -v -E "^($(echo "${OT_EXCLUDE_DIRS[@]}" | tr ' ' '|'))" \
222        | xargs -n10 -P"$OT_BUILD_JOBS" npx prettier@2.0.4 --check
223}
224
225do_python_format()
226{
227    echo -e '========================================'
228    echo -e '     format python'
229    echo -e '========================================'
230
231    git ls-files "${OT_PYTHON_SOURCES[@]}" | grep -v -E "^($(echo "${OT_EXCLUDE_DIRS[@]}" | tr ' ' '|'))" \
232        | xargs -n10 -P"$OT_BUILD_JOBS" python3 -m yapf --verbose --style '{based_on_style: google, column_limit: 119}' -ipr
233}
234
235do_python_check()
236{
237    echo -e '========================================'
238    echo -e '     check python'
239    echo -e '========================================'
240
241    git ls-files "${OT_PYTHON_SOURCES[@]}" | grep -v -E "^($(echo "${OT_EXCLUDE_DIRS[@]}" | tr ' ' '|'))" \
242        | xargs -n10 -P"$OT_BUILD_JOBS" python3 -m yapf --verbose --style '{based_on_style: google, column_limit: 119}' -dpr
243}
244
245do_shell_format()
246{
247    echo -e '========================================'
248    echo -e '     format shell'
249    echo -e '========================================'
250
251    git ls-files | xargs shfmt -f | grep -v -E "^($(echo "${OT_EXCLUDE_DIRS[@]}" | tr ' ' '|'))" \
252        | xargs -n10 -P"$OT_BUILD_JOBS" shfmt -i 4 -bn -ci -fn -s -w
253}
254
255do_shell_check()
256{
257    echo -e '========================================'
258    echo -e '     check shell'
259    echo -e '========================================'
260
261    git ls-files | xargs shfmt -f | grep -v -E "^($(echo "${OT_EXCLUDE_DIRS[@]}" | tr ' ' '|'))" \
262        | xargs -n10 -P"$OT_BUILD_JOBS" shfmt -i 4 -bn -ci -fn -s -d
263
264    git ls-files | xargs shfmt -f | grep -v -E "^($(echo "${OT_EXCLUDE_DIRS[@]}" | tr ' ' '|'))" \
265        | xargs -n10 -P"$OT_BUILD_JOBS" shellcheck
266}
267
268do_check()
269{
270    if [ $# == 0 ]; then
271        do_clang_format_check
272        do_clang_tidy_check
273        do_markdown_check
274        do_python_check
275        do_shell_check
276    elif [ "$1" == 'clang' ]; then
277        do_clang_format_check
278        do_clang_tidy_check
279    elif [ "$1" == 'clang-format' ]; then
280        do_clang_format_check
281    elif [ "$1" == 'clang-tidy' ]; then
282        do_clang_tidy_check
283    elif [ "$1" == 'markdown' ]; then
284        do_markdown_check
285    elif [ "$1" == 'python' ]; then
286        do_python_check
287    elif [ "$1" == 'shell' ]; then
288        do_shell_check
289    else
290        echo >&2 "Unsupported check: $1. Supported: clang, markdown, python, shell"
291        # 128 for Invalid arguments
292        exit 128
293    fi
294}
295
296main()
297{
298    if [ $# == 0 ]; then
299        do_clang_tidy_fix
300        do_clang_format
301        do_markdown_format
302        do_python_format
303        do_shell_format
304    elif [ "$1" == 'clang' ]; then
305        do_clang_tidy_fix
306        do_clang_format
307    elif [ "$1" == 'clang-format' ]; then
308        do_clang_format
309    elif [ "$1" == 'clang-tidy' ]; then
310        do_clang_tidy_fix
311    elif [ "$1" == 'markdown' ]; then
312        do_markdown_format
313    elif [ "$1" == 'python' ]; then
314        do_python_format
315    elif [ "$1" == 'shell' ]; then
316        do_shell_format
317    elif [ "$1" == 'check' ]; then
318        shift
319        do_check "$@"
320    else
321        echo >&2 "Unsupported action: $1. Supported: clang, markdown, python, shell"
322        # 128 for Invalid arguments
323        exit 128
324    fi
325
326}
327
328main "$@"
329