-
Notifications
You must be signed in to change notification settings - Fork 70
/
setup.py
155 lines (135 loc) · 4.46 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import os
import subprocess
import sys
from glob import glob
from itertools import chain
from os.path import exists, join
import numpy as np
import setuptools.command.build_ext
from setuptools import Extension, setup
platform_is_windows = sys.platform == "win32"
msvc_extra_compile_args_config = [
"/source-charset:utf-8",
"/execution-charset:utf-8",
]
def msvc_extra_compile_args(compile_args):
cas = set(compile_args)
xs = filter(lambda x: x not in cas, msvc_extra_compile_args_config)
return list(chain(compile_args, xs))
class custom_build_ext(setuptools.command.build_ext.build_ext):
def build_extensions(self):
compiler_type_is_msvc = self.compiler.compiler_type == "msvc"
for entry in self.extensions:
if compiler_type_is_msvc:
entry.extra_compile_args = msvc_extra_compile_args(
entry.extra_compile_args
if hasattr(entry, "extra_compile_args")
else []
)
setuptools.command.build_ext.build_ext.build_extensions(self)
def check_cmake_in_path():
try:
result = subprocess.run(
["cmake", "--version"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
)
if result.returncode == 0:
# CMake is in the system path
return True, result.stdout.strip()
else:
# CMake is not in the system path
return False, None
except FileNotFoundError:
# CMake command not found
return False, None
if os.name == "nt": # Check if the OS is Windows
# Check if CMake is in the system path
cmake_found, cmake_version = check_cmake_in_path()
if cmake_found:
print(
f"CMake is in the system path. Version: \
{cmake_version}"
)
else:
raise SystemError(
"CMake is not found in the \
system path. Make sure CMake \
is installed and in the system \
path."
)
# open_jtalk sources
src_top = join("lib", "open_jtalk", "src")
# generate config.h for mecab
# NOTE: need to run cmake to generate config.h
# we could do it on python side but it would be very tricky,
# so far let's use cmake tool
if not exists(join(src_top, "mecab", "src", "config.h")):
cwd = os.getcwd()
build_dir = join(src_top, "build")
os.makedirs(build_dir, exist_ok=True)
os.chdir(build_dir)
# NOTE: The wrapped OpenJTalk does not depend on HTS_Engine,
# but since HTSEngine is included in CMake's dependencies, it refers to a dummy path.
r = subprocess.run(
["cmake", "..", "-DHTS_ENGINE_INCLUDE_DIR=.", "-DHTS_ENGINE_LIB=dummy"]
)
r.check_returncode()
os.chdir(cwd)
all_src = []
include_dirs = []
for s in [
"jpcommon",
"mecab/src",
"mecab2njd",
"njd",
"njd2jpcommon",
"njd_set_accent_phrase",
"njd_set_accent_type",
"njd_set_digit",
"njd_set_long_vowel",
"njd_set_pronunciation",
"njd_set_unvoiced_vowel",
"text2mecab",
]:
all_src += glob(join(src_top, s, "*.c"))
all_src += glob(join(src_top, s, "*.cpp"))
include_dirs.append(join(os.getcwd(), src_top, s))
# Extension for OpenJTalk frontend
ext_modules = [
Extension(
name="pyopenjtalk.openjtalk",
sources=[join("pyopenjtalk", "openjtalk.pyx")] + all_src,
include_dirs=[np.get_include()] + include_dirs,
extra_compile_args=[],
extra_link_args=[],
language="c++",
define_macros=[
("HAVE_CONFIG_H", None),
("DIC_VERSION", "102"),
("MECAB_DEFAULT_RC", '"dummy"'),
("PACKAGE", '"open_jtalk"'),
("VERSION", '"1.10"'),
("CHARSET_UTF_8", None),
],
)
]
# Extension for HTSEngine backend
htsengine_src_top = join("lib", "hts_engine_API", "src")
all_htsengine_src = glob(join(htsengine_src_top, "lib", "*.c"))
ext_modules += [
Extension(
name="pyopenjtalk.htsengine",
sources=[join("pyopenjtalk", "htsengine.pyx")] + all_htsengine_src,
include_dirs=[np.get_include(), join(htsengine_src_top, "include")],
extra_compile_args=[],
extra_link_args=[],
libraries=["winmm"] if platform_is_windows else [],
language="c++",
define_macros=[
("AUDIO_PLAY_NONE", None),
],
)
]
setup(ext_modules=ext_modules, cmdclass={"build_ext": custom_build_ext})