blob: 38af8853fc5b392af1f274eae2c32a933a8ab430 [file] [log] [blame]
Ben Clayton750660e2019-12-18 15:09:46 +00001#!/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
Alexis Hetu9bfd9622022-12-12 09:46:28 -050011git config --global --add safe.directory '*'
12
Ben Clayton750660e2019-12-18 15:09:46 +000013if 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
43# Validate commit message
44function run_bug_in_commit_msg() {
David Dorwind71ded62020-07-10 12:46:10 -070045 git log -1 --pretty=%B | grep -E '^(Bug|Issue|Fixes):(\s?)(((b\/)|(\w+:))([0-9]+)|[^0-9]+)$|(^Regres:)|(^PiperOrigin-RevId:)'
Ben Clayton750660e2019-12-18 15:09:46 +000046
47 if [ $? -ne 0 ]
48 then
Nicolas Capensf554c542020-01-09 17:19:35 +000049 echo "${red}Git commit message must have a Bug: line"
Nicolas Capensd79c7342020-01-07 23:09:37 -050050 echo "followed by a bug ID in the form b/# for Buganizer bugs or"
David Dorwind71ded62020-07-10 12:46:10 -070051 echo "project:# for Monorail bugs (e.g. 'Bug: chromium:123' or 'Bug: fuchsia:123')."
Nicolas Capensf554c542020-01-09 17:19:35 +000052 echo "Omit any digits when no ID is required (e.g. 'Bug: fix build').${normal}"
Ben Clayton750660e2019-12-18 15:09:46 +000053 return 1
54 fi
55}
56
57function run_copyright_headers() {
58 tmpfile=`mktemp`
59 for suffix in "cpp" "hpp" "go" "h"; do
60 # Grep flag '-L' print files that DO NOT match the copyright regex
61 # Grep seems to match "(standard input)", filter this out in the for loop output
62 find ${SRC_DIR} -type f -name "*.${suffix}" | xargs grep -L "Copyright .* The SwiftShader Authors\|Microsoft Visual C++ generated\|GNU Bison"
63 done | grep -v "(standard input)" > ${tmpfile}
64 if test -s ${tmpfile}; then
65 # tempfile is NOT empty
Nicolas Capensf554c542020-01-09 17:19:35 +000066 echo "${red}Copyright issue in these files:"
Ben Clayton750660e2019-12-18 15:09:46 +000067 cat ${tmpfile}
68 rm ${tmpfile}
Nicolas Capensf554c542020-01-09 17:19:35 +000069 echo "${normal}"
Ben Clayton750660e2019-12-18 15:09:46 +000070 return 1
71 else
72 rm ${tmpfile}
73 return 0
74 fi
75}
76
77function run_clang_format() {
78 ${SRC_DIR}/clang-format-all.sh
79}
80
81function run_gofmt() {
82 find ${SRC_DIR} ${TESTS_DIR} -name "*.go" | xargs $GOFMT -w
83}
84
Ben Clayton682232b2020-04-07 16:24:35 +010085function run_check_build_files() {
86 go run ${TESTS_DIR}/check_build_files/main.go --root="${ROOT_DIR}"
87}
88
Jari Komppab5bf8262020-10-30 15:31:10 +020089function run_scan_sources() {
90 python3 ${TESTS_DIR}/scan_sources/main.py ${SRC_DIR}
91}
92
Ben Clayton750660e2019-12-18 15:09:46 +000093# Ensure we are clean to start out with.
94check "git workspace must be clean" true
95
96# Check for 'Bug: ' line in commit
97check bug-in-commi-msg run_bug_in_commit_msg
98
99# Check copyright headers
100check copyright-headers run_copyright_headers
101
102# Check clang-format.
103check clang-format run_clang_format
104
105# Check gofmt.
106check gofmt run_gofmt
107
Ben Clayton682232b2020-04-07 16:24:35 +0100108# Check build files.
109check "build files don't reference non-existent files" run_check_build_files
110
Jari Komppab5bf8262020-10-30 15:31:10 +0200111# Check source files.
112check scan_sources run_scan_sources
113
Ben Clayton750660e2019-12-18 15:09:46 +0000114echo
Nicolas Capensf554c542020-01-09 17:19:35 +0000115echo "${green}All check completed successfully.${normal}"