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
67OT_BUILD_JOBS=$(getconf _NPROCESSORS_ONLN)
68readonly OT_BUILD_JOBS
69
70OT_EXCLUDE_DIRS=(third_party doc/site)
71readonly OT_EXCLUDE_DIRS
72
73OT_CLANG_SOURCES=('*.c' '*.cc' '*.cpp' '*.h' '*.hpp')
74readonly OT_CLANG_SOURCES
75
76OT_MARKDOWN_SOURCES=('*.md')
77readonly OT_MARKDOWN_SOURCES
78
79OT_PYTHON_SOURCES=('*.py')
80readonly OT_PYTHON_SOURCES
81
82OT_CLANG_TIDY_FIX_DIRS=('examples' 'include' 'src' 'tests')
83readonly OT_CLANG_TIDY_FIX_DIRS
84
85OT_CLANG_TIDY_BUILD_OPTS=(
86    '-DCMAKE_EXPORT_COMPILE_COMMANDS=ON'
87    '-DOT_ANYCAST_LOCATOR=ON'
88    '-DOT_APP_RCP=OFF'
89    '-DOT_MTD=OFF'
90    '-DOT_RCP=OFF'
91    '-DOT_PLATFORM=simulation'
92    '-DOT_BACKBONE_ROUTER=ON'
93    '-DOT_BORDER_AGENT=ON'
94    '-DOT_BORDER_ROUTER=ON'
95    '-DOT_BORDER_ROUTING=ON'
96    '-DOT_BORDER_ROUTING_DHCP6_PD=ON'
97    '-DOT_CHANNEL_MANAGER=ON'
98    '-DOT_CHANNEL_MANAGER_CSL=ON'
99    '-DOT_CHANNEL_MONITOR=ON'
100    '-DOT_COAP=ON'
101    '-DOT_COAP_BLOCK=ON'
102    '-DOT_COAP_OBSERVE=ON'
103    '-DOT_COAPS=ON'
104    '-DOT_COMMISSIONER=ON'
105    '-DOT_CSL_RECEIVER=ON'
106    '-DOT_DATASET_UPDATER=ON'
107    '-DOT_DHCP6_CLIENT=ON'
108    '-DOT_DHCP6_SERVER=ON'
109    '-DOT_DIAGNOSTIC=ON'
110    '-DOT_DNS_CLIENT=ON'
111    '-DOT_DNS_DSO=ON'
112    '-DOT_DNS_UPSTREAM_QUERY=ON'
113    '-DOT_DNSSD_SERVER=ON'
114    '-DOT_DUA=ON'
115    '-DOT_MLR=ON'
116    '-DOT_ECDSA=ON'
117    '-DOT_HISTORY_TRACKER=ON'
118    '-DOT_IP6_FRAGM=ON'
119    '-DOT_JAM_DETECTION=ON'
120    '-DOT_JOINER=ON'
121    '-DOT_LINK_RAW=ON'
122    '-DOT_LINK_METRICS_INITIATOR=ON'
123    '-DOT_LINK_METRICS_SUBJECT=ON'
124    '-DOT_MAC_FILTER=ON'
125    '-DOT_MDNS=ON'
126    '-DOT_MESH_DIAG=ON'
127    '-DOT_NAT64_BORDER_ROUTING=ON'
128    '-DOT_NAT64_TRANSLATOR=ON'
129    '-DOT_NETDATA_PUBLISHER=ON'
130    '-DOT_NETDIAG_CLIENT=ON'
131    '-DOT_PING_SENDER=ON'
132    '-DOT_REFERENCE_DEVICE=ON'
133    '-DOT_SERVICE=ON'
134    '-DOT_SLAAC=ON'
135    '-DOT_SNTP_CLIENT=ON'
136    '-DOT_SRP_ADV_PROXY=ON'
137    '-DOT_SRP_CLIENT=ON'
138    '-DOT_SRP_SERVER=ON'
139    '-DOT_THREAD_VERSION=1.3'
140    '-DOT_TREL=ON'
141    '-DOT_COVERAGE=ON'
142    '-DOT_LOG_LEVEL_DYNAMIC=ON'
143    '-DOT_COMPILE_WARNING_AS_ERROR=ON'
144    '-DOT_UPTIME=ON'
145)
146readonly OT_CLANG_TIDY_BUILD_OPTS
147
148do_clang_format()
149{
150    echo -e '========================================'
151    echo -e '     format c/c++ (clang-format)'
152    echo -e '========================================'
153
154    git ls-files "${OT_CLANG_SOURCES[@]}" | grep -v -E "^($(echo "${OT_EXCLUDE_DIRS[@]}" | tr ' ' '|'))" \
155        | xargs -n3 -P"$OT_BUILD_JOBS" script/clang-format -style=file -i -verbose
156}
157
158do_clang_format_check()
159{
160    echo -e '========================================'
161    echo -e '     check c/c++ (clang-format)'
162    echo -e '========================================'
163
164    git ls-files "${OT_CLANG_SOURCES[@]}" | grep -v -E "^($(echo "${OT_EXCLUDE_DIRS[@]}" | tr ' ' '|'))" \
165        | xargs -n3 -P"$OT_BUILD_JOBS" script/clang-format-check
166}
167
168do_clang_tidy_fix()
169{
170    echo -e '========================================'
171    echo -e '     format c/c++ (clang-tidy)'
172    echo -e '========================================'
173
174    (mkdir -p ./build/cmake-tidy \
175        && cd ./build/cmake-tidy \
176        && THREAD_VERSION=1.3 cmake "${OT_CLANG_TIDY_BUILD_OPTS[@]}" ../.. \
177        && ../../script/clang-tidy -j"$OT_BUILD_JOBS" "${OT_CLANG_TIDY_FIX_DIRS[@]}" -fix)
178}
179
180do_clang_tidy_check()
181{
182    echo -e '========================================'
183    echo -e '     check c/c++ (clang-tidy)'
184    echo -e '========================================'
185
186    (mkdir -p ./build/cmake-tidy \
187        && cd ./build/cmake-tidy \
188        && THREAD_VERSION=1.3 cmake "${OT_CLANG_TIDY_BUILD_OPTS[@]}" ../.. \
189        && ../../script/clang-tidy -j"$OT_BUILD_JOBS" "${OT_CLANG_TIDY_FIX_DIRS[@]}")
190}
191
192do_markdown_format()
193{
194    echo -e '========================================'
195    echo -e '     format markdown'
196    echo -e '========================================'
197
198    git ls-files "${OT_MARKDOWN_SOURCES[@]}" | grep -v -E "^($(echo "${OT_EXCLUDE_DIRS[@]}" | tr ' ' '|'))" \
199        | xargs -n10 -P"$OT_BUILD_JOBS" npx prettier@2.0.4 --write
200}
201
202do_markdown_check()
203{
204    echo -e '========================================'
205    echo -e '     check markdown'
206    echo -e '========================================'
207
208    git ls-files "${OT_MARKDOWN_SOURCES[@]}" | grep -v -E "^($(echo "${OT_EXCLUDE_DIRS[@]}" | tr ' ' '|'))" \
209        | xargs -n10 -P"$OT_BUILD_JOBS" npx prettier@2.0.4 --check
210}
211
212do_python_format()
213{
214    echo -e '========================================'
215    echo -e '     format python'
216    echo -e '========================================'
217
218    git ls-files "${OT_PYTHON_SOURCES[@]}" | grep -v -E "^($(echo "${OT_EXCLUDE_DIRS[@]}" | tr ' ' '|'))" \
219        | xargs -n10 -P"$OT_BUILD_JOBS" python3 -m yapf --verbose --style '{based_on_style: google, column_limit: 119}' -ipr
220}
221
222do_python_check()
223{
224    echo -e '========================================'
225    echo -e '     check python'
226    echo -e '========================================'
227
228    git ls-files "${OT_PYTHON_SOURCES[@]}" | grep -v -E "^($(echo "${OT_EXCLUDE_DIRS[@]}" | tr ' ' '|'))" \
229        | xargs -n10 -P"$OT_BUILD_JOBS" python3 -m yapf --verbose --style '{based_on_style: google, column_limit: 119}' -dpr
230}
231
232do_shell_format()
233{
234    echo -e '========================================'
235    echo -e '     format shell'
236    echo -e '========================================'
237
238    git ls-files | xargs shfmt -f | grep -v -E "^($(echo "${OT_EXCLUDE_DIRS[@]}" | tr ' ' '|'))" \
239        | xargs -n10 -P"$OT_BUILD_JOBS" shfmt -i 4 -bn -ci -fn -s -w
240}
241
242do_shell_check()
243{
244    echo -e '========================================'
245    echo -e '     check shell'
246    echo -e '========================================'
247
248    git ls-files | xargs shfmt -f | grep -v -E "^($(echo "${OT_EXCLUDE_DIRS[@]}" | tr ' ' '|'))" \
249        | xargs -n10 -P"$OT_BUILD_JOBS" shfmt -i 4 -bn -ci -fn -s -d
250
251    git ls-files | xargs shfmt -f | grep -v -E "^($(echo "${OT_EXCLUDE_DIRS[@]}" | tr ' ' '|'))" \
252        | xargs -n10 -P"$OT_BUILD_JOBS" shellcheck
253}
254
255do_check()
256{
257    if [ $# == 0 ]; then
258        do_clang_format_check
259        do_clang_tidy_check
260        do_markdown_check
261        do_python_check
262        do_shell_check
263    elif [ "$1" == 'clang' ]; then
264        do_clang_format_check
265        do_clang_tidy_check
266    elif [ "$1" == 'clang-format' ]; then
267        do_clang_format_check
268    elif [ "$1" == 'clang-tidy' ]; then
269        do_clang_tidy_check
270    elif [ "$1" == 'markdown' ]; then
271        do_markdown_check
272    elif [ "$1" == 'python' ]; then
273        do_python_check
274    elif [ "$1" == 'shell' ]; then
275        do_shell_check
276    else
277        echo >&2 "Unsupported check: $1. Supported: clang, markdown, python, shell"
278        # 128 for Invalid arguments
279        exit 128
280    fi
281}
282
283main()
284{
285    if [ $# == 0 ]; then
286        do_clang_tidy_fix
287        do_clang_format
288        do_markdown_format
289        do_python_format
290        do_shell_format
291    elif [ "$1" == 'clang' ]; then
292        do_clang_tidy_fix
293        do_clang_format
294    elif [ "$1" == 'clang-format' ]; then
295        do_clang_format
296    elif [ "$1" == 'clang-tidy' ]; then
297        do_clang_tidy_fix
298    elif [ "$1" == 'markdown' ]; then
299        do_markdown_format
300    elif [ "$1" == 'python' ]; then
301        do_python_format
302    elif [ "$1" == 'shell' ]; then
303        do_shell_format
304    elif [ "$1" == 'check' ]; then
305        shift
306        do_check "$@"
307    else
308        echo >&2 "Unsupported action: $1. Supported: clang, markdown, python, shell"
309        # 128 for Invalid arguments
310        exit 128
311    fi
312
313}
314
315main "$@"
316