25
25
import os
26
26
from pathlib import Path
27
27
import shutil
28
+ import subprocess
28
29
from zipfile import ZipFile
29
30
30
31
from setuptools import setup , find_packages , Extension
@@ -105,6 +106,9 @@ def add_optional_flags(self):
105
106
cxxflags = []
106
107
if 'CXXFLAGS' in os .environ :
107
108
cxxflags .append (os .environ ['CXXFLAGS' ])
109
+ ldflags = []
110
+ if 'LDFLAGS' in os .environ :
111
+ ldflags .append (os .environ ['LDFLAGS' ])
108
112
109
113
if has_flag (self .compiler , '-fvisibility=hidden' ):
110
114
for ext in self .extensions :
@@ -116,9 +120,37 @@ def add_optional_flags(self):
116
120
continue
117
121
ext .extra_compile_args .append ('-fvisibility-inlines-hidden' )
118
122
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
119
150
120
151
env ['CPPFLAGS' ] = ' ' .join (cppflags )
121
152
env ['CXXFLAGS' ] = ' ' .join (cxxflags )
153
+ env ['LDFLAGS' ] = ' ' .join (ldflags )
122
154
123
155
return env
124
156
0 commit comments