Kokoro: Add style presubmit checks

Checks for Bug: line, licenses, clang-format (v 8.0.0) and gofmt.

These checks are performed by a separate kokoro instance.

Fixed up broken licenses that it found.

Counterpart: cl/286343852
Bug: b/144825072
Bug: b/141892461
Change-Id: If4ffda0b6144efe3cd5560cf17a6f4ad33bf985a
Reviewed-on: https://swiftshader-review.googlesource.com/c/SwiftShader/+/39708
Reviewed-by: Antonio Maiorano <amaiorano@google.com>
Tested-by: Ben Clayton <bclayton@google.com>
Kokoro-Presubmit: kokoro <noreply+kokoro@google.com>
diff --git a/src/Reactor/EmulatedReactor.cpp b/src/Reactor/EmulatedReactor.cpp
index 6fb7385..8e93909 100644
--- a/src/Reactor/EmulatedReactor.cpp
+++ b/src/Reactor/EmulatedReactor.cpp
@@ -1,3 +1,17 @@
+// Copyright 2019 The SwiftShader Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
 #include "EmulatedReactor.hpp"
 
 #include <cmath>
diff --git a/src/Reactor/SubmoduleCheck/gtest/gtest.h b/src/Reactor/SubmoduleCheck/gtest/gtest.h
index 4861ef3..6111dac 100644
--- a/src/Reactor/SubmoduleCheck/gtest/gtest.h
+++ b/src/Reactor/SubmoduleCheck/gtest/gtest.h
@@ -1,2 +1,16 @@
+// Copyright 2019 The SwiftShader Authors. All Rights Reserved.

+//

+// Licensed under the Apache License, Version 2.0 (the "License");

+// you may not use this file except in compliance with the License.

+// You may obtain a copy of the License at

+//

+//    http://www.apache.org/licenses/LICENSE-2.0

+//

+// Unless required by applicable law or agreed to in writing, software

+// distributed under the License is distributed on an "AS IS" BASIS,

+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

+// See the License for the specific language governing permissions and

+// limitations under the License.

+

 // These are not the headers you're looking for

 #error "Missing googletest submodule. Run git submodule update --init"
\ No newline at end of file
diff --git a/src/WSI/XcbSurfaceKHR.cpp b/src/WSI/XcbSurfaceKHR.cpp
index b1af1e5..1c55ea0 100644
--- a/src/WSI/XcbSurfaceKHR.cpp
+++ b/src/WSI/XcbSurfaceKHR.cpp
@@ -1,3 +1,6 @@
+// Copyright 2018 The SwiftShader Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
 // you may not use this file except in compliance with the License.
 // You may obtain a copy of the License at
 //
diff --git a/src/clang-format-all.sh b/src/clang-format-all.sh
index c39ce41..51ade10 100755
--- a/src/clang-format-all.sh
+++ b/src/clang-format-all.sh
@@ -1,10 +1,11 @@
 SRC_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
-pushd ${SRC_DIR}
+
+CLANG_FORMAT=${CLANG_FORMAT:-clang-format}
+
 for DIR in "Device" "Pipeline" "Reactor" "System" "Vulkan" "WSI"
 do
     # Double clang-format, as it seems that one pass isn't always enough
-    find ${SRC_DIR}/$DIR  -iname "*.hpp" -o -iname "*.cpp" -o -iname "*.inl" | xargs clang-format -i
-    find ${SRC_DIR}/$DIR  -iname "*.hpp" -o -iname "*.cpp" -o -iname "*.inl" | xargs clang-format -i
+    find ${SRC_DIR}/${DIR} -iname "*.hpp" -o -iname "*.cpp" -o -iname "*.inl" | xargs ${CLANG_FORMAT} -i -style=file
+    find ${SRC_DIR}/${DIR} -iname "*.hpp" -o -iname "*.cpp" -o -iname "*.inl" | xargs ${CLANG_FORMAT} -i -style=file
 done
-popd
 
diff --git a/src/clang-format-separate.sh b/src/clang-format-separate.sh
index 3f2a511..094a2d5 100755
--- a/src/clang-format-separate.sh
+++ b/src/clang-format-separate.sh
@@ -1,6 +1,5 @@
 SRC_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
 
-pushd ${SRC_DIR}
 for DIR in "Device" "Pipeline" "Reactor" "System" "Vulkan" "WSI"
 do
     # Double clang-format, as it seems that one pass isn't always enough
@@ -11,5 +10,4 @@
     CHANGE_ID="$(echo $CHANGE_ID_SEED $DIR | openssl sha1)"
     git commit -m "clang-format the src/$DIR directory" -m "Bug: b/144825072" -m "Change-Id:I$CHANGE_ID"
 done
-popd
 
