blob: 10ccf53b6f7b8c9195e9b2a23448298adc78066c [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
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() {
David Dorwind71ded62020-07-10 12:46:10 -070043 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 +000044
45 if [ $? -ne 0 ]
46 then
Nicolas Capensf554c542020-01-09 17:19:35 +000047 echo "${red}Git commit message must have a Bug: line"
Nicolas Capensd79c7342020-01-07 23:09:37 -050048 echo "followed by a bug ID in the form b/# for Buganizer bugs or"
David Dorwind71ded62020-07-10 12:46:10 -070049 echo "project:# for Monorail bugs (e.g. 'Bug: chromium:123' or 'Bug: fuchsia:123')."
Nicolas Capensf554c542020-01-09 17:19:35 +000050 echo "Omit any digits when no ID is required (e.g. 'Bug: fix build').${normal}"
Ben Clayton750660e2019-12-18 15:09:46 +000051 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
Nicolas Capensf554c542020-01-09 17:19:35 +000064 echo "${red}Copyright issue in these files:"
Ben Clayton750660e2019-12-18 15:09:46 +000065 cat ${tmpfile}
66 rm ${tmpfile}
Nicolas Capensf554c542020-01-09 17:19:35 +000067 echo "${normal}"
Ben Clayton750660e2019-12-18 15:09:46 +000068 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
Ben Clayton682232b2020-04-07 16:24:35 +010083function run_check_build_files() {
84 go run ${TESTS_DIR}/check_build_files/main.go --root="${ROOT_DIR}"
85}
86
Jari Komppab5bf8262020-10-30 15:31:10 +020087function run_scan_sources() {
88 python3 ${TESTS_DIR}/scan_sources/main.py ${SRC_DIR}
89}
90
Ben Clayton750660e2019-12-18 15:09:46 +000091# 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
Ben Clayton682232b2020-04-07 16:24:35 +0100106# Check build files.
107check "build files don't reference non-existent files" run_check_build_files
108
Jari Komppab5bf8262020-10-30 15:31:10 +0200109# Check source files.
110check scan_sources run_scan_sources
111
Ben Clayton750660e2019-12-18 15:09:46 +0000112echo
Nicolas Capensf554c542020-01-09 17:19:35 +0000113echo "${green}All check completed successfully.${normal}"