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
+SOONG_HOST_EXECUTABLES := $(SOONG_OUT_DIR)/host/$(HOST_PREBUILT_TAG)/bin
+KATI := $(SOONG_HOST_EXECUTABLES)/ckati
+MAKEPARALLEL := $(SOONG_HOST_EXECUTABLES)/makeparallel
+
+# 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))
+endif
+else
+SOONG_TOP_RELPATH := $(abspath $(TOP))
+endif
+
 SOONG := $(SOONG_OUT_DIR)/soong
 SOONG_BUILD_NINJA := $(SOONG_OUT_DIR)/build.ninja
 SOONG_ANDROID_MK := $(SOONG_OUT_DIR)/Android.mk
 SOONG_VARIABLES := $(SOONG_OUT_DIR)/soong.variables
 SOONG_IN_MAKE := $(SOONG_OUT_DIR)/.soong.in_make
-SOONG_HOST_EXECUTABLES := $(SOONG_OUT_DIR)/host/$(HOST_PREBUILT_TAG)/bin
-KATI := $(SOONG_HOST_EXECUTABLES)/ckati
-MAKEPARALLEL := $(SOONG_HOST_EXECUTABLES)/makeparallel
-
-ifeq (,$(filter /%,$(SOONG_OUT_DIR)))
-SOONG_TOP_RELPATH := $(shell python -c "import os; print os.path.relpath('$(TOP)', '$(SOONG_OUT_DIR)')")
-else
-SOONG_TOP_RELPATH := $(realpath $(TOP))
-endif
 
 # Bootstrap soong.  Run only the first time for clean builds
 $(SOONG):