diff --git a/tests/kokoro/gcp_ubuntu/check_style.sh b/tests/kokoro/gcp_ubuntu/check_style.sh
new file mode 100755
index 0000000..b05ab20
--- /dev/null
+++ b/tests/kokoro/gcp_ubuntu/check_style.sh
@@ -0,0 +1,27 @@
+#!/bin/bash
+
+set -x # Display commands being run.
+
+# Download clang tar, verify.
+CLANG_TAR=/tmp/clang-8.tar.xz
+curl http://releases.llvm.org/8.0.0/clang+llvm-8.0.0-x86_64-linux-gnu-ubuntu-14.04.tar.xz > ${CLANG_TAR}
+echo "552ea458b70961b7922a4bbe9de1434688342dbf" ${CLANG_TAR} | sha1sum -c -
+if [ $? -ne 0 ]
+then
+  echo "clang download's sha was not as expected"
+  return 1
+fi
+
+set -e # Fail on any error
+
+# Untar into tmp
+CLANG_DIR=/tmp/clang-8
+mkdir ${CLANG_DIR}
+tar -xf ${CLANG_TAR} -C ${CLANG_DIR}
+
+# Set up env vars
+export CLANG_FORMAT=${CLANG_DIR}/clang+llvm-8.0.0-x86_64-linux-gnu-ubuntu-14.04/bin/clang-format
+
+# Run presubmit tests
+cd git/SwiftShader
+./tests/presubmit.sh
diff --git a/tests/kokoro/gcp_ubuntu/check_style/presubmit.cfg b/tests/kokoro/gcp_ubuntu/check_style/presubmit.cfg
new file mode 100644
index 0000000..922bae3
--- /dev/null
+++ b/tests/kokoro/gcp_ubuntu/check_style/presubmit.cfg
@@ -0,0 +1,4 @@
+# Format: //devtools/kokoro/config/proto/build.proto
+
+# Location of the continuous bash script in Git.
+build_file: "SwiftShader/tests/kokoro/gcp_ubuntu/check_style.sh"
diff --git a/tests/kokoro/gcp_ubuntu/continuous.sh b/tests/kokoro/gcp_ubuntu/continuous.sh
old mode 100644
new mode 100755
index ca67ba6..486a306
--- a/tests/kokoro/gcp_ubuntu/continuous.sh
+++ b/tests/kokoro/gcp_ubuntu/continuous.sh
@@ -2,19 +2,8 @@
 
 cd git/SwiftShader
 
-# Validate commit message
-git log -1 --pretty=%B | grep -E '^Bug:|^Issue:|^Fixes:|^Regres:'
-
-if [ $? -ne 0 ]
-then
-  echo "error: Git commit message must have a Bug: line."
-  exit 1
-fi
-
-# Fail on any error.
-set -e
-# Display commands being run.
-set -x
+set -e # Fail on any error.
+set -x # Display commands being run.
 
 # Download all submodules
 git submodule update --init
diff --git a/tests/presubmit.sh b/tests/presubmit.sh
new file mode 100755
index 0000000..99db52b
--- /dev/null
+++ b/tests/presubmit.sh
@@ -0,0 +1,95 @@
+#!/bin/bash
+
+ROOT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}")"/.. >/dev/null 2>&1 && pwd )"
+SRC_DIR=${ROOT_DIR}/src
+TESTS_DIR=${ROOT_DIR}/tests
+
+# Presubmit Checks Script.
+CLANG_FORMAT=${CLANG_FORMAT:-clang-format}
+GOFMT=${GOFMT:-gofmt}
+
+if test -t 1; then
+  ncolors=$(tput colors)
+  if test -n "$ncolors" && test $ncolors -ge 8; then
+    normal="$(tput sgr0)"
+    red="$(tput setaf 1)"
+    green="$(tput setaf 2)"
+  fi
+fi
+
+function check() {
+  local name=$1; shift
+  echo -n "Running check $name... "
+
+  if ! "$@"; then
+    echo "${red}FAILED${normal}"
+    echo "  Error executing: $@";
+    exit 1
+  fi
+
+  if ! git diff --quiet HEAD; then
+    echo "${red}FAILED${normal}"
+    echo "  Git workspace not clean:"
+    git --no-pager diff -p HEAD
+    echo "${red}Check $name failed.${normal}"
+    exit 1
+  fi
+
+  echo "${green}OK${normal}"
+}
+
+# Validate commit message
+function run_bug_in_commit_msg() {
+  git log -1 --pretty=%B | grep -E '^Bug:|^Issue:|^Fixes:|^Regres:'
+
+  if [ $? -ne 0 ]
+  then
+    echo "Git commit message must have a Bug: line."
+    return 1
+  fi
+}
+
+function run_copyright_headers() {
+  tmpfile=`mktemp`
+  for suffix in "cpp" "hpp" "go" "h"; do
+    # Grep flag '-L' print files that DO NOT match the copyright regex
+    # Grep seems to match "(standard input)", filter this out in the for loop output
+    find ${SRC_DIR} -type f -name "*.${suffix}" | xargs grep -L "Copyright .* The SwiftShader Authors\|Microsoft Visual C++ generated\|GNU Bison"
+  done | grep -v "(standard input)" > ${tmpfile}
+  if test -s ${tmpfile}; then
+    # tempfile is NOT empty
+    echo "Copyright issue in these files:"
+    cat ${tmpfile}
+    rm ${tmpfile}
+    return 1
+  else
+    rm ${tmpfile}
+    return 0
+  fi
+}
+
+function run_clang_format() {
+  ${SRC_DIR}/clang-format-all.sh
+}
+
+function run_gofmt() {
+  find ${SRC_DIR} ${TESTS_DIR} -name "*.go" | xargs $GOFMT -w
+}
+
+# Ensure we are clean to start out with.
+check "git workspace must be clean" true
+
+# Check for 'Bug: ' line in commit
+check bug-in-commi-msg run_bug_in_commit_msg
+
+# Check copyright headers
+check copyright-headers run_copyright_headers
+
+# Check clang-format.
+check clang-format run_clang_format
+
+# Check gofmt.
+check gofmt run_gofmt
+
+echo
+echo "${green}All check completed successfully.$normal"
\ No newline at end of file