forked from QuakeMigrate/QuakeMigrate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.py
executable file
·184 lines (147 loc) · 5.45 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
# -*- coding: utf-8 -*-
"""
A Python package for automatic earthquake detection and location using waveform
migration and stacking.
QuakeMigrate is a Python package for automatic earthquake detection and
location using waveform migration and stacking. It can be used to produce
catalogues of earthquakes, including hypocentres, origin times, phase arrival
picks, and local magnitude estimates, as well as rigorous estimates of the
associated uncertainties.
The package has been built with a modular architecture, providing the potential
for extension and adaptation at numerous entry points.
:copyright:
2020-2022, QuakeMigrate developers.
:license:
GNU General Public License, Version 3
(https://www.gnu.org/licenses/gpl-3.0.html)
"""
import os
import pathlib
import platform
import shutil
import sys
from distutils.ccompiler import get_default_compiler
from setuptools import Extension, setup
# The minimum python version which can be used to run QuakeMigrate
MIN_PYTHON_VERSION = (3, 8)
# Fail fast if the user is on an unsupported version of Python.
if sys.version_info < MIN_PYTHON_VERSION:
msg = (
f"QuakeMigrate requires python version >= {MIN_PYTHON_VERSION}"
f" you are using python version {sys.version_info}"
)
print(msg, file=sys.stderr)
sys.exit(1)
# Check if we are on RTD and don't build extensions if we are.
READ_THE_DOCS = os.environ.get("READTHEDOCS", None) == "True"
if READ_THE_DOCS:
try:
environ = os.environb
except AttributeError:
environ = os.environ
environ[b"CC"] = b"x86_64-linux-gnu-gcc"
environ[b"LD"] = b"x86_64-linux-gnu-ld"
environ[b"AR"] = b"x86_64-linux-gnu-ar"
# Directory of the current file
SETUP_DIRECTORY = pathlib.Path.cwd()
# Check for MSVC (Windows)
if platform.system() == "Windows" and (
"msvc" in sys.argv or "-c" not in sys.argv and get_default_compiler() == "msvc"
):
IS_MSVC = True
else:
IS_MSVC = False
# Monkey patch for MS Visual Studio
if IS_MSVC:
# Remove 'init' entry in exported symbols
def _get_export_symbols(self, ext):
return ext.export_symbols
from setuptools.command.build_ext import build_ext
build_ext.get_export_symbols = _get_export_symbols
def export_symbols(path):
"""
Required for Windows systems - functions defined in qmlib.def.
"""
with (SETUP_DIRECTORY / path).open("r") as f:
lines = f.readlines()[2:]
return [s.strip() for s in lines if s.strip() != ""]
def get_extensions():
"""
Config function used to compile C code into a Python extension.
"""
import numpy
extensions = []
if READ_THE_DOCS:
return []
extension_args = {
"include_dirs": [
str(pathlib.Path.cwd() / "quakemigrate" / "core" / "src"),
str(pathlib.Path(sys.prefix) / "include"),
numpy.get_include(),
],
"library_dirs": [str(pathlib.Path(sys.prefix) / "lib")],
}
if platform.system() == "Darwin":
extension_args["include_dirs"].extend(
["/usr/local/include", "/usr/local/opt/llvm/include"]
)
extension_args["library_dirs"].extend(
["/usr/local/lib", "/usr/local/opt/llvm/lib", "/usr/local/opt/libomp/lib"]
)
sources = [str(pathlib.Path("quakemigrate") / "core/src/quakemigrate.c")]
extra_link_args = []
if IS_MSVC:
extra_compile_args = ["/openmp", "/TP", "/O2"]
extension_args["export_symbols"] = export_symbols(
"quakemigrate/core/src/qmlib.def"
)
extension_args["library_dirs"].extend(
[
str(pathlib.Path.cwd() / "quakemigrate" / "core"),
str(pathlib.Path(sys.prefix) / "bin"),
]
)
else:
extra_compile_args = []
extra_link_args.extend(["-lm"])
if platform.system() == "Darwin":
extra_link_args.extend(["-lomp"])
extra_compile_args.extend(["-Xpreprocessor"])
else:
extra_link_args.extend(["-lgomp"])
extra_compile_args.extend(["-fopenmp", "-fPIC", "-Ofast"])
extension_args["extra_link_args"] = extra_link_args
extension_args["extra_compile_args"] = extra_compile_args
extensions.extend(
[Extension("quakemigrate.core.src.qmlib", sources=sources, **extension_args)]
)
return extensions
def setup_package():
"""Setup package"""
package_dir, package_data = {}, {}
if IS_MSVC:
package_dir["quakemigrate.core"] = str(pathlib.Path("quakemigrate") / "core")
package_data["quakemigrate.core"] = ["quakemigrate/core/src/*.dll"]
setup_args = {
"ext_modules": get_extensions(),
"package_data": package_data,
"package_dir": package_dir,
}
shutil.rmtree(str(SETUP_DIRECTORY / "build"), ignore_errors=True)
setup(**setup_args)
if __name__ == "__main__":
# clean --all does not remove extensions automatically
if "clean" in sys.argv and "--all" in sys.argv:
# Delete complete build directory
path = SETUP_DIRECTORY / "build"
shutil.rmtree(str(path), ignore_errors=True)
# Delete all shared libs from clib directory
path = SETUP_DIRECTORY / "quakemigrate" / "core" / "src"
for filename in path.glob("*.pyd"):
filename.unlink(missing_ok=True)
for filename in path.glob("*.so"):
filename.unlink(missing_ok=True)
for filename in path.glob("*.dll"):
filename.unlink(missing_ok=True)
else:
setup_package()