Skip to content

Commit 0d0ea48

Browse files
committed
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.)
1 parent 4ad7f7f commit 0d0ea48

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

Lib/distutils/sysconfig.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ def get_python_lib(plat_specific=0, standard_lib=0, prefix=None):
141141
"I don't know where Python installs its library "
142142
"on platform '%s'" % os.name)
143143

144+
_USE_CLANG = None
144145

145146
def customize_compiler(compiler):
146147
"""Do any platform-specific customization of a CCompiler instance.
@@ -153,8 +154,38 @@ def customize_compiler(compiler):
153154
get_config_vars('CC', 'CXX', 'OPT', 'CFLAGS',
154155
'CCSHARED', 'LDSHARED', 'SO')
155156

157+
newcc = None
156158
if 'CC' in os.environ:
157-
cc = os.environ['CC']
159+
newcc = os.environ['CC']
160+
elif sys.platform == 'darwin' and cc == 'gcc-4.2':
161+
# Issue #13590:
162+
# Since Apple removed gcc-4.2 in Xcode 4.2, we can no
163+
# longer assume it is available for extension module builds.
164+
# If Python was built with gcc-4.2, check first to see if
165+
# it is available on this system; if not, try to use clang
166+
# instead unless the caller explicitly set CC.
167+
global _USE_CLANG
168+
if _USE_CLANG is None:
169+
from distutils import log
170+
from subprocess import Popen, PIPE
171+
p = Popen("! type gcc-4.2 && type clang && exit 2",
172+
shell=True, stdout=PIPE, stderr=PIPE)
173+
p.wait()
174+
if p.returncode == 2:
175+
_USE_CLANG = True
176+
log.warn("gcc-4.2 not found, using clang instead")
177+
else:
178+
_USE_CLANG = False
179+
if _USE_CLANG:
180+
newcc = 'clang'
181+
if newcc:
182+
# On OS X, if CC is overridden, use that as the default
183+
# command for LDSHARED as well
184+
if (sys.platform == 'darwin'
185+
and 'LDSHARED' not in os.environ
186+
and ldshared.startswith(cc)):
187+
ldshared = newcc + ldshared[len(cc):]
188+
cc = newcc
158189
if 'CXX' in os.environ:
159190
cxx = os.environ['CXX']
160191
if 'LDSHARED' in os.environ:

Misc/NEWS

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,19 @@ Core and Builtins
9090
Library
9191
-------
9292

93+
- Issue #13590: On OS X 10.7 and 10.6 with Xcode 4.2, building
94+
Distutils-based packages with C extension modules may fail because
95+
Apple has removed gcc-4.2, the version used to build python.org
96+
64-bit/32-bit Pythons. If the user does not explicitly override
97+
the default C compiler by setting the CC environment variable,
98+
Distutils will now attempt to compile extension modules with clang
99+
if gcc-4.2 is required but not found. Also as a convenience, if
100+
the user does explicitly set CC, substitute its value as the default
101+
compiler in the Distutils LDSHARED configuration variable for OS X.
102+
(Note, the python.org 32-bit-only Pythons use gcc-4.0 and the 10.4u
103+
SDK, neither of which are available in Xcode 4. This change does not
104+
attempt to override settings to support their use with Xcode 4.)
105+
93106
- Issue #9021: Add an introduction to the copy module documentation.
94107

95108
- Issue #6005: Examples in the socket library documentation use sendall, where

0 commit comments

Comments
 (0)