check_style_cpplint.sh 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #!/bin/bash
  2. #
  3. # Copyright 2020 Mobvoi Inc. (authors: Fangjun Kuang)
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. #
  17. # Usage:
  18. #
  19. # (1) To check files of the last commit
  20. # ./scripts/check_style_cpplint.sh
  21. #
  22. # (2) To check changed files not committed yet
  23. # ./scripts/check_style_cpplint.sh 1
  24. #
  25. # (3) To check all files in the project
  26. # ./scripts/check_style_cpplint.sh 2
  27. cpplint_version="1.5.4"
  28. cur_dir=$(cd $(dirname $BASH_SOURCE) && pwd)
  29. sherpa_ncnn_dir=$(cd $cur_dir/.. && pwd)
  30. build_dir=$sherpa_ncnn_dir/build
  31. mkdir -p $build_dir
  32. cpplint_src=$build_dir/cpplint-${cpplint_version}/cpplint.py
  33. if [ ! -d "$build_dir/cpplint-${cpplint_version}" ]; then
  34. pushd $build_dir
  35. if command -v wget &> /dev/null; then
  36. wget https://github.com/cpplint/cpplint/archive/${cpplint_version}.tar.gz
  37. elif command -v curl &> /dev/null; then
  38. curl -O -SL https://github.com/cpplint/cpplint/archive/${cpplint_version}.tar.gz
  39. else
  40. echo "Please install wget or curl to download cpplint"
  41. exit 1
  42. fi
  43. tar xf ${cpplint_version}.tar.gz
  44. rm ${cpplint_version}.tar.gz
  45. # cpplint will report the following error for: __host__ __device__ (
  46. #
  47. # Extra space before ( in function call [whitespace/parens] [4]
  48. #
  49. # the following patch disables the above error
  50. sed -i "3490i\ not Search(r'__host__ __device__\\\s+\\\(', fncall) and" $cpplint_src
  51. popd
  52. fi
  53. source $sherpa_ncnn_dir/scripts/utils.sh
  54. # return true if the given file is a c++ source file
  55. # return false otherwise
  56. function is_source_code_file() {
  57. case "$1" in
  58. *.cc|*.h|*.cu)
  59. echo true;;
  60. *)
  61. echo false;;
  62. esac
  63. }
  64. function check_style() {
  65. python3 $cpplint_src $1 || abort $1
  66. }
  67. function check_last_commit() {
  68. files=$(git diff HEAD^1 --name-only --diff-filter=ACDMRUXB)
  69. echo $files
  70. }
  71. function check_current_dir() {
  72. files=$(git status -s -uno --porcelain | awk '{
  73. if (NF == 4) {
  74. # a file has been renamed
  75. print $NF
  76. } else {
  77. print $2
  78. }}')
  79. echo $files
  80. }
  81. function do_check() {
  82. case "$1" in
  83. 1)
  84. echo "Check changed files"
  85. files=$(check_current_dir)
  86. ;;
  87. 2)
  88. echo "Check all files"
  89. files=$(find $sherpa_ncnn_dir/sherpa-ncnn -name "*.h" -o -name "*.cc")
  90. ;;
  91. *)
  92. echo "Check last commit"
  93. files=$(check_last_commit)
  94. ;;
  95. esac
  96. for f in $files; do
  97. need_check=$(is_source_code_file $f)
  98. if $need_check; then
  99. [[ -f $f ]] && check_style $f
  100. fi
  101. done
  102. }
  103. function main() {
  104. do_check $1
  105. ok "Great! Style check passed!"
  106. }
  107. cd $sherpa_ncnn_dir
  108. main $1