Use a prebuilt toolchain for reliable builds.

This means the presubmit server can produce correct images which can be
run in QEMU emulation to perform tests. Source code style and use of
error prone constructs are also checked in presubmit.

To run the tests locally:
    ./kokoro/ubuntu/test.sh

To run a full presubmit locally:
    ./kokoro/ubuntu/build.sh

Change-Id: Ib7d0bdf8b3d1fc2432f84c203458559085919e44
diff --git a/.gitmodules b/.gitmodules
index ee2019a..d274169 100644
--- a/.gitmodules
+++ b/.gitmodules
@@ -1,6 +1,7 @@
 [submodule "prebuilts"]
 	path = prebuilts
 	url = sso://hafnium/hafnium/prebuilts
+	shallow = true
 [submodule "driver/linux"]
 	path = driver/linux
 	url = sso://hafnium/hafnium/driver/linux
diff --git a/Makefile b/Makefile
index e1227e7..8ad1562 100644
--- a/Makefile
+++ b/Makefile
@@ -1,12 +1,13 @@
 OUT ?= out
 
-# Set path to prebuilts
+# Set path to prebuilts used in the build.
 UNNAME_S := $(shell uname -s | tr '[:upper:]' '[:lower:]')
-PREBUILTS := prebuilts/$(UNNAME_S)-x64
+PREBUILTS := $(PWD)/prebuilts/$(UNNAME_S)-x64
 GN ?= $(PREBUILTS)/gn/gn
 NINJA ?= $(PREBUILTS)/ninja/ninja
+export PATH := $(PREBUILTS)/clang/bin:$(PATH)
 
-# Configure the build arguments
+# Configure the build arguments.
 GCC ?= false
 ARCH ?= aarch64
 PLATFORM ?= qemu
@@ -19,7 +20,7 @@
 	@$(GN) --export-compile-commands gen $(OUT)
 
 # Configure the build by loading the configuration arguments for the
-# architecture and platform
+# architecture and platform.
 $(OUT)/args.gn: build/arch/$(ARCH)/$(PLATFORM).args
 	@echo Copying config for $(ARCH) on $(PLATFORM)
 	@mkdir -p $(OUT)
@@ -36,7 +37,7 @@
 clobber:
 	rm -rf $(OUT)
 
-# see .clang-format
+# see .clang-format.
 .PHONY: format
 format:
 	@echo "Formatting..."
@@ -45,7 +46,7 @@
 	@find test/ -name *.c -o -name *.h | xargs clang-format -style file -i
 	@find . -name *.gn -o -name *.gni -exec $(GN) format {} \;
 
-# see .clang-tidy
+# see .clang-tidy.
 .PHONY: tidy
 tidy: $(OUT)/build.ninja
 	@$(NINJA) -C $(OUT)
diff --git a/build/BUILD.gn b/build/BUILD.gn
index d383359..8ef2258 100644
--- a/build/BUILD.gn
+++ b/build/BUILD.gn
@@ -46,12 +46,6 @@
       "-ffunction-sections",
     ]
 
-    if (!use_gcc) {
-      cflags += [
-        "-Wno-extended-offsetof", # have clang give us some slack
-      ]
-    }
-
     defines += [
       # TODO: move these build args for the platforms
       "MAX_CPUS=8",
diff --git a/build/toolchain/arch/BUILD.gn b/build/toolchain/arch/BUILD.gn
index 6523339..b04ed4c 100644
--- a/build/toolchain/arch/BUILD.gn
+++ b/build/toolchain/arch/BUILD.gn
@@ -77,7 +77,7 @@
   cc_toolchain(target_name) {
     cc = "clang"
     cflags = "-target ${invoker.target} -fcolor-diagnostics"
-    ld = "${invoker.arch_tool_prefix}ld"
+    ld = "ld.lld"
 
     toolchain_args = {
       arch_tool_prefix = invoker.arch_tool_prefix
diff --git a/kokoro/ubuntu/build.sh b/kokoro/ubuntu/build.sh
index 95da482..4596757 100755
--- a/kokoro/ubuntu/build.sh
+++ b/kokoro/ubuntu/build.sh
@@ -12,23 +12,36 @@
 	# Server
 	cd git/hafnium
 	make out/args.gn
-	echo "arch_cc_version = \"3.9\"" >> out/args.gn
-	echo "host_cc_version = \"3.9\"" >> out/args.gn
 else
 	# Local
 	echo "Testing kokoro build locally..."
 	make out/args.gn
 fi
 
-# TODO: Kokoro is missing ninja, gcc-4.9 or above and qemu
-# Check the build works
-make
-
-# # Check to code looks healthy, failing if any changes were made
-# make format
-# make tidy
 #
-# if [[ `git status --porcelain` ]]
-# then
-# 	exit 1
-# fi
+# Step 1: make sure it builds.
+#
+
+# TODO: add a gcc-4.9 or above prebuilt to check the gcc build too?
+# Check the build works.
+make
+#TODO: static analysis with make check
+
+#
+# Step 2: make sure it works.
+#
+
+./kokoro/ubuntu/test.sh
+
+#
+# Step 3: make sure the code looks good.
+#
+
+make format
+make tidy
+
+if [[ `git status --porcelain` ]]
+then
+	echo "Run `make format` and `make tidy` locally to fix this."
+	exit 1
+fi
diff --git a/kokoro/ubuntu/test.sh b/kokoro/ubuntu/test.sh
new file mode 100755
index 0000000..c313d6d
--- /dev/null
+++ b/kokoro/ubuntu/test.sh
@@ -0,0 +1,18 @@
+#!/bin/bash
+
+# Note: this assumes that the images have all been built and the current working
+# directory is the root of the repo.
+
+# Fail on any error.
+set -e
+# Display commands being run.
+set -x
+
+TIMEOUT="timeout --foreground"
+QEMU="./prebuilts/linux-x64/qemu/qemu-system-aarch64 -M virt -cpu cortex-a57 -m 8M -machine virtualization=true -nographic -nodefaults -serial stdio"
+HAFNIUM="out/clang_aarch64/hafnium.bin"
+INITRD="out/clang_aarch64/initrd"
+
+# Run the QEMU tests with a timeout so they can't loop forever.
+$TIMEOUT 5s $QEMU -kernel $HAFNIUM -initrd $INITRD/primary_only_test.img
+$TIMEOUT 5s $QEMU -kernel $HAFNIUM -initrd $INITRD/primary_with_secondary_test.img
diff --git a/prebuilts b/prebuilts
index 7ea7831..5e1ddfa 160000
--- a/prebuilts
+++ b/prebuilts
@@ -1 +1 @@
-Subproject commit 7ea7831e75ae22fa0a1467c302e656819132a6fd
+Subproject commit 5e1ddfae19f7052e9e27b1bf2c2d8ea3c9bcf5b0