Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c266293

Browse files
committed
Enable LTO for all compiled extensions.
1 parent 17566b2 commit c266293

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

setup.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import os
2626
from pathlib import Path
2727
import shutil
28+
import subprocess
2829
from zipfile import ZipFile
2930

3031
from setuptools import setup, find_packages, Extension
@@ -105,6 +106,9 @@ def add_optional_flags(self):
105106
cxxflags = []
106107
if 'CXXFLAGS' in os.environ:
107108
cxxflags.append(os.environ['CXXFLAGS'])
109+
ldflags = []
110+
if 'LDFLAGS' in os.environ:
111+
ldflags.append(os.environ['LDFLAGS'])
108112

109113
if has_flag(self.compiler, '-fvisibility=hidden'):
110114
for ext in self.extensions:
@@ -116,9 +120,37 @@ def add_optional_flags(self):
116120
continue
117121
ext.extra_compile_args.append('-fvisibility-inlines-hidden')
118122
cxxflags.append('-fvisibility-inlines-hidden')
123+
ranlib = 'RANLIB' in env
124+
if not ranlib and self.compiler.compiler_type == 'unix':
125+
try:
126+
result = subprocess.run(self.compiler.compiler +
127+
['--version'],
128+
stdout=subprocess.PIPE,
129+
stderr=subprocess.STDOUT,
130+
universal_newlines=True)
131+
except Exception as e:
132+
pass
133+
else:
134+
version = result.stdout.lower()
135+
if 'gcc' in version:
136+
ranlib = shutil.which('gcc-ranlib')
137+
elif 'clang' in version:
138+
if sys.platform == 'darwin':
139+
ranlib = True
140+
else:
141+
ranlib = shutil.which('llvm-ranlib')
142+
if ranlib and has_flag(self.compiler, '-flto'):
143+
for ext in self.extensions:
144+
ext.extra_compile_args.append('-flto')
145+
cppflags.append('-flto')
146+
ldflags.append('-flto')
147+
# Needed so FreeType static library doesn't lose its LTO objects.
148+
if isinstance(ranlib, str):
149+
env['RANLIB'] = ranlib
119150

120151
env['CPPFLAGS'] = ' '.join(cppflags)
121152
env['CXXFLAGS'] = ' '.join(cxxflags)
153+
env['LDFLAGS'] = ' '.join(ldflags)
122154

123155
return env
124156

0 commit comments

Comments
 (0)