-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.py
More file actions
167 lines (137 loc) · 5.25 KB
/
setup.py
File metadata and controls
167 lines (137 loc) · 5.25 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
import os
import platform
import shutil
import subprocess
import sys
from packaging import version
from pathlib import Path
from setuptools import Command, Extension, setup
from wheel.bdist_wheel import bdist_wheel as _bdist_wheel
try:
subprocess.check_call([sys.executable, "-m", "pip", "install", "numpy"])
subprocess.check_call([sys.executable, "-m", "pip", "install", "Cython"])
subprocess.check_call([sys.executable, "-m", "pip", "install", "jax"])
subprocess.check_call([sys.executable, "-m", "pip", "install", "tqdm"])
except Exception as e:
pass
class bdist_wheel(_bdist_wheel):
def finalize_options(self):
_bdist_wheel.finalize_options(self)
# Mark as platform-specific wheel
self.root_is_pure = False
def get_tag(self):
python, abi, plat = _bdist_wheel.get_tag(self)
# Replace 'linux_x86_64' with 'manylinux' tag
if plat.startswith('linux'):
plat = 'manylinux2014_x86_64'
return python, abi, plat
CYTHON_MIN_VERSION = version.parse("3.0.10")
__version__ = "0.7.1"
class clean(Command):
user_options = [("all", "a", "")]
def initialize_options(self):
self.all = True
self.delete_dirs = []
self.delete_files = []
for root, dirs, files in os.walk("cybooster"):
root = Path(root)
for d in dirs:
if d == "__pycache__":
self.delete_dirs.append(root / d)
if "__pycache__" in root.name:
continue
for f in (root / x for x in files):
ext = f.suffix
if ext == ".pyc" or ext == ".so":
self.delete_files.append(f)
if ext in (".c", ".cpp"):
source_file = f.with_suffix(".pyx")
if source_file.exists():
self.delete_files.append(f)
build_path = Path("build")
if build_path.exists():
self.delete_dirs.append(build_path)
def finalize_options(self):
pass
def run(self):
for delete_dir in self.delete_dirs:
shutil.rmtree(delete_dir)
for delete_file in self.delete_files:
delete_file.unlink()
EXTENSIONS = {
"_boosterc": {"sources": ["cybooster/_boosterc.pyx"]},
"_ngboost": {"sources": ["cybooster/_ngboost.pyx"]},
"_ngboostclf": {"sources": ["cybooster/_ngboostclf.pyx"]}
}
def get_module_from_sources(sources):
for src_path in map(Path, sources):
if src_path.suffix == ".pyx":
return ".".join(src_path.parts[:-1] + (src_path.stem,))
raise ValueError(f"Could not find module from sources: {sources!r}")
def _check_cython_version():
message = f"Please install Cython with a version >= {CYTHON_MIN_VERSION}"
try:
import Cython
except ModuleNotFoundError:
raise ModuleNotFoundError(message)
if version.parse(Cython.__version__) < CYTHON_MIN_VERSION:
message += f" The current version is {Cython.__version__} in {Cython.__path__}."
raise ValueError(message)
def cythonize_extensions(extensions):
_check_cython_version()
from Cython.Build import cythonize
directives = {
"language_level": "3",
"embedsignature": True,
"boundscheck": False,
"wraparound": False
}
macros = [("NPY_NO_DEPRECATED_API", "NPY_1_7_API_VERSION")]
for ext in extensions:
if ext.define_macros is None:
ext.define_macros = macros
else:
ext.define_macros += macros
return cythonize(extensions, compiler_directives=directives)
def get_extensions():
import numpy
numpy_includes = [numpy.get_include()]
extensions = []
for config in EXTENSIONS.values():
name = get_module_from_sources(config["sources"])
include_dirs = numpy_includes + config.get("include_dirs", [])
# Platform-specific compile args
extra_compile_args = []
if sys.platform == "darwin":
extra_compile_args.extend(["-stdlib=libc++", "-mmacosx-version-min=10.15"])
if platform.machine() == "arm64":
extra_compile_args.extend(["-arch", "arm64"])
else:
extra_compile_args.extend(["-arch", "x86_64"])
elif sys.platform == "win32":
extra_compile_args.extend(["/EHsc", "/O2"])
ext = Extension(
name=name,
sources=config["sources"],
include_dirs=include_dirs,
extra_compile_args=extra_compile_args,
language="c",
)
extensions.append(ext)
if "sdist" not in sys.argv and "clean" not in sys.argv:
extensions = cythonize_extensions(extensions)
return extensions
if __name__ == "__main__":
setup(
ext_modules=get_extensions(),
version=__version__,
zip_safe=False,
cmdclass={"clean": clean, 'bdist_wheel': bdist_wheel},
options={
'bdist_wheel': {
'universal': False, # Platform-specific wheel
'plat_name': 'manylinux2014_x86_64' if sys.platform == 'linux' else None,
}
},
platforms=['Linux', 'MacOS', 'Windows'],
)