forked from metawards/MetaWards
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.py
350 lines (282 loc) · 11.6 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
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
# Package setup copied from
# https://github.com/FedericoStra/cython-package-example
# Thanks - this was really helpful :-)
import os
import versioneer
from setuptools import setup, Extension
import distutils.sysconfig
import distutils.ccompiler
import multiprocessing
from glob import glob
import platform
import tempfile
import shutil
import sys
try:
from Cython.Build import cythonize
have_cython = True
except Exception:
have_cython = False
_system_input = input
# has the user asked for a build?
is_build = False
for arg in sys.argv[1:]:
lower = arg.lower()
if arg in ["build", "bdist_wheel", "build_py"]:
is_build = True
break
def setup_package():
# First, set some flags regarding the distribution
IS_MAC = False
IS_LINUX = False
IS_WINDOWS = False
MACHINE = platform.machine()
openmp_flags = ["-fopenmp", "-openmp"]
if platform.system() == "Darwin":
IS_MAC = True
if is_build:
print(f"\nCompiling on a Mac ({MACHINE})")
elif platform.system() == "Windows":
IS_WINDOWS = True
openmp_flags.insert(0, "/openmp") # MSVC flag
if is_build:
print(f"\nCompiling on Windows ({MACHINE})")
elif platform.system() == "Linux":
IS_LINUX = True
if is_build:
print(f"\nCompiling on Linux ({MACHINE})")
else:
if is_build:
print(f"Unrecognised platform {platform.system()}. Assuming Linux")
IS_LINUX = True
# Get the compiler that (I think) distutils will use
# - I will need this to add options etc.
compiler = distutils.ccompiler.new_compiler()
distutils.sysconfig.customize_compiler(compiler)
user_openmp_flag = os.getenv("OPENMP_FLAG", None)
if user_openmp_flag is not None:
openmp_flags.insert(user_openmp_flag, 0)
# override 'input' so that defaults can be used when this is run in batch
# or CI/CD
def input(prompt: str, default="y"):
"""Wrapper for 'input' that returns 'default' if it detected
that this is being run from within a batch job or other
service that doesn't have access to a tty
"""
import sys
try:
if sys.stdin.isatty():
return _system_input(prompt)
else:
print(f"Not connected to a console, so having to use "
f"the default ({default})")
return default
except Exception as e:
print(f"Unable to get the input: {e.__class__} {e}")
print(f"Using the default ({default}) instead")
return default
# Check if compiler support openmp (and find the correct openmp flag)
def get_openmp_flag():
openmp_test = \
r"""
#include <omp.h>
#include <stdio.h>
int main(int argc, char **argv)
{
int nthreads, thread_id;
#pragma omp parallel private(nthreads, thread_id)
{
thread_id = omp_get_thread_num();
nthreads = omp_get_num_threads();
printf("I am thread %d of %d\n", thread_id, nthreads);
}
return 0;
}
"""
tmpdir = tempfile.mkdtemp()
curdir = os.getcwd()
os.chdir(tmpdir)
filename = r'openmp_test.c'
with open(filename, 'w') as file:
file.write(openmp_test)
file.flush()
openmp_flag = None
if user_openmp_flag:
openmp_flags.insert(0, user_openmp_flag)
for flag in openmp_flags:
try:
# Compiler and then link using each openmp flag...
compiler.compile(sources=["openmp_test.c"],
extra_preargs=[flag])
openmp_flag = flag
break
except Exception as e:
print(f"Cannot compile: {e.__class__} {e}")
pass
# clean up
os.chdir(curdir)
shutil.rmtree(tmpdir)
return openmp_flag
if is_build:
openmp_flag = get_openmp_flag()
else:
openmp_flag = None
include_dirs = []
if is_build and (openmp_flag is None):
print(f"\nYour compiler {compiler.compiler_so[0]} does not support "
f"OpenMP with any of the known OpenMP flags {openmp_flags}. "
f"If you know which flag to use can you specify it using "
f"the environent variable OPENMP_FLAG. Otherwise, we will "
f"have to compile the serial version of the code.")
if IS_MAC:
print(f"\nThis is common on Mac, as the default compiler does not "
f"support OpenMP. If you want to compile with OpenMP then "
f"install llvm via homebrew, e.g. 'brew install llvm', see "
f"https://embeddedartistry.com/blog/2017/02/24/installing-llvm-clang-on-osx/")
print(f"\nRemember then to choose that compiler by setting the "
f"CC environment variable, or passing it on the 'make' line, "
f"e.g. 'CC=/usr/local/opt/llvm/bin/clang make'")
result = input("\nDo you want compile without OpenMP? (y/n) ",
default="y")
if result is None or result.strip().lower()[0] != "y":
sys.exit(-1)
include_dirs.append("src/metawards/disable_openmp")
cflags = "-O3"
lflags = []
if openmp_flag:
cflags = f"{cflags} {openmp_flag}"
lflags.append(openmp_flag)
nbuilders = int(os.getenv("CYTHON_NBUILDERS", 2))
if nbuilders < 1:
nbuilders = 1
if is_build:
print(f"Number of builders equals {nbuilders}\n")
compiler_directives = {"language_level": 3, "embedsignature": True,
"boundscheck": False, "cdivision": True,
"initializedcheck": False,
"cdivision_warnings": False,
"wraparound": False, "binding": False,
"nonecheck": False, "overflowcheck": False}
if os.getenv("CYTHON_LINETRACE", 0):
if is_build:
print("Compiling with Cython line-tracing support - will be SLOW")
define_macros = [("CYTHON_TRACE", "1")]
compiler_directives["linetrace"] = True
else:
define_macros = []
# Thank you Priyaj for pointing out this little documented feature - finally
# I can build the random code into a library!
# https://www.edureka.co/community/21524/setuptools-shared-libary-cython-wrapper-linked-shared-libary
ext_lib_path = "src/metawards/ran_binomial"
sources = ["mt19937.c", "distributions.c"]
ext_libraries = [['metawards_random', {
'sources': [os.path.join(ext_lib_path, src)
for src in sources],
'include_dirs': [],
'macros': [],
}]]
def no_cythonize(extensions, **_ignore):
# https://cython.readthedocs.io/en/latest/src/userguide/source_files_and_compilation.html#distributing-cython-modules
for extension in extensions:
sources = []
for sfile in extension.sources:
path, ext = os.path.splitext(sfile)
if ext in (".pyx", ".py"):
if extension.language == "c++":
ext = ".cpp"
else:
ext = ".c"
sfile = path + ext
sources.append(sfile)
extension.sources[:] = sources
return extensions
utils_pyx_files = glob("src/metawards/utils/*.pyx")
iterator_pyx_files = glob("src/metawards/iterators/*.pyx")
extractor_pyx_files = glob("src/metawards/extractors/*.pyx")
mixer_pyx_files = glob("src/metawards/mixers/*.pyx")
mover_pyx_files = glob("src/metawards/movers/*.pyx")
libraries = ["metawards_random"]
extensions = []
for pyx in utils_pyx_files:
_, name = os.path.split(pyx)
name = name[0:-4]
module = f"metawards.utils.{name}"
extensions.append(Extension(module, [pyx], define_macros=define_macros,
libraries=libraries,
extra_compile_args=lflags,
include_dirs=include_dirs))
for pyx in iterator_pyx_files:
_, name = os.path.split(pyx)
name = name[0:-4]
module = f"metawards.iterators.{name}"
extensions.append(Extension(module, [pyx], define_macros=define_macros,
libraries=libraries,
extra_compile_args=lflags,
include_dirs=include_dirs))
for pyx in extractor_pyx_files:
_, name = os.path.split(pyx)
name = name[0:-4]
module = f"metawards.extractors.{name}"
extensions.append(Extension(module, [pyx], define_macros=define_macros,
libraries=libraries,
extra_compile_args=lflags,
include_dirs=include_dirs))
for pyx in mixer_pyx_files:
_, name = os.path.split(pyx)
name = name[0:-4]
module = f"metawards.mixers.{name}"
extensions.append(Extension(module, [pyx], define_macros=define_macros,
libraries=libraries,
extra_compile_args=lflags,
include_dirs=include_dirs))
for pyx in mover_pyx_files:
_, name = os.path.split(pyx)
name = name[0:-4]
module = f"metawards.movers.{name}"
extensions.append(Extension(module, [pyx], define_macros=define_macros,
libraries=libraries,
extra_compile_args=lflags,
include_dirs=include_dirs))
CYTHONIZE = bool(int(os.getenv("CYTHONIZE", 0)))
if not have_cython:
CYTHONIZE = False
os.environ['CFLAGS'] = cflags
if CYTHONIZE:
extensions = cythonize(extensions,
compiler_directives=compiler_directives,
nthreads=nbuilders)
else:
extensions = no_cythonize(extensions)
with open("requirements.txt") as fp:
install_requires = fp.read().strip().split("\n")
with open("requirements-dev.txt") as fp:
dev_requires = fp.read().strip().split("\n")
setup(
version=versioneer.get_version(),
cmdclass=versioneer.get_cmdclass(),
ext_modules=extensions,
install_requires=install_requires,
libraries=ext_libraries,
extras_require={
"dev": dev_requires,
"docs": ["sphinx", "sphinx-rtd-theme"]
},
entry_points={
"console_scripts": [
"metawards = metawards.app.run:cli",
"metawards-plot = metawards.scripts.plot:cli",
"metawards-install = metawards.scripts.install:cli",
"metawards-python = metawards.scripts.pyexe:cli",
"metawards-jupyter = metawards.scripts.jupexe:cli",
"metawards-reticulate = metawards.scripts.retexe:cli",
"metawards-update = metawards.scripts.update:cli"
]
},
data_files=[("share/metawards/requirements",
["requirements.txt", "requirements-optional.txt"])]
)
if __name__ == "__main__":
# Freeze to support parallel compilation when using spawn instead of fork
# (thanks to pandas for showing how to do this in their setup.py)
multiprocessing.freeze_support()
setup_package()