Skip to content

Commit 1ab29e7

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 2.7.3 and inadvertently dropped in 2.7.4.
1 parent 3fcf2d3 commit 1ab29e7

File tree

3 files changed

+46
-3
lines changed

3 files changed

+46
-3
lines changed

Lib/distutils/sysconfig.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,9 +175,15 @@ def customize_compiler(compiler):
175175
'CCSHARED', 'LDSHARED', 'SO', 'AR',
176176
'ARFLAGS')
177177

178-
newcc = None
179178
if 'CC' in os.environ:
180-
cc = os.environ['CC']
179+
newcc = os.environ['CC']
180+
if (sys.platform == 'darwin'
181+
and 'LDSHARED' not in os.environ
182+
and ldshared.startswith(cc)):
183+
# On OS X, if CC is overridden, use that as the default
184+
# command for LDSHARED as well
185+
ldshared = newcc + ldshared[len(cc):]
186+
cc = newcc
181187
if 'CXX' in os.environ:
182188
cxx = os.environ['CXX']
183189
if 'LDSHARED' in os.environ:

Lib/distutils/tests/test_unixccompiler.py

Lines changed: 33 additions & 1 deletion
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.test_support import run_unittest
5+
from test.test_support import EnvironmentVarGuard, run_unittest
56

67
from distutils import sysconfig
78
from distutils.unixccompiler import UnixCCompiler
@@ -122,6 +123,37 @@ def gcv(v):
122123
sysconfig.get_config_var = gcv
123124
self.assertEqual(self.cc.rpath_foo(), '-R/foo')
124125

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

126158
def test_suite():
127159
return unittest.makeSuite(UnixCCompilerTestCase)

Misc/NEWS

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

31+
- Issue #18080: When building a C extension module on OS X, if the compiler
32+
is overriden with the CC environment variable, use the new compiler as
33+
the default for linking if LDSHARED is not also overriden. This restores
34+
Distutils behavior introduced in 2.7.3 and inadvertently dropped in 2.7.4.
35+
3136
IDLE
3237
----
3338

0 commit comments

Comments
 (0)