Treat DIST_DIR and OUT_DIR as separate directories

If DIST_DIR is defined, create logs there and copy and
interesting artifacts to it.

The CI build (pKVM Busytown) expects build artifacts to be in
DIST_DIR. Previously the makefile would use the out directory for
that. However, this would clutter it with object files and
temporary build files that aren't needed.

Signed-off-by: Fuad Tabba <tabba@google.com>
Change-Id: I06669489553dbb4c1dc1900fb76701f12bc21ac0
diff --git a/Makefile b/Makefile
index 2c9fee4..0ff98b5 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
 
 ##
@@ -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
@@ -170,6 +183,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
 
 
@@ -184,6 +198,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
@@ -198,6 +213,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) \
@@ -223,10 +240,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)
@@ -300,7 +319,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)
@@ -322,7 +341,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