handle external/ libs that we also have ebuilds for

Since we have a bunch of libs already in external/, make sure we stub
out all the ebuilds that duplicate things.  This way we don't build
from two different sources and clobber things in weird ways.

In order for packages to find their headers though, we need to set up
a system include tree for packages that aren't "installed".

BUG=23171578

Change-Id: Idb5a4711dd30f9c5011dda8d06a874849a3609cb
diff --git a/Android.mk b/Android.mk
index b3f463f..7d49ce3 100644
--- a/Android.mk
+++ b/Android.mk
@@ -21,9 +21,53 @@
 3RD_PARTY_WRAPPERS :=
 include $(call all-subdir-makefiles)
 
+# Some libraries are available in external/ already.
+# Note: Keep this in sync with gentoo/integration/portage/package.provided.in.
+# We do not list packages that do not provide libs/headers.
+3RD_PARTY_EXTERNAL_LIBS := \
+	libavahi-client libavahi-common libavahi-core \
+	libbz \
+	libcrypto libssl \
+	libcares \
+	libcurl \
+	libdbus \
+	libext2_blkid libext2_com_err libext2_e2p libext2fs libext2_quota libext2_uuid libtune2fs \
+	libdw libdwelf libdwfl libebl libelf \
+	libexpat \
+	libFLAC \
+	libgmock \
+	libgtest \
+	libicui18n libicuuc \
+	libiprouteutil libnetlink \
+	libip4tc libip6tc libxtables \
+	libjemalloc \
+	libjpeg \
+	libcap \
+	libcap-ng \
+	libdaemon \
+	libdivsufsort libdivsufsort64 \
+	libevent \
+	libmicrohttpd \
+	libnl \
+	libogg \
+	libopus \
+	libselinux \
+	libunwind \
+	libvpx libwebm \
+	libpcre libpcrecpp \
+	libsepol \
+	libspeex \
+	libtinyxml \
+	libtinyxml2 \
+	libwpa_client \
+	libz \
+	$(NULL)
+# Only depend on the packages that are actually requested.
+3RD_PARTY_EXTERNAL_DEPS += $(filter $(3RD_PARTY_EXTERNAL_LIBS),$(product_MODULES) $(PRODUCT_PACKAGES))
+
 # Actually run the emerge command for the specific package.
 %.emerge: TARGET_TO_ATOM = $(notdir $(patsubst %/,%,$(dir $(1))))/$(notdir $(1:.emerge=))
-%.emerge: | $(3RD_PARTY_WRAPPERS) $(3RD_PARTY_CONFIGS)
+%.emerge: | $(3RD_PARTY_WRAPPERS) $(3RD_PARTY_CONFIGS) $(3RD_PARTY_EXTERNAL_DEPS)
 	$(hide)mkdir -p $(dir $@) $(TARGET_OUT_GENTOO)/tmp
 	$(hide)emerge $(call TARGET_TO_ATOM,$@)
 	$(hide)3rd-party-merge \
diff --git a/external-packages/include/curl b/external-packages/include/curl
new file mode 120000
index 0000000..f26a48c
--- /dev/null
+++ b/external-packages/include/curl
@@ -0,0 +1 @@
+../../../../curl/include/curl
\ No newline at end of file
diff --git a/portage/package.provided.in b/portage/package.provided.in
index fc73a07..9abe373 100644
--- a/portage/package.provided.in
+++ b/portage/package.provided.in
@@ -15,9 +15,6 @@
 # We have bionic available for the system C library.
 virtual/libc-0
 
-# Android includes its own certificate store.
-app-misc/ca-certificates-20150826
-
 # Android doesn't believe in including an editor ;).  You can use adb pull to
 # copy a file locally, edit it however you want, and then adb push it back to
 # the device.
@@ -36,3 +33,57 @@
 # We don't tweak runtime knobs on the device itself.
 app-admin/eselect-1.4.5
 app-eselect/eselect-python-20140125
