python: make libdir lookup a bit more robust

Currently the distutils libdir lookup just pokes directories on disk
and tries to find the first match.  This works as long as all the ABI
paths are created correctly, but it all falls down if one package uses
the wrong path once (as subsequent builds will keep using that broken
path).

Lets improve this by first adding a lookup of standard Gentoo env vars
$ABI and $LIBDIR_$ABI.  If those two are set, we'll use whatever they
say we should (just like Gentoo does).  If they aren't, we'll fall back
to the existing fragile logic today.

BUG=chromium:877680
TEST=precq passes

Change-Id: Ifdb181c03a21c46e1c88440404d2ba967aec2b54
Reviewed-on: https://chromium-review.googlesource.com/1189003
Tested-by: Mike Frysinger <vapier@chromium.org>
Reviewed-by: Jason Clinton <jclinton@chromium.org>
diff --git a/dev-lang/python/files/python-2.7.10-cross-distutils.patch b/dev-lang/python/files/python-2.7.10-cross-distutils.patch
index adf8977..40a4115 100644
--- a/dev-lang/python/files/python-2.7.10-cross-distutils.patch
+++ b/dev-lang/python/files/python-2.7.10-cross-distutils.patch
@@ -73,7 +73,7 @@
  # Path to the base directory of the project. On Windows the binary may
  # live in project/PCBuild9.  If we're dealing with an x64 Windows build,
  # it'll live in project/PCbuild/amd64.
-@@ -114,13 +122,21 @@
+@@ -114,13 +122,30 @@
  
      If 'prefix' is supplied, use it instead of sys.prefix or
      sys.exec_prefix -- i.e., ignore 'plat_specific'.
@@ -90,14 +90,23 @@
      if os.name == "posix":
 -        libpython = os.path.join(prefix,
 -                                 "@@GENTOO_LIBDIR@@", "python" + get_python_version())
-+        for libdir in ['@@GENTOO_LIBDIR@@', 'lib64', 'lib32', 'libx32', 'lib']:
-+            libpython = os.path.join(prefix, libdir, "python" + get_python_version())
-+            if os.path.exists(libpython):
-+                break
++        # Search known Gentoo vars first.
++        libpython = None
++        if SYSROOT:
++            abi = os.environ.get('ABI')
++            libdir = os.environ.get('LIBDIR_%s' % abi)
++            if libdir:
++                libpython = os.path.join(prefix, libdir, "python" + get_python_version())
++        if not libpython:
++            # Fallback to hardcoded search.
++            for libdir in ['@@GENTOO_LIBDIR@@', 'lib64', 'lib32', 'libx32', 'lib']:
++                libpython = os.path.join(prefix, libdir, "python" + get_python_version())
++                if os.path.exists(libpython):
++                    break
          if standard_lib:
              return libpython
          else:
-@@ -409,10 +425,14 @@
+@@ -409,10 +434,14 @@
  def _init_posix():
      """Initialize the module as appropriate for POSIX systems."""
      # _sysconfigdata is generated at build time, see the sysconfig module