1#!/bin/bash 2 3ROOT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}")"/.. >/dev/null 2>&1 && pwd )" 4SRC_DIR=${ROOT_DIR}/src 5TESTS_DIR=${ROOT_DIR}/tests 6 7# Presubmit Checks Script. 8CLANG_FORMAT=${CLANG_FORMAT:-clang-format} 9GOFMT=${GOFMT:-gofmt} 10 11git config --global --add safe.directory '*' 12 13if test -t 1; then 14 ncolors=$(tput colors) 15 if test -n "$ncolors" && test $ncolors -ge 8; then 16 normal="$(tput sgr0)" 17 red="$(tput setaf 1)" 18 green="$(tput setaf 2)" 19 fi 20fi 21 22function check() { 23 local name=$1; shift 24 echo -n "Running check $name... " 25 26 if ! "$@"; then 27 echo "${red}FAILED${normal}" 28 echo " Error executing: $@"; 29 exit 1 30 fi 31 32 if ! git diff --quiet HEAD; then 33 echo "${red}FAILED${normal}" 34 echo " Git workspace not clean:" 35 git --no-pager diff -p HEAD 36 echo "${red}Check $name failed.${normal}" 37 exit 1 38 fi 39 40 echo "${green}OK${normal}" 41} 42 43function run_copyright_headers() { 44 tmpfile=`mktemp` 45 for suffix in "cpp" "hpp" "go" "h"; do 46 # Grep flag '-L' print files that DO NOT match the copyright regex 47 # Grep seems to match "(standard input)", filter this out in the for loop output 48 find ${SRC_DIR} -type f -name "*.${suffix}" | xargs grep -L "Copyright .* The SwiftShader Authors\|Microsoft Visual C++ generated\|GNU Bison" 49 done | grep -v "(standard input)" > ${tmpfile} 50 if test -s ${tmpfile}; then 51 # tempfile is NOT empty 52 echo "${red}Copyright issue in these files:" 53 cat ${tmpfile} 54 rm ${tmpfile} 55 echo "${normal}" 56 return 1 57 else 58 rm ${tmpfile} 59 return 0 60 fi 61} 62 63function run_clang_format() { 64 ${SRC_DIR}/clang-format-all.sh 65} 66 67function run_gofmt() { 68 find ${SRC_DIR} ${TESTS_DIR} -name "*.go" | xargs $GOFMT -w 69} 70 71function run_check_build_files() { 72 go run ${TESTS_DIR}/check_build_files/main.go --root="${ROOT_DIR}" 73} 74 75function run_scan_sources() { 76 python3 ${TESTS_DIR}/scan_sources/main.py ${SRC_DIR} 77} 78 79# Ensure we are clean to start out with. 80check "git workspace must be clean" true 81 82# Check copyright headers 83check copyright-headers run_copyright_headers 84 85# Check clang-format. 86check clang-format run_clang_format 87 88# Check gofmt. 89check gofmt run_gofmt 90 91# Check build files. 92check "build files don't reference non-existent files" run_check_build_files 93 94# Check source files. 95check scan_sources run_scan_sources 96 97echo 98echo "${green}All check completed successfully.${normal}" 99