-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsetup.py
337 lines (269 loc) · 9.78 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
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
import os
import subprocess
import sys
from sysconfig import get_config_vars
from setuptools import Extension, distutils, find_packages, setup
from setuptools.command.build_ext import build_ext
try:
from api_compatibility import check_kim_api_compatibility
except ImportError:
# new version of pip does not add cwd to path
sys.path.append(os.getcwd())
from api_compatibility import check_kim_api_compatibility
# remove `-Wstrict-prototypes' that is for C not C++
cfg_vars = get_config_vars()
for _key, _value in cfg_vars.items():
if isinstance(_value, str) and "-Wstrict-prototypes" in _value:
cfg_vars[_key] = _value.replace("-Wstrict-prototypes", "")
def inquire_kim_api(kim_api_key):
"""Get compile and link flags of kim-api package."""
try:
output = subprocess.check_output(
["pkg-config", kim_api_key, "libkim-api"], universal_newlines=True
)
except:
msg = 'Package "libkim-api" was not found in the search path.\n'
msg += 'Make sure "kim-api" is installed and do not forget to '
msg += '"source path/to/kim-api-activate".\nSee '
msg += '"Installing the KIM API" at https://openkim.org/kim-api '
msg += "for more detailed information."
raise Exception(msg)
split_config = output.strip().split()
# remove identifier
identifier = kim_api_key[-2:]
if identifier in ["-I", "-L", "-l"]:
split_config = [
s.replace(identifier, "") for s in split_config if s.startswith(identifier)
]
# remove empty string
split_config = [s for s in split_config if s != ""]
return split_config
class get_pybind_include:
"""Helper class to determine the pybind11 include path.
The purpose of this class is to postpone importing pybind11
until it is actually installed, so that the ``get_include()``
method can be invoked.
"""
def __str__(self):
import pybind11
return pybind11.get_include()
def has_flag(compiler, flag_name):
"""Return a boolean indicating whether a flag name is supported on
the specified compiler.
"""
import tempfile
with tempfile.NamedTemporaryFile("w", suffix=".cpp", delete=False) as f:
f.write("int main (int argc, char **argv) { return 0; }")
tempfile_name = f.name
try:
compiler.compile([tempfile_name], extra_postargs=[flag_name])
except distutils.errors.CompileError:
print(
f"Checking whether the compiler supports flag: '{flag_name}'. "
"You may see compilation error (e.g. gcc: error: unrecognized "
f"command line option {flag_name}). Just ignore it; we are on "
"the right track."
)
return False
finally:
try:
os.remove(tempfile_name)
except OSError:
pass
return True
def cpp_flag(compiler_type, compiler):
"""Return the -std=c++[11/14/17] compiler flag.
The newer version is preferred over c++11 (when it is available).
"""
if compiler_type == "unix":
flags = ["-std=c++20", "-std=c++17", "-std=c++14", "-std=c++11"]
else:
flags = ["-std=c++20", "-std=c++17"]
for flag in flags:
if has_flag(compiler, flag):
return flag
if compiler_type == "unix":
msg = "Unsupported compiler -- at least C++11 support is needed!"
else:
msg = "Unsupported compiler -- at least C++17 support is needed!"
raise Exception(msg)
class BuildExt(build_ext):
"""A custom build extension for adding compiler-specific options."""
c_opts = {
"msvc": ["/EHsc"],
"unix": [],
}
l_opts = {
"msvc": [],
"unix": [],
}
if sys.platform == "darwin":
c_opts["unix"].append("-mmacosx-version-min=10.7")
l_opts["unix"].append("-mmacosx-version-min=10.7")
def build_extensions(self):
compiler_type = self.compiler.compiler_type
if sys.platform == "darwin":
if has_flag(self.compiler, "-stdlib=libc++"):
self.c_opts["unix"].append("-stdlib=libc++")
self.l_opts["unix"].append("-stdlib=libc++")
opts = self.c_opts.get(compiler_type, [])
opts.append(cpp_flag(compiler_type, self.compiler))
if has_flag(self.compiler, "-fvisibility=hidden"):
opts.append("-fvisibility=hidden")
link_opts = self.l_opts.get(compiler_type, [])
kim_extra_link_args = inquire_kim_api("--libs-only-other")
for link_arg in kim_extra_link_args:
link_opts.append(link_arg)
kim_include_dirs = inquire_kim_api("--cflags-only-I")
for include_dir in kim_include_dirs:
self.compiler.add_include_dir(include_dir)
kim_library_dirs = inquire_kim_api("--libs-only-L")
for library_dir in kim_library_dirs:
self.compiler.add_library_dir(library_dir)
kim_libraries = inquire_kim_api("--libs-only-l")
for library in kim_libraries:
self.compiler.add_library(library)
for ext in self.extensions:
ext.define_macros = [
("VERSION_INFO", '"{}"'.format(self.distribution.get_version()))
]
ext.extra_compile_args = opts
ext.extra_link_args = link_opts
build_ext.build_extensions(self)
def get_include_dirs():
"""Return the list of directories that will be searched."""
include_dirs = inquire_kim_api("--cflags-only-I")
include_dirs.append(get_pybind_include())
include_dirs.append(
os.path.join(os.path.dirname(os.path.abspath(__file__)), "kimpy", "neighlist")
)
return include_dirs
def get_kimpy_version():
"""Get the kimpy version.
Returns:
str: kimpy version
"""
kimpy_init_file = os.path.join(
os.path.dirname(os.path.abspath(__file__)), "kimpy", "__init__.py"
)
version = None
with open(kimpy_init_file) as fin:
for line in fin:
line = line.strip()
if "__version__" in line:
sline = line.split("=")[1]
# stripe white space, and ' or " in string
if "'" in sline:
version = sline.strip("' ")
elif '"' in sline:
version = sline.strip('" ')
break
if version is None:
msg = "the current kimpy version can not be found."
raise Exception(msg)
return version
def get_extension(name):
"""Return the Extension instance, used to describe C/C++ extension modules.
Args:
name (str): the name of the extension
Returns:
Extension obj: Return an Extension instance
"""
# the full name of the extension
module_name = "kimpy.{}".format(name)
if name == "neighlist":
sources = [
os.path.join("kimpy", "neighlist", "neighbor_list.cpp"),
os.path.join("kimpy", "neighlist", "neighbor_list_bind.cpp"),
]
else:
tname = name.split("_")
name = [i.title() for i in tname]
name = "".join(name)
sources = [os.path.join("kimpy", "KIM_{}_bind.cpp".format(name))]
return Extension(
module_name,
sources=sources,
include_dirs=get_include_dirs(),
library_dirs=inquire_kim_api("--libs-only-L"),
libraries=inquire_kim_api("--libs-only-l"),
extra_link_args=inquire_kim_api("--libs-only-other"),
language="c++",
)
def get_kimpy_ext_modules():
"""Get kimpy Extension instances.
Returns:
list: kimpy Extension instances
"""
module_names = [
"model",
"compute_arguments",
"compute_argument_name",
"compute_callback_name",
"data_type",
"species_name",
"language_name",
"model_routine_name",
"numbering",
"support_status",
"log_verbosity",
"length_unit",
"energy_unit",
"charge_unit",
"temperature_unit",
"time_unit",
"sem_ver",
"log",
"collections",
"collection",
"collection_item_type",
"simulator_model",
"neighlist",
]
kimpy_ext_modules = [get_extension(name) for name in module_names]
return kimpy_ext_modules
def chech_kim_api_compatibility():
"""Check the KIM-API compatibility."""
modversion = inquire_kim_api("--modversion")[0]
if "-" in modversion:
kim_api_version = modversion.split("-")[0]
elif "+" in modversion:
kim_api_version = modversion.split("+")[0]
else:
kim_api_version = modversion
kimpy_version = get_kimpy_version()
kim_api_compatibility = check_kim_api_compatibility(kimpy_version, kim_api_version)
if kim_api_compatibility is not None:
raise Exception(kim_api_compatibility)
# Check kim-api compatibility
chech_kim_api_compatibility()
setup(
name="kimpy",
version=get_kimpy_version(),
packages=find_packages(),
install_requires=["numpy"],
extras_require={
"test": ["pytest", "ase"],
},
python_requires=">=3.7",
cmdclass={"build_ext": BuildExt},
ext_modules=get_kimpy_ext_modules(),
author="Mingjian Wen, Yaser Afshar",
author_email="[email protected], [email protected]",
url="https://github.com/openkim/kimpy",
description="Python interface to the KIM-API",
long_description="Python interface to the KIM-API. For more "
"information about the KIM-API, please see: "
"https://openkim.org/kim-api/",
license="CDDL-1.0",
classifiers=[
"Development Status :: 5 - Production/Stable",
"Intended Audience :: Science/Research",
"Topic :: Scientific/Engineering",
"Operating System :: OS Independent",
"License :: OSI Approved :: Common Development and Distribution "
"License 1.0 (CDDL-1.0)",
],
keywords=["kimpy"],
zip_safe=False,
)