forked from simnibs/simnibs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.py
429 lines (389 loc) · 13.8 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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
from setuptools import setup, Extension
import os
import sys
import glob
import shutil
import urllib
import tempfile
import zipfile
import tarfile
from setuptools.command.build_ext import build_ext
from distutils.dep_util import newer_group
import numpy as np
''' C extensions
CGAL Compilation
-----------------
CGAL >= 5 is a header-only library, so we download it right before compiling.
Compilation requires:
GCC >= 6.3 or Apple Clang == 10.0.1 or MSVC >= 14.0
conda install gcc_linux-64 gxx_linux-64 gfortran_linux-64
Boost >= 1.57
Boost can be instaled with
Ubuntu: sudo apt install libboost-all-dev
MacOS: brew install boost
Windows: conda install boost
Boost is also header-only, so we only need it during compile time
For more info, refer to https://doc.cgal.org/latest/Manual/thirdparty.html
'''
# Information for CGAL download
CGAL_version = '5.4'
CGAL_headers = os.path.abspath(f'CGAL-{CGAL_version}/include')
CGAL_url = (
f'https://github.com/CGAL/cgal/releases/download/'
#f'releases/CGAL-{CGAL_version}/'
f'v{CGAL_version}/'
f'CGAL-{CGAL_version}-library.zip'
)
cgal_mesh_macros = [
('CGAL_MESH_3_NO_DEPRECATED_SURFACE_INDEX', None),
('CGAL_MESH_3_NO_DEPRECATED_C3T3_ITERATORS', None),
('CGAL_CONCURRENT_MESH_3', None),
('CGAL_EIGEN3_ENABLED', None),
('CGAL_USE_ZLIB', 1),
('CGAL_LINKED_WITH_TBB', None)
]
# Information for eigen library
# I don't download it because gitlab does not allow it
eigen_version = '3.3.7'
eigen_headers = os.path.abspath(f'simnibs/external/include/eigen-{eigen_version}')
# Information for Intel TBB download
tbb_version = '2020.1'
tbb_path = os.path.abspath('tbb')
tbb_headers = os.path.join(tbb_path, 'tbb', 'include')
if sys.platform == 'win32':
tbb_url = (
f'https://github.com/intel/tbb/releases/download/'
f'v{tbb_version}/tbb-{tbb_version}-win.zip'
)
tbb_libs = [
os.path.join(tbb_path, 'tbb', 'bin', 'intel64', 'vc14', 'tbb.dll'),
os.path.join(tbb_path, 'tbb', 'lib', 'intel64', 'vc14', 'tbb.lib'),
os.path.join(tbb_path, 'tbb', 'bin', 'intel64', 'vc14', 'tbbmalloc.dll'),
os.path.join(tbb_path, 'tbb', 'lib', 'intel64', 'vc14', 'tbbmalloc.lib'),
]
elif sys.platform == 'linux':
tbb_url = (
f'https://github.com/intel/tbb/releases/download/'
f'v{tbb_version}/tbb-{tbb_version}-lin.tgz'
)
tbb_libs = [
os.path.join(tbb_path, 'tbb', 'lib', 'intel64', 'gcc4.8', 'libtbb.so'),
os.path.join(tbb_path, 'tbb', 'lib', 'intel64', 'gcc4.8', 'libtbb.so.2'),
os.path.join(tbb_path, 'tbb', 'lib', 'intel64', 'gcc4.8', 'libtbbmalloc.so'),
os.path.join(tbb_path, 'tbb', 'lib', 'intel64', 'gcc4.8', 'libtbbmalloc.so.2'),
]
elif sys.platform == 'darwin':
tbb_url = (
f'https://github.com/intel/tbb/releases/download/'
f'v{tbb_version}/tbb-{tbb_version}-mac.tgz'
)
tbb_libs = [
os.path.join(tbb_path, 'tbb', 'lib', 'libtbb.dylib'),
os.path.join(tbb_path, 'tbb', 'lib', 'libtbbmalloc.dylib'),
]
else:
raise OSError('OS not supported!')
#### Setup compilation arguments
is_conda = 'CONDA_PREFIX' in os.environ
if sys.platform == 'win32':
petsc_libs = ['libpetsc', 'msmpi']
petsc_include = [
np.get_include(),
'simnibs/external/include/win/petsc',
'simnibs/external/include/win/hypre',
'simnibs/external/include/win/mpi'
]
petsc_dirs = ['simnibs/external/lib/win']
petsc_runtime = None
petsc_extra_link_args = None
cgal_libs = ['libmpfr-4', 'libgmp-10', 'zlib', 'tbb', 'tbbmalloc']
cgal_include = [
np.get_include(),
CGAL_headers,
eigen_headers,
tbb_headers,
'simnibs/external/include/win/mpfr',
'simnibs/external/include/win/gmp'
]
# Find boost headers if installed with conda
if is_conda:
cgal_include += [os.path.join(os.environ['CONDA_PREFIX'], 'Library', 'include')]
cgal_dirs = ['simnibs/external/lib/win']
cgal_runtime = None
# Got those arguments from compiling a CGAL program following the instructions in the website
cgal_compile_args = [
'/Zi', '/WX-', '/diagnostics:classic', '/Ob0', '/Oy',
'/D WIN32', '/D _WINDOWS', '/D _SCL_SECURE_NO_DEPRECATE',
'/D _SCL_SECURE_NO_WARNINGS', '/D BOOST_ALL_DYN_LINK=1',
'/D _MBCS'
]
cgal_link_args = None
cat_compile_args = None
elif sys.platform == 'linux':
petsc_libs = ['petsc']
petsc_include = [
np.get_include(),
'simnibs/external/include/linux/petsc'
]
petsc_dirs = ['simnibs/external/lib/linux']
petsc_runtime = ['$ORIGIN/../external/lib/linux']
petsc_extra_link_args = None
cgal_libs = ['mpfr', 'gmp', 'z', 'tbb', 'tbbmalloc', 'pthread']
cgal_include = [
np.get_include(),
CGAL_headers,
eigen_headers,
tbb_headers,
'simnibs/external/include/linux/mpfr',
'simnibs/external/include/linux/gmp'
]
# To find the boost headers if installed with conda
if is_conda:
cgal_include += [os.path.join(os.environ['CONDA_PREFIX'], 'include')]
cgal_dirs = ['simnibs/external/lib/linux']
cgal_runtime = ['$ORIGIN/../../external/lib/linux']
# Add -Os -flto for much smaller binaries
cgal_compile_args = [
'-Os', '-flto',
'-frounding-math',
'-std=gnu++14',
]
cgal_mesh_macros += [('NOMINMAX', None)]
cgal_link_args = None
cat_compile_args = [
'-std=gnu99',
]
elif sys.platform == 'darwin':
petsc_libs = ['petsc']
petsc_include = [
np.get_include(),
'simnibs/external/include/osx/petsc'
]
petsc_dirs = ['simnibs/external/lib/osx']
petsc_runtime = None
# add RPATH as the _runtime argument does not work in MacOS, likely bug in setuptools
petsc_extra_link_args = ['-Wl,-rpath,@loader_path/../external/lib/osx']
cgal_libs = ['mpfr', 'gmp', 'z', 'tbb', 'tbbmalloc']
cgal_include = [
np.get_include(),
CGAL_headers,
eigen_headers,
tbb_headers,
'simnibs/external/include/osx/mpfr',
'simnibs/external/include/osx/gmp'
]
if is_conda:
cgal_include += [os.path.join(os.environ['CONDA_PREFIX'], 'include')]
cgal_dirs = ['simnibs/external/lib/osx']
cgal_runtime = None
cgal_compile_args = [
'-std=gnu++14',
'-stdlib=libc++',
]
cgal_mesh_macros += [('NOMINMAX', None)]
cgal_link_args = [
'-stdlib=libc++',
'-Wl,-rpath,@loader_path/../../external/lib/osx'
]
cat_compile_args = None
else:
raise OSError('OS not supported!')
cython_msh = Extension(
'simnibs.mesh_tools.cython_msh',
["simnibs/mesh_tools/cython_msh.pyx"],
include_dirs=[np.get_include()]
)
marching_cubes_lewiner_cy = Extension(
'simnibs.segmentation._marching_cubes_lewiner_cy',
["simnibs/segmentation/_marching_cubes_lewiner_cy.pyx"],
include_dirs=[np.get_include()]
)
cat_c_utils = Extension(
'simnibs.segmentation._cat_c_utils',
["simnibs/segmentation/_cat_c_utils.pyx", "simnibs/segmentation/cat_c_utils/genus0.c"],
include_dirs=[np.get_include(), 'simnibs/segmentation/cat_c_utils'],
extra_compile_args=cat_compile_args
)
thickness = Extension(
'simnibs.segmentation._thickness',
["simnibs/segmentation/_thickness.pyx"],
include_dirs=[np.get_include()]
)
petsc_solver = Extension(
'simnibs.simulation.petsc_solver',
sources=["simnibs/simulation/petsc_solver.pyx"],
depends=["simnibs/simulation/_solver.c"],
include_dirs=petsc_include,
library_dirs=petsc_dirs,
libraries=petsc_libs,
runtime_library_dirs=petsc_runtime,
extra_link_args=petsc_extra_link_args
)
# I separated the CGAL functions into several files for two reasons
# 1. Reduce memory consumption during compilation in Linux
# 2. Fix some compilation problems in Windows
create_mesh_surf = Extension(
'simnibs.mesh_tools.cgal.create_mesh_surf',
sources=["simnibs/mesh_tools/cgal/create_mesh_surf.pyx"],
depends=["simnibs/mesh_tools/cgal/_mesh_surfaces.cpp"],
language='c++',
include_dirs=cgal_include,
libraries=cgal_libs,
library_dirs=cgal_dirs,
runtime_library_dirs=cgal_runtime,
extra_compile_args=cgal_compile_args,
extra_link_args=cgal_link_args,
define_macros=cgal_mesh_macros
)
create_mesh_vol = Extension(
'simnibs.mesh_tools.cgal.create_mesh_vol',
sources=["simnibs/mesh_tools/cgal/create_mesh_vol.pyx"],
depends=["simnibs/mesh_tools/cgal/_mesh_volumes.cpp"],
language='c++',
include_dirs=cgal_include,
libraries=cgal_libs,
library_dirs=cgal_dirs,
runtime_library_dirs=cgal_runtime,
extra_compile_args=cgal_compile_args,
extra_link_args=cgal_link_args,
define_macros=cgal_mesh_macros
)
cgal_misc = Extension(
'simnibs.mesh_tools.cgal.cgal_misc',
sources=["simnibs/mesh_tools/cgal/cgal_misc.pyx"],
depends=["simnibs/mesh_tools/cgal/_cgal_intersect.cpp"],
language='c++',
include_dirs=cgal_include,
libraries=cgal_libs,
library_dirs=cgal_dirs,
runtime_library_dirs=cgal_runtime,
extra_compile_args=cgal_compile_args,
extra_link_args=cgal_link_args,
)
cgal_pmp = Extension(
"simnibs.mesh_tools.cgal.polygon_mesh_processing",
sources = ["simnibs/mesh_tools/cgal/polygon_mesh_processing.pyx"],
depends = ["simnibs/mesh_tools/cgal/polygon_mesh_processing_src.cpp"],
language='c++',
include_dirs=cgal_include,
libraries=cgal_libs,
library_dirs=cgal_dirs,
runtime_library_dirs=cgal_runtime,
extra_compile_args=cgal_compile_args,
extra_link_args=cgal_link_args,
)
extensions = [
cython_msh,
marching_cubes_lewiner_cy,
cat_c_utils,
thickness,
petsc_solver,
create_mesh_surf,
create_mesh_vol,
cgal_misc,
cgal_pmp,
]
def download_and_extract(url, path='.'):
''' Downloads and extracts a zip or tar-gz folder '''
print('Downloading:', url)
with urllib.request.urlopen(url) as response:
with tempfile.NamedTemporaryFile('wb', delete=False) as tmpf:
shutil.copyfileobj(response, tmpf)
tmpname = tmpf.name
if url.endswith('.zip'):
with zipfile.ZipFile(tmpname) as z:
z.extractall(path)
elif url.endswith('.tgz') or url.endswith('.tar.gz'):
with tarfile.open(tmpname, 'r:gz') as z:
z.extractall(path)
else:
raise IOError('Could not extract file, unrecognized extension')
os.remove(tmpname)
def install_lib(url, path, libs, build_path):
''' Downloads a compiled library from the internet and move to "lib" folder '''
download_and_extract(url, path)
if sys.platform == 'darwin':
folder_name = 'osx'
elif sys.platform == 'linux':
folder_name = 'linux'
elif sys.platform == 'win32':
folder_name = 'win'
for l in libs:
shutil.copy(
l, f'simnibs/external/lib/{folder_name}',
follow_symlinks=False
)
if build_path:
shutil.copy(
l, f'{build_path}simnibs/external/lib/{folder_name}',
follow_symlinks=False
)
class build_ext_(build_ext):
'''
Build the extension, download some dependencies and remove stuff from other OS
'''
def run(self):
from Cython.Build import cythonize
## Cythonize
self.extension = cythonize(self.extensions)
## Download requirements
changed_meshing = (
# newer_group(
# create_mesh_surf.sources + create_mesh_surf.depends,
# self.get_ext_fullpath(create_mesh_surf.name),
# 'newer'
# ) or
newer_group(
create_mesh_vol.sources + create_mesh_vol.depends,
self.get_ext_fullpath(create_mesh_vol.name),
'newer'
) or
newer_group(
cgal_misc.sources + cgal_misc.depends,
self.get_ext_fullpath(cgal_misc.name),
'newer'
)
)
if self.force or changed_meshing:
download_and_extract(CGAL_url)
if self.inplace:
build_lib = ""
else:
build_lib = self.build_lib + "/"
install_lib(tbb_url, tbb_path, tbb_libs, build_lib)
# Compile
build_ext.run(self)
# cleanup downloads
if self.force or changed_meshing:
shutil.rmtree(f'CGAL-{CGAL_version}', ignore_errors=True)
shutil.rmtree(tbb_path, ignore_errors=True)
# Remove unescessary binary files
linux_folders = [
os.path.join(self.build_lib, 'simnibs', 'external', 'bin', 'linux'),
os.path.join(self.build_lib, 'simnibs', 'external', 'include', 'linux'),
os.path.join(self.build_lib, 'simnibs', 'external', 'lib', 'linux'),
]
osx_folders = [
os.path.join(self.build_lib, 'simnibs', 'external', 'bin', 'osx'),
os.path.join(self.build_lib, 'simnibs', 'external', 'include', 'osx'),
os.path.join(self.build_lib, 'simnibs', 'external', 'lib', 'osx'),
]
win_folders = [
os.path.join(self.build_lib, 'simnibs', 'external', 'bin', 'win'),
os.path.join(self.build_lib, 'simnibs', 'external', 'include', 'win'),
os.path.join(self.build_lib, 'simnibs', 'external', 'lib', 'win'),
]
if sys.platform == 'linux':
[shutil.rmtree(f, True) for f in osx_folders]
[shutil.rmtree(f, True) for f in win_folders]
if sys.platform == 'darwin':
[shutil.rmtree(f, True) for f in linux_folders]
[shutil.rmtree(f, True) for f in win_folders]
if sys.platform == 'win32':
[shutil.rmtree(f, True) for f in linux_folders]
[shutil.rmtree(f, True) for f in osx_folders]
setup(ext_modules=extensions,
cmdclass={
'build_ext': build_ext_
},
)