kernel: Use absolute path for kernel O= argument

The kernel make command is executed in the build root. Without absolute
out paths, kernel out start with "out/" meaning it would be created in
the kernel source. Add BUILD_ROOT to the argument to ensure it's always
made in the actual out/ dir.

Change-Id: I425134a893d4d5c9f31efccaf75e153b5462b76c
diff --git a/build/soong/Android.bp b/build/soong/Android.bp
index a666b29..cd9f16c 100644
--- a/build/soong/Android.bp
+++ b/build/soong/Android.bp
@@ -27,7 +27,7 @@
     name: "generated_kernel_includes",
 
     // The headers make command
-    cmd: "$(PATH_OVERRIDE_SOONG) $(KERNEL_MAKE_CMD) $(KERNEL_MAKE_FLAGS) -C $(TARGET_KERNEL_SOURCE) O=$(genDir) ARCH=$(KERNEL_ARCH) $(KERNEL_CROSS_COMPILE) headers_install",
+    cmd: "$(PATH_OVERRIDE_SOONG) $(KERNEL_MAKE_CMD) $(KERNEL_MAKE_FLAGS) -C $(TARGET_KERNEL_SOURCE) O=$(KERNEL_BUILD_OUT_PREFIX)$(genDir) ARCH=$(KERNEL_ARCH) $(KERNEL_CROSS_COMPILE) headers_install",
 
     // Directories that can be imported by a cc_* module generated_headers property
     export_include_dirs: ["usr/include", "usr/techpack/audio/include"],
diff --git a/build/tasks/kernel.mk b/build/tasks/kernel.mk
index a199eb2..f4dd92d 100644
--- a/build/tasks/kernel.mk
+++ b/build/tasks/kernel.mk
@@ -196,7 +196,7 @@
 # $(1): output path (The value passed to O=)
 # $(2): target to build (eg. defconfig, modules, dtbo.img)
 define internal-make-kernel-target
-$(PATH_OVERRIDE) $(KERNEL_MAKE_CMD) $(KERNEL_MAKE_FLAGS) -C $(KERNEL_SRC) O=$(1) ARCH=$(KERNEL_ARCH) $(KERNEL_CROSS_COMPILE) $(KERNEL_CLANG_TRIPLE) $(KERNEL_CC) $(2)
+$(PATH_OVERRIDE) $(KERNEL_MAKE_CMD) $(KERNEL_MAKE_FLAGS) -C $(KERNEL_SRC) O=$(KERNEL_BUILD_OUT_PREFIX)$(1) ARCH=$(KERNEL_ARCH) $(KERNEL_CROSS_COMPILE) $(KERNEL_CLANG_TRIPLE) $(KERNEL_CC) $(2)
 endef
 
 # Make a kernel target
diff --git a/config/BoardConfigKernel.mk b/config/BoardConfigKernel.mk
index e923b94..18291fd 100644
--- a/config/BoardConfigKernel.mk
+++ b/config/BoardConfigKernel.mk
@@ -148,3 +148,11 @@
 endif
 KERNEL_MAKE_FLAGS += HOSTCC=$(KERNEL_HOST_TOOLCHAIN_ROOT)gcc
 KERNEL_MAKE_FLAGS += HOSTCXX=$(KERNEL_HOST_TOOLCHAIN_ROOT)g++
+
+# Set the out dir for the kernel's O= arg
+# This needs to be an absolute path, so only set this if the standard out dir isn't used
+OUT_DIR_PREFIX := $(shell echo $(OUT_DIR) | sed -e 's|/target/.*$$||g')
+KERNEL_BUILD_OUT_PREFIX :=
+ifeq ($(OUT_DIR_PREFIX),out)
+KERNEL_BUILD_OUT_PREFIX := $(BUILD_TOP)/
+endif
diff --git a/config/BoardConfigSoong.mk b/config/BoardConfigSoong.mk
index fbdbd04..f3e9b66 100644
--- a/config/BoardConfigSoong.mk
+++ b/config/BoardConfigSoong.mk
@@ -3,6 +3,7 @@
 # Add variables that we wish to make available to soong here.
 EXPORT_TO_SOONG := \
     KERNEL_ARCH \
+    KERNEL_BUILD_OUT_PREFIX \
     KERNEL_CROSS_COMPILE \
     KERNEL_MAKE_CMD \
     KERNEL_MAKE_FLAGS \