generated from pybind/python_example
-
Notifications
You must be signed in to change notification settings - Fork 4
/
setup.py
162 lines (132 loc) · 5.67 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
156
157
158
159
160
161
162
import glob
from sys import version_info
from setuptools import setup
from setuptools._distutils._modified import newer_group
from setuptools._distutils.errors import DistutilsSetupError
from setuptools._distutils import log
from setuptools.command.test import test as tester
# Available at setup time due to pyproject.toml
try:
from pybind11.setup_helpers import Pybind11Extension as Extension
from pybind11.setup_helpers import build_ext
except ImportError:
from setuptools import Extension
from setuptools.command.build_ext import build_ext
__version__ = "1.6.4"
basic_dependency = ["pybind11", "setuptools"]
if version_info.major != 3 or version_info.minor < 8:
raise RuntimeError("pysilk only support python 3.8 or newer")
class CustomBuilder(build_ext):
def build_extension(self, ext):
sources = ext.sources
if sources is None or not isinstance(sources, (list, tuple)):
raise DistutilsSetupError(
"in 'ext_modules' option (extension '%s'), "
"'sources' must be present and must be "
"a list of source filenames" % ext.name)
sources = list(sources)
ext_path = self.get_ext_fullpath(ext.name)
depends = sources + ext.depends
if not (self.force or newer_group(depends, ext_path, 'newer')):
log.debug("skipping '%s' extension (up-to-date)", ext.name)
return
else:
log.info("building '%s' extension", ext.name)
# First, scan the sources for SWIG definition files (.i), run
# SWIG on 'em to create .c files, and modify the sources list
# accordingly.
sources = self.swig_sources(sources, ext)
# Next, compile the source code to object files.
# XXX not honouring 'define_macros' or 'undef_macros' -- the
# CCompiler API needs to change to accommodate this, and I
# want to do one thing at a time!
# Two possible sources for extra compiler arguments:
# - 'extra_compile_args' in Extension object
# - CFLAGS environment variable (not particularly
# elegant, but people seem to expect it and I
# guess it's useful)
# The environment variable should take precedence, and
# any sensible compiler will give precedence to later
# command line args. Hence we combine them in order:
extra_args = ext.extra_compile_args or []
macros = ext.define_macros[:]
for undef in ext.undef_macros:
macros.append((undef,))
# split sources
cxx_files = []
for s in sources: # type: str
if s.endswith(".cpp"):
sources.remove(s)
cxx_files.append(s)
c_build_args = []
for e in extra_args:
if not e.startswith("-std="):
c_build_args.append(e)
objects = self.compiler.compile(sources,
output_dir=self.build_temp,
macros=macros,
include_dirs=ext.include_dirs,
debug=self.debug,
extra_postargs=c_build_args,
depends=ext.depends)
objects += self.compiler.compile(cxx_files,
output_dir=self.build_temp,
macros=macros,
include_dirs=ext.include_dirs,
debug=self.debug,
extra_postargs=extra_args,
depends=ext.depends)
# XXX outdated variable, kept here in case third-part code
# needs it.
self._built_objects = objects[:]
# Now link the object files together into a "shared object" --
# of course, first we have to figure out all the other things
# that go into the mix.
if ext.extra_objects:
objects.extend(ext.extra_objects)
extra_args = ext.extra_link_args or []
# Detect target language, if not provided
language = ext.language or self.compiler.detect_language(sources)
self.compiler.link_shared_object(
objects, ext_path,
libraries=self.get_libraries(ext),
library_dirs=ext.library_dirs,
runtime_library_dirs=ext.runtime_library_dirs,
extra_postargs=extra_args,
export_symbols=self.get_export_symbols(ext),
debug=self.debug,
build_temp=self.build_temp,
target_lang=language)
class PyTest(tester):
user_options = [('pytest-args=', 'a', "Arguments to pass into py.test")]
def initialize_options(self):
tester.initialize_options(self)
self.pytest_args = []
def finalize_options(self):
tester.finalize_options(self)
self.test_args = []
self.test_suite = True
def run_tests(self):
import pytest
exit(
pytest.main(self.pytest_args)
)
def get_compile_file_list():
files = glob.glob("src/silk/src/*.c")
files.extend(["src/silk/_pysilk.cpp", "src/silk/codec.cpp"])
return files
setup(
version=__version__,
requires=basic_dependency,
tests_require=basic_dependency + ["pytest"],
cmdclass={"test": PyTest, "build_ext": CustomBuilder},
zip_safe=True,
ext_modules=[
Extension(
"pysilk.coder", get_compile_file_list(),
include_dirs=["src/silk/interface"],
define_macros=[('VERSION_INFO', __version__)],
extra_compile_args=["-std=c++11"]
)
]
)