Skip to content

Commit 988bfc5

Browse files
committed
Reduce redundancy while checking *FLAGS for LTO.
1 parent 8bb0379 commit 988bfc5

File tree

1 file changed

+20
-25
lines changed

1 file changed

+20
-25
lines changed

setup.py

+20-25
Original file line numberDiff line numberDiff line change
@@ -111,31 +111,26 @@ def add_optimization_flags(self):
111111
enable_lto = setupext.config.getboolean('libs', 'enable_lto',
112112
fallback=None)
113113

114-
if 'CFLAGS' in os.environ:
115-
if '-fno-lto' in os.environ['CFLAGS']:
116-
if enable_lto is True:
117-
raise ValueError('Configuration enable_lto=True, but '
118-
'CFLAGS contains -fno-lto')
119-
enable_lto = False
120-
cppflags = []
121-
if 'CPPFLAGS' in os.environ:
122-
cppflags.append(os.environ['CPPFLAGS'])
123-
if '-fno-lto' in os.environ['CPPFLAGS']:
124-
if enable_lto is True:
125-
raise ValueError('Configuration enable_lto=True, but '
126-
'CPPFLAGS contains -fno-lto')
127-
enable_lto = False
128-
cxxflags = []
129-
if 'CXXFLAGS' in os.environ:
130-
cxxflags.append(os.environ['CXXFLAGS'])
131-
if '-fno-lto' in os.environ['CXXFLAGS']:
132-
if enable_lto is True:
133-
raise ValueError('Configuration enable_lto=True, but '
134-
'CXXFLAGS contains -fno-lto')
135-
enable_lto = False
136-
ldflags = []
137-
if 'LDFLAGS' in os.environ:
138-
ldflags.append(os.environ['LDFLAGS'])
114+
def prepare_flags(name, enable_lto):
115+
"""
116+
Prepare *FLAGS from the environment.
117+
118+
If set, return them, and also check whether LTO is disabled in each
119+
one, raising an error if Matplotlib config explicitly enabled LTO.
120+
"""
121+
if name in os.environ:
122+
if '-fno-lto' in os.environ[name]:
123+
if enable_lto is True:
124+
raise ValueError('Configuration enable_lto=True, but '
125+
'{0} contains -fno-lto'.format(name))
126+
enable_lto = False
127+
return [os.environ[name]], enable_lto
128+
return [], enable_lto
129+
130+
_, enable_lto = prepare_flags('CFLAGS', enable_lto) # Only check lto.
131+
cppflags, enable_lto = prepare_flags('CPPFLAGS', enable_lto)
132+
cxxflags, enable_lto = prepare_flags('CXXFLAGS', enable_lto)
133+
ldflags, enable_lto = prepare_flags('LDFLAGS', enable_lto)
139134

140135
if enable_lto is False:
141136
return env

0 commit comments

Comments
 (0)