-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathsetup_user.py
More file actions
235 lines (209 loc) · 8.58 KB
/
setup_user.py
File metadata and controls
235 lines (209 loc) · 8.58 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import shutil
import warnings
warnings.filterwarnings("ignore")
import platform
import subprocess as sb
from os.path import isfile, join
from os import listdir
import numpy
from setuptools import setup, find_packages
from setuptools.extension import Extension
from setuptools.command.build_ext import build_ext
from Cython.Build import cythonize
from Cython.Compiler.Errors import CompileError
# ----------------------------------------------------------------------
# Paths / environment
# ----------------------------------------------------------------------
try:
from isp import ROOT_DIR
except Exception as e:
raise RuntimeError(f"Could not import isp. Ensure ISP is importable. Error: {e}")
system_computer = platform.system().lower()
machine_computer = platform.machine().lower()
if system_computer in ("linux", "linux2"):
system_path = os.path.join(ROOT_DIR, "linux_bin")
elif system_computer == "darwin":
if machine_computer == "arm64":
system_path = os.path.join(ROOT_DIR, "mac_m_bin") # Apple Silicon
else:
system_path = os.path.join(ROOT_DIR, "mac_bin") # Intel mac
else:
# Unsupported platforms won't stop the build; just print info.
print(f"Unsupported system: {system_computer} ({machine_computer})")
system_path = os.path.join(ROOT_DIR, "bin_unknown")
cy_path = os.path.join(ROOT_DIR, "cython_code")
# ----------------------------------------------------------------------
# Helper to run subprocess safely
# ----------------------------------------------------------------------
def exc_cmd(cmd, **kwargs):
stdout = kwargs.pop("stdout", sb.PIPE)
stdin = kwargs.pop("stdin", sb.PIPE)
stderr = kwargs.pop("stderr", sb.PIPE)
encoding = kwargs.pop("encoding", "utf-8")
with sb.Popen(cmd, stdin=stdin, stdout=stdout, stderr=stderr, encoding=encoding, **kwargs) as p:
try:
std_out, std_err = p.communicate(timeout=15)
except sb.TimeoutExpired:
p.kill()
std_out, std_err = p.communicate()
if p.returncode != 0:
raise sb.CalledProcessError(p.returncode, std_err.strip())
elif len(std_err) != 0:
raise sb.SubprocessError(std_err.strip())
return std_out.strip()
# ----------------------------------------------------------------------
# Folder preparation for third-party binaries (NLL7, FOCMEC, MTI)
# ----------------------------------------------------------------------
def nll_create_folders():
try:
folder_nll = os.path.join(ROOT_DIR, "NLL7")
if os.path.isdir(folder_nll):
shutil.rmtree(folder_nll)
except Exception:
pass
folder_create = os.path.join(ROOT_DIR, "NLL7", "src", "bin")
source_code = os.path.join(system_path, "NLL_new")
try:
os.makedirs(folder_create, exist_ok=True)
except IOError as error:
print(error)
return source_code, folder_create
def focmec_create_folders():
try:
folder_focmec = os.path.join(ROOT_DIR, "focmec")
if os.path.isdir(folder_focmec):
shutil.rmtree(folder_focmec)
except Exception:
pass
folder_create = os.path.join(ROOT_DIR, "focmec", "bin")
source_code = os.path.join(system_path, "FOCMEC", "bin")
try:
os.makedirs(folder_create, exist_ok=True)
except IOError as error:
print(error)
return source_code, folder_create
def mti_create_folders():
try:
folder_mti = os.path.join(ROOT_DIR, "mti", "green_source")
if os.path.isdir(folder_mti):
shutil.rmtree(folder_mti)
except Exception:
pass
try:
folder_work_mti = os.path.join(ROOT_DIR, "mti", "green")
if os.path.isdir(folder_work_mti):
shutil.rmtree(folder_work_mti)
except Exception:
pass
folder_work_create = os.path.join(ROOT_DIR, "mti", "green")
folder_create = os.path.join(ROOT_DIR, "mti", "green_source")
source_code = os.path.join(system_path, "mti_green")
try:
os.makedirs(folder_create, exist_ok=True)
os.makedirs(folder_work_create, exist_ok=True)
except IOError as error:
print(error)
return source_code, folder_create, folder_work_create
# ----------------------------------------------------------------------
# Custom build command to copy binaries + build Cython
# ----------------------------------------------------------------------
class CustomBuildExtCommand(build_ext):
def run(self):
print("Creating 3rd-party package folders & copying binaries...")
src, dst = nll_create_folders()
self._copy_binaries(src, dst, label="NLL")
src, dst = focmec_create_folders()
self._copy_binaries(src, dst, label="FOCMEC")
src, dst, work = mti_create_folders()
self._copy_binaries(src, dst, label="MTI")
self._copy_binaries(src, work, label="MTI(work)")
# Now run normal Cython build
super().run()
def _copy_binaries(self, source_code, destination, label=""):
print(f"Gathering {label} files from: {source_code}")
try:
allfiles = [f for f in listdir(source_code) if isfile(join(source_code, f))]
except Exception as e:
warnings.warn(f"Could not list {label} binaries at {source_code}: {e}")
return
for f in allfiles:
src_path = os.path.join(source_code, f)
dst_path = os.path.join(destination, f)
try:
shutil.copy(src_path, dst_path)
print(f" + {src_path} -> {dst_path}")
except Exception as e:
warnings.warn(f"Failed to copy {src_path} to {dst_path}: {e}")
# ----------------------------------------------------------------------
# Build Cython extensions with per-module try/except
# ----------------------------------------------------------------------
extra_compile_args = ["/O2"] if system_computer == "windows" else ["-O3"]
extra_link_args = []
extra_libraries = [] if system_computer == "windows" else ["m"] # libm on POSIX
def try_add_extension(modname, src):
"""
Try to cythonize a single extension. On failure, warn and continue.
Returns a list (possibly empty) of cythonized Extension objects.
"""
try:
ext = Extension(
name=modname,
sources=[src],
include_dirs=[numpy.get_include(), cy_path],
language="c",
extra_compile_args=extra_compile_args,
extra_link_args=extra_link_args,
libraries=extra_libraries,
)
return cythonize(
[ext],
language_level=3,
compiler_directives={
"boundscheck": False,
"wraparound": False,
"cdivision": True,
"nonecheck": False,
},
)
except CompileError as e:
warnings.warn(f"Could not compile {modname}: {e}. Skipping.")
except Exception as e:
warnings.warn(f"Unexpected error compiling {modname}: {e}. Skipping.")
return []
# Collect target .pyx modules (explicit list; add/remove as needed)
# cy_modules = [
# ("isp.cython_code.hampel", os.path.join(cy_path, "hampel.pyx")),
# # CF modules:
# ("isp.cython_code.rec_filter", os.path.join(cy_path, "rec_filter.pyx")),
# ("isp.cython_code.lib_rec_hos", os.path.join(cy_path, "lib_rec_hos.pyx")),
# ("isp.cython_code.lib_rec_rms", os.path.join(cy_path, "lib_rec_rms.pyx")),
# ("isp.cython_code.lib_rosenberger",os.path.join(cy_path, "lib_rosenberger.pyx")),
# ("isp.cython_code.lib_rec_cc", os.path.join(cy_path, "lib_rec_cc.pyx")),
# # Optional/problematic ones (won't break the build if they fail):
# ("isp.cython_code.ccwt_cy", os.path.join(cy_path, "ccwt_cy.pyx")),
# ("isp.cython_code.whiten", os.path.join(cy_path, "whiten.pyx")),
# ]
cy_modules = [
("isp.cython_code.hampel", os.path.join(cy_path, "hampel.pyx")),
("isp.cython_code.whiten", os.path.join(cy_path, "whiten.pyx"))]
ext_list = []
for modname, src in cy_modules:
if os.path.isfile(src):
ext_list.extend(try_add_extension(modname, src))
else:
warnings.warn(f"Source not found for {modname}: {src}. Skipping.")
# ----------------------------------------------------------------------
# Setup
# ----------------------------------------------------------------------
setup(
cmdclass={'build_ext': CustomBuildExtCommand},
name='isp_package',
version='2.0',
description='ISP setup script',
packages=find_packages(include=['isp', 'isp.*']),
include_dirs=[numpy.get_include()],
ext_modules=ext_list,
)