1#!/bin/bash 2# Copyright 2015 gRPC authors. 3# 4# Licensed under the Apache License, Version 2.0 (the "License"); 5# you may not use this file except in compliance with the License. 6# You may obtain a copy of the License at 7# 8# http://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, 12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13# See the License for the specific language governing permissions and 14# limitations under the License. 15 16set -e 17 18# directories to run against 19DIRS="examples/cpp src/core src/cpp test/core test/cpp include src/compiler src/ruby src/objective-c tools/distrib/python src/python/grpcio_observability" 20 21# file matching patterns to check 22GLOB="*.h *.c *.cc *.m *.mm" 23 24# clang format command 25CLANG_FORMAT=${CLANG_FORMAT:-clang-format} 26 27# number of CPUs available 28CPU_COUNT=`nproc` 29 30files= 31for dir in $DIRS 32do 33 for glob in $GLOB 34 do 35 files="$files `find ${CLANG_FORMAT_ROOT}/$dir -name $glob \ 36 -and -not -name '*.generated.*' \ 37 -and -not -name '*.upb.h' \ 38 -and -not -name '*.upbdefs.h' \ 39 -and -not -name '*.upbdefs.c' \ 40 -and -not -name '*.upb_minitable.h' \ 41 -and -not -name '*.upb_minitable.c' \ 42 -and -not -name '*.pb.h' \ 43 -and -not -name '*.pb.c' \ 44 -and -not -name '*.pb.cc' \ 45 -and -not -name '*.pbobjc.h' \ 46 -and -not -name '*.pbobjc.m' \ 47 -and -not -name '*.pbrpc.h' \ 48 -and -not -name '*.pbrpc.m' \ 49 -and -not -name end2end_tests.cc \ 50 -and -not -name grpc_shadow_boringssl.h \ 51 -and -not -name grpc_tls_credentials_options.h \ 52 -and -not -name grpc_tls_credentials_options_comparator_test.cc \ 53 -and -not -path '*/cmake/build/*' \ 54 -and -not -path '*/third_party/*' \ 55 -and -not -path '*/src/python/grpcio_observability/grpc_root/*' \ 56 `" 57 done 58done 59 60# The CHANGED_FILES variable is used to restrict the set of files to check. 61# Here we set files to the intersection of files and CHANGED_FILES 62if [ -n "$CHANGED_FILES" ]; then 63 files=$(comm -12 <(echo $files | tr ' ' '\n' | sort -u) <(echo $CHANGED_FILES | tr ' ' '\n' | sort -u)) 64fi 65 66files=`echo $files | sort -R` 67 68FILES_PER_PROCESS="$(expr $(echo "$files" | grep -o '\n' | wc -l) / $CPU_COUNT + 1)" 69 70if [ "$TEST" == "" ] 71then 72 echo $files | xargs -P $CPU_COUNT -n $FILES_PER_PROCESS $CLANG_FORMAT -i 73else 74 ok=yes 75 for file in $files 76 do 77 tmp=`mktemp` 78 $CLANG_FORMAT $file > $tmp 79 diff -u $file $tmp || ok=no 80 rm $tmp 81 done 82 if [ $ok == no ] 83 then 84 false 85 fi 86fi 87