-
-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathsetup.py
More file actions
233 lines (185 loc) · 6.78 KB
/
setup.py
File metadata and controls
233 lines (185 loc) · 6.78 KB
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
"""A setuptools based setup module.
See:
https://packaging.python.org/en/latest/distributing.html
"""
from setuptools import Command, Extension, setup, find_packages
from setuptools.command.build_ext import build_ext
import os
from os.path import join
import sys
import platform
from subprocess import call
import shutil
# import numpy
import versioneer
try:
from Cython.Build import cythonize
_CYTHON_INSTALLED = True
except:
_CYTHON_INSTALLED = False
setup_dir = os.path.abspath(os.path.dirname(__file__))
print("setup directory: %r" % setup_dir)
arch_x64 = False
if sys.maxsize > 2**32:
arch_x64 = True
if not arch_x64:
raise Exception("Only the 64-bit Python is supported")
def is_platform_windows():
return sys.platform == "win32" or sys.platform == "cygwin"
def is_platform_mac():
return sys.platform == "darwin"
def is_platform_linux():
return sys.platform == "linux"
def maybe_cythonize(extensions, *args, **kwargs):
"""
Function derived from Pandas Project. This skips cythonize for the following commands
* clean
* sdist
"""
if "clean" in sys.argv or "sdist" in sys.argv:
# See https://github.com/cython/cython/issues/1495
return extensions
elif not _CYTHON_INSTALLED:
raise RuntimeError("Cannot cythonize without Cython installed.")
return cythonize(extensions, *args, **kwargs)
class CleanCommand(Command):
"""Custom command to clean the ext and build files derived from Pandas project"""
user_options = [("all", "a", "")]
def initialize_options(self):
self.all = True
self._clean_me = [
join("pydsstools", "src", "core_heclib.c"),
join("pydsstools", "heclib", "dsslog.dss"),
]
# self._clean_trees = [join('pydsstools', 'src', 'external', 'gridv6', 'x64'),
# join('pydsstools', 'src', 'external', 'gridv6', 'build')
# ]
self._clean_trees = []
self._clean_exclude = []
self._clean_exclude_dirs = [
join("pydsstools", "src", "external", "dss"),
join("pydsstools", "src", "external", "zlib"),
]
# source
for root, dirs, files in os.walk("pydsstools"):
if root in self._clean_exclude_dirs:
continue
for d in dirs:
if d == "__pycache__":
self._clean_trees.append(join(root, d))
for x in self._clean_exclude_dirs:
if x in root:
continue
for f in files:
filepath = join(root, f)
if filepath in self._clean_exclude:
continue
if os.path.splitext(f)[-1] in (
".pyc",
".pyo",
".pyd",
".o",
):
self._clean_me.append(filepath)
# build
for d in ("build", "pydsstools.egg-info"):
if os.path.exists(d):
self._clean_trees.append(d)
def finalize_options(self):
pass
def run(self):
for clean_me in self._clean_me:
try:
os.unlink(clean_me)
except OSError:
pass
for clean_tree in self._clean_trees:
try:
shutil.rmtree(clean_tree)
except OSError:
pass
class BuildExt(build_ext):
def build_grid_library(self):
if is_platform_windows():
# print('Building external library: grid.lib')
# batch_file = join(setup_dir,r'pydsstools/src/external/gridv6/build.bat ')
# call(batch_file)
pass
else:
# print('Building external library: grid.a')
# make_dir = join(setup_dir,r'pydsstools/src/external/gridv6')
# call('make all', shell=True, cwd=make_dir)
pass
def build_extensions(self):
# self.build_grid_library()
super().build_extensions()
def finalize_options(self):
super().finalize_options()
import numpy as np
self.include_dirs.append(np.get_include())
print(f"Build-time NumPy version: {np.__version__}")
print(f"NumPy include path: {np.get_include()}")
# Create compiler arguments
include_dirs = []
include_dirs.append(r"pydsstools/src/external/dss/headers")
# include_dirs.append(r'pydsstools/src/external/gridv6/headers')
library_dirs = []
if is_platform_windows():
# headers
include_dirs.append(r"pydsstools/src/external/zlib")
# lib dirs
library_dirs.append(r"pydsstools/src/external/dss/win64")
# library_dirs.append(r'pydsstools/src/external/gridv6/build')
library_dirs.append(r"pydsstools/src/external/zlib")
# libs
# libraries = ['heclib_c', 'heclib_f', 'zlibstatic', 'grid']
libraries = ["heclib_c", "heclib_f", "zlibstatic"]
# extra compile args
extra_compile_args = []
extra_link_args = ["/NODEFAULTLIB:LIBCMT"]
else:
# lib dirs
library_dirs.append(r"pydsstools/src/external/dss/linux64")
# library_dirs.append(r'pydsstools/src/external/gridv6/build')
# libs
# libraries = [':heclib.a', ':grid.a','gfortran', 'pthread', 'm', 'quadmath', 'z', 'stdc++']
libraries = [":heclib.a", "gfortran", "pthread", "m", "quadmath", "z", "stdc++"]
# extra compile args
extra_compile_args = []
extra_link_args = []
# include_dirs.append(numpy.get_include())
# NumPy 2.0 compatibility:
# - NPY_NO_DEPRECATED_API suppresses deprecated API warnings and ensures forward compatibility
# - NPY_1_7_API_VERSION is the minimum stable API version that works with both NumPy 1.x and 2.x
# - When building with NumPy 1.x (via oldest-supported-numpy), wheels are forward-compatible
# with NumPy 2.x if we use only the stable C API subset
# - The previous error "C2039: 'dimensions' is not a member of 'tagPyArrayObject'" was caused
# by using deprecated array access macros. Cython 0.29.x+ handles this properly.
macros = [("NPY_NO_DEPRECATED_API", "NPY_1_7_API_VERSION")]
extensions = [
Extension(
"pydsstools._lib.x64.core_heclib",
sources=[r"pydsstools/src/core_heclib.pyx"],
include_dirs=include_dirs,
library_dirs=library_dirs,
libraries=libraries,
define_macros=macros,
extra_compile_args=extra_compile_args,
extra_link_args=extra_link_args,
),
]
compiler_directives = {
"embedsignature": True,
"language_level": "3",
"c_string_type": "str",
"c_string_encoding": "ascii",
}
try:
version = versioneer.get_version()
except:
version = versioneer.get_versions()["version"]
setup(
version=version,
ext_modules=maybe_cythonize(extensions, compiler_directives=compiler_directives),
cmdclass=versioneer.get_cmdclass({"clean": CleanCommand, "build_ext": BuildExt}),
)