Skip to content

bpo-33990: Add CPPFLAGS during ./configure when customizing the compiler in distutils #7997

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions Lib/distutils/sysconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,8 @@ def customize_compiler(compiler):
_osx_support.customize_compiler(_config_vars)
_config_vars['CUSTOMIZED_OSX_COMPILER'] = 'True'

(cc, cxx, opt, cflags, ccshared, ldshared, shlib_suffix, ar, ar_flags) = \
get_config_vars('CC', 'CXX', 'OPT', 'CFLAGS',
(cc, cxx, opt, cppflags, cflags, ccshared, ldshared, shlib_suffix, ar, ar_flags) = \
get_config_vars('CC', 'CXX', 'OPT', 'CPPFLAGS', 'CFLAGS',
'CCSHARED', 'LDSHARED', 'SHLIB_SUFFIX', 'AR', 'ARFLAGS')

if 'CC' in os.environ:
Expand All @@ -197,17 +197,17 @@ def customize_compiler(compiler):
cflags = opt + ' ' + os.environ['CFLAGS']
ldshared = ldshared + ' ' + os.environ['CFLAGS']
if 'CPPFLAGS' in os.environ:
cpp = cpp + ' ' + os.environ['CPPFLAGS']
cflags = cflags + ' ' + os.environ['CPPFLAGS']
ldshared = ldshared + ' ' + os.environ['CPPFLAGS']
cppflags = os.environ['CPPFLAGS']
if 'AR' in os.environ:
ar = os.environ['AR']
if 'ARFLAGS' in os.environ:
archiver = ar + ' ' + os.environ['ARFLAGS']
else:
archiver = ar + ' ' + ar_flags

cc_cmd = cc + ' ' + cflags
cpp = cpp + ' ' + cppflags
ldshared = ldshared + ' ' + cppflags
cc_cmd = cc + ' ' + cppflags + ' ' + cflags
compiler.set_executables(
preprocessor=cpp,
compiler=cc_cmd,
Expand Down
34 changes: 34 additions & 0 deletions Lib/distutils/tests/test_sysconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,40 @@ def set_executables(self, **kw):
sysconfig.customize_compiler(comp)
self.assertEqual(comp.exes['archiver'], 'my_ar -arflags')

@unittest.skipUnless(get_default_compiler() == 'unix',
'not testing if default compiler is not unix')
def test_customize_compiler_flag_inclusion(self):
# Clear out environment variables to ensure calls to `get_config_var()`
# pick up the flags captured during `./configure`.
os.environ.pop('CPPFLAGS', None)
os.environ.pop('CFLAGS', None)
os.environ.pop('LDSHARED', None)

cppflags = sysconfig.get_config_var('CPPFLAGS')
cflags = sysconfig.get_config_var('CFLAGS')
ldflags = sysconfig.get_config_var('LDSHARED')

class compiler:
compiler_type = 'unix'

def set_executables(self, **kw):
self.exes = kw

comp = compiler()
sysconfig.customize_compiler(comp)

# Ensure CPPFLAGS are present.
self.assertIn(cppflags, comp.exes['preprocessor'])
self.assertIn(cppflags, comp.exes['compiler'])
self.assertIn(cppflags, comp.exes['compiler_so'])
self.assertIn(cppflags, comp.exes['linker_so'])

# Ensure CFLAGS are present.
self.assertIn(cflags, comp.exes['compiler'])

# Ensure LDSHARED are present.
self.assertIn(ldflags, comp.exes['linker_so'])

def test_parse_makefile_base(self):
self.makefile = TESTFN
fd = open(self.makefile, 'w')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Add the captured ``CPPFLAGS`` passed during ``./configure`` to the
executables when customizing the compiler command in
``sysconfig.cusomize_compiler`` for building C/C++ Python extensions.