-
Notifications
You must be signed in to change notification settings - Fork 25
/
find_library.py
243 lines (218 loc) · 8.34 KB
/
find_library.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#
# This is a cross-platform shared library search mechanism. Tested on:
# OSX, Ubuntu, Fedora, and Windows XP+
#
import os
import glob
import platform
import re
import sys
import shutil
from subprocess import check_call
from tar_download import download_and_extract
try:
from subprocess import getstatusoutput
except ImportError:
from commands import getstatusoutput
BASE_DIR = os.path.abspath(os.path.dirname(__file__))
if __name__ == '__main__':
download_and_extract('http://downloads.sourceforge.net/hunspell/hunspell-1.3.3.tar.gz', 'external')
def get_architecture():
return 'x64' if sys.maxsize > 2**32 else 'x86'
def get_prefered_msvc():
if sys.version_info[0] > 3 or sys.version_info[0] < 2:
raise RuntimeError('Unknown python version')
# Python 2.6-3.2
if sys.version_info[0] < 3 or sys.version_info[1] < 3:
# We should return this, but the 11 build works and we provide it
#return 'msvc9'
return 'msvc11'
# Python 3.3-3.4
elif sys.version_info[1] < 5:
# We should return this, but the 11 build works and we provide it
#return 'msvc10'
return 'msvc11'
# Python 3.5+
else:
# These versions need msvc14+ compilations
return 'msvc14'
def form_possible_names(lib, exts, extact=False):
ret = []
for ext in exts:
if not extact:
ret.append('{}*{}'.format(lib, ext))
else:
ret.append('{}{}'.format(lib, ext))
for ext in exts:
if not extact:
ret.append('lib{}*{}'.format(lib, ext))
else:
ret.append('lib{}{}'.format(lib, ext))
return ret
def do_search(paths, names=[], test_fn=None):
for pn in paths:
globbed = []
for name in names:
if '*' in name:
globbed.extend(glob.glob(os.path.join(pn, name)))
else:
globbed.append(os.path.join(pn, name))
for filepath in globbed:
if test_fn:
if test_fn(filepath):
return filepath, pn
elif os.path.exists(filepath):
return filepath, pn
return None, None
def is_library(filepath, acceptable_exts):
# TODO - This is broken for ".dll.a"
return os.path.isfile(filepath) and (os.path.splitext(filepath)[-1] in acceptable_exts)
def is_header(filepath):
return os.path.isfile(filepath)
def include_dirs(*packages):
dirs = []
if 'hunspell' in packages:
dirs = [
os.path.abspath(os.path.join(BASE_DIR, 'hunspell')),
# Download path for windows if missing
os.path.abspath(os.path.join(BASE_DIR, 'external', 'hunspell-1.3.3', 'src')),
]
if platform.system() != 'Windows':
dirs.extend([
'/usr/local/include',
'/opt/include',
'/usr/include'
])
return [path for path in dirs if os.path.isdir(path)]
def library_dirs(check_local=False):
dirs = [os.path.abspath(BASE_DIR)]
if platform.system() == 'Windows':
dirs.extend([
os.path.dirname(__file__),
os.path.abspath(BASE_DIR),
os.path.join(os.environ.get('SystemRoot'), 'system'),
os.path.join(os.environ.get('SystemRoot'), 'system32'),
os.environ.get('SystemRoot')
])
if check_local:
dirs.append(os.path.join(os.path.dirname(__file__), 'libs', 'msvc'))
dirs.extend(list(set(os.environ.get('PATH').split(os.path.pathsep))))
dirs = [os.path.abspath(path) for path in dirs]
else:
dirs.extend([
os.path.join(os.path.dirname(__file__), 'libs', 'unix'),
'/usr/local/lib64',
'/usr/local/lib',
'/usr/local/libdata',
'/opt/local/lib',
'/usr/lib/x86_64-linux-gnu',
'/usr/lib64',
'/usr/lib',
'/usr/X11/lib',
'/usr/share',
])
dirs = list(set(dirs))
try:
while True:
dirs.remove(None)
except ValueError:
pass
return [path for path in dirs if os.path.isdir(path)]
def get_library_path(lib, check_local=False):
paths = library_dirs(check_local)
acceptable_exts = [
'',
'.so'
]
if platform.system() == 'Windows':
acceptable_exts = [
'.lib'
]
elif platform.system() == 'Darwin':
acceptable_exts.append('.dylib')
names = form_possible_names(lib, acceptable_exts, check_local)
found_lib, found_path = do_search(paths, names, lambda filepath: is_library(filepath, acceptable_exts))
if found_lib and platform.system() == 'Windows':
found_lib = os.path.splitext(found_lib)[0]
return found_lib, found_path
def get_library_linker_name(lib):
found_lib, found_path = get_library_path(lib)
if not found_lib:
# Try x86 or x64
found_lib, found_path = get_library_path(lib + get_architecture(), True)
if not found_lib:
found_lib, found_path = get_library_path('-'.join(
[lib, get_prefered_msvc(), get_architecture()]), True)
if found_lib:
found_lib = re.sub(r'.dylib$|.so$|.dll$|.dll.a$|.a$', '', found_lib.split(os.path.sep)[-1])
if platform.system() != 'Windows':
found_lib = re.sub(r'^lib|', '', found_lib)
return found_lib, found_path
def package_found(package, include_dirs):
for idir in include_dirs:
package_path = os.path.join(idir, package)
if os.path.exists(package_path) and os.access(package_path, os.R_OK):
return True
return False
def build_package(package, directory):
tmp_lib_path = os.path.abspath(os.path.join(os.path.dirname(__file__), 'libs', 'tmp'))
if not os.path.exists(tmp_lib_path):
os.makedirs(tmp_lib_path)
olddir = os.getcwd()
try:
os.chdir(directory)
check_call(['./configure', '--prefix='+tmp_lib_path])
check_call('make')
check_call(['make', 'install'])
finally:
os.chdir(olddir)
lib_path = os.path.abspath(os.path.join(os.path.dirname(__file__), 'libs', 'unix'))
if not os.path.exists(lib_path):
os.makedirs(lib_path)
if package == 'hunspell':
shutil.copyfile(
os.path.join(tmp_lib_path, 'lib', 'libhunspell-1.3.so.0.0.0'),
os.path.join(lib_path, 'libhunspell-1.3.so.0'))
os.symlink(
os.path.join(lib_path, 'libhunspell-1.3.so.0'),
os.path.join(lib_path, 'libhunspell.so'))
shutil.rmtree(tmp_lib_path)
def append_links(pkg, kw):
linker_name, linker_path = get_library_linker_name(pkg)
if linker_name:
kw['libraries'].append(linker_name)
if linker_path:
kw['library_dirs'].append(linker_path)
return linker_name
def pkgconfig(*packages, **kw):
try:
flag_map = {'-I': 'include_dirs', '-L': 'library_dirs', '-l': 'libraries'}
status, response = getstatusoutput("pkg-config --libs --cflags {}".format(' '.join(packages)))
if status != 0:
raise Exception(response)
for token in response.split():
kw.setdefault(flag_map.get(token[:2]), []).append(token[2:])
if token[:2] in flag_map:
arg = flag_map.get(token[:2])
kw.setdefault(arg, []).append(token[2:])
kw[arg] = list(set(kw[arg]))
else: # throw others to extra_link_args
kw.setdefault('extra_link_args', []).append(token)
kw['extra_link_args'] = list(set(kw['extra_link_args']))
except:
kw['include_dirs'] = include_dirs(*packages)
kw['library_dirs'] = []
kw['libraries'] = []
if 'hunspell' in packages and not package_found('hunspell', kw['include_dirs']):
# Prepare for hunspell if it's missing
download_and_extract('http://downloads.sourceforge.net/hunspell/hunspell-1.3.3.tar.gz', 'external')
kw['include_dirs'] = include_dirs(*packages)
for pkg in packages:
if not append_links(pkg, kw):
if pkg == 'hunspell' and platform.system() != 'Windows':
build_package(pkg, os.path.join('external', 'hunspell-1.3.3'))
if not append_links(pkg, kw):
print("Couldn't find lib dependency after building: {}".format(pkg))
else:
print("Couldn't find lib dependency: {}".format(pkg))
return kw