Merge "Add build rules for u-boot"
diff --git a/Makefile b/Makefile
index 45e44b1..0d05b19 100644
--- a/Makefile
+++ b/Makefile
@@ -22,14 +22,27 @@
 ##
 ROOT_DIR := $(CURDIR)
 
-# DIST_DIR might be defined by the CI build (pKVM Busytown)
-ifdef DIST_DIR
-OUT_DIR := $(DIST_DIR)
-else
 OUT_DIR := $(ROOT_DIR)/out
+
+# DIST_DIR: target directory for artifacts and logs.
+# The CI build (pKVM Busytown) uses it to separate artifacts from other
+# temporary build files, which are placed in in OUT_DIR.
+# For other users, OUT_DIR and DIST_DIR are typically the same.
+DIST_DIR ?= $(OUT_DIR)
+
+# Copy specified file or directory only if OUT_DIR and DIST_DIR aren't the same.
+#
+# Parameters:
+# $(1): Source files or directories (to be copied recursively)
+# $(2): Destination directory (is created if it doesn't exist)
+ifneq ($(abspath $(OUT_DIR)), $(abspath $(DIST_DIR)))
+define copy_to_dist
+	mkdir -p $(2) && cp -r $(1) $(2)
+endef
 endif
 
 LINUX_OUT := $(OUT_DIR)/linux
+LINUX_DIST := $(DIST_DIR)/linux
 SCRIPTS_DIR := $(ROOT_DIR)/build
 
 ##
@@ -56,7 +69,7 @@
 
 # Find the toolchain path for the current OS.
 UNNAME_S := $(shell uname -s | tr '[:upper:]' '[:lower:]')
-TOOLCHAIN_CLANG := $(ROOT_DIR)/toolchains/$(UNNAME_S)-x86/clang/clang-r383902/
+TOOLCHAIN_CLANG := $(ROOT_DIR)/toolchains/$(UNNAME_S)-x86/clang/clang-r407598b/
 TOOLCHAIN_GCC_49 := $(ROOT_DIR)/toolchains/$(UNNAME_S)-x86/gcc/aarch64-linux-gnu-4.9/
 TOOLCHAIN_GCC_51 := $(ROOT_DIR)/toolchains/$(UNNAME_S)-x86/gcc/aarch64-linux-gnu-5.1/
 TOOLCHAIN_GCC_92 := $(ROOT_DIR)/toolchains/$(UNNAME_S)-x86/gcc/aarch64-linux-gnu-9.2/
@@ -82,7 +95,7 @@
 OBJDUMP := $(TOOLCHAIN_BIN)/llvm-objdump
 # The GCC toolchain is needed when building kvm-unit-tests as well as Linux
 # with Clang, because of lack of support for the LLVM integrated assembler.
-GCC_TOOLCHAIN_DIR = $(TOOLCHAIN_GCC_51)
+GCC_TOOLCHAIN_DIR = $(TOOLCHAIN_GCC_49)
 GCC_TOOLCHAIN_BIN = $(GCC_TOOLCHAIN_DIR)/bin/
 LLVM = "LLVM=1"
 # Some targets require additional flags to build with Clang.
@@ -114,7 +127,7 @@
 
 .PHONY: distclean
 distclean:
-	- rm -rf $(OUT_DIR)
+	- rm -rf $(OUT_DIR) $(DIST_DIR)
 
 .PHONY: all
 all: buildroot kvm-unit-tests linux u-boot
@@ -172,6 +185,7 @@
 ##
 KUT_SRC := $(ROOT_DIR)/kvm-unit-tests
 KUT_OUT := $(OUT_DIR)/kvm-unit-tests
+KUT_DIST := $(DIST_DIR)/kvm-unit-tests
 KUT_STAMP := $(KUT_OUT)/kvm-unit-tests.stamp
 
 
@@ -186,6 +200,7 @@
 			--objcopy=$(OBJCOPY) --objdump=$(OBJDUMP)
 	COMMON_CFLAGS="$(COMMON_CFLAGS)" \
 		$(MAKE) -C $(KUT_OUT) standalone
+	$(call copy_to_dist, $(KUT_OUT)/tests, $(KUT_DIST))
 	touch $(KUT_STAMP)
 
 .PHONY: kvm-unit-tests_clean
@@ -200,6 +215,8 @@
 LINUX_DEFCONFIG ?= defconfig
 LINUX_VERBOSE ?= $(VERBOSE)
 LINUX_SRC ?= $(ROOT_DIR)/linux
+LINUX_VMLINUX = $(LINUX_OUT)/vmlinux
+LINUX_DIST_FILES = $(LINUX_CONFIG) $(LINUX_VMLINUX) $(KERNEL_IMAGE)
 
 LINUX_MAKE := \
 	PATH=$(TOOLCHAIN_BIN):$(PATH) \
@@ -225,10 +242,12 @@
 .PHONY: linux
 linux: $(LINUX_CONFIG)
 	+ $(LINUX_MAKE)
+	$(call copy_to_dist, $(LINUX_DIST_FILES), $(LINUX_DIST))
 
 .PHONY: linux_image
 linux_image: $(LINUX_CONFIG)
 	+ $(LINUX_MAKE) Image.gz
+	$(call copy_to_dist, $(LINUX_DIST_FILES), $(LINUX_DIST))
 
 # If using own kernel image (KERNEL_IMAGE is set in the environment), then skip.
 ifeq ($(CUSTOM_KERNEL_IMAGE), 0)
@@ -357,7 +376,7 @@
 KUT_GEN_MAKEFILE := $(SCRIPTS_DIR)/kvm-unit-tests/gen_makefile.sh
 KUT_MAKEFILE := $(OUT_DIR)/Makefile.kvm-unit-tests
 KUT_TEST_DIR := $(KUT_OUT)/tests
-KUT_LOG_DIR := $(OUT_DIR)/test/kvm-unit-tests/
+KUT_LOG_DIR := $(DIST_DIR)/logs/kvm-unit-tests/
 
 # Exclude tests that require user interaction or are known to fail.
 KUT_EXCLUDE := (.+migrat.+)|(gicv2-.+)|(pmu-event-introspection)|(micro-bench)
@@ -379,7 +398,7 @@
 #
 
 CROSVM_HELLOWORLD_SCRIPT_DIR := $(SCRIPTS_DIR)/crosvm/helloworld/
-CROSVM_HELLOWORLD_LOG_DIR := $(OUT_DIR)/test/crosvm/helloworld/
+CROSVM_HELLOWORLD_LOG_DIR := $(DIST_DIR)/logs/crosvm/helloworld/
 CROSVM_HELLOWORLD_TESTS := test-crosvm-vhe-helloworld \
 			   test-crosvm-nvhe-helloworld \
 			   test-crosvm-pkvm-helloworld
diff --git a/aarch64/run_qemu.sh b/aarch64/run_qemu.sh
index 9ffea8c..924177b 100755
--- a/aarch64/run_qemu.sh
+++ b/aarch64/run_qemu.sh
@@ -22,7 +22,7 @@
 default_var ROOTFS		"${PREBUILTS_KUT_ROOTFS}"
 default_var TEMPLATE		""
 default_var CPU			"max"
-default_var SMP			2
+default_var SMP			4
 default_var RAM			512
 default_var GIC			3
 default_var GDB			0