-
Notifications
You must be signed in to change notification settings - Fork 12
/
setup.py
139 lines (109 loc) · 4.51 KB
/
setup.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
#!/usr/bin/env python3
#
# Copyright (c) 2020 Xiaomi Corporation (author: Fangjun Kuang)
import glob
import os
import re
import shutil
import sys
import setuptools
from setuptools.command.build_ext import build_ext
cur_dir = os.path.dirname(os.path.abspath(__file__))
def cmake_extension(name, *args, **kwargs) -> setuptools.Extension:
kwargs["language"] = "c++"
sources = []
return setuptools.Extension(name, sources, *args, **kwargs)
class BuildExtension(build_ext):
def build_extension(self, ext: setuptools.extension.Extension):
# build/temp.linux-x86_64-3.8
build_dir = self.build_temp
os.makedirs(build_dir, exist_ok=True)
# build/lib.linux-x86_64-3.8
os.makedirs(self.build_lib, exist_ok=True)
ot_dir = os.path.dirname(os.path.abspath(__file__))
cmake_args = os.environ.get("OT_CMAKE_ARGS", "")
make_args = os.environ.get("OT_MAKE_ARGS", "")
system_make_args = os.environ.get("MAKEFLAGS", "")
if cmake_args == "":
cmake_args = "-DCMAKE_BUILD_TYPE=Release"
if make_args == "" and system_make_args == "":
print("For fast compilation, run:")
print('export OT_MAKE_ARGS="-j"; python setup.py install')
if "PYTHON_EXECUTABLE" not in cmake_args:
print(f"Setting PYTHON_EXECUTABLE to {sys.executable}")
cmake_args += f" -DPYTHON_EXECUTABLE={sys.executable}"
build_cmd = f"""
cd {self.build_temp}
cmake {cmake_args} {ot_dir}
make {make_args} _optimized_transducer
"""
print(f"build command is:\n{build_cmd}")
ret = os.system(build_cmd)
if ret != 0:
raise Exception(
"\nBuild optimized_transducer failed. Please check the error "
"message.\n"
"You can ask for help by creating an issue on GitHub.\n"
"\nClick:\n"
"\thttps://github.com/csukuangfj/optimized_transducer/issues/new\n" # noqa
)
lib_so = glob.glob(f"{build_dir}/lib/*.so*")
for so in lib_so:
print(f"Copying {so} to {self.build_lib}/")
shutil.copy(f"{so}", f"{self.build_lib}/")
# macos
lib_so = glob.glob(f"{build_dir}/lib/*.dylib*")
for so in lib_so:
print(f"Copying {so} to {self.build_lib}/")
shutil.copy(f"{so}", f"{self.build_lib}/")
print(
f"Copying {ot_dir}/optimized_transducer/python/optimized_transducer/torch_version.py to {self.build_lib}/optimized_transducer" # noqa
)
shutil.copy(
f"{ot_dir}/optimized_transducer/python/optimized_transducer/torch_version.py",
f"{self.build_lib}/optimized_transducer",
)
def read_long_description():
with open("README.md", encoding="utf8") as f:
readme = f.read()
return readme
def get_package_version():
with open("CMakeLists.txt") as f:
content = f.read()
latest_version = re.search(r"set\(OT_VERSION (.*)\)", content).group(1)
latest_version = latest_version.strip('"')
return latest_version
package_name = "optimized_transducer"
with open("optimized_transducer/python/optimized_transducer/__init__.py", "a") as f:
f.write(f"__version__ = '{get_package_version()}'\n")
setuptools.setup(
name=package_name,
version=get_package_version(),
author="Fangjun Kuang",
author_email="[email protected]",
package_dir={
package_name: "optimized_transducer/python/optimized_transducer",
},
packages=[package_name],
url="https://github.com/csukuangfj/optimized_transducer",
long_description=read_long_description(),
long_description_content_type="text/markdown",
ext_modules=[cmake_extension("_optimized_transducer")],
cmdclass={"build_ext": BuildExtension},
zip_safe=False,
classifiers=[
"Programming Language :: C++",
"Programming Language :: Python",
"Topic :: Scientific/Engineering :: Artificial Intelligence",
],
license="Apache licensed, as found in the LICENSE file",
)
# remove the line __version__ from optimized_transducer/python/optimized_transducer/__init__.py
with open("optimized_transducer/python/optimized_transducer/__init__.py", "r") as f:
lines = f.readlines()
with open("optimized_transducer/python/optimized_transducer/__init__.py", "w") as f:
for line in lines:
if "__version__" in line and "torch" not in line:
# skip __version__ = "x.x.x"
continue
f.write(line)