-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathsetup.py
More file actions
456 lines (373 loc) · 14.4 KB
/
setup.py
File metadata and controls
456 lines (373 loc) · 14.4 KB
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
from distutils.dir_util import remove_tree
from setuptools import setup, Extension
import distutils.command.build as _build
import distutils.command.bdist as _bdist
import distutils.command.build_ext as _build_ext
import distutils.command.clean as _clean
import distutils.command.sdist as _sdist
import json
import os
import sys
import subprocess
import multiprocessing
import shutil
import glob
import platform
from contextlib import contextmanager
from os import path
pykeyvi_pyx = "_core.pyx"
pykeyvi_cpp = "_core.cpp"
pykeyvi_p_cpp = "_core_p.cpp"
keyvi_cpp_source = "../keyvi"
keyvi_cpp = "src/cpp"
keyvi_cpp_link = path.join(keyvi_cpp, "keyvi")
keyvi_build_dir = path.join(keyvi_cpp, "build-{}".format(platform.platform()))
here = os.path.abspath(os.path.dirname(__file__))
try:
cpu_count = multiprocessing.cpu_count()
except NotImplementedError:
cpu_count = 1
#################
VERSION_MAJOR = 0
VERSION_MINOR = 7
VERSION_PATCH = 4
VERSION_DEV = 0
IS_RELEASED = False
VERSION = "{}.{}.{}".format(VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH)
if not IS_RELEASED:
VERSION += ".dev{}".format(VERSION_DEV)
###################
def run_once(f):
def wrapper(*args, **kwargs):
if not wrapper.has_run:
wrapper.has_run = True
wrapper.ret = f(*args, **kwargs)
return wrapper.ret
wrapper.has_run = False
wrapper.ret = None
return wrapper
def write_version_file():
version_file_path = os.path.join(here, "src/py/keyvi/_version.py")
content = """
# THIS FILE IS GENERATED FROM KEYVI SETUP.PY
__version__ = '{}'
""".format(VERSION)
with open(version_file_path, "w") as f_out:
f_out.write(content)
def clean_pykeyvi_build_directory():
if os.path.exists(keyvi_build_dir):
remove_tree(keyvi_build_dir)
def generate_pykeyvi_source():
addons = glob.glob("src/addons/*")
pxds = glob.glob("src/pxds/*")
converters = "src/converters"
converter_files = glob.glob(path.join(converters, "*"))
max_modification_time = max(
[path.getmtime(fn) for fn in addons + pxds + converter_files]
)
if not path.exists(pykeyvi_cpp) or max_modification_time > path.getmtime(
pykeyvi_cpp
):
try:
import autowrap.Main
autowrap.Main.run(
pxds,
addons,
[converters],
pykeyvi_pyx,
extra_opts={"compiler_directives": {"freethreading_compatible": True}},
)
# rewrite generated cpp to use std::shared_ptr instead of boost::shared_ptr
with open(pykeyvi_cpp, "rt") as fin:
with open(pykeyvi_p_cpp, "wt") as fout:
for line in fin:
if line.find("shared_ptr.hpp") > 0:
continue
fout.write(line.replace("boost::shared_ptr", "std::shared_ptr"))
except ModuleNotFoundError:
if not path.exists(pykeyvi_cpp):
raise
else:
print(
"Could not find autowrap, probably running from sdist environment"
)
@contextmanager
def symlink_keyvi():
if not path.exists(keyvi_cpp_link):
try:
if not path.exists(keyvi_cpp):
os.makedirs(keyvi_cpp)
os.symlink(path.abspath(keyvi_cpp_source), keyvi_cpp_link)
shutil.copy("../CMakeLists.txt", path.join(keyvi_cpp, "CMakeLists.txt"))
shutil.copytree("../cmake_modules", path.join(keyvi_cpp, "cmake_modules"))
keyvi_source_path = os.path.realpath(
os.path.join(os.getcwd(), keyvi_cpp_source)
)
pykeyvi_source_path = os.path.join(os.getcwd(), keyvi_cpp_link)
yield (pykeyvi_source_path, keyvi_source_path)
finally:
os.unlink(keyvi_cpp_link)
os.remove(path.join(keyvi_cpp, "CMakeLists.txt"))
shutil.rmtree(path.join(keyvi_cpp, "cmake_modules"))
else:
yield None, None
@run_once
def cmake_configure(
build_path,
build_type,
zlib_root,
additional_compile_flags,
osx_architectures=None,
cmake_prefix_path=None,
):
# needed for shared library
CMAKE_CXX_FLAGS = additional_compile_flags + " -fPIC"
cmake_configure_cmd = "mkdir -p {}".format(build_path)
cmake_configure_cmd += " && cd {}".format(build_path)
cmake_configure_cmd += ' && cmake -D CMAKE_CXX_FLAGS="{CXX_FLAGS}"'.format(
CXX_FLAGS=CMAKE_CXX_FLAGS.strip()
)
cmake_configure_cmd += f" -D CMAKE_BUILD_TYPE={build_type}"
if osx_architectures is not None:
cmake_configure_cmd += ' -D CMAKE_OSX_ARCHITECTURES="{OSX_ARCH}"'.format(
OSX_ARCH=osx_architectures
)
if zlib_root is not None:
cmake_configure_cmd += " -D ZLIB_ROOT={ZLIB_ROOT}".format(ZLIB_ROOT=zlib_root)
if cmake_prefix_path is not None:
cmake_configure_cmd += " -D CMAKE_PREFIX_PATH={CMAKE_PREFIX_PATH}".format(
CMAKE_PREFIX_PATH=cmake_prefix_path
)
cmake_configure_cmd += " .."
print("Building in {0} mode".format(build_type))
print("Run keyvi C++ cmake: " + cmake_configure_cmd)
subprocess.call(cmake_configure_cmd, shell=True)
cmake_flags = {}
with open(os.path.join(build_path, "keyvi", "flags")) as flags:
for line in flags:
k, v = line.strip().split("=", 1)
cmake_flags[k] = " ".join(v.split())
# set additional compiler flags
set_additional_flags(
"extra_compile_args", cmake_flags["KEYVI_CXX_FLAGS_ALL"].split(" ")
)
# set defines
if cmake_flags["KEYVI_COMPILE_DEFINITIONS"]:
define_macros = []
for macro in cmake_flags["KEYVI_COMPILE_DEFINITIONS"].split(" "):
if macro.count("=") == 0:
define_macros.append((macro, None))
else:
define_macros.append(macro.split("=", 1))
set_additional_flags("define_macros", define_macros)
# set includes
if cmake_flags["KEYVI_INCLUDES"]:
set_additional_flags("include_dirs", cmake_flags["KEYVI_INCLUDES"].split(" "))
# set link libraries
if cmake_flags["KEYVI_LINK_LIBRARIES_STATIC"]:
if sys.platform == "darwin":
set_additional_flags(
"libraries", cmake_flags["KEYVI_LINK_LIBRARIES_STATIC"].split(" ")
)
else:
extra_link_arguments = ["-Wl,-Bstatic"]
for lib in cmake_flags["KEYVI_LINK_LIBRARIES_STATIC"].split(" "):
extra_link_arguments.append("-l{}".format(lib))
# reset to dynamic
extra_link_arguments.append("-Wl,-Bdynamic")
set_additional_flags("extra_link_args", extra_link_arguments)
if cmake_flags["KEYVI_LINK_LIBRARIES_DYNAMIC"]:
set_additional_flags(
"libraries", cmake_flags["KEYVI_LINK_LIBRARIES_DYNAMIC"].split(" ")
)
# set link args
if cmake_flags["KEYVI_LINK_FLAGS"]:
set_additional_flags(
"extra_link_args", cmake_flags["KEYVI_LINK_FLAGS"].split(" ")
)
return cmake_flags
def set_additional_flags(key, additional_flags):
# patch the flags specified in key
for ext_m in ext_modules:
flags = getattr(ext_m, key) + additional_flags
setattr(ext_m, key, flags)
def patch_for_custom_zlib(zlib_root):
for ext_m in ext_modules:
include_dirs = [path.join(zlib_root, "include")] + getattr(
ext_m, "include_dirs"
)
setattr(ext_m, "include_dirs", include_dirs)
library_dirs = [path.join(zlib_root, "lib")] + getattr(ext_m, "library_dirs")
setattr(ext_m, "library_dirs", library_dirs)
with symlink_keyvi() as (pykeyvi_source_path, keyvi_source_path):
# workaround for autowrap bug (includes incompatible boost)
autowrap_data_dir = "autowrap_includes"
dictionary_sources = path.abspath(keyvi_cpp_link)
additional_compile_flags = ""
# re-map the source files in the debug symbol tables to there original location so that stepping in a debugger works
if pykeyvi_source_path is not None:
additional_compile_flags += " -fdebug-prefix-map={}={}".format(
pykeyvi_source_path, keyvi_source_path
)
link_library_dirs = [keyvi_build_dir]
# if _CMAKE_PREFIX_PATH is set, append the lib subfolder for linking
cmake_prefix_path = os.environ.get("_CMAKE_PREFIX_PATH")
if cmake_prefix_path is not None:
link_library_dirs.append(os.path.join(cmake_prefix_path, "lib"))
else:
# as of 17/07/2022 Python 3.10 build on GH actions needs '/usr/local/lib/' link library dir
link_library_dirs.append("/usr/local/lib/")
link_library_dirs.append("/opt/homebrew/lib")
#########################
# Custom 'build' command
#########################
custom_user_options = [
("mode=", None, "build mode."),
("zlib-root=", None, "zlib installation root"),
]
class custom_opts:
parent = None
def initialize_options(self):
self.parent.initialize_options(self)
self.mode = None
self.zlib_root = None
self.options = {}
def load_options(self):
# preserves setting between build and install
if not self.mode and not self.zlib_root:
try:
f = open(path.join(keyvi_build_dir, "custom_opts"), "r")
self.options = json.loads(f.readline())
return
except FileNotFoundError:
pass
self.options["mode"] = "release" if not self.mode else self.mode
if self.zlib_root:
self.options["zlib_root"] = self.zlib_root
def save_options(self):
# store the options
f = open(path.join(keyvi_build_dir, "custom_opts"), "w")
f.write(json.dumps(self.options))
def run(self):
self.load_options()
# cross-compilation support for cibuildwheel for building M1 targets on x86_64
osx_architectures = None
archflags = os.environ.get("ARCHFLAGS")
if archflags is not None:
osx_architectures = ";".join(
set(archflags.split()) & {"x86_64", "arm64"}
)
cmake_prefix_path = os.environ.get("_CMAKE_PREFIX_PATH")
self.cmake_flags = cmake_configure(
keyvi_build_dir,
self.options["mode"],
self.options.get("zlib_root"),
additional_compile_flags,
osx_architectures,
cmake_prefix_path,
)
self.save_options()
self.parent.run(self)
class build(custom_opts, _build.build):
parent = _build.build
user_options = _build.build.user_options + custom_user_options
class sdist(_sdist.sdist):
def run(self):
clean_pykeyvi_build_directory()
generate_pykeyvi_source()
_sdist.sdist.run(self)
class bdist(custom_opts, _bdist.bdist):
parent = _bdist.bdist
user_options = _bdist.bdist.user_options + custom_user_options
class clean(_clean.clean):
def run(self):
clean_pykeyvi_build_directory()
_clean.clean.run(self)
class build_cxx(_build_ext.build_ext):
description = "customized for keyvi: " + _build_ext.build_ext.description
def initialize_options(self):
_build_ext.build_ext.initialize_options(self)
def run(self):
generate_pykeyvi_source()
# custom zlib location
if "zlib_root" in self.options:
patch_for_custom_zlib(self.options["zlib_root"])
keyvi_build_cmd = "cd {} && make -j {} bindings".format(
keyvi_build_dir, cpu_count
)
print("Building keyvi C++ part: " + keyvi_build_cmd)
subprocess.call(keyvi_build_cmd, shell=True)
_build_ext.build_ext.run(self)
class build_ext(custom_opts, build_cxx):
parent = build_cxx
user_options = build_cxx.user_options + custom_user_options
ext_modules = [
Extension(
"keyvi._core",
include_dirs=[autowrap_data_dir],
language="c++",
sources=[pykeyvi_p_cpp],
library_dirs=link_library_dirs,
)
]
PACKAGE_NAME = "keyvi"
with open(os.path.join(here, "description.md"), "rt", encoding="utf-8") as desc_f:
long_desc = desc_f.read()
install_requires = [
"msgpack>=1.0.0",
]
commands = {
"build_ext": build_ext,
"sdist": sdist,
"build": build,
"bdist": bdist,
"clean": clean,
}
for e in ext_modules:
e.cython_directives = {"embedsignature": True}
write_version_file()
setup(
name=PACKAGE_NAME,
version=VERSION,
description="Python package for keyvi",
long_description=long_desc,
long_description_content_type="text/markdown",
author="Hendrik Muhs",
author_email="hendrik.muhs@gmail.com",
license="ASL 2.0",
cmdclass=commands,
scripts=["src/py/bin/keyvi"],
packages=[
"keyvi",
"keyvi.cli",
"keyvi.compiler",
"keyvi.completion",
"keyvi.dictionary",
"keyvi.index",
"keyvi.util",
"keyvi.vector",
"keyvi._pycore",
],
package_dir={"": "src/py"},
ext_modules=ext_modules,
zip_safe=False,
url="http://keyvi.org",
download_url="https://github.com/KeyviDev/keyvi/tarball/v{}".format(VERSION),
keywords=["FST"],
classifiers=[
"Programming Language :: C++",
"Programming Language :: Cython",
"Programming Language :: Python",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Programming Language :: Python :: 3.13",
"Programming Language :: Python :: 3.14",
"Programming Language :: Python :: Implementation :: CPython",
"Programming Language :: Python :: Implementation :: PyPy",
"Operating System :: MacOS :: MacOS X",
"Operating System :: Unix",
],
install_requires=install_requires,
)