forked from CederGroupHub/smol
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.py
152 lines (126 loc) · 4.18 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
"""smol -- Statistical Mechanics On Lattices"""
import os
import platform
import shutil
import sys
import numpy
from setuptools import Command, Extension, setup
from setuptools.command.build_ext import build_ext as build_ext_
from tools.build_helpers import (
check_openmp_support,
cythonize_extensions,
get_openmp_flag,
)
COMPILE_OPTIONS = {
"msvc": [
"/O2",
"/EHsc",
],
"mingw32": ["-Wall", "-Wextra"],
"other": ["-Wall", "-Wextra", "-ffast-math", "-O3"],
}
LINK_OPTIONS = {
"msvc": ["-Wl, --allow-multiple-definition"],
"mingw32": [["-Wl, --allow-multiple-definition"]],
"other": [],
}
COMPILER_DIRECTIVES = {
"language_level": 3,
}
if sys.platform.startswith("darwin"):
if "PYTHON_CROSSENV" not in os.environ:
COMPILE_OPTIONS["other"] += ["-mcpu=native"]
elif platform.machine() == "x86_64": # cross compiling for macos arm64
COMPILE_OPTIONS["other"] += ["-mcpu=apple-m1"]
# otherwise we don't specific -mcpu
# custom clean command to remove .c files
# taken from sklearn setup.py
class clean(Command):
description = "Remove build artifacts from the source tree"
user_options = []
def initialize_options(self):
pass
def finalize_options(self):
pass
def run(self):
# Remove c files if we are not within a sdist package
cwd = os.path.abspath(os.path.dirname(__file__))
remove_c_files = not os.path.exists(os.path.join(cwd, "PKG-INFO"))
if remove_c_files:
print("Will remove generated .c files")
if os.path.exists("build"):
shutil.rmtree("build")
for dirpath, dirnames, filenames in os.walk("smol"):
for filename in filenames:
_, extension = os.path.splitext(filename)
if extension in [".so", ".pyd", ".dll", ".pyc"]:
os.unlink(os.path.join(dirpath, filename))
if remove_c_files and extension in [".c"]:
pyx_file = str.replace(filename, extension, ".pyx")
if os.path.exists(os.path.join(dirpath, pyx_file)):
os.unlink(os.path.join(dirpath, filename))
for dirname in dirnames:
if dirname == "__pycache__":
shutil.rmtree(os.path.join(dirpath, dirname))
class build_ext_options:
def build_options(self):
OMP_SUPPORTED = check_openmp_support(self.compiler)
for e in self.extensions:
e.extra_compile_args += COMPILE_OPTIONS.get(
self.compiler.compiler_type, COMPILE_OPTIONS["other"]
)
e.extra_link_args += LINK_OPTIONS.get(
self.compiler.compiler_type, LINK_OPTIONS["other"]
)
if OMP_SUPPORTED:
omp_flag = get_openmp_flag(self.compiler)
e.extra_compile_args += omp_flag
e.extra_link_args += omp_flag
class build_ext(build_ext_, build_ext_options):
def build_extensions(self):
build_ext_options.build_options(self)
build_ext_.build_extensions(self)
# Compile option for cython extensions
cython_kwargs = {}
if "--force-cython" in sys.argv:
cython_kwargs["force"] = True
sys.argv.remove("--force-cython")
if "--annotate-cython" in sys.argv:
cython_kwargs["annotate"] = True
sys.argv.remove("--annotate-cython")
extensions = [
Extension(
"smol.utils.cluster.evaluator",
["smol/utils/cluster/evaluator.pyx"],
language="c",
),
Extension(
"smol.utils.cluster.ewald",
["smol/utils/cluster/ewald.pyx"],
language="c",
),
Extension(
"smol.utils.cluster.container",
["smol/utils/cluster/container.pyx"],
language="c",
),
Extension(
"smol.utils.cluster.correlations",
["smol/utils/cluster/correlations.pyx"],
language="c",
),
Extension(
"smol.utils._openmp_helpers",
["smol/utils/_openmp_helpers.pyx"],
language="c",
),
]
cmdclass = {
"clean": clean,
"build_ext": build_ext,
}
setup(
ext_modules=cythonize_extensions(extensions, **cython_kwargs),
cmdclass=cmdclass,
include_dirs=numpy.get_include(),
)