-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathsetup.py
More file actions
executable file
·268 lines (232 loc) · 8.45 KB
/
Copy pathsetup.py
File metadata and controls
executable file
·268 lines (232 loc) · 8.45 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
#!/usr/bin/env python
from setuptools import setup, Extension
from setuptools.command.build_ext import build_ext
from os.path import exists, realpath, dirname, join as path_join
from os import environ
from subprocess import run, PIPE
from sys import path as sys_path
from sysconfig import get_platform
sys_path.insert(0, realpath(dirname(__file__)))
from build_tools.CheckVersion import CheckVersion
BUILD_MODE_ENVVAR = "LIBG722_BUILD_MODE"
PACKAGE_VARIANT_ENVVAR = "LIBG722_PACKAGE_VARIANT"
def infer_package_variant_from_metadata(repo_dir):
fname = path_join(repo_dir, "PKG-INFO")
if not exists(fname):
return None
with open(fname, "r", encoding="utf-8", errors="replace") as fh:
for line in fh:
if not line.startswith("Name:"):
continue
name = line.split(":", 1)[1].strip().lower().replace("_", "-")
if name == "g722-numpy":
return "numpy-addon"
if name == "g722":
return "core"
break
return None
def get_package_variant(repo_dir):
inferred = infer_package_variant_from_metadata(repo_dir)
if inferred is not None:
return inferred
value = environ.get(PACKAGE_VARIANT_ENVVAR)
if value is None:
return "core"
value = value.strip().lower()
aliases = {
"main": "core",
"base": "core",
"addon": "numpy-addon",
"numpy": "numpy-addon",
"numpy_addon": "numpy-addon",
}
value = aliases.get(value, value)
if value not in {"core", "numpy-addon"}:
raise RuntimeError(
f"Invalid {PACKAGE_VARIANT_ENVVAR}={value!r}. "
f"Expected one of: core, numpy-addon."
)
return value
def get_build_mode_setting():
value = environ.get(BUILD_MODE_ENVVAR, "auto").strip().lower()
aliases = {
"prod": "production",
"release": "production",
"dev": "debug",
}
value = aliases.get(value, value)
if value not in {"auto", "debug", "production"}:
raise RuntimeError(
f"Invalid {BUILD_MODE_ENVVAR}={value!r}. "
f"Expected one of: auto, debug, production."
)
return value
def run_git_command(args, repo_dir):
try:
return run(
["git", "-C", repo_dir] + args,
stdout=PIPE,
stderr=PIPE,
text=True,
check=False,
)
except FileNotFoundError:
return None
def git_has_diff_against_tag(repo_dir, tag):
is_repo = run_git_command(["rev-parse", "--is-inside-work-tree"], repo_dir)
if is_repo is None:
return None
if is_repo.returncode != 0 or is_repo.stdout.strip() != "true":
return None
has_tag = run_git_command(
["rev-parse", "-q", "--verify", f"refs/tags/{tag}"], repo_dir
)
if has_tag is None:
return None
if has_tag.returncode != 0:
return True
diff = run_git_command(["diff", "--quiet", tag, "--", "."], repo_dir)
if diff is None:
return None
return diff.returncode != 0
def resolve_build_mode(repo_dir, version):
setting = get_build_mode_setting()
if setting != "auto":
return setting
has_diff = git_has_diff_against_tag(repo_dir, f"v{version}")
if has_diff is None:
return "production"
return "debug" if has_diff else "production"
def get_extension_flags(platform_name, build_mode):
is_win = platform_name.startswith('win')
if is_win:
if build_mode == "debug":
return {
"compile_args": ["/Zi", "/Od"],
"link_args": ["/DEBUG"],
"debug_cflags": ["/DDEBUG_MOD"],
"debug_link_args": [],
}
return {
"compile_args": ["/O2"],
"link_args": [],
"debug_cflags": ["/DDEBUG_MOD"],
"debug_link_args": [],
}
compile_args = ['-flto']
link_args = ['-flto']
if build_mode == "debug":
compile_args.extend(['-g3', '-O0'])
link_args.extend(['-g3', '-O0'])
else:
compile_args.append('-O2')
link_args.append('-O2')
return {
"compile_args": compile_args,
"link_args": link_args,
"debug_cflags": ['-DDEBUG_MOD'],
"debug_link_args": [],
}
def main():
mod_name = "G722"
mod_name_dbg = mod_name + "_debug"
version = '1.2.8'
repo_dir = realpath(dirname(__file__))
package_variant = get_package_variant(repo_dir)
src_dir = "."
py_src_dir = "python"
mod_fname = mod_name + "_mod.c"
readme_path = path_join(repo_dir, "README.md")
if exists(readme_path):
with open(readme_path, "r", encoding="utf-8", errors="replace") as fh:
long_description = fh.read()
else:
long_description = "This is a package for G.722 module"
if package_variant == "numpy-addon":
class BuildExtWithNumpy(build_ext):
def finalize_options(self):
super().finalize_options()
try:
import numpy as np
except ImportError:
# During build requirement discovery NumPy may not be installed yet.
# setup_requires requests it; finalize_options runs again for build.
return
if self.include_dirs is None:
self.include_dirs = []
self.include_dirs.append(np.get_include())
addon_module = Extension(
"G722_numpy",
sources=[path_join(py_src_dir, "G722_numpy_mod.c")],
include_dirs=[py_src_dir],
)
kwargs = {
"name": "G722-numpy",
"version": version,
"description": "Optional NumPy backend for G.722 module",
"long_description": long_description,
"long_description_content_type": "text/markdown",
"author": "Maksym Sobolyev",
"author_email": "sobomax@sippysoft.com",
"url": "https://github.com/sippy/libg722",
"ext_modules": [addon_module],
"setup_requires": ["numpy"],
"install_requires": [f"G722=={version}", "numpy"],
"cmdclass": {"checkversion": CheckVersion, "build_ext": BuildExtWithNumpy},
"license": "Public-Domain",
"classifiers": [
"Operating System :: OS Independent",
"Programming Language :: C",
"Programming Language :: Python",
],
}
setup(**kwargs)
return
build_mode = resolve_build_mode(repo_dir, version)
is_win = get_platform().startswith('win')
is_mac = get_platform().startswith('macosx-')
flags = get_extension_flags(get_platform(), build_mode)
compile_args = flags["compile_args"]
link_args = flags["link_args"]
if not is_mac and not is_win:
smap_fname = path_join(py_src_dir, "symbols.map")
link_args.append(f'-Wl,--version-script={smap_fname}')
debug_cflags = flags["debug_cflags"]
debug_link_args = flags["debug_link_args"]
mod_common_args = {
'sources': [
path_join(py_src_dir, mod_fname),
path_join(src_dir, 'g722_decode.c'),
path_join(src_dir, 'g722_encode.c'),
],
'include_dirs': [src_dir, py_src_dir],
'extra_compile_args': compile_args,
'extra_link_args': link_args
}
mod_debug_args = mod_common_args.copy()
mod_debug_args['extra_compile_args'] = mod_debug_args['extra_compile_args'] + debug_cflags
mod_debug_args['extra_link_args'] = mod_debug_args['extra_link_args'] + debug_link_args
module1 = Extension(mod_name, **mod_common_args)
module2 = Extension(mod_name_dbg, **mod_debug_args)
kwargs = {
'name':mod_name,
'version': version,
'description':'This is a package for G.722 module',
'long_description': long_description,
'long_description_content_type': "text/markdown",
'author':'Maksym Sobolyev',
'author_email':'sobomax@sippysoft.com',
'url':'https://github.com/sippy/libg722',
'ext_modules': [module1, module2],
'install_requires': [],
'extras_require': {'numpy': [f'G722-numpy=={version}']},
'cmdclass': {'checkversion': CheckVersion},
'license': 'Public-Domain',
'classifiers': [
'Operating System :: OS Independent',
'Programming Language :: C',
'Programming Language :: Python'
]
}
setup (**kwargs)
if __name__ == '__main__': main()