From 0d0ea48709186b1ee7505d98816fc75e2cf14df9 Mon Sep 17 00:00:00 2001 From: Ned Deily Date: Fri, 10 Feb 2012 12:59:06 +0100 Subject: [PATCH] Issue #13590: On OS X 10.7 and 10.6 with Xcode 4.2, building Distutils-based packages with C extension modules may fail because Apple has removed gcc-4.2, the version used to build python.org 64-bit/32-bit Pythons. If the user does not explicitly override the default C compiler by setting the CC environment variable, Distutils will now attempt to compile extension modules with clang if gcc-4.2 is required but not found. Also as a convenience, if the user does explicitly set CC, substitute its value as the default compiler in the Distutils LDSHARED configuration variable for OS X. (Note, the python.org 32-bit-only Pythons use gcc-4.0 and the 10.4u SDK, neither of which are available in Xcode 4. This change does not attempt to override settings to support their use with Xcode 4.) --- Lib/distutils/sysconfig.py | 33 ++++++++++++++++++++++++++++++++- Misc/NEWS | 13 +++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/Lib/distutils/sysconfig.py b/Lib/distutils/sysconfig.py index 0d6d4c4feab..4f9041a794a 100644 --- a/Lib/distutils/sysconfig.py +++ b/Lib/distutils/sysconfig.py @@ -141,6 +141,7 @@ def get_python_lib(plat_specific=0, standard_lib=0, prefix=None): "I don't know where Python installs its library " "on platform '%s'" % os.name) +_USE_CLANG = None def customize_compiler(compiler): """Do any platform-specific customization of a CCompiler instance. @@ -153,8 +154,38 @@ def customize_compiler(compiler): get_config_vars('CC', 'CXX', 'OPT', 'CFLAGS', 'CCSHARED', 'LDSHARED', 'SO') + newcc = None if 'CC' in os.environ: - cc = os.environ['CC'] + newcc = os.environ['CC'] + elif sys.platform == 'darwin' and cc == 'gcc-4.2': + # Issue #13590: + # Since Apple removed gcc-4.2 in Xcode 4.2, we can no + # longer assume it is available for extension module builds. + # If Python was built with gcc-4.2, check first to see if + # it is available on this system; if not, try to use clang + # instead unless the caller explicitly set CC. + global _USE_CLANG + if _USE_CLANG is None: + from distutils import log + from subprocess import Popen, PIPE + p = Popen("! type gcc-4.2 && type clang && exit 2", + shell=True, stdout=PIPE, stderr=PIPE) + p.wait() + if p.returncode == 2: + _USE_CLANG = True + log.warn("gcc-4.2 not found, using clang instead") + else: + _USE_CLANG = False + if _USE_CLANG: + newcc = 'clang' + if newcc: + # On OS X, if CC is overridden, use that as the default + # command for LDSHARED as well + if (sys.platform == 'darwin' + and 'LDSHARED' not in os.environ + and ldshared.startswith(cc)): + ldshared = newcc + ldshared[len(cc):] + cc = newcc if 'CXX' in os.environ: cxx = os.environ['CXX'] if 'LDSHARED' in os.environ: diff --git a/Misc/NEWS b/Misc/NEWS index e912b49c453..024a616878d 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -90,6 +90,19 @@ Core and Builtins Library ------- +- Issue #13590: On OS X 10.7 and 10.6 with Xcode 4.2, building + Distutils-based packages with C extension modules may fail because + Apple has removed gcc-4.2, the version used to build python.org + 64-bit/32-bit Pythons. If the user does not explicitly override + the default C compiler by setting the CC environment variable, + Distutils will now attempt to compile extension modules with clang + if gcc-4.2 is required but not found. Also as a convenience, if + the user does explicitly set CC, substitute its value as the default + compiler in the Distutils LDSHARED configuration variable for OS X. + (Note, the python.org 32-bit-only Pythons use gcc-4.0 and the 10.4u + SDK, neither of which are available in Xcode 4. This change does not + attempt to override settings to support their use with Xcode 4.) + - Issue #9021: Add an introduction to the copy module documentation. - Issue #6005: Examples in the socket library documentation use sendall, where