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