# HG changeset patch
# Parent 687295c6c8f2b98b4ad0b9d5cfdcc390764b8138
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.
diff --git a/Lib/distutils/sysconfig.py b/Lib/distutils/sysconfig.py
|
|
|
175 | 175 | 'CCSHARED', 'LDSHARED', 'SO', 'AR', |
176 | 176 | 'ARFLAGS') |
177 | 177 | |
178 | | newcc = None |
179 | 178 | 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 |
181 | 187 | if 'CXX' in os.environ: |
182 | 188 | cxx = os.environ['CXX'] |
183 | 189 | if 'LDSHARED' in os.environ: |
diff --git a/Lib/distutils/tests/test_unixccompiler.py b/Lib/distutils/tests/test_unixccompiler.py
|
|
|
1 | 1 | """Tests for distutils.unixccompiler.""" |
| 2 | import os |
2 | 3 | import sys |
3 | 4 | import unittest |
4 | | from test.test_support import run_unittest |
| 5 | from test.test_support import EnvironmentVarGuard, run_unittest |
5 | 6 | |
6 | 7 | from distutils import sysconfig |
7 | 8 | from distutils.unixccompiler import UnixCCompiler |
… |
… |
|
122 | 123 | sysconfig.get_config_var = gcv |
123 | 124 | self.assertEqual(self.cc.rpath_foo(), '-R/foo') |
124 | 125 | |
| 126 | @unittest.skipUnless(sys.platform == 'darwin', 'test only relevant for OS X') |
| 127 | def test_osx_cc_overrides_ldshared(self): |
| 128 | # ensure that setting CC env variable also changes default linker |
| 129 | def gcv(v): |
| 130 | if v == 'LDSHARED': |
| 131 | return 'gcc-4.2 -bundle -undefined dynamic_lookup ' |
| 132 | return 'gcc-4.2' |
| 133 | sysconfig.get_config_var = gcv |
| 134 | with EnvironmentVarGuard() as env: |
| 135 | env['CC'] = 'my_cc' |
| 136 | del env['LDSHARED'] |
| 137 | sysconfig.customize_compiler(self.cc) |
| 138 | self.assertEqual(self.cc.linker_so[0], 'my_cc') |
| 139 | |
| 140 | @unittest.skipUnless(sys.platform == 'darwin', 'test only relevant for OS X') |
| 141 | def test_osx_explict_ldshared(self): |
| 142 | # ensure that setting CC env variable does not change |
| 143 | # explicit LDSHARED setting for linker |
| 144 | def gcv(v): |
| 145 | if v == 'LDSHARED': |
| 146 | return 'gcc-4.2 -bundle -undefined dynamic_lookup ' |
| 147 | return 'gcc-4.2' |
| 148 | sysconfig.get_config_var = gcv |
| 149 | with EnvironmentVarGuard() as env: |
| 150 | env['CC'] = 'my_cc' |
| 151 | env['LDSHARED'] = 'my_ld -bundle -dynamic' |
| 152 | sysconfig.customize_compiler(self.cc) |
| 153 | self.assertEqual(self.cc.linker_so[0], 'my_ld') |
| 154 | |
125 | 155 | |
126 | 156 | def test_suite(): |
127 | 157 | return unittest.makeSuite(UnixCCompilerTestCase) |