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 11if test -t 1; then 12 ncolors=$(tput colors) 13 if test -n "$ncolors" && test $ncolors -ge 8; then 14 normal="$(tput sgr0)" 15 red="$(tput setaf 1)" 16 green="$(tput setaf 2)" 17 fi 18fi 19 20function check() { 21 local name=$1; shift 22 echo -n "Running check $name... " 23 24 if ! "$@"; then 25 echo "${red}FAILED${normal}" 26 echo " Error executing: $@"; 27 exit 1 28 fi 29 30 if ! git diff --quiet HEAD; then 31 echo "${red}FAILED${normal}" 32 echo " Git workspace not clean:" 33 git --no-pager diff -p HEAD 34 echo "${red}Check $name failed.${normal}" 35 exit 1 36 fi 37 38 echo "${green}OK${normal}" 39} 40 41# Validate commit message 42function run_bug_in_commit_msg() { 43 git log -1 --pretty=%B | grep -E '^(Bug|Issue|Fixes):(\s?)(((b\/)|(\w+:))([0-9]+)|[^0-9]+)$|(^Regres:)|(^PiperOrigin-RevId:)' 44 45 if [ $? -ne 0 ] 46 then 47 echo "${red}Git commit message must have a Bug: line" 48 echo "followed by a bug ID in the form b/# for Buganizer bugs or" 49 echo "project:# for Monorail bugs (e.g. 'Bug: chromium:123' or 'Bug: fuchsia:123')." 50 echo "Omit any digits when no ID is required (e.g. 'Bug: fix build').${normal}" 51 return 1 52 fi 53} 54 55function run_copyright_headers() { 56 tmpfile=`mktemp` 57 for suffix in "cpp" "hpp" "go" "h"; do 58 # Grep flag '-L' print files that DO NOT match the copyright regex 59 # Grep seems to match "(standard input)", filter this out in the for loop output 60 find ${SRC_DIR} -type f -name "*.${suffix}" | xargs grep -L "Copyright .* The SwiftShader Authors\|Microsoft Visual C++ generated\|GNU Bison" 61 done | grep -v "(standard input)" > ${tmpfile} 62 if test -s ${tmpfile}; then 63 # tempfile is NOT empty 64 echo "${red}Copyright issue in these files:" 65 cat ${tmpfile} 66 rm ${tmpfile} 67 echo "${normal}" 68 return 1 69 else 70 rm ${tmpfile} 71 return 0 72 fi 73} 74 75function run_clang_format() { 76 ${SRC_DIR}/clang-format-all.sh 77} 78 79function run_gofmt() { 80 find ${SRC_DIR} ${TESTS_DIR} -name "*.go" | xargs $GOFMT -w 81} 82 83function run_check_build_files() { 84 go run ${TESTS_DIR}/check_build_files/main.go --root="${ROOT_DIR}" 85} 86 87function run_scan_sources() { 88 python3 ${TESTS_DIR}/scan_sources/main.py ${SRC_DIR} 89} 90 91# Ensure we are clean to start out with. 92check "git workspace must be clean" true 93 94# Check for 'Bug: ' line in commit 95check bug-in-commi-msg run_bug_in_commit_msg 96 97# Check copyright headers 98check copyright-headers run_copyright_headers 99 100# Check clang-format. 101check clang-format run_clang_format 102 103# Check gofmt. 104check gofmt run_gofmt 105 106# Check build files. 107check "build files don't reference non-existent files" run_check_build_files 108 109# Check source files. 110check scan_sources run_scan_sources 111 112echo 113echo "${green}All check completed successfully.${normal}" 114