Ben Clayton | 750660e | 2019-12-18 15:09:46 +0000 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
| 3 | ROOT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}")"/.. >/dev/null 2>&1 && pwd )" |
| 4 | SRC_DIR=${ROOT_DIR}/src |
| 5 | TESTS_DIR=${ROOT_DIR}/tests |
| 6 | |
| 7 | # Presubmit Checks Script. |
| 8 | CLANG_FORMAT=${CLANG_FORMAT:-clang-format} |
| 9 | GOFMT=${GOFMT:-gofmt} |
| 10 | |
| 11 | if 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 |
| 18 | fi |
| 19 | |
| 20 | function 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 |
| 42 | function run_bug_in_commit_msg() { |
David Dorwin | d71ded6 | 2020-07-10 12:46:10 -0700 | [diff] [blame] | 43 | git log -1 --pretty=%B | grep -E '^(Bug|Issue|Fixes):(\s?)(((b\/)|(\w+:))([0-9]+)|[^0-9]+)$|(^Regres:)|(^PiperOrigin-RevId:)' |
Ben Clayton | 750660e | 2019-12-18 15:09:46 +0000 | [diff] [blame] | 44 | |
| 45 | if [ $? -ne 0 ] |
| 46 | then |
Nicolas Capens | f554c54 | 2020-01-09 17:19:35 +0000 | [diff] [blame] | 47 | echo "${red}Git commit message must have a Bug: line" |
Nicolas Capens | d79c734 | 2020-01-07 23:09:37 -0500 | [diff] [blame] | 48 | echo "followed by a bug ID in the form b/# for Buganizer bugs or" |
David Dorwin | d71ded6 | 2020-07-10 12:46:10 -0700 | [diff] [blame] | 49 | echo "project:# for Monorail bugs (e.g. 'Bug: chromium:123' or 'Bug: fuchsia:123')." |
Nicolas Capens | f554c54 | 2020-01-09 17:19:35 +0000 | [diff] [blame] | 50 | echo "Omit any digits when no ID is required (e.g. 'Bug: fix build').${normal}" |
Ben Clayton | 750660e | 2019-12-18 15:09:46 +0000 | [diff] [blame] | 51 | return 1 |
| 52 | fi |
| 53 | } |
| 54 | |
| 55 | function 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 |
Nicolas Capens | f554c54 | 2020-01-09 17:19:35 +0000 | [diff] [blame] | 64 | echo "${red}Copyright issue in these files:" |
Ben Clayton | 750660e | 2019-12-18 15:09:46 +0000 | [diff] [blame] | 65 | cat ${tmpfile} |
| 66 | rm ${tmpfile} |
Nicolas Capens | f554c54 | 2020-01-09 17:19:35 +0000 | [diff] [blame] | 67 | echo "${normal}" |
Ben Clayton | 750660e | 2019-12-18 15:09:46 +0000 | [diff] [blame] | 68 | return 1 |
| 69 | else |
| 70 | rm ${tmpfile} |
| 71 | return 0 |
| 72 | fi |
| 73 | } |
| 74 | |
| 75 | function run_clang_format() { |
| 76 | ${SRC_DIR}/clang-format-all.sh |
| 77 | } |
| 78 | |
| 79 | function run_gofmt() { |
| 80 | find ${SRC_DIR} ${TESTS_DIR} -name "*.go" | xargs $GOFMT -w |
| 81 | } |
| 82 | |
Ben Clayton | 682232b | 2020-04-07 16:24:35 +0100 | [diff] [blame] | 83 | function run_check_build_files() { |
| 84 | go run ${TESTS_DIR}/check_build_files/main.go --root="${ROOT_DIR}" |
| 85 | } |
| 86 | |
Jari Komppa | b5bf826 | 2020-10-30 15:31:10 +0200 | [diff] [blame] | 87 | function run_scan_sources() { |
| 88 | python3 ${TESTS_DIR}/scan_sources/main.py ${SRC_DIR} |
| 89 | } |
| 90 | |
Ben Clayton | 750660e | 2019-12-18 15:09:46 +0000 | [diff] [blame] | 91 | # Ensure we are clean to start out with. |
| 92 | check "git workspace must be clean" true |
| 93 | |
| 94 | # Check for 'Bug: ' line in commit |
| 95 | check bug-in-commi-msg run_bug_in_commit_msg |
| 96 | |
| 97 | # Check copyright headers |
| 98 | check copyright-headers run_copyright_headers |
| 99 | |
| 100 | # Check clang-format. |
| 101 | check clang-format run_clang_format |
| 102 | |
| 103 | # Check gofmt. |
| 104 | check gofmt run_gofmt |
| 105 | |
Ben Clayton | 682232b | 2020-04-07 16:24:35 +0100 | [diff] [blame] | 106 | # Check build files. |
| 107 | check "build files don't reference non-existent files" run_check_build_files |
| 108 | |
Jari Komppa | b5bf826 | 2020-10-30 15:31:10 +0200 | [diff] [blame] | 109 | # Check source files. |
| 110 | check scan_sources run_scan_sources |
| 111 | |
Ben Clayton | 750660e | 2019-12-18 15:09:46 +0000 | [diff] [blame] | 112 | echo |
Nicolas Capens | f554c54 | 2020-01-09 17:19:35 +0000 | [diff] [blame] | 113 | echo "${green}All check completed successfully.${normal}" |