+
+# Some packages are provided by external/ and such instead.
+# We use a large -r# so that various changes in ebuilds don't try to pull
+# in the version anyways (e.g. requiring >=r1 and we only have r0 below).
+# Note: Keep this in sync with gentoo/integration/Android.mk.
+sys-libs/timezone-data-2015g-r999        # bionic/libc/zoneinfo/
+net-dns/avahi-0.6.31-r999                # external/avahi/
+app-arch/bzip2-1.0.6-r999                # external/bzip2/
+dev-libs/openssl-1.0.2-r999              # external/boringssl/
+net-dns/c-ares-1.7.5-r999                # external/c-ares/
+net-misc/curl-7.43.0-r999                # external/curl/
+sys-apps/dbus-1.6.8-r999                 # external/dbus/
+net-misc/dhcpcd-6.8.2-r999               # external/dhcpcd-6.8.2/
+net-dns/dnsmasq-2.51-r999                # external/dnsmasq/
+sys-fs/e2fsprogs-1.42.9-r999             # external/e2fsprogs/
+sys-libs/e2fsprogs-libs-1.42.9-r999      # external/e2fsprogs/
+dev-libs/elfutils-0.164-r999             # external/elfutils/
+dev-libs/expat-2.1.0-r999                # external/expat/
+media-libs/flac-1.3.1-r999               # external/flac/
+dev-cpp/gmock-1.7.0-r999                 # external/gmock/
+dev-cpp/gtest-1.7.0-r999                 # external/gtest/
+dev-libs/icu-56.1-r999                   # external/icu/
+sys-apps/iproute2-4.0.0-r999             # external/iproute2/
+net-firewall/iptables-1.4.20-r999        # external/iptables/
+net-misc/iputils-20121221-r999           # external/iputils/
+net-wireless/iw-4.1-r999                 # external/iw/
+dev-libs/jemalloc-4.0.1-r999             # external/jemalloc/
+media-libs/jpeg-6b-r999                  # external/jpeg/
+sys-libs/libcap-2.24-r999                # external/libcap/
+sys-libs/libcap-ng-0.7-r999              # external/libcap-ng/
+dev-libs/libdaemon-0.14-r999             # external/libdaemon/
+dev-libs/libdivsufsort-2.0.1-r999        # external/libdivsufsort/
+dev-libs/libevent-2.0.22-r999            # external/libevent/
+net-libs/libmicrohttpd-0.9.42-r999       # external/libmicrohttpd/
+dev-libs/libnl-2.0-r999                  # external/libnl/
+media-libs/libogg-1.2.0-r999             # external/libogg/
+media-libs/opus-1.1-r999                 # external/libopus/
+sys-libs/libselinux-2.4-r999             # external/libselinux/
+sys-libs/libunwind-1.1-r999              # external/libunwind/
+media-libs/libvpx-1.3.0-r999             # external/libvpx/
+dev-libs/libpcre-8.37-r999               # external/pcre/
+sys-libs/libsemanage-2.4-r999            # external/selinux/libsemanage/
+sys-libs/libsepol-2.4-r999               # external/selinux/libsepol/
+media-libs/speex-1.2_rc1-r999            # external/speex/
+sys-fs/squashfs-tools-4.3-r999           # external/squashfs-tools/
+dev-util/strace-4.10-r999                # external/strace/
+dev-libs/tinyxml-2.4.0-r999              # external/tinyxml/
+dev-libs/tinyxml2-1.0.6-r999             # external/tinyxml2/
+net-misc/tlsdate-0.0.13-r999             # external/tlsdate/
+sys-apps/toybox-0.6.0-r999               # external/toybox/
+net-wireless/wpa_supplicant-2.5-r999     # external/wpa_supplicant_8/wpa_supplicant/
+net-wireless/hostapd-2.5-r999            # external/wpa_supplicant_8/hostapd/
+sys-libs/zlib-1.2.8-r999                 # external/zlib/
+app-misc/ca-certificates-20150826-r999   # system/ca-certificates/
diff --git a/toolchain/Android.mk b/toolchain/Android.mk
index 62d5186..f8d7db2 100644
--- a/toolchain/Android.mk
+++ b/toolchain/Android.mk
@@ -1,5 +1,8 @@
 LOCAL_PATH := $(call my-dir)
 
+EXTERNAL_PACKAGES := $(dir $(LOCAL_PATH))/external-packages
+EXTERNAL_PACKAGES_INCLUDE := $(EXTERNAL_PACKAGES)/include
+
 include $(CLEAR_VARS)
 
 LOCAL_MODULE := 3rd-party-gcc
@@ -13,7 +16,7 @@
 	$(hide)mkdir -p $(dir $@)
 	$(hide)sed \
 		-e 's:@CC@:$(notdir $(TARGET_CC)):' \
-		-e 's:@CFLAGS@:$(foreach p,$(TARGET_C_INCLUDES),-isystem "$${ANDROID_BUILD_TOP}/$(p)"):' \
+		-e 's:@CFLAGS@:$(foreach p,$(TARGET_C_INCLUDES) $(EXTERNAL_PACKAGES_INCLUDE),-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 \