toolchain: add wrapper logic

The way android builds things is via relative paths everywhere and with
the working directory of the top level checkout.  When we go to build
external packages though, they often need to be in a different working
directory which means all of the relative paths break.

Add a compiler wrapper to merge the existing android env vars and the
relative paths to create absolute paths we can leverage.

Further, android will pass flags to the system paths during compile and
link.  If those aren't used, then the build/link will fail in weird ways.
Unfortunately, some build systems might filter/sort flags, so add those
to the wrapper too rather than try and track down/fix every build system.

BUG=24611334
TEST=built packages and they worked in qemu

Change-Id: I93432c7ed23941cd44d799e7a8bdb83d745d1952
diff --git a/toolchain/3rd-party-compiler.in b/toolchain/3rd-party-compiler.in
new file mode 100644
index 0000000..ef8e708
--- /dev/null
+++ b/toolchain/3rd-party-compiler.in
@@ -0,0 +1,8 @@
+#!/bin/sh
+exec \
+	"${ANDROID_TOOLCHAIN}/@CC@" \
+	@CFLAGS@ \
+	@LDFLAGS@ \
+	--sysroot "${ANDROID_PRODUCT_OUT}/@ROOT_SUBDIR@" \
+	-Wl,-rpath,/system/usr/lib \
+	"$@"
diff --git a/toolchain/Android.mk b/toolchain/Android.mk
new file mode 100644
index 0000000..2e40683
--- /dev/null
+++ b/toolchain/Android.mk
@@ -0,0 +1,25 @@
+LOCAL_PATH := $(call my-dir)
+
+include $(CLEAR_VARS)
+LOCAL_MODULE := 3rd-party-compiler
+LOCAL_MODULE_CLASS := EXECUTABLES
+LOCAL_IS_HOST_MODULE := true
+
+include $(BUILD_SYSTEM)/base_rules.mk
+
+compiler = $(intermediates)/$(LOCAL_MODULE)
+$(compiler): $(LOCAL_PATH)/3rd-party-compiler.in
+	@mkdir -p $(dir $@)
+	$(hide): $(PRODUCT_OUT) $(TARGET_OUT_INTERMEDIATE_LIBRARIES)
+	$(hide)sed \
+		-e 's:@CC@:$(notdir $(TARGET_CC)):' \
+		-e 's:@CFLAGS@:$(foreach p,$(TARGET_C_INCLUDES),-isystem "$${ANDROID_BUILD_TOP}/$(p)"):' \
+		-e 's:@LDFLAGS@:-B"$(patsubst $(PRODUCT_OUT)/%,$${ANDROID_PRODUCT_OUT}/%,$(TARGET_OUT_INTERMEDIATE_LIBRARIES))":' \
+		-e 's:@ROOT_SUBDIR@:$(3RD_PARTY_ROOT_SUBDIR):g' \
+		$< > $@.tmp \
+		&& chmod a+rx $@.tmp && mv $@.tmp $@
+
+LOCAL_BUILT_MODULE = $(compiler)
+LOCAL_GENERATED_SOURCES += $(3RD_PARTY_COMPILER)
+
+include $(CLEAR_VARS)