[script] simplify pretty (#4660)

* No need to bootstrap and configure for make pretty
* Use parallel to speed up make pretty
This commit is contained in:
Yakun Xu
2020-03-10 22:15:07 -07:00
committed by GitHub
parent e81562f182
commit 34b83ae6c5
62 changed files with 1255 additions and 1551 deletions
+8 -13
View File
@@ -1,4 +1,4 @@
#!/bin/sh
#!/bin/bash
#
# Copyright (c) 2018, The OpenThread Authors.
# All rights reserved.
@@ -33,23 +33,18 @@
# replacements.
#
set -x
set -euo pipefail
die() {
echo " *** ERROR: " $*
echo " *** ERROR: $*"
exit 1
}
# from `man diff`:
# Exit status is 0 if inputs are the same, 1 if different, 2 if trouble.
$(dirname "$0")/clang-format.sh -style=file $@ | diff -u $@ - || die
for arg; do true; done
file=$arg
[ -n "$(tail -c1 $file)" ] && {
echo " *** ERROR: Missing EOF newline: " $file
exit 1
}
exit 0
for file in "$@"; do
echo "Checking ${file}"
"$(dirname "$0")"/clang-format.sh -style=file "${file}" | diff -u "${file}" - || die "${file} is not pretty."
[ -z "$(tail -c1 "${file}")" ] || die "${file} misses EOF newline."
done
+5 -5
View File
@@ -30,13 +30,13 @@
CLANG_FORMAT_VERSION="clang-format version 6.0"
die() {
echo " *** ERROR: " $*
echo " *** ERROR: $*"
exit 1
}
if which clang-format-6.0 > /dev/null; then
if command -v clang-format-6.0 > /dev/null; then
alias clang-format=clang-format-6.0
elif which clang-format > /dev/null; then
elif command -v clang-format > /dev/null; then
case "$(clang-format --version)" in
"$CLANG_FORMAT_VERSION"*)
;;
@@ -48,7 +48,7 @@ else
die "clang-format 6.0 required"
fi
clang-format $@ || die
clang-format "$@" || die
# ensure EOF newline
REPLACE=no
@@ -64,7 +64,7 @@ done
file=$arg
[ $REPLACE != yes ] || {
[ -n "$(tail -c1 $file)" ] && echo >> $file
[ -n "$(tail -c1 "$file")" ] && echo >> "$file"
}
exit 0
+77 -18
View File
@@ -27,45 +27,104 @@
# POSSIBILITY OF SUCH DAMAGE.
#
#
# The script to check or format source code of OpenThread.
#
# Format python and c/c++:
#
# script/make-pretty
#
# Format python only:
#
# script/make-pretty python
#
# Format c/c++ only:
#
# script/make-pretty clang
#
# Check only:
#
# script/make-pretty check clang
# script/make-pretty check python
#
set -euo pipefail
readonly TMP_DIR="/tmp/ot-make-pretty-$(date +%Y%m%d%H%M%S)"
readonly SRC_DIR="$PWD"
at_exit()
{
EXIT_CODE=$?
[[ ! -d "${TMP_DIR}" ]] || rm -rf "${TMP_DIR}"
exit $EXIT_CODE
}
readonly OT_CLANG_DIRS=(examples include src tests tools)
readonly OT_PYTHON_DIRS=(tests tools)
readonly OT_BUILD_JOBS=$(getconf _NPROCESSORS_ONLN)
readonly OT_CLANG_SOURCES=('*.c' '*.cc' '*.cpp' '*.h' '*.hpp')
do_clang_format()
{
./bootstrap
mkdir "${TMP_DIR}"
cd "${TMP_DIR}"
"${SRC_DIR}/configure"
make pretty
echo -e '====================='
echo -e ' format c/c++'
echo -e '====================='
git ls-files "${OT_CLANG_SOURCES[@]}" | grep -E "^($(echo "${OT_CLANG_DIRS[@]}" | tr ' ' '|'))" \
| xargs -n3 -P"${OT_BUILD_JOBS}" script/clang-format.sh -style=file -i -verbose
}
do_clang_check()
{
echo -e '====================='
echo -e ' check c/c++'
echo -e '====================='
git ls-files "${OT_CLANG_SOURCES[@]}" | grep -E "^($(echo "${OT_CLANG_DIRS[@]}" | tr ' ' '|'))" \
| xargs -n3 -P"${OT_BUILD_JOBS}" script/clang-format-check.sh
}
do_python_format()
{
python3 -m yapf --verbose --style google -ipr "${SRC_DIR}/tests" "${SRC_DIR}/tools"
echo -e '======================'
echo -e ' format python'
echo -e '======================'
git ls-files '*.py' | grep -E "^($(echo "${OT_PYTHON_DIRS[@]}" | tr ' ' '|'))" \
| xargs -n10 -P"${OT_BUILD_JOBS}" python3 -m yapf --verbose --style google -ipr
}
do_python_check()
{
echo -e '====================='
echo -e ' check python'
echo -e '====================='
git ls-files '*.py' | grep -E "^($(echo "${OT_PYTHON_DIRS[@]}" | tr ' ' '|'))" \
| xargs -n10 -P"${OT_BUILD_JOBS}" python3 -m yapf --verbose --style google -dpr
}
do_check()
{
if [ $# == 0 ]; then
do_python_check
do_clang_check
elif [ "$1" == 'clang' ]; then
do_clang_check
elif [ "$1" == 'python' ]; then
do_python_check
else
>&2 echo "Unsupported check: $1. Supported: clang, python"
# 128 for Invalid arguments
exit 128
fi
}
main()
{
if [ $# == 0 ]; then
trap at_exit INT TERM EXIT
do_clang_format
do_python_format
elif [ "$1" == 'python' ]; then
do_python_format
elif [ "$1" == 'clang' ]; then
do_clang_format
elif [ "$1" == 'check' ]; then
shift
do_check "$@"
else
>&2 echo "Unsupported action: $1"
>&2 echo "Unsupported action: $1. Supported: clang, python"
# 128 for Invalid arguments
exit 128
fi