portage: add wrappers/helpers for interacting with portage

This creates emerge/ebuild/portageq wrappers people can execute directly
(as well as the android build system will use), and the target-specific
config files they rely on.

The tools should work off of the android env vars to locate the current
board/build directories rather than hardcoding any of them in the output.

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

Change-Id: I97171f3d8d88230880af9ba3045d691951e0b5fe
diff --git a/portage/Android.mk b/portage/Android.mk
new file mode 100644
index 0000000..1c2d500
--- /dev/null
+++ b/portage/Android.mk
@@ -0,0 +1,163 @@
+LOCAL_PATH := $(call my-dir)
+
+PORTAGE_PREFIX := $(LOCAL_PATH)/../../portage
+BINDIR := $(PORTAGE_PREFIX)/usr/bin
+
+define CREATE_WRAPPER
+	$(hide)mkdir -p $(dir $@)
+	$(hide) echo $(HOST_OUT) $(3RD_PARTY_ROOT)
+	$(hide)sed \
+		-e 's:@ROOT_SUBDIR@:$(3RD_PARTY_ROOT_SUBDIR):g' \
+		-e 's:@BIN@:$(BINDIR)/$(notdir $@):' \
+		$< > $@.tmp \
+		&& chmod a+rx $@.tmp && mv $@.tmp $@
+endef
+
+CBUILD = $(CLANG_CONFIG_$(HOST_ARCH)_HOST_TRIPLE)
+CHOST  = $(CLANG_CONFIG_$(TARGET_ARCH)_TARGET_TRIPLE)
+
+# Map android's arch naming to Gentoo's.
+ANDROID_ARCH-mips64 = mips
+ANDROID_ARCH-x86_64 = amd64
+gentoo-arch = $(if $(ANDROID_ARCH-$(1)),$(ANDROID_ARCH-$(1)),$(1))
+HOST_KEYWORD   := $(call gentoo-arch,$(HOST_ARCH))
+TARGET_KEYWORD := $(call gentoo-arch,$(TARGET_ARCH))
+
+# Common build doesn't set this up.
+TARGET_RANLIB ?= $(TARGET_AR:ar=ranlib)
+
+MAIN_OVERLAY_REPO := `readlink -f $(LOCAL_PATH)`/../../overlays/gentoo
+MAIN_OVERLAY_REPO_REL := $(LOCAL_PATH)/../../overlays/gentoo
+
+include $(CLEAR_VARS)
+
+#
+# Create /etc/portage config files.
+#
+
+3RD_PARTY_CONFIGS :=
+
+make.profile := $(3RD_PARTY_ROOT)/etc/portage/make.profile
+$(make.profile):
+	$(hide)mkdir -p $(dir $@)
+	$(hide)ln -sfT \
+		`echo $(dir $@) | sed 's:[^/]*/:../:g'`/$(MAIN_OVERLAY_REPO_REL)/profiles/default/linux/$(TARGET_KEYWORD) \
+		$@
+
+LOCAL_MODULE := make.conf
+LOCAL_MODULE_CLASS := ETC
+LOCAL_MODULE_PATH := $(3RD_PARTY_ROOT)/etc/portage
+3RD_PARTY_CONFIGS += $(LOCAL_MODULE_PATH)/$(LOCAL_MODULE)
+
+include $(BUILD_SYSTEM)/base_rules.mk
+
+make.conf := $(intermediates)/make.conf
+$(make.conf): $(LOCAL_PATH)/make.conf.in $(make.profile)
+	$(hide)mkdir -p $(dir $@)
+	$(hide)sed \
+		-e 's:@CC@:$(notdir $(3RD_PARTY_COMPILER)):g' \
+		-e 's:@CXX@:$(notdir $(TARGET_CXX)):g' \
+		-e 's:@AR@:$(notdir $(TARGET_AR)):g' \
+		-e 's:@RANLIB@:$(notdir $(TARGET_RANLIB)):g' \
+		-e 's:@LD@:$(notdir $(TARGET_LD)):g' \
+		-e 's:@OBJCOPY@:$(notdir $(TARGET_OBJCOPY)):g' \
+		-e 's:@STRIP@:$(notdir $(TARGET_STRIP)):g' \
+		-e 's:@READELF@:$(notdir $(TARGET_READELF)):g' \
+		-e 's:@ARCH@:$(TARGET_ARCH):g' \
+		-e 's:@CBUILD@:$(CBUILD):g' \
+		-e 's:@CHOST@:$(CHOST):g' \
+		-e 's:@COMMON_OBJ@:$${ANDROID_BUILD_TOP}/$(TARGET_OUT_COMMON_GENTOO):' \
+		-e 's:@PRODUCT_OBJ@:$${ANDROID_PRODUCT_OUT}/obj/gentoo:g' \
+		-e 's:@PORTAGE_PREFIX@:$(PORTAGE_PREFIX):g' \
+		$< > $@
+
+include $(CLEAR_VARS)
+
+LOCAL_MODULE := package.provided
+LOCAL_MODULE_CLASS := ETC
+LOCAL_MODULE_PATH := $(3RD_PARTY_ROOT)/etc/portage/profile
+3RD_PARTY_CONFIGS += $(LOCAL_MODULE_PATH)/$(LOCAL_MODULE)
+
+include $(BUILD_SYSTEM)/base_rules.mk
+
+package.provided := $(intermediates)/package.provided
+$(package.provided): $(LOCAL_PATH)/package.provided.in
+	$(hide)mkdir -p $(dir $@)
+	$(hide)cp $< $@
+
+include $(CLEAR_VARS)
+
+LOCAL_MODULE := gentoo.conf
+LOCAL_MODULE_CLASS := ETC
+LOCAL_MODULE_PATH := $(3RD_PARTY_ROOT)/etc/portage/repos.conf
+3RD_PARTY_CONFIGS += $(LOCAL_MODULE_PATH)/$(LOCAL_MODULE)
+
+include $(BUILD_SYSTEM)/base_rules.mk
+
+gentoo.conf := $(intermediates)/gentoo.conf
+$(gentoo.conf): $(LOCAL_PATH)/repos.conf.in
+	$(hide)mkdir -p $(dir $@)
+	$(hide)sed -e "s:@OVERLAY_LOCATION@:$(MAIN_OVERLAY_REPO):" $< > $@
+
+#
+# Create various bin/ wrappers.
+# XXX: These contain product specific paths and should not.
+#
+
+3RD_PARTY_WRAPPERS :=
+
+include $(CLEAR_VARS)
+
+LOCAL_MODULE := ebuild
+LOCAL_MODULE_CLASS := EXECUTABLES
+LOCAL_IS_HOST_MODULE := true
+3RD_PARTY_WRAPPERS += $(HOST_OUT_EXECUTABLES)/$(LOCAL_MODULE)
+
+include $(BUILD_SYSTEM)/base_rules.mk
+
+ebuild := $(intermediates)/$(LOCAL_MODULE)
+$(ebuild): $(LOCAL_PATH)/wrapper.in
+	$(CREATE_WRAPPER)
+
+LOCAL_BUILT_MODULE := $(ebuild)
+
+include $(CLEAR_VARS)
+
+LOCAL_MODULE := emerge
+LOCAL_MODULE_CLASS := EXECUTABLES
+LOCAL_IS_HOST_MODULE := true
+3RD_PARTY_WRAPPERS += $(HOST_OUT_EXECUTABLES)/$(LOCAL_MODULE)
+
+include $(BUILD_SYSTEM)/base_rules.mk
+
+LOCAL_BUILT_MODULE := $(intermediates)/$(LOCAL_MODULE)
+$(LOCAL_BUILT_MODULE): $(LOCAL_PATH)/wrapper.in | $(patsubst %,$(HOST_OUT_EXECUTABLES)/%,ebuild portageq)
+	$(CREATE_WRAPPER)
+
+include $(CLEAR_VARS)
+
+LOCAL_MODULE := portageq
+LOCAL_MODULE_CLASS := EXECUTABLES
+LOCAL_IS_HOST_MODULE := true
+3RD_PARTY_WRAPPERS += $(HOST_OUT_EXECUTABLES)/$(LOCAL_MODULE)
+
+include $(BUILD_SYSTEM)/base_rules.mk
+
+portageq := $(intermediates)/portageq
+$(portageq): $(LOCAL_PATH)/wrapper.in
+	$(CREATE_WRAPPER)
+
+LOCAL_BUILT_MODULE := $(portageq)
+
+include $(CLEAR_VARS)
+
+LOCAL_MODULE := 3rd-party-merge
+LOCAL_MODULE_CLASS := EXECUTABLES
+LOCAL_IS_HOST_MODULE := true
+3RD_PARTY_WRAPPERS += $(HOST_OUT_EXECUTABLES)/$(LOCAL_MODULE)
+
+include $(BUILD_SYSTEM)/base_rules.mk
+
+LOCAL_BUILT_MODULE := $(intermediates)/$(LOCAL_MODULE)
+$(LOCAL_BUILT_MODULE): $(LOCAL_PATH)/wrapper.in
+	$(CREATE_WRAPPER)
diff --git a/portage/make.conf.in b/portage/make.conf.in
new file mode 100644
index 0000000..99dc330
--- /dev/null
+++ b/portage/make.conf.in
@@ -0,0 +1,37 @@
+# AUTO GENERATED.  DO NOT TOUCH.  DO NOT ASK OTHERS TO TOUCH.
+
+EPREFIX='/'
+PORTAGE_PREFIX="${ANDROID_BUILD_TOP}/@PORTAGE_PREFIX@"
+
+ROOTPATH="/usr/bin:/bin:/usr/sbin:/sbin:${PORTAGE_PREFIX}/usr/bin:${ANDROID_BUILD_PATHS}"
+
+ACCEPT_KEYWORDS="~${ARCH}"
+
+PORTAGE_BUNZIP2_COMMAND="bzip2"
+FEATURES="-sandbox -usersandbox"
+EMERGE_DEFAULT_OPTS="--root-deps=rdeps --quiet-build y --jobs 4"
+
+COMMON_OBJ="@COMMON_OBJ@"
+DISTDIR="${COMMON_OBJ}/distdir"
+
+PRODUCT_OBJ="@PRODUCT_OBJ@"
+SYSROOT="${PRODUCT_OBJ}/root"
+ROOT="${SYSROOT}"
+PORTAGE_TMPDIR="${PRODUCT_OBJ}/tmp"
+PORT_LOGDIR="${PRODUCT_OBJ}/logs"
+PKGDIR="${PRODUCT_OBJ}/packages"
+PKG_CONFIG="${SYSROOT}/build/bin/pkg-config"
+
+CC="@CC@"
+CXX="@CXX@"
+AR="@AR@"
+RANLIB="@RANLIB@"
+LD="@LD@"
+OBJCOPY="@OBJCOPY@"
+STRIP="@STRIP@"
+READELF="@READELF@"
+
+CBUILD="@CBUILD@"
+CHOST="@CHOST@"
+
+WANT_AUTOMAKE="1.14"
diff --git a/portage/package.provided.in b/portage/package.provided.in
new file mode 100644
index 0000000..92aded8
--- /dev/null
+++ b/portage/package.provided.in
@@ -0,0 +1,4 @@
+# AUTO GENERATED.
+# We use the git eclass which wants git to build things, but we don't actually
+# need it in the sysroot as it's a host tool.  Stub it out.
+dev-vcs/git-2
diff --git a/portage/repos.conf.in b/portage/repos.conf.in
new file mode 100644
index 0000000..8434785
--- /dev/null
+++ b/portage/repos.conf.in
@@ -0,0 +1,5 @@
+[DEFAULT]
+main-repo = gentoo
+
+[gentoo]
+location = @OVERLAY_LOCATION@
diff --git a/portage/wrapper.in b/portage/wrapper.in
new file mode 100644
index 0000000..5c1209f
--- /dev/null
+++ b/portage/wrapper.in
@@ -0,0 +1,4 @@
+#!/bin/sh
+PORTAGE_CONFIGROOT="${ANDROID_PRODUCT_OUT}/@ROOT_SUBDIR@" \
+ROOT="${PORTAGE_CONFIGROOT}" \
+exec "${ANDROID_BUILD_TOP}/@BIN@" "$@"