-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsetup.py
153 lines (125 loc) · 4.92 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
# -*- coding: utf-8 -*-
import glob
import os
from pathlib import Path
import re
from setuptools import setup, Extension
import sys
from Cython.Build import cythonize
# the following allows us to build a shared library on Windows named libmint.so, see
# https://github.com/himbeles/ctypes-example.git. Works if one uses ctypes to call the
# shared library.
# https://stackoverflow.com/questions/4529555/building-a-ctypes-based-c-library-with-distutils
from distutils.command.build_ext import build_ext as build_ext_orig
class CTypesExtension(Extension): pass
class build_ext(build_ext_orig):
def build_extension(self, ext):
self._ctypes = isinstance(ext, CTypesExtension)
return super().build_extension(ext)
def get_export_symbols(self, ext):
if self._ctypes:
return ext.export_symbols
return super().get_export_symbols(ext)
def get_ext_filename(self, ext_name):
if self._ctypes:
return ext_name + '.so'
return super().get_ext_filename(ext_name)
PACKAGE = "mint"
def getCondaVTK():
"""get the VTK installed by conda"""
vtk_libs = [
"vtkCommonComputationalGeometry",
"vtkIOCore",
"vtkIOLegacy",
"vtkCommonExecutionModel",
"vtkCommonDataModel",
"vtkCommonTransforms",
"vtkCommonMisc",
"vtkCommonMath",
"vtkCommonSystem",
"vtkCommonCore",
"vtksys",
]
include_dir = Path(sys.exec_prefix) / Path("include")
if os.name == 'nt':
# On Windows the include files are under Library
include_dir = Path(sys.exec_prefix) / Path("Library") / Path("include")
try:
version = list(include_dir.glob("vtk-*"))[-1].name
except IndexError:
raise RuntimeError('ERROR: you need to "conda install -c conda-forge vtk"')
version = re.sub(r"vtk-", "", version)
include_dir = str(include_dir / Path(f"vtk-{version}"))
libraries_dir = str(Path(sys.exec_prefix) / Path("lib"))
if os.name == 'nt':
# Windows
libraries_dir = str(Path(sys.exec_prefix) / Path("Library") / Path("lib"))
libraries = [f"{lib}-{version}" for lib in vtk_libs]
result = {
"VTK_VERSION": version,
"VTK_INCLUDE_DIR": include_dir,
"VTK_LIBRARIES_DIR": libraries_dir,
"VTK_LIBRARIES": libraries,
}
return result
def getCondaNetCDF():
"""Get the NetCDF installed by conda."""
include_dir = str(Path(sys.exec_prefix) / Path("include"))
libraries_dir = str(Path(sys.exec_prefix) / Path("lib"))
if os.name == 'nt':
# Windows
include_dir = str(Path(sys.exec_prefix) / Path("Library") / Path("include"))
libraries_dir = str(Path(sys.exec_prefix) / Path("Library") / Path("lib"))
libraries = ["netcdf", "hdf5"]
if not (include_dir / Path("netcdf.h")).exists():
raise RuntimeError('ERROR: you need to "conda install libnetcdf"')
result = {
"NETCDF_INCLUDE_DIR": include_dir,
"NETCDF_LIBRARIES_DIR": libraries_dir,
"NETCDF_LIBRARIES": libraries,
}
return result
# extract the MINT version from file version.txt
with open("version.txt") as f:
VERSION = f.read().strip()
# generate mint/__init__.py from mint/__init__.py.in
init_file = ""
with open(f"{PACKAGE}/__init__.py.in") as fi:
init_file = re.sub(r"@VERSION@", VERSION, fi.read())
with open(f"{PACKAGE}/__init__.py", "w") as fo:
fo.write(init_file)
vtklib = getCondaVTK()
nclib = getCondaNetCDF()
extra_compile_args = []
cpp_flags = os.getenv("CPPFLAGS")
cxx_flags = os.getenv("CXXFLAGS")
if cxx_flags is not None:
extra_compile_args = cxx_flags.split()
elif cpp_flags is not None:
extra_compile_args = cpp_flags.split()
if os.name != 'nt':
extra_compile_args.append("-std=c++11")
print(f'VTK_VERSION = {vtklib["VTK_VERSION"]}')
print(f'VTK_INCLUDE_DIR = {vtklib["VTK_INCLUDE_DIR"]}')
print(f'VTK_LIBRARIES_DIR = {vtklib["VTK_LIBRARIES_DIR"]}')
print(f'VTK_LIBRARIES = {vtklib["VTK_LIBRARIES"]}')
print(f'NETCDF_INCLUDE_DIR = {nclib["NETCDF_INCLUDE_DIR"]}')
print(f'NETCDF_LIBRARIES_DIR = {nclib["NETCDF_LIBRARIES_DIR"]}')
print(f'NETCDF_LIBRARIES = {nclib["NETCDF_LIBRARIES"]}')
print(f"extra_compile_args = {extra_compile_args}")
extensions = [
CTypesExtension(
f"lib{PACKAGE}",
sources=glob.glob("src/*.cpp"),
define_macros=[],
include_dirs=["src/", vtklib["VTK_INCLUDE_DIR"], nclib["NETCDF_INCLUDE_DIR"]],
libraries=vtklib["VTK_LIBRARIES"] + nclib["NETCDF_LIBRARIES"],
library_dirs=[vtklib["VTK_INCLUDE_DIR"], nclib["NETCDF_LIBRARIES_DIR"]],
extra_compile_args=extra_compile_args,
language="c++",
)
]
setup(
ext_modules=cythonize(extensions, compiler_directives=dict(language_level=3)),
cmdclass={'build_ext': build_ext},
)