Skip to content

Commit fae8a09

Browse files
committed
Reduce redundancy while checking *FLAGS for LTO.
1 parent 559044e commit fae8a09

File tree

1 file changed

+21
-25
lines changed

1 file changed

+21
-25
lines changed

setup.py

+21-25
Original file line numberDiff line numberDiff line change
@@ -111,31 +111,27 @@ 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+
flags = []
122+
if name in os.environ:
123+
flags.append(os.environ[name])
124+
if '-fno-lto' in os.environ[name]:
125+
if enable_lto is True:
126+
raise ValueError('Configuration enable_lto=True, but '
127+
'{0} contains -fno-lto'.format(name))
128+
enable_lto = False
129+
return flags, enable_lto
130+
131+
_, enable_lto = prepare_flags('CFLAGS', enable_lto) # Only check lto.
132+
cppflags, enable_lto = prepare_flags('CPPFLAGS', enable_lto)
133+
cxxflags, enable_lto = prepare_flags('CXXFLAGS', enable_lto)
134+
ldflags, enable_lto = prepare_flags('LDFLAGS', enable_lto)
139135

140136
if enable_lto is False:
141137
return env

0 commit comments

Comments
 (0)