Skip to content

Commit 9937748

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 fa3702d commit 9937748

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
@@ -146,6 +146,7 @@ def get_python_lib(plat_specific=0, standard_lib=0, prefix=None):
146146
"I don't know where Python installs its library "
147147
"on platform '%s'" % os.name)
148148

149+
_USE_CLANG = None
149150

150151
def customize_compiler(compiler):
151152
"""Do any platform-specific customization of a CCompiler instance.
@@ -158,8 +159,38 @@ def customize_compiler(compiler):
158159
get_config_vars('CC', 'CXX', 'OPT', 'CFLAGS',
159160
'CCSHARED', 'LDSHARED', 'SO', 'AR', 'ARFLAGS')
160161

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

Misc/NEWS

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,19 @@ Core and Builtins
113113
Library
114114
-------
115115

116+
- Issue #13590: On OS X 10.7 and 10.6 with Xcode 4.2, building
117+
Distutils-based packages with C extension modules may fail because
118+
Apple has removed gcc-4.2, the version used to build python.org
119+
64-bit/32-bit Pythons. If the user does not explicitly override
120+
the default C compiler by setting the CC environment variable,
121+
Distutils will now attempt to compile extension modules with clang
122+
if gcc-4.2 is required but not found. Also as a convenience, if
123+
the user does explicitly set CC, substitute its value as the default
124+
compiler in the Distutils LDSHARED configuration variable for OS X.
125+
(Note, the python.org 32-bit-only Pythons use gcc-4.0 and the 10.4u
126+
SDK, neither of which are available in Xcode 4. This change does not
127+
attempt to override settings to support their use with Xcode 4.)
128+
116129
- Issue #13960: HTMLParser is now able to handle broken comments when
117130
strict=False.
118131

0 commit comments

Comments
 (0)