Skip to content

Commit 6c0e0d1

Browse files
bpo-36235: Fix CFLAGS in distutils customize_compiler() (GH-12236) (GH-12348)
Fix CFLAGS in customize_compiler() of distutils.sysconfig: when the CFLAGS environment variable is defined, don't override CFLAGS variable with the OPT variable anymore. Initial patch written by David Malcolm. Co-Authored-By: David Malcolm <[email protected]> (cherry picked from commit 86082c2)
1 parent 0b9bd5b commit 6c0e0d1

File tree

3 files changed

+18
-7
lines changed

3 files changed

+18
-7
lines changed

Lib/distutils/sysconfig.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,8 @@ def customize_compiler(compiler):
183183
_osx_support.customize_compiler(_config_vars)
184184
_config_vars['CUSTOMIZED_OSX_COMPILER'] = 'True'
185185

186-
(cc, cxx, opt, cflags, ccshared, ldshared, shlib_suffix, ar, ar_flags) = \
187-
get_config_vars('CC', 'CXX', 'OPT', 'CFLAGS',
186+
(cc, cxx, cflags, ccshared, ldshared, shlib_suffix, ar, ar_flags) = \
187+
get_config_vars('CC', 'CXX', 'CFLAGS',
188188
'CCSHARED', 'LDSHARED', 'SHLIB_SUFFIX', 'AR', 'ARFLAGS')
189189

190190
if 'CC' in os.environ:
@@ -207,7 +207,7 @@ def customize_compiler(compiler):
207207
if 'LDFLAGS' in os.environ:
208208
ldshared = ldshared + ' ' + os.environ['LDFLAGS']
209209
if 'CFLAGS' in os.environ:
210-
cflags = opt + ' ' + os.environ['CFLAGS']
210+
cflags = cflags + ' ' + os.environ['CFLAGS']
211211
ldshared = ldshared + ' ' + os.environ['CFLAGS']
212212
if 'CPPFLAGS' in os.environ:
213213
cpp = cpp + ' ' + os.environ['CPPFLAGS']

Lib/distutils/tests/test_sysconfig.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from distutils import sysconfig
1010
from distutils.ccompiler import get_default_compiler
1111
from distutils.tests import support
12-
from test.support import TESTFN, run_unittest, check_warnings
12+
from test.support import TESTFN, run_unittest, check_warnings, swap_item
1313

1414
class SysconfigTestCase(support.EnvironGuard, unittest.TestCase):
1515
def setUp(self):
@@ -78,7 +78,9 @@ def test_srcdir_independent_of_cwd(self):
7878
'not testing if default compiler is not unix')
7979
def test_customize_compiler(self):
8080
os.environ['AR'] = 'my_ar'
81-
os.environ['ARFLAGS'] = '-arflags'
81+
os.environ['CC'] = 'my_cc'
82+
os.environ['ARFLAGS'] = '--myarflags'
83+
os.environ['CFLAGS'] = '--mycflags'
8284

8385
# make sure AR gets caught
8486
class compiler:
@@ -87,9 +89,14 @@ class compiler:
8789
def set_executables(self, **kw):
8890
self.exes = kw
8991

92+
# Make sure that sysconfig._config_vars is initialized
93+
sysconfig.get_config_vars()
94+
9095
comp = compiler()
91-
sysconfig.customize_compiler(comp)
92-
self.assertEqual(comp.exes['archiver'], 'my_ar -arflags')
96+
with swap_item(sysconfig._config_vars, 'CFLAGS', '--sysconfig-cflags'):
97+
sysconfig.customize_compiler(comp)
98+
self.assertEqual(comp.exes['archiver'], 'my_ar --myarflags')
99+
self.assertEqual(comp.exes['compiler'], 'my_cc --sysconfig-cflags --mycflags')
93100

94101
def test_parse_makefile_base(self):
95102
self.makefile = TESTFN
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Fix ``CFLAGS`` in ``customize_compiler()`` of ``distutils.sysconfig``: when
2+
the ``CFLAGS`` environment variable is defined, don't override ``CFLAGS``
3+
variable with the ``OPT`` variable anymore. Initial patch written by David
4+
Malcolm.

0 commit comments

Comments
 (0)