forked from pyoomph/pyoomph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.py
284 lines (222 loc) · 8.85 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
from setuptools import setup, Extension, find_packages
from setuptools.command.build_ext import build_ext
from pybind11.setup_helpers import ParallelCompile
import sys
import setuptools
import glob
import os.path
import pathlib
config_file=os.environ.get("PYOOMPH_CONFIG_FILE",'pyoomph_config.env')
if os.path.isfile(config_file):
envfile=open(config_file)
for line in envfile:
line=line.strip()
if line.startswith('#') or line=="":
continue
key, value = line.split('=', 1)
key=key.strip()
value=value.strip()
if key.startswith("export "):
key=key[len("export "):]
key=key.strip()
os.environ[key] = value
envfile.close()
no_mpi=os.environ.get("PYOOMPH_USE_MPI")=="false"
march_native=os.environ.get("PYOOMPH_MARCH_NATIVE")!="false"
paranoid=os.environ.get("PYOOMPH_PARANOID")=="true"
debug_symbols=os.environ.get("PYOOMPH_DEBUG_INFOS")!="false"
with_tcc=os.environ.get("PYOOMPH_NO_TCC")=="false"
cross_compile_for_win=os.environ.get("PYOOMPH_CROSS_COMPILE_MINGW")=="true"
fast_multi_version_build=os.environ.get("PYOOMPH_FAST_MULTI_VERSION_BUILD")=="true"
print("CONFIGURATION READ FROM "+config_file)
print("==========================================")
for k,v in os.environ.items():
if k.startswith("PYOOMPH"):
print(k,v)
print("==========================================")
no_mpi_indicator_file = pathlib.Path("pyoomph/NO_MPI")
if no_mpi:
if not no_mpi_indicator_file.exists():
f=open("pyoomph/NO_MPI","w")
f.close()
else:
if no_mpi_indicator_file.exists():
no_mpi_indicator_file.unlink()
import re
try:
__version__=open("pyoomph/_version.py", "rt").read().split("=")[1].strip().strip("\"")
except:
raise
class get_pybind_include(object):
"""Helper class to determine the pybind11 include path
The purpose of this class is to postpone importing pybind11
until it is actually installed, so that the ``get_include()``
method can be invoked. """
def __str__(self):
import pybind11
return os.environ.get("PYOOMPH_PYBIND11_INCLUDE",pybind11.get_include())
odir="./src/thirdparty/oomph-lib"
oidir=odir+"/include/"
oldir=odir+"/lib/"
tccdir="./src/thirdparty/tinycc"
mpideps=["mpi_usempif08", "mpi_usempi_ignore_tkr", "mpi_mpifh","mpi"]
mpideps=["mpi"]
if no_mpi:
mpideps=[]
c_source_files=([] if fast_multi_version_build else glob.glob("src/*.cpp"))+glob.glob("src/pybind/*.cpp")#+glob.glob("src/thirdparty/oomph-lib/include/*.cc")
#c_source_files=list(set(c_source_files)-set(glob.glob("src/thirdparty/oomph-lib/include/*.template.cc")))
def get_all_include_dirs():
res=[get_pybind_include(),oidir,"./src/","./src/thirdparty/"]
if "PYOOMPH_GINAC_INCLUDE_DIR" in os.environ:
res.append(os.environ.get("PYOOMPH_GINAC_INCLUDE_DIR"))
if "PYOOMPH_CLN_INCLUDE_DIR" in os.environ:
res.append(os.environ.get("PYOOMPH_CLN_INCLUDE_DIR"))
return res
def get_all_lib_dirs():
res=[oldir]
if with_tcc:
res.append(tccdir)
if "PYOOMPH_GINAC_LIB_DIR" in os.environ:
res.append(os.environ.get("PYOOMPH_GINAC_LIB_DIR"))
if "PYOOMPH_GINAC_LIB_DIR" in os.environ:
res.append(os.environ.get("PYOOMPH_GINAC_LIB_DIR"))
if "PYOOMPH_CROSS_COMPILE_PYTHON_LIB_DIR" in os.environ:
res.append(os.environ.get("PYOOMPH_CROSS_COMPILE_PYTHON_LIB_DIR"))
if fast_multi_version_build:
res.append("./src/lib")
return res
win_cross_python_lib=[os.environ.get("PYOOMPH_CROSS_COMPILE_PYTHON_LIB_FILE",None)]
if win_cross_python_lib[0] is None:
win_cross_python_lib=[]
ext_modules = [
Extension(
'_pyoomph',
sorted(c_source_files),
include_dirs=get_all_include_dirs(),
library_dirs=get_all_lib_dirs(),
libraries=(["pyoomph_main"] if fast_multi_version_build else [])+['generic',"ginac","cln"]+([] if cross_compile_for_win else ["dl"] )+(["tcc"] if with_tcc else [])+mpideps+win_cross_python_lib, #,
language='c++'
),
]
if os.environ.get("PYOOMPH_SKIP_EXTENSION")=="true":
ext_modules=[]
#ext_modules[0].linker[0] = ext_modules[0].compiler_cxx[0]
# cf http://bugs.python.org/issue26689
def has_flag(compiler, flagname):
"""Return a boolean indicating whether a flag name is supported on
the specified compiler.
"""
import tempfile
import os
with tempfile.NamedTemporaryFile('w', suffix='.cpp', delete=False) as f:
f.write('int main (int argc, char **argv) { return 0; }')
fname = f.name
try:
compiler.compile([fname], extra_postargs=[flagname])
except setuptools.distutils.errors.CompileError:
return False
finally:
try:
os.remove(fname)
except OSError:
pass
return True
def cpp_flag(compiler):
"""Return the -std=c++[11/14/17] compiler flag.
The newer version is prefered over c++11 (when it is available).
"""
# flags = ['-std=c++17', '-std=c++14', '-std=c++11']
flags = ['-std=c++11']
for flag in flags:
if has_flag(compiler, flag):
return flag
raise RuntimeError('Unsupported compiler -- at least C++11 support '
'is needed!')
class BuildExt(build_ext):
"""A custom build extension for adding compiler-specific options."""
c_opts = {
'msvc': ['/EHsc'],
'unix': [],
}
l_opts = {
'msvc': [],
'unix': [],
}
if sys.platform == 'darwin':
darwin_opts = ['-stdlib=libc++'] #, '-mmacosx-version-min=10.7']
c_opts['unix'] += darwin_opts
l_opts['unix'] += darwin_opts
elif sys.platform =="linux":
if not debug_symbols:
l_opts['unix'] += ["-s"]
if debug_symbols:
c_opts['unix'] += ["-g3"]
l_opts['unix'] += ["-g3"]
else:
c_opts['unix'] += ["-g0"]
l_opts['unix'] += ["-g0"]
def build_extensions(self):
# Remove the -Wstrict-prototypes option, it's not valid for C++. Fixed
# in Py3.7 as bpo-5755.
try:
self.compiler.compiler_so.remove('-Wstrict-prototypes')
except (ValueError, AttributeError):
pass
if "PYOOMPH_PYTHON_HEADER_PATH" in os.environ.keys():
self.compiler.include_dirs=[os.environ["PYOOMPH_PYTHON_HEADER_PATH"]]
ct = self.compiler.compiler_type
opts = self.c_opts.get(ct, [])
link_opts = self.l_opts.get(ct, [])
# print("COPTS",opts)
# print("LOPTS",link_opts)
# print("EXT",self.extensions)
if ct == 'unix':
opts.append(cpp_flag(self.compiler))
if has_flag(self.compiler, '-fvisibility=hidden'):
if sys.platform!="darwin": # Clashes with GiNaC on Mac
opts.append('-fvisibility=hidden')
if march_native:
if sys.platform=="linux":
opts.append("-march=native")
opts.append("-O3")
else:
opts.append("-O2")
# link_opts+=["-Wl,--dynamic-list=src/exported.txt"]
# print("COOOOOMPH CXX",self.compiler.compiler_cxx)
# print("COOOOOMPH CC",self.compiler.compiler)
# print("COOOOOMPH SO",self.compiler.compiler_so)
# print("COOOOOMPH",dir(self.compiler))
#self.compiler.linker[0] = self.compiler.compiler_cxx[0]
for ext in self.extensions:
ext.define_macros = [('VERSION_INFO', '"{}"'.format(self.distribution.get_version()))] + ([] if no_mpi else [('OOMPH_HAS_MPI',None)])
ext.define_macros = ext.define_macros + ([('PARANOID', None)] if paranoid else []) + ([] if with_tcc else [('PYOOMPH_NO_TCC',None)])
ext.extra_compile_args = opts
ext.extra_link_args = link_opts
build_ext.build_extensions(self)
excluded = []
#if is_wheel:
# excluded.append('extlibs.future')
def exclude_package(pkg):
for exclude in excluded:
if pkg.startswith(exclude):
return True
return False
def create_package_list(base_package):
return ([base_package] +
[base_package + '.' + pkg
for pkg
in find_packages(base_package)
if not exclude_package(pkg)])
ParallelCompile("NPY_NUM_BUILD_JOBS",default=4).install()
setup(
name='pyoomph',
version=__version__,
packages=create_package_list('pyoomph')+["_pyoomph-stubs"],
ext_modules=ext_modules,
setup_requires=['pybind11>=2.5.0'],
cmdclass={'build_ext': BuildExt},
zip_safe=False,
install_requires=["meshio", "pygmsh","numpy","scipy","matplotlib","mkl","more_itertools"]+([] if no_mpi else ["mpi4py"])+([] if with_tcc else ["tccbox"]),
package_data={'pyoomph.jitbridge': ['*.h']+(["libtcc1.a"] if with_tcc else []), 'pyoomph' : ["py.typed"]+(["NO_MPI"] if no_mpi else []), "_pyoomph-stubs" : ["py.typed","__init__.pyi"],"pyoomph.materials.csv_data":["*.csv"]},
include_package_data=True,
)