Handle out being a symlink

Soong needs to be able to reverse the relative path to out. Switch to
absolute paths if .. won't work because of out being a symlink to
somewhere else.

Bug: 27612837
Change-Id: Ib8b9e0f80523148cfd4b69a03d164578916d8fd5
(cherry picked from commit d53c0c7b45c3b18949fc62b784a8e8a877323493)
[ccross: resolved conflicts in nyc-dev]
diff --git a/core/soong.mk b/core/soong.mk
index 578aac9..dee9bb8 100644
--- a/core/soong.mk
+++ b/core/soong.mk
@@ -1,18 +1,27 @@
 SOONG_OUT_DIR := $(OUT_DIR)/soong
+# This needs to exist before the realpath checks below
+$(shell mkdir -p $(SOONG_OUT_DIR))
+ifeq (,$(filter /%,$(SOONG_OUT_DIR)))
+SOONG_TOP_RELPATH := $(shell python -c "import os; print os.path.relpath('$(TOP)', '$(SOONG_OUT_DIR)')")
+# Protect against out being a symlink and relative paths not working
+ifneq ($(realpath $(SOONG_OUT_DIR)/$(SOONG_TOP_RELPATH)),$(realpath $(TOP)))
+SOONG_OUT_DIR := $(abspath $(SOONG_OUT_DIR))
+SOONG_TOP_RELPATH := $(abspath $(TOP))
+SOONG_TOP_RELPATH := $(abspath $(TOP))
 SOONG := $(SOONG_OUT_DIR)/soong
 SOONG_VARIABLES := $(SOONG_OUT_DIR)/soong.variables
 SOONG_IN_MAKE := $(SOONG_OUT_DIR)/.soong.in_make
-ifeq (,$(filter /%,$(SOONG_OUT_DIR)))
-SOONG_TOP_RELPATH := $(shell python -c "import os; print os.path.relpath('$(TOP)', '$(SOONG_OUT_DIR)')")
-SOONG_TOP_RELPATH := $(realpath $(TOP))
 # Bootstrap soong.  Run only the first time for clean builds