Build different platforms into different directories.

This avoids the different configs interfering with each other's builds.
Cleaning only affects the current build target while clobbering wipes
out all traces of the build for all targets.

Change-Id: Ia0135e118a4cf0b382d4833638648430ed1a2ac0
diff --git a/Makefile b/Makefile
index 9a2cf40..d6f537f 100644
--- a/Makefile
+++ b/Makefile
@@ -1,5 +1,3 @@
-OUT ?= out
-
 # Set path to prebuilts used in the build.
 UNNAME_S := $(shell uname -s | tr '[:upper:]' '[:lower:]')
 PREBUILTS := $(PWD)/prebuilts/$(UNNAME_S)-x64
@@ -8,22 +6,27 @@
 export PATH := $(PREBUILTS)/clang/bin:$(PATH)
 
 # Configure the build arguments.
-GCC ?= false
 ARCH ?= aarch64
 PLATFORM ?= qemu
+GCC ?= false
+
+# Place builds for different architectures and platforms in different
+# directories.
+OUT ?= out
+OUT_DIR = out/$(ARCH)/$(PLATFORM)
 
 .PHONY: all
-all: $(OUT)/build.ninja
-	@$(NINJA) -C $(OUT)
+all: $(OUT_DIR)/build.ninja
+	@$(NINJA) -C $(OUT_DIR)
 
-$(OUT)/build.ninja: $(OUT)/args.gn
-	@$(GN) --export-compile-commands gen $(OUT)
+$(OUT_DIR)/build.ninja: $(OUT_DIR)/args.gn
+	@$(GN) --export-compile-commands gen $(OUT_DIR)
 
 # Configure the build by loading the configuration arguments for the
 # architecture and platform.
-$(OUT)/args.gn: build/arch/$(ARCH)/$(PLATFORM).args
+$(OUT_DIR)/args.gn: build/arch/$(ARCH)/$(PLATFORM).args
 	@echo Copying config for $(ARCH) on $(PLATFORM)
-	@mkdir -p $(OUT)
+	@mkdir -p $(OUT_DIR)
 	@echo "arch = \"$(ARCH)\"" >> $@
 	@echo "use_gcc = $(GCC)" >> $@
 	@echo >> $@
@@ -31,8 +34,8 @@
 
 .PHONY: clean
 clean:
-	@$(NINJA) -C $(OUT) -t clean
-	rm -f $(OUT)/args.gn
+	@$(NINJA) -C $(OUT_DIR) -t clean
+	rm -f $(OUT_DIR)/args.gn
 
 .PHONY: clobber
 clobber:
@@ -49,15 +52,15 @@
 
 # see .clang-tidy.
 .PHONY: tidy
-tidy: $(OUT)/build.ninja
-	@$(NINJA) -C $(OUT)
+tidy: $(OUT_DIR)/build.ninja
+	@$(NINJA) -C $(OUT_DIR)
 	@echo "Tidying..."
-	@find src/ -name *.c -exec clang-tidy -p $(OUT) -fix {} \;
-	@find test/ -name *.c -exec clang-tidy -p $(OUT) -fix {} \;
+	@find src/ -name *.c -exec clang-tidy -p $(OUT_DIR) -fix {} \;
+	@find test/ -name *.c -exec clang-tidy -p $(OUT_DIR) -fix {} \;
 
 .PHONY: check
-check: $(OUT)/build.ninja
-	@$(NINJA) -C $(OUT)
+check: $(OUT_DIR)/build.ninja
+	@$(NINJA) -C $(OUT_DIR)
 	@echo "Checking..."
-	@find src/ -name *.c -exec clang-check -p $(OUT) -analyze {} \;
-	@find test/ -name *.c -exec clang-check -p $(OUT) -analyze {} \;
+	@find src/ -name *.c -exec clang-check -p $(OUT_DIR) -analyze {} \;
+	@find test/ -name *.c -exec clang-check -p $(OUT_DIR) -analyze {} \;
diff --git a/kokoro/ubuntu/build.sh b/kokoro/ubuntu/build.sh
index 4596757..9a7f510 100755
--- a/kokoro/ubuntu/build.sh
+++ b/kokoro/ubuntu/build.sh
@@ -11,17 +11,18 @@
 then
 	# Server
 	cd git/hafnium
-	make out/args.gn
 else
 	# Local
 	echo "Testing kokoro build locally..."
-	make out/args.gn
 fi
 
 #
 # Step 1: make sure it builds.
 #
 
+export ARCH=aarch64
+export PLATFORM=qemu
+
 # TODO: add a gcc-4.9 or above prebuilt to check the gcc build too?
 # Check the build works.
 make
diff --git a/kokoro/ubuntu/test.sh b/kokoro/ubuntu/test.sh
index c313d6d..623c4aa 100755
--- a/kokoro/ubuntu/test.sh
+++ b/kokoro/ubuntu/test.sh
@@ -10,8 +10,8 @@
 
 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"
+HAFNIUM="out/aarch64/qemu/clang_aarch64/hafnium.bin"
+INITRD="out/aarch64/qemu/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