diff setup.py @ 81859:8ee6d96a1019 2.7

- Issue #17086: Backport the patches from the 3.3 branch to cross-build the package.
author doko@python.org
date Thu, 31 Jan 2013 23:52:03 +0100
parents 49294e55311f
children bf43e8c30a83
line wrap: on
line diff
--- a/setup.py	Thu Jan 31 16:10:15 2013 +0200
+++ b/setup.py	Thu Jan 31 23:52:03 2013 +0100
@@ -17,8 +17,20 @@
 from distutils.command.install_lib import install_lib
 from distutils.spawn import find_executable
 
+cross_compiling = "_PYTHON_HOST_PLATFORM" in os.environ
+
+def get_platform():
+    # cross build
+    if "_PYTHON_HOST_PLATFORM" in os.environ:
+        return os.environ["_PYTHON_HOST_PLATFORM"]
+    # Get value of sys.platform
+    if sys.platform.startswith('osf1'):
+        return 'osf1'
+    return sys.platform
+host_platform = get_platform()
+
 # Were we compiled --with-pydebug or with #define Py_DEBUG?
-COMPILED_WITH_PYDEBUG = hasattr(sys, 'gettotalrefcount')
+COMPILED_WITH_PYDEBUG = ('--with-pydebug' in sysconfig.get_config_var("CONFIG_ARGS"))
 
 # This global variable is used to hold the list of modules to be disabled.
 disabled_module_list = []
