Repository navigation
Expand file tree
/
Copy pathsetup.py
More file actions
260 lines (225 loc) · 8.94 KB
/
Copy pathsetup.py
File metadata and controls
260 lines (225 loc) · 8.94 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
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
# NOTE: the Fortran/C extensions are built with f2py via numpy.distutils,
# which was removed from Python's packaging tooling in Python 3.12. This
# setup.py therefore only works on Python 3.8-3.11; building on Python >= 3.12
# requires migrating to meson-python / scikit-build.
from setuptools import setup
from numpy.distutils.core import Extension, setup as np_setup
import importlib.util
import os
import sys
import shutil
import subprocess
from setuptools.command.install import install as _install
on_rtd = os.environ.get('READTHEDOCS') == 'True'
name = "shakermaker"
# Single source of truth for the package version: shakermaker/version.py
_version_globals = {}
with open(os.path.join(os.path.dirname(__file__), "shakermaker", "version.py")) as _vf:
exec(_vf.read(), _version_globals)
version = _version_globals["shakermaker_version"]
release = "0.01"
author = "Jose A. Abell, Jorge Crempien D., and Matias Recabarren"
profile = False
srcdir = "shakermaker/core/"
ffiles = [
"subgreen.f", "subgreen2.f", "subfocal.f", "subfk.f", "subtrav.f",
"tau_p.f", "kernel.f", "prop.f", "source.f", "bessel.f", "haskell.f",
"fft.c", "Complex.c",
]
srcs = [srcdir + "core.pyf"] + [srcdir + f for f in ffiles]
if on_rtd:
ext_modules = []
else:
if profile:
ext1 = Extension(
name='shakermaker.core',
sources=srcs,
extra_f77_compile_args=[
"-ffixed-line-length-132", "-Wno-tabs",
"-Wno-unused-dummy-argument", "-pg", "-fopenmp",
],
extra_link_args=["-pg", "-fopenmp"],
)
else:
# Windows change: Intel ifx compiler flags (via intelvem f2py backend)
if sys.platform == 'win32':
f77_args = ["/Qopenmp", "/extend-source:132"]
link_args = ["/Qopenmp", "/F67108864"]
compile_args = ["-DNPY_NO_DEPRECATED_API=NPY_1_7_API_VERSION"]
else:
f77_args = [
"-ffixed-line-length-132", "-Wno-tabs",
"-Wno-unused-dummy-argument", "-fPIC", "-fopenmp",
]
link_args = ["-fopenmp"]
compile_args = ["-DNPY_NO_DEPRECATED_API=NPY_1_7_API_VERSION"]
ext1 = Extension(
name='shakermaker.core',
sources=srcs,
extra_f77_compile_args=f77_args,
extra_compile_args=compile_args,
extra_link_args=link_args,
)
ext_modules = [ext1]
# =============================================================================
# FFSP compilation support
# =============================================================================
def _f2py_backend_args():
"""f2py's ``-c`` compile mode only grew a ``--backend`` flag in NumPy
1.26, when the legacy distutils backend started being phased out
(NumPy 2.0 removes it outright, requiring ``--backend meson``). Earlier
f2py versions don't recognise the flag at all -- it gets swallowed as a
positional source argument instead, so passing it unconditionally
breaks the build on NumPy < 1.26, which is what this project's own
build currently requires (numpy.distutils for shakermaker.core; see
docs/web/guides/installation.md). Only ask for meson when it's both
available and necessary.
"""
import numpy
major, minor = (int(x) for x in numpy.__version__.split(".")[:2])
return ["--backend", "meson"] if (major, minor) >= (1, 26) else []
def compile_ffsp():
"""Compile the FFSP Fortran wrapper using f2py."""
ffsp_dir = os.path.join(os.path.dirname(__file__), 'shakermaker', 'ffsp')
if sys.platform == 'win32':
_compile_ffsp_windows(ffsp_dir)
else:
_compile_ffsp_linux(ffsp_dir)
def _compile_ffsp_linux(ffsp_dir):
"""Compile FFSP on Linux / macOS using gfortran via f2py."""
fortran_sources = [
"ffsp_wrapper.f90", "ffsp_comm.f90", "spfield_n.f90",
"dcf_subs_1.f90", "slip_rate.f90", "ffsp_tool.f",
]
for f in os.listdir(ffsp_dir):
if f.startswith('ffsp_core') and (f.endswith('.so') or f.endswith('.pyd')):
os.remove(os.path.join(ffsp_dir, f))
cmd = [
sys.executable, "-m", "numpy.f2py",
"-c", "ffsp.pyf",
*fortran_sources,
*_f2py_backend_args(),
"--f90flags=-O3 -fPIC",
"--f77flags=-O3 -std=legacy -fPIC",
"-m", "ffsp_core",
]
print("[ffsp] compiling on Linux/macOS...")
result = subprocess.run(cmd, cwd=ffsp_dir, capture_output=True, text=True)
if result.returncode != 0:
print(result.stdout[-3000:])
print(result.stderr[-3000:])
raise RuntimeError("[ffsp] compilation failed -- see output above")
so_files = [f for f in os.listdir(ffsp_dir)
if f.startswith('ffsp_core') and f.endswith('.so')]
if so_files:
print(f"[OK] FFSP compiled (Linux): {so_files[0]}")
else:
raise RuntimeError("FFSP .so not found after compilation")
def _compile_ffsp_windows(ffsp_dir):
"""Compile FFSP on Windows using Intel ifx via f2py intelvem backend.
capture_output=False so ifx compiler errors print directly to console.
"""
fortran_sources = [
"ffsp_wrapper.f90", "ffsp_comm.f90", "spfield_n.f90",
"dcf_subs_1.f90", "slip_rate.f90", "ffsp_tool.f",
]
for f in os.listdir(ffsp_dir):
if f.startswith('ffsp_core') and (f.endswith('.pyd') or f.endswith('.so')):
os.remove(os.path.join(ffsp_dir, f))
cmd = [
sys.executable, "-m", "numpy.f2py",
"-c", "ffsp.pyf",
*fortran_sources,
"--fcompiler=intelvem",
"--f90flags=/Qopenmp",
"--f77flags=/Qopenmp",
"-m", "ffsp_core",
]
result = subprocess.run(cmd, cwd=ffsp_dir, check=False)
if result.returncode != 0:
raise RuntimeError(
f"[ffsp] f2py/ifx failed (exit {result.returncode}) "
"-- read the ifx error lines above this message.")
pyd_files = [f for f in os.listdir(ffsp_dir)
if f.startswith('ffsp_core') and f.endswith('.pyd')]
if pyd_files:
print(f"[OK] FFSP compiled (Windows): {pyd_files[0]}")
else:
raise RuntimeError(
"FFSP .pyd not found after compilation despite returncode=0")
def _install_ffsp_binary(ffsp_dir):
"""Copy compiled .so/.pyd into site-packages after install."""
import site
ext = ".pyd" if sys.platform == "win32" else ".so"
for sp in site.getsitepackages():
dest = os.path.join(sp, "shakermaker", "ffsp")
if os.path.isdir(dest):
for f in os.listdir(ffsp_dir):
if f.startswith("ffsp_core") and f.endswith(ext):
shutil.copy2(os.path.join(ffsp_dir, f),
os.path.join(dest, f))
print(f"[ffsp] installed {f} -> {dest}")
return
class PostInstallCommand(_install):
"""Custom install command: compile FFSP after the main install."""
def run(self):
_install.run(self)
ffsp_dir = os.path.join(
os.path.dirname(os.path.abspath(__file__)), "shakermaker", "ffsp")
compile_ffsp()
_install_ffsp_binary(ffsp_dir)
# =============================================================================
found_sphinx = importlib.util.find_spec('sphinx') is not None
cmdclass = {'install': PostInstallCommand}
command_options = {}
np_setup(
name = name,
version = version,
author = author,
author_email = "info@joseabell.com",
url = "http://www.joseabell.com",
description = "Physics-based earthquake ground motion synthesis (FK Green's functions + DRM).",
long_description = """\
shakermaker
-------------------------------------
Create realistic seismograms!
""",
keywords = ["earthquake", "engineering", "drm", "simulation"],
classifiers = [
"Programming Language :: Python",
"Programming Language :: Python :: 3",
"Programming Language :: Fortran",
"Development Status :: 5 - Production/Stable",
"Environment :: Console",
"Intended Audience :: Science/Research",
"License :: GPL",
"Operating System :: OS Independent",
"Topic :: Scientific/Engineering :: Physics",
],
python_requires = ">=3.8,<3.12",
package_dir = {'shakermaker': 'shakermaker'},
packages = [
"shakermaker",
"shakermaker.cm_library",
"shakermaker.sl_extensions",
"shakermaker.slw_extensions",
"shakermaker.stf_extensions",
"shakermaker.tools",
"shakermaker.ffsp",
"shakermaker.sw4_exporter",
"shakermaker.crust1",
],
package_data = {
'shakermaker.ffsp': [
'ffsp_core.cpython-*.so',
'ffsp_core.cpython-*.pyd',
'*.f90', '*.f',
'makefile', 'Makefile_f2py',
'ffsp.pyf',
],
'shakermaker.crust1': ['crust1.0/*'],
},
ext_modules = ext_modules,
cmdclass = cmdclass,
command_options = command_options,
)