Skip to content

Commit 1f3188c

Browse files
committed
Refactored customize_compiler to reduce logical branches and extract _add_flags logic.
Reduces cyclomatic complexity so it passes QA checks.
1 parent 52cd70b commit 1f3188c

File tree

1 file changed

+34
-32
lines changed

1 file changed

+34
-32
lines changed

distutils/sysconfig.py

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ def _customize_macos():
287287
)
288288

289289

290-
def customize_compiler(compiler): # noqa: C901
290+
def customize_compiler(compiler):
291291
"""Do any platform-specific customization of a CCompiler instance.
292292
293293
Mainly needed on Unix, so we can plug in the information that
@@ -329,40 +329,32 @@ def customize_compiler(compiler): # noqa: C901
329329
# command for LDSHARED as well
330330
ldshared = newcc + ldshared[len(cc) :]
331331
cc = newcc
332-
if 'CXX' in os.environ:
333-
cxx = os.environ['CXX']
334-
if 'LDSHARED' in os.environ:
335-
ldshared = os.environ['LDSHARED']
336-
if 'LDCXXSHARED' in os.environ:
337-
ldcxxshared = os.environ['LDCXXSHARED']
338-
if 'CPP' in os.environ:
339-
cpp = os.environ['CPP']
340-
else:
341-
cpp = cc + " -E" # not always
342-
if 'LDFLAGS' in os.environ:
343-
ldshared = ldshared + ' ' + os.environ['LDFLAGS']
344-
ldcxxshared = ldcxxshared + ' ' + os.environ['LDFLAGS']
345-
if 'CFLAGS' in os.environ:
346-
cflags = cflags + ' ' + os.environ['CFLAGS']
347-
ldshared = ldshared + ' ' + os.environ['CFLAGS']
348-
if 'CXXFLAGS' in os.environ:
349-
cxxflags = os.environ['CXXFLAGS']
350-
ldcxxshared = ldcxxshared + ' ' + os.environ['CXXFLAGS']
351-
if 'CPPFLAGS' in os.environ:
352-
cpp = cpp + ' ' + os.environ['CPPFLAGS']
353-
cflags = cflags + ' ' + os.environ['CPPFLAGS']
354-
cxxflags = cxxflags + ' ' + os.environ['CPPFLAGS']
355-
ldshared = ldshared + ' ' + os.environ['CPPFLAGS']
356-
ldcxxshared = ldcxxshared + ' ' + os.environ['CPPFLAGS']
357-
if 'AR' in os.environ:
358-
ar = os.environ['AR']
359-
if 'ARFLAGS' in os.environ:
360-
archiver = ar + ' ' + os.environ['ARFLAGS']
361-
else:
362-
archiver = ar + ' ' + ar_flags
332+
cxx = os.environ.get('CXX', cxx)
333+
ldshared = os.environ.get('LDSHARED', ldshared)
334+
ldcxxshared = os.environ.get('LDCXXSHARED', ldcxxshared)
335+
cpp = os.environ.get(
336+
'CPP',
337+
cc + " -E", # not always
338+
)
363339

340+
ldshared = _add_flags(ldshared, 'LD')
341+
ldcxxshared = _add_flags(ldcxxshared, 'LD')
342+
cflags = _add_flags(cflags, 'C')
343+
ldshared = _add_flags(ldshared, 'C')
344+
cxxflags = os.environ.get('CXXFLAGS', cxxflags)
345+
ldcxxshared = _add_flags(ldcxxshared, 'CXX')
346+
cpp = _add_flags(cpp, 'CPP')
347+
cflags = _add_flags(cflags, 'CPP')
348+
cxxflags = _add_flags(cxxflags, 'CPP')
349+
ldshared = _add_flags(ldshared, 'CPP')
350+
ldcxxshared = _add_flags(ldcxxshared, 'CPP')
351+
352+
ar = os.environ.get('AR', ar)
353+
354+
archiver = ar + ' ' + os.environ.get('ARFLAGS', ar_flags)
364355
cc_cmd = cc + ' ' + cflags
365356
cxx_cmd = cxx + ' ' + cxxflags
357+
366358
compiler.set_executables(
367359
preprocessor=cpp,
368360
compiler=cc_cmd,
@@ -577,3 +569,13 @@ def get_config_var(name):
577569

578570
warnings.warn('SO is deprecated, use EXT_SUFFIX', DeprecationWarning, 2)
579571
return get_config_vars().get(name)
572+
573+
574+
def _add_flags(value: str, type: str) -> str:
575+
"""
576+
Add any flags from the environment for the given type.
577+
578+
type is the prefix to FLAGS in the environment key (e.g. "C" for "CFLAGS").
579+
"""
580+
flags = os.environ.get(f'{type}FLAGS')
581+
return f'{value} {flags}' if flags else value

0 commit comments

Comments
 (0)