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_APP_RCP=OFF'
78    '-DOT_MTD=OFF'
79    '-DOT_RCP=OFF'
80    '-DOT_PLATFORM=simulation'
81    '-DOT_BACKBONE_ROUTER=ON'
82    '-DOT_BORDER_AGENT=ON'
83    '-DOT_BORDER_ROUTER=ON'
84    '-DOT_CHANNEL_MANAGER=ON'
85    '-DOT_CHANNEL_MONITOR=ON'
86    '-DOT_CHILD_SUPERVISION=ON'
87    '-DOT_COAP=ON'
88    '-DOT_COAP_BLOCK=ON'
89    '-DOT_COAP_OBSERVE=ON'
90    '-DOT_COAPS=ON'
91    '-DOT_COMMISSIONER=ON'
92    '-DOT_CSL_RECEIVER=ON'
93    '-DOT_DATASET_UPDATER=ON'
94    '-DOT_DHCP6_CLIENT=ON'
95    '-DOT_DHCP6_SERVER=ON'
96    '-DOT_DIAGNOSTIC=ON'
97    '-DOT_DNS_CLIENT=ON'
98    '-DOT_DNSSD_SERVER=ON'
99    '-DOT_DUA=ON'
100    '-DOT_MLR=ON'
101    '-DOT_ECDSA=ON'
102    '-DOT_HISTORY_TRACKER=ON'
103    '-DOT_IP6_FRAGM=ON'
104    '-DOT_JAM_DETECTION=ON'
105    '-DOT_JOINER=ON'
106    '-DOT_LEGACY=ON'
107    '-DOT_LINK_RAW=ON'
108    '-DOT_LINK_METRICS_INITIATOR=ON'
109    '-DOT_LINK_METRICS_SUBJECT=ON'
110    '-DOT_MAC_FILTER=ON'
111    '-DOT_MTD_NETDIAG=ON'
112    '-DOT_NETDATA_PUBLISHER=ON'
113    '-DOT_PING_SENDER=ON'
114    '-DOT_REFERENCE_DEVICE=ON'
115    '-DOT_SERVICE=ON'
116    '-DOT_SLAAC=ON'
117    '-DOT_SNTP_CLIENT=ON'
118    '-DOT_SRP_CLIENT=ON'
119    '-DOT_SRP_SERVER=ON'
120    '-DOT_THREAD_VERSION=1.2'
121    '-DOT_TREL=ON'
122    '-DOT_COVERAGE=ON'
123    '-DOT_LOG_LEVEL_DYNAMIC=ON'
124    '-DOT_COMPILE_WARNING_AS_ERROR=ON'
125)
126
127readonly OT_CLANG_TIDY_CHECKS="\
128-*,\
129google-explicit-constructor,\
130google-readability-casting,\
131misc-unused-using-decls,\
132modernize-use-bool-literals,\
133modernize-use-equals-default,\
134modernize-use-equals-delete,\
135modernize-use-nullptr,\
136readability-avoid-const-params-in-decls,\
137readability-else-after-return,\
138readability-inconsistent-declaration-parameter-name,\
139readability-make-member-function-const,\
140readability-redundant-member-init,\
141readability-simplify-boolean-expr,\
142readability-static-accessed-through-instance,\
143"
144
145#performance-for-range-copy\
146
147do_clang_format()
148{
149    echo -e '========================================'
150    echo -e '     format c/c++ (clang-format)'
151    echo -e '========================================'
152
153    git ls-files "${OT_CLANG_SOURCES[@]}" | grep -v -E "^($(echo "${OT_EXCLUDE_DIRS[@]}" | tr ' ' '|'))" \
154        | xargs -n3 -P"$OT_BUILD_JOBS" script/clang-format -style=file -i -verbose
155}
156
157do_clang_format_check()
158{
159    echo -e '========================================'
160    echo -e '     check c/c++ (clang-format)'
161    echo -e '========================================'
162
163    git ls-files "${OT_CLANG_SOURCES[@]}" | grep -v -E "^($(echo "${OT_EXCLUDE_DIRS[@]}" | tr ' ' '|'))" \
164        | xargs -n3 -P"$OT_BUILD_JOBS" script/clang-format-check
165}
166
167do_clang_tidy_fix()
168{
169    echo -e '========================================'
170    echo -e '     format c/c++ (clang-tidy)'
171    echo -e '========================================'
172
173    (mkdir -p ./build/cmake-tidy \
174        && cd ./build/cmake-tidy \
175        && THREAD_VERSION=1.2 cmake "${OT_CLANG_TIDY_BUILD_OPTS[@]}" ../.. \
176        && ../../script/clang-tidy -header-filter='.*' -checks="${OT_CLANG_TIDY_CHECKS}" -j"$OT_BUILD_JOBS" "${OT_CLANG_TIDY_FIX_DIRS[@]}" -fix)
177}
178
179do_clang_tidy_check()
180{
181    echo -e '========================================'
182    echo -e '     check c/c++ (clang-tidy)'
183    echo -e '========================================'
184
185    (
186        mkdir -p ./build/cmake-tidy \
187            && cd ./build/cmake-tidy \
188            && THREAD_VERSION=1.2 cmake "${OT_CLANG_TIDY_BUILD_OPTS[@]}" ../.. \
189            && ../../script/clang-tidy -header-filter='.*' -checks="${OT_CLANG_TIDY_CHECKS}" -j"$OT_BUILD_JOBS" "${OT_CLANG_TIDY_FIX_DIRS[@]}" \
190            | grep -v -E "third_party" >output.txt
191        if grep -q "warning: \|error: " output.txt; then
192            echo "You must pass the clang tidy checks before submitting a pull request"
193            echo ""
194            grep --color -E 'warning: |error: ' -A 5 output.txt
195            exit 1
196        fi
197    )
198}
199
200do_markdown_format()
201{
202    echo -e '========================================'
203    echo -e '     format markdown'
204    echo -e '========================================'
205
206    git ls-files "${OT_MARKDOWN_SOURCES[@]}" | grep -v -E "^($(echo "${OT_EXCLUDE_DIRS[@]}" | tr ' ' '|'))" \
207        | xargs -n10 -P"$OT_BUILD_JOBS" npx prettier@2.0.4 --write
208}
209
210do_markdown_check()
211{
212    echo -e '========================================'
213    echo -e '     check markdown'
214    echo -e '========================================'
215
216    git ls-files "${OT_MARKDOWN_SOURCES[@]}" | grep -v -E "^($(echo "${OT_EXCLUDE_DIRS[@]}" | tr ' ' '|'))" \
217        | xargs -n10 -P"$OT_BUILD_JOBS" npx prettier@2.0.4 --check
218}
219
220do_python_format()
221{
222    echo -e '========================================'
223    echo -e '     format python'
224    echo -e '========================================'
225
226    git ls-files "${OT_PYTHON_SOURCES[@]}" | grep -v -E "^($(echo "${OT_EXCLUDE_DIRS[@]}" | tr ' ' '|'))" \
227        | xargs -n10 -P"$OT_BUILD_JOBS" python3 -m yapf --verbose --style '{based_on_style: google, column_limit: 119}' -ipr
228}
229
230do_python_check()
231{
232    echo -e '========================================'
233    echo -e '     check python'
234    echo -e '========================================'
235
236    git ls-files "${OT_PYTHON_SOURCES[@]}" | grep -v -E "^($(echo "${OT_EXCLUDE_DIRS[@]}" | tr ' ' '|'))" \
237        | xargs -n10 -P"$OT_BUILD_JOBS" python3 -m yapf --verbose --style '{based_on_style: google, column_limit: 119}' -dpr
238}
239
240do_shell_format()
241{
242    echo -e '========================================'
243    echo -e '     format shell'
244    echo -e '========================================'
245
246    git ls-files | xargs shfmt -f | grep -v -E "^($(echo "${OT_EXCLUDE_DIRS[@]}" | tr ' ' '|'))" \
247        | xargs -n10 -P"$OT_BUILD_JOBS" shfmt -i 4 -bn -ci -fn -s -w
248}
249
250do_shell_check()
251{
252    echo -e '========================================'
253    echo -e '     check shell'
254    echo -e '========================================'
255
256    git ls-files | xargs shfmt -f | grep -v -E "^($(echo "${OT_EXCLUDE_DIRS[@]}" | tr ' ' '|'))" \
257        | xargs -n10 -P"$OT_BUILD_JOBS" shfmt -i 4 -bn -ci -fn -s -d
258
259    git ls-files | xargs shfmt -f | grep -v -E "^($(echo "${OT_EXCLUDE_DIRS[@]}" | tr ' ' '|'))" \
260        | xargs -n10 -P"$OT_BUILD_JOBS" shellcheck
261}
262
263do_check()
264{
265    if [ $# == 0 ]; then
266        do_clang_format_check
267        do_clang_tidy_check
268        do_markdown_check
269        do_python_check
270        do_shell_check
271    elif [ "$1" == 'clang' ]; then
272        do_clang_format_check
273        do_clang_tidy_check
274    elif [ "$1" == 'clang-format' ]; then
275        do_clang_format_check
276    elif [ "$1" == 'clang-tidy' ]; then
277        do_clang_tidy_check
278    elif [ "$1" == 'markdown' ]; then
279        do_markdown_check
280    elif [ "$1" == 'python' ]; then
281        do_python_check
282    elif [ "$1" == 'shell' ]; then
283        do_shell_check
284    else
285        echo >&2 "Unsupported check: $1. Supported: clang, markdown, python, shell"
286        # 128 for Invalid arguments
287        exit 128
288    fi
289}
290
291main()
292{
293    if [ $# == 0 ]; then
294        do_clang_tidy_fix
295        do_clang_format
296        do_markdown_format
297        do_python_format
298        do_shell_format
299    elif [ "$1" == 'clang' ]; then
300        do_clang_tidy_fix
301        do_clang_format
302    elif [ "$1" == 'clang-format' ]; then
303        do_clang_format
304    elif [ "$1" == 'clang-tidy' ]; then
305        do_clang_tidy_fix
306    elif [ "$1" == 'markdown' ]; then
307        do_markdown_format
308    elif [ "$1" == 'python' ]; then
309        do_python_format
310    elif [ "$1" == 'shell' ]; then
311        do_shell_format
312    elif [ "$1" == 'check' ]; then
313        shift
314        do_check "$@"
315    else
316        echo >&2 "Unsupported action: $1. Supported: clang, markdown, python, shell"
317        # 128 for Invalid arguments
318        exit 128
319    fi
320
321}
322
323main "$@"
324