@@ -62,7 +74,7 @@
     'paths' is a list of additional locations to check; if the file is
         found in one of them, the resulting list will contain the directory.
     """
-    if sys.platform == 'darwin':
+    if host_platform == 'darwin':
         # Honor the MacOSX SDK setting when one was specified.
         # An SDK is a directory with the same structure as a real
         # system, but with only header files and libraries.
@@ -72,7 +84,7 @@
     for dir in std_dirs:
         f = os.path.join(dir, filename)
 
-        if sys.platform == 'darwin' and is_macosx_sdk_path(dir):
+        if host_platform == 'darwin' and is_macosx_sdk_path(dir):
             f = os.path.join(sysroot, dir[1:], filename)
 
         if os.path.exists(f): return []
@@ -81,7 +93,7 @@
     for dir in paths:
         f = os.path.join(dir, filename)
 
-        if sys.platform == 'darwin' and is_macosx_sdk_path(dir):
+        if host_platform == 'darwin' and is_macosx_sdk_path(dir):
             f = os.path.join(sysroot, dir[1:], filename)
 
         if os.path.exists(f):
@@ -95,7 +107,7 @@
     if result is None:
         return None
 
-    if sys.platform == 'darwin':
+    if host_platform == 'darwin':
         sysroot = macosx_sdk_root()
 
     # Check whether the found file is in one of the standard directories
@@ -104,7 +116,7 @@
         # Ensure path doesn't end with path separator
         p = p.rstrip(os.sep)
 
-        if sys.platform == 'darwin' and is_macosx_sdk_path(p):
+        if host_platform == 'darwin' and is_macosx_sdk_path(p):
             if os.path.join(sysroot, p[1:]) == dirname:
                 return [ ]
 
@@ -117,7 +129,7 @@
         # Ensure path doesn't end with path separator
         p = p.rstrip(os.sep)
 
-        if sys.platform == 'darwin' and is_macosx_sdk_path(p):
+        if host_platform == 'darwin' and is_macosx_sdk_path(p):
             if os.path.join(sysroot, p[1:]) == dirname:
                 return [ p ]
 
@@ -174,8 +186,8 @@
 
         # Platform-dependent module source and include directories
         incdirlist = []
-        platform = self.get_platform()
-        if platform == 'darwin' and ("--disable-toolbox-glue" not in
+
+        if host_platform == 'darwin' and ("--disable-toolbox-glue" not in
             sysconfig.get_config_var("CONFIG_ARGS")):
             # Mac OS X also includes some mac-specific modules
             macmoddir = os.path.join(srcdir, 'Mac/Modules')
@@ -288,7 +300,7 @@
                 ext.name)
             return
 
-        if self.get_platform() == 'darwin' and (
+        if host_platform == 'darwin' and (
                 sys.maxint > 2**32 and '-arch' in ext.extra_link_args):
             # Don't bother doing an import check when an extension was
             # build with an explicit '-arch' flag on OSX. That's currently
@@ -302,13 +314,18 @@
 
         # Workaround for Cygwin: Cygwin currently has fork issues when many
         # modules have been imported
-        if self.get_platform() == 'cygwin':
+        if host_platform == 'cygwin':
             self.announce('WARNING: skipping import check for Cygwin-based "%s"'
                 % ext.name)
             return
         ext_filename = os.path.join(
             self.build_lib,
             self.get_ext_filename(self.get_ext_fullname(ext.name)))
+
+        # Don't try to load extensions for cross builds
+        if cross_compiling:
+            return
+
         try:
             imp.load_dynamic(ext.name, ext_filename)
         except ImportError, why:
@@ -340,13 +357,6 @@
                           level=3)
             self.failed.append(ext.name)
 
-    def get_platform(self):
-        # Get value of sys.platform
-        for platform in ['cygwin', 'beos', 'darwin', 'atheos', 'osf1']:
-            if sys.platform.startswith(platform):
-                return platform
-        return sys.platform
-
     def add_multiarch_paths(self):
         # Debian/Ubuntu multiarch support.
         # https://wiki.ubuntu.com/MultiarchSpec
@@ -373,12 +383,15 @@
 
         if not find_executable('dpkg-architecture'):
             return
+        opt = ''
+        if cross_compiling:
+            opt = '-t' + sysconfig.get_config_var('HOST_GNU_TYPE')
         tmpfile = os.path.join(self.build_temp, 'multiarch')
         if not os.path.exists(self.build_temp):
             os.makedirs(self.build_temp)
         ret = os.system(
-            'dpkg-architecture -qDEB_HOST_MULTIARCH > %s 2> /dev/null' %
-            tmpfile)
+            'dpkg-architecture %s -qDEB_HOST_MULTIARCH > %s 2> /dev/null' %
+            (opt, tmpfile))
         try:
             if ret >> 8 == 0:
                 with open(tmpfile) as fp:
@@ -390,6 +403,38 @@
         finally:
             os.unlink(tmpfile)
 
+    def add_gcc_paths(self):
+        gcc = sysconfig.get_config_var('CC')
+        tmpfile = os.path.join(self.build_temp, 'gccpaths')
+        if not os.path.exists(self.build_temp):
+            os.makedirs(self.build_temp)
+        ret = os.system('%s -E -v - </dev/null 2>%s 1>/dev/null' % (gcc, tmpfile))
+        is_gcc = False
+        in_incdirs = False
+        inc_dirs = []
+        lib_dirs = []
+        try:
+            if ret >> 8 == 0:
+                with open(tmpfile) as fp:
+                    for line in fp.readlines():
+                        if line.startswith("gcc version"):
+                            is_gcc = True
+                        elif line.startswith("#include <...>"):
+                            in_incdirs = True
+                        elif line.startswith("End of search list"):
+                            in_incdirs = False
+                        elif is_gcc and line.startswith("LIBRARY_PATH"):
+                            for d in line.strip().split("=")[1].split(":"):
+                                d = os.path.normpath(d)
+                                if '/gcc/' not in d:
+                                    add_dir_to_list(self.compiler.library_dirs,
+                                                    d)
+                        elif is_gcc and in_incdirs and '/gcc/' not in line:
+                            add_dir_to_list(self.compiler.include_dirs,
+                                            line.strip())
+        finally:
+            os.unlink(tmpfile)
+
     def detect_modules(self):
         # Ensure that /usr/local is always used
         add_dir_to_list(self.compiler.library_dirs, '/usr/local/lib')
@@ -449,36 +494,42 @@
         # lib_dirs and inc_dirs are used to search for files;
         # if a file is found in one of those directories, it can
         # be assumed that no additional -I,-L directives are needed.
-        lib_dirs = self.compiler.library_dirs + [
-            '/lib64', '/usr/lib64',
-            '/lib', '/usr/lib',
-            ]
-        inc_dirs = self.compiler.include_dirs + ['/usr/include']
+        inc_dirs = self.compiler.include_dirs[:]
+        lib_dirs = self.compiler.library_dirs[:]
+        if not cross_compiling:
+            for d in (
+                '/usr/include',
+                ):
+                add_dir_to_list(inc_dirs, d)
+            for d in (
+                '/lib64', '/usr/lib64',
+                '/lib', '/usr/lib',
+                ):
+                add_dir_to_list(lib_dirs, d)
         exts = []
         missing = []
 
         config_h = sysconfig.get_config_h_filename()
         config_h_vars = sysconfig.parse_config_h(open(config_h))
 
-        platform = self.get_platform()
         srcdir = sysconfig.get_config_var('srcdir')
 
         # Check for AtheOS which has libraries in non-standard locations
-        if platform == 'atheos':
+        if host_platform == 'atheos':
             lib_dirs += ['/system/libs', '/atheos/autolnk/lib']
             lib_dirs += os.getenv('LIBRARY_PATH', '').split(os.pathsep)
             inc_dirs += ['/system/include', '/atheos/autolnk/include']
             inc_dirs += os.getenv('C_INCLUDE_PATH', '').split(os.pathsep)
 
         # OSF/1 and Unixware have some stuff in /usr/ccs/lib (like -ldb)
-        if platform in ['osf1', 'unixware7', 'openunix8']:
+        if host_platform in ['osf1', 'unixware7', 'openunix8']:
             lib_dirs += ['/usr/ccs/lib']
 
         # HP-UX11iv3 keeps files in lib/hpux folders.
-        if platform == 'hp-ux11':
+        if host_platform == 'hp-ux11':
             lib_dirs += ['/usr/lib/hpux64', '/usr/lib/hpux32']
 
-        if platform == 'darwin':
+        if host_platform == 'darwin':
             # This should work on any unixy platform ;-)
             # If the user has bothered specifying additional -I and -L flags
             # in OPT and LDFLAGS we might as well use them here.
@@ -497,7 +548,7 @@
 
         # Check for MacOS X, which doesn't need libm.a at all
         math_libs = ['m']
-        if platform in ['darwin', 'beos']:
+        if host_platform in ['darwin', 'beos']:
             math_libs = []
 
         # XXX Omitted modules: gl, pure, dl, SGI-specific modules
@@ -569,7 +620,7 @@
             locale_libs = ['intl']
         else:
             locale_libs = []
-        if platform == 'darwin':
+        if host_platform == 'darwin':
             locale_extra_link_args = ['-framework', 'CoreFoundation']
         else:
             locale_extra_link_args = []
@@ -611,7 +662,7 @@
         exts.append( Extension('cPickle', ['cPickle.c']) )
 
         # Memory-mapped files (also works on Win32).
-        if platform not in ['atheos']:
+        if host_platform not in ['atheos']:
             exts.append( Extension('mmap', ['mmapmodule.c']) )
         else:
             missing.append('mmap')
@@ -676,7 +727,7 @@
         elif self.compiler.find_library_file(lib_dirs, 'curses'):
             curses_library = 'curses'
 
-        if platform == 'darwin':
+        if host_platform == 'darwin':
             os_release = int(os.uname()[2].split('.')[0])
             dep_target = sysconfig.get_config_var('MACOSX_DEPLOYMENT_TARGET')
             if dep_target and dep_target.split('.') < ['10', '5']:
@@ -688,7 +739,7 @@
                 if find_file('readline/rlconf.h', inc_dirs, []) is None:
                     do_readline = False
         if do_readline:
-            if platform == 'darwin' and os_release < 9:
+            if host_platform == 'darwin' and os_release < 9:
                 # In every directory on the search path search for a dynamic
                 # library and then a static library, instead of first looking
                 # for dynamic libraries on the entiry path.
@@ -766,7 +817,7 @@
                 inc_dirs + search_for_ssl_incs_in)
         if opensslv_h:
             name = os.path.join(opensslv_h[0], 'openssl/opensslv.h')
-            if sys.platform == 'darwin' and is_macosx_sdk_path(name):
+            if host_platform == 'darwin' and is_macosx_sdk_path(name):
                 name = os.path.join(macosx_sdk_root(), name[1:])
             try:
                 incfile = open(name, 'r')
@@ -890,6 +941,9 @@
             db_inc_paths.append('/pkg/db-3.%d/include' % x)
             db_inc_paths.append('/opt/db-3.%d/include' % x)
 
+        if cross_compiling:
+            db_inc_paths = []
+
         # Add some common subdirectories for Sleepycat DB to the list,
         # based on the standard include directories. This way DB3/4 gets
         # picked up when it is installed in a non-standard prefix and
@@ -910,7 +964,7 @@
 
         db_ver_inc_map = {}
 
-        if sys.platform == 'darwin':
+        if host_platform == 'darwin':
             sysroot = macosx_sdk_root()
 
         class db_found(Exception): pass
@@ -920,7 +974,7 @@
             for d in inc_dirs + db_inc_paths:
                 f = os.path.join(d, "db.h")
 
-                if sys.platform == 'darwin' and is_macosx_sdk_path(d):
+                if host_platform == 'darwin' and is_macosx_sdk_path(d):
                     f = os.path.join(sysroot, d[1:], "db.h")
 
                 if db_setup_debug: print "db: looking for db.h in", f
@@ -970,7 +1024,7 @@
                     db_incdir.replace("include", 'lib'),
                 ]
 
-                if sys.platform != 'darwin':
+                if host_platform != 'darwin':
                     db_dirs_to_check = filter(os.path.isdir, db_dirs_to_check)
 
                 else:
@@ -1038,6 +1092,8 @@
                              '/usr/local/include/sqlite',
                              '/usr/local/include/sqlite3',
                            ]
+        if cross_compiling:
+            sqlite_inc_paths = []
         MIN_SQLITE_VERSION_NUMBER = (3, 0, 8)
         MIN_SQLITE_VERSION = ".".join([str(x)
                                     for x in MIN_SQLITE_VERSION_NUMBER])
@@ -1045,12 +1101,12 @@
         # Scan the default include directories before the SQLite specific
         # ones. This allows one to override the copy of sqlite on OSX,
         # where /usr/include contains an old version of sqlite.
-        if sys.platform == 'darwin':
+        if host_platform == 'darwin':
             sysroot = macosx_sdk_root()
 
         for d_ in inc_dirs + sqlite_inc_paths:
             d = d_
-            if sys.platform == 'darwin' and is_macosx_sdk_path(d):
+            if host_platform == 'darwin' and is_macosx_sdk_path(d):
                 d = os.path.join(sysroot, d[1:])
 
             f = os.path.join(d, "sqlite3.h")
@@ -1100,7 +1156,7 @@
                 '_sqlite/util.c', ]
 
             sqlite_defines = []
-            if sys.platform != "win32":
+            if host_platform != "win32":
                 sqlite_defines.append(('MODULE_NAME', '"sqlite3"'))
             else:
                 sqlite_defines.append(('MODULE_NAME', '\\"sqlite3\\"'))
@@ -1108,7 +1164,7 @@
             # Comment this out if you want the sqlite3 module to be able to load extensions.
             sqlite_defines.append(("SQLITE_OMIT_LOAD_EXTENSION", "1"))
 
-            if sys.platform == 'darwin':
+            if host_platform == 'darwin':
                 # In every directory on the search path search for a dynamic
                 # library and then a static library, instead of first looking
                 # for dynamic libraries on the entire path.
@@ -1142,7 +1198,7 @@
         # when attempting to compile and it will fail.
         f = "/usr/include/db.h"
 
-        if sys.platform == 'darwin':
+        if host_platform == 'darwin':
             if is_macosx_sdk_path(f):
                 sysroot = macosx_sdk_root()
                 f = os.path.join(sysroot, f[1:])
@@ -1155,7 +1211,7 @@
                 ### XXX this should be fixed to not be platform-dependent
                 ### but I don't have direct access to an osf1 platform and
                 ### seemed to be muffing the search somehow
-                libraries = platform == "osf1" and ['db'] or None
+                libraries = host_platform == "osf1" and ['db'] or None
                 if libraries is not None:
                     exts.append(Extension('bsddb185', ['bsddbmodule.c'],
                                           libraries=libraries))
@@ -1168,7 +1224,7 @@
 
         dbm_order = ['gdbm']
         # The standard Unix dbm module:
-        if platform not in ['cygwin']:
+        if host_platform not in ['cygwin']:
             config_args = [arg.strip("'")
                            for arg in sysconfig.get_config_var("CONFIG_ARGS").split()]
             dbm_args = [arg for arg in config_args
@@ -1250,17 +1306,17 @@
             missing.append('gdbm')
 
         # Unix-only modules
-        if platform not in ['win32']:
+        if host_platform not in ['win32']:
             # Steen Lumholt's termios module
             exts.append( Extension('termios', ['termios.c']) )
             # Jeremy Hylton's rlimit interface
-            if platform not in ['atheos']:
+            if host_platform not in ['atheos']:
                 exts.append( Extension('resource', ['resource.c']) )
             else:
                 missing.append('resource')
 
             # Sun yellow pages. Some systems have the functions in libc.
-            if (platform not in ['cygwin', 'atheos', 'qnx6'] and
+            if (host_platform not in ['cygwin', 'atheos', 'qnx6'] and
                 find_file('rpcsvc/yp_prot.h', inc_dirs, []) is not None):
                 if (self.compiler.find_library_file(lib_dirs, 'nsl')):
                     libs = ['nsl']
@@ -1284,7 +1340,7 @@
             curses_libs = [curses_library]
             exts.append( Extension('_curses', ['_cursesmodule.c'],
                                    libraries = curses_libs) )
-        elif curses_library == 'curses' and platform != 'darwin':
+        elif curses_library == 'curses' and host_platform != 'darwin':
                 # OSX has an old Berkeley curses, not good enough for
                 # the _curses module.
             if (self.compiler.find_library_file(lib_dirs, 'terminfo')):
@@ -1335,7 +1391,7 @@
                     break
             if version >= version_req:
                 if (self.compiler.find_library_file(lib_dirs, 'z')):
-                    if sys.platform == "darwin":
+                    if host_platform == "darwin":
                         zlib_extra_link_args = ('-Wl,-search_paths_first',)
                     else:
                         zlib_extra_link_args = ()
@@ -1367,7 +1423,7 @@
 
         # Gustavo Niemeyer's bz2 module.
         if (self.compiler.find_library_file(lib_dirs, 'bz2')):
-            if sys.platform == "darwin":
+            if host_platform == "darwin":
                 bz2_extra_link_args = ('-Wl,-search_paths_first',)
             else:
                 bz2_extra_link_args = ()
@@ -1440,7 +1496,7 @@
         if sys.maxint == 0x7fffffff:
             # This requires sizeof(int) == sizeof(long) == sizeof(char*)
             dl_inc = find_file('dlfcn.h', [], inc_dirs)
-            if (dl_inc is not None) and (platform not in ['atheos']):
+            if (dl_inc is not None) and (host_platform not in ['atheos']):
                 exts.append( Extension('dl', ['dlmodule.c']) )
             else:
                 missing.append('dl')
@@ -1451,29 +1507,29 @@
         self.detect_ctypes(inc_dirs, lib_dirs)
 
         # Richard Oudkerk's multiprocessing module
-        if platform == 'win32':             # Windows
+        if host_platform == 'win32':             # Windows
             macros = dict()
             libraries = ['ws2_32']
 
-        elif platform == 'darwin':          # Mac OSX
+        elif host_platform == 'darwin':          # Mac OSX
             macros = dict()
             libraries = []
 
-        elif platform == 'cygwin':          # Cygwin
+        elif host_platform == 'cygwin':          # Cygwin
             macros = dict()
             libraries = []
 
-        elif platform in ('freebsd4', 'freebsd5', 'freebsd6', 'freebsd7', 'freebsd8'):
+        elif host_platform in ('freebsd4', 'freebsd5', 'freebsd6', 'freebsd7', 'freebsd8'):
             # FreeBSD's P1003.1b semaphore support is very experimental
             # and has many known problems. (as of June 2008)
             macros = dict()
             libraries = []
 
-        elif platform.startswith('openbsd'):
+        elif host_platform.startswith('openbsd'):
             macros = dict()
             libraries = []
 
-        elif platform.startswith('netbsd'):
+        elif host_platform.startswith('netbsd'):
             macros = dict()
             libraries = []
 
@@ -1481,7 +1537,7 @@
             macros = dict()
             libraries = ['rt']
 
-        if platform == 'win32':
+        if host_platform == 'win32':
             multiprocessing_srcs = [ '_multiprocessing/multiprocessing.c',
                                      '_multiprocessing/semaphore.c',
                                      '_multiprocessing/pipe_connection.c',
@@ -1508,26 +1564,26 @@
 
 
         # Platform-specific libraries
-        if platform == 'linux2':
+        if host_platform == 'linux2':
             # Linux-specific modules
             exts.append( Extension('linuxaudiodev', ['linuxaudiodev.c']) )
         else:
             missing.append('linuxaudiodev')
 
-        if (platform in ('linux2', 'freebsd4', 'freebsd5', 'freebsd6',
+        if (host_platform in ('linux2', 'freebsd4', 'freebsd5', 'freebsd6',
                         'freebsd7', 'freebsd8')
-            or platform.startswith("gnukfreebsd")):
+            or host_platform.startswith("gnukfreebsd")):
             exts.append( Extension('ossaudiodev', ['ossaudiodev.c']) )
         else:
             missing.append('ossaudiodev')
 
-        if platform == 'sunos5':
+        if host_platform == 'sunos5':
             # SunOS specific modules
             exts.append( Extension('sunaudiodev', ['sunaudiodev.c']) )
         else:
             missing.append('sunaudiodev')
 
-        if platform == 'darwin':
+        if host_platform == 'darwin':
             # _scproxy
             exts.append(Extension("_scproxy", [os.path.join(srcdir, "Mac/Modules/_scproxy.c")],
                 extra_link_args= [
@@ -1536,7 +1592,7 @@
                 ]))
 
 
-        if platform == 'darwin' and ("--disable-toolbox-glue" not in
+        if host_platform == 'darwin' and ("--disable-toolbox-glue" not in
                 sysconfig.get_config_var("CONFIG_ARGS")):
 
             if int(os.uname()[2].split('.')[0]) >= 8:
@@ -1732,8 +1788,7 @@
         # Rather than complicate the code below, detecting and building
         # AquaTk is a separate method. Only one Tkinter will be built on
         # Darwin - either AquaTk, if it is found, or X11 based Tk.
-        platform = self.get_platform()
-        if (platform == 'darwin' and
+        if (host_platform == 'darwin' and
             self.detect_tkinter_darwin(inc_dirs, lib_dirs)):
             return
 
@@ -1756,7 +1811,7 @@
             # Check for the include files on Debian and {Free,Open}BSD, where
             # they're put in /usr/include/{tcl,tk}X.Y
             dotversion = version
-            if '.' not in dotversion and "bsd" in sys.platform.lower():
+            if '.' not in dotversion and "bsd" in host_platform.lower():
                 # OpenBSD and FreeBSD use Tcl/Tk library names like libtcl83.a,
                 # but the include subdirs are named like .../include/tcl8.3.
                 dotversion = dotversion[:-1] + '.' + dotversion[-1]
@@ -1782,7 +1837,7 @@
                 include_dirs.append(dir)
 
         # Check for various platform-specific directories
-        if platform == 'sunos5':
+        if host_platform == 'sunos5':
             include_dirs.append('/usr/openwin/include')
             added_lib_dirs.append('/usr/openwin/lib')
         elif os.path.exists('/usr/X11R6/include'):
@@ -1798,7 +1853,7 @@
             added_lib_dirs.append('/usr/X11/lib')
 
         # If Cygwin, then verify that X is installed before proceeding
-        if platform == 'cygwin':
+        if host_platform == 'cygwin':
             x11_inc = find_file('X11/Xlib.h', [], include_dirs)
             if x11_inc is None:
                 return
@@ -1817,11 +1872,11 @@
         libs.append('tk'+ version)
         libs.append('tcl'+ version)
 
-        if platform in ['aix3', 'aix4']:
+        if host_platform in ['aix3', 'aix4']:
             libs.append('ld')
 
         # Finally, link with the X11 libraries (not appropriate on cygwin)
-        if platform != "cygwin":
+        if host_platform != "cygwin":
             libs.append('X11')
 
         ext = Extension('_tkinter', ['_tkinter.c', 'tkappinit.c'],
@@ -1873,7 +1928,7 @@
 
     def configure_ctypes(self, ext):
         if not self.use_system_libffi:
-            if sys.platform == 'darwin':
+            if host_platform == 'darwin':
                 return self.configure_ctypes_darwin(ext)
 
             srcdir = sysconfig.get_config_var('srcdir')
@@ -1891,7 +1946,8 @@
                                          ffi_configfile):
                 from distutils.dir_util import mkpath
                 mkpath(ffi_builddir)
-                config_args = []
+                config_args = [arg for arg in sysconfig.get_config_var("CONFIG_ARGS").split()
+                               if (('--host=' in arg) or ('--build=' in arg))]
                 if not self.verbose:
                     config_args.append("-q")
 
@@ -1935,7 +1991,7 @@
                    '_ctypes/cfield.c']
         depends = ['_ctypes/ctypes.h']
 
-        if sys.platform == 'darwin':
+        if host_platform == 'darwin':
             sources.append('_ctypes/malloc_closure.c')
             sources.append('_ctypes/darwin/dlfcn_simple.c')
             extra_compile_args.append('-DMACOSX')
@@ -1943,7 +1999,7 @@
 # XXX Is this still needed?
 ##            extra_link_args.extend(['-read_only_relocs', 'warning'])
 
-        elif sys.platform == 'sunos5':
+        elif host_platform == 'sunos5':
             # XXX This shouldn't be necessary; it appears that some
             # of the assembler code is non-PIC (i.e. it has relocations
             # when it shouldn't. The proper fix would be to rewrite
@@ -1954,7 +2010,7 @@
             # finding some -z option for the Sun compiler.
             extra_link_args.append('-mimpure-text')
 
-        elif sys.platform.startswith('hp-ux'):
+        elif host_platform.startswith('hp-ux'):
             extra_link_args.append('-fPIC')
 
         ext = Extension('_ctypes',
@@ -1971,7 +2027,7 @@
         if not '--with-system-ffi' in sysconfig.get_config_var("CONFIG_ARGS"):
             return
 
-        if sys.platform == 'darwin':
+        if host_platform == 'darwin':
             # OS X 10.5 comes with libffi.dylib; the include files are
             # in /usr/include/ffi
             inc_dirs.append('/usr/include/ffi')