Skip to content

Commit 9734568

Browse files
committed
Issue #18080: When building a C extension module on OS X, if the compiler
is overriden with the CC environment variable, use the new compiler as the default for linking if LDSHARED is not also overriden. This restores Distutils behavior introduced in 3.2.3 and inadvertently dropped in 3.3.0.
1 parent dce0550 commit 9734568

File tree

3 files changed

+47
-4
lines changed

3 files changed

+47
-4
lines changed

Lib/distutils/sysconfig.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,9 +195,15 @@ def customize_compiler(compiler):
195195
get_config_vars('CC', 'CXX', 'OPT', 'CFLAGS',
196196
'CCSHARED', 'LDSHARED', 'SHLIB_SUFFIX', 'AR', 'ARFLAGS')
197197

198-
newcc = None
199198
if 'CC' in os.environ:
200-
cc = os.environ['CC']
199+
newcc = os.environ['CC']
200+
if (sys.platform == 'darwin'
201+
and 'LDSHARED' not in os.environ
202+
and ldshared.startswith(cc)):
203+
# On OS X, if CC is overridden, use that as the default
204+
# command for LDSHARED as well
205+
ldshared = newcc + ldshared[len(cc):]
206+
cc = newcc
201207
if 'CXX' in os.environ:
202208
cxx = os.environ['CXX']
203209
if 'LDSHARED' in os.environ:

Lib/distutils/tests/test_unixccompiler.py

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
"""Tests for distutils.unixccompiler."""
2+
import os
23
import sys
34
import unittest
4-
from test.support import run_unittest
5+
from test.support import EnvironmentVarGuard, run_unittest
56

67
from distutils import sysconfig
78
from distutils.unixccompiler import UnixCCompiler
@@ -94,7 +95,6 @@ def gcv(v):
9495
sysconfig.get_config_var = gcv
9596
self.assertEqual(self.cc.rpath_foo(), '-Wl,--enable-new-dtags,-R/foo')
9697

97-
9898
# non-GCC GNULD
9999
sys.platform = 'bar'
100100
def gcv(v):
@@ -115,6 +115,38 @@ def gcv(v):
115115
sysconfig.get_config_var = gcv
116116
self.assertEqual(self.cc.rpath_foo(), '-R/foo')
117117

118+
@unittest.skipUnless(sys.platform == 'darwin', 'test only relevant for OS X')
119+
def test_osx_cc_overrides_ldshared(self):
120+
# Issue #18080:
121+
# ensure that setting CC env variable also changes default linker
122+
def gcv(v):
123+
if v == 'LDSHARED':
124+
return 'gcc-4.2 -bundle -undefined dynamic_lookup '
125+
return 'gcc-4.2'
126+
sysconfig.get_config_var = gcv
127+
with EnvironmentVarGuard() as env:
128+
env['CC'] = 'my_cc'
129+
del env['LDSHARED']
130+
sysconfig.customize_compiler(self.cc)
131+
self.assertEqual(self.cc.linker_so[0], 'my_cc')
132+
133+
@unittest.skipUnless(sys.platform == 'darwin', 'test only relevant for OS X')
134+
def test_osx_explict_ldshared(self):
135+
# Issue #18080:
136+
# ensure that setting CC env variable does not change
137+
# explicit LDSHARED setting for linker
138+
def gcv(v):
139+
if v == 'LDSHARED':
140+
return 'gcc-4.2 -bundle -undefined dynamic_lookup '
141+
return 'gcc-4.2'
142+
sysconfig.get_config_var = gcv
143+
with EnvironmentVarGuard() as env:
144+
env['CC'] = 'my_cc'
145+
env['LDSHARED'] = 'my_ld -bundle -dynamic'
146+
sysconfig.customize_compiler(self.cc)
147+
self.assertEqual(self.cc.linker_so[0], 'my_ld')
148+
149+
118150
def test_suite():
119151
return unittest.makeSuite(UnixCCompilerTestCase)
120152

Misc/NEWS

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ Library
5555
- Issue #17269: Workaround for socket.getaddrinfo crash on MacOS X
5656
with port None or "0" and flags AI_NUMERICSERV.
5757

58+
- Issue #18080: When building a C extension module on OS X, if the compiler
59+
is overriden with the CC environment variable, use the new compiler as
60+
the default for linking if LDSHARED is not also overriden. This restores
61+
Distutils behavior introduced in 3.2.3 and inadvertently dropped in 3.3.0.
62+
5863
IDLE
5964
----
6065

0 commit comments

Comments
 (0)