forked from metatensor/featomic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.py
188 lines (155 loc) · 5.98 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
import glob
import os
import shutil
import subprocess
from setuptools import Extension, setup
from setuptools.command.build_ext import build_ext
from setuptools.command.install import install as distutils_install
from wheel.bdist_wheel import bdist_wheel
ROOT = os.path.realpath(os.path.dirname(__file__))
RASCALINE_BUILD_TYPE = os.environ.get("RASCALINE_BUILD_TYPE", "release")
if RASCALINE_BUILD_TYPE not in ["debug", "release"]:
raise Exception(
f"invalid build type passed: '{RASCALINE_BUILD_TYPE}',"
"expected 'debug' or 'release'"
)
RUST_BUILD_TARGET = os.environ.get("RUST_BUILD_TARGET", None)
class universal_wheel(bdist_wheel):
"""Helper class for override wheel tag.
When building the wheel, the `wheel` package assumes that if we have a
binary extension then we are linking to `libpython.so`; and thus the wheel
is only usable with a single python version. This is not the case for
here, and the wheel will be compatible with any Python >=3.6. This is
tracked in https://github.com/pypa/wheel/issues/185, but until then we
manually override the wheel tag.
"""
def get_tag(self):
"""Get the tag for override."""
tag = bdist_wheel.get_tag(self)
# tag[2:] contains the os/arch tags, we want to keep them
return ("py3", "none") + tag[2:]
class cmake_ext(build_ext):
"""Build the native library using cmake."""
def run(self):
"""Run cmake build and install the resulting library."""
source_dir = os.path.join(ROOT, "rascaline-c-api")
build_dir = os.path.join(ROOT, "build", "cmake-build")
install_dir = os.path.join(os.path.realpath(self.build_lib), "rascaline")
try:
os.mkdir(build_dir)
except OSError:
pass
cmake_options = [
f"-DCMAKE_INSTALL_PREFIX={install_dir}",
f"-DCMAKE_BUILD_TYPE={RASCALINE_BUILD_TYPE}",
# do not include chemfiles inside rascaline, instead users should
# use chemfiles python bindings directly
"-DRASCALINE_DISABLE_CHEMFILES=ON",
"-DRASCALINE_FETCH_EQUISTORE=ON",
"-DRASCALINE_BUILD_FOR_PYTHON=ON",
]
if "CARGO" in os.environ:
cmake_options.append(f"-DCARGO_EXE={os.environ['CARGO']}")
if RUST_BUILD_TARGET is not None:
cmake_options.append(f"-DRUST_BUILD_TARGET={RUST_BUILD_TARGET}")
subprocess.run(
["cmake", source_dir, *cmake_options],
cwd=build_dir,
check=True,
)
subprocess.run(
["cmake", "--build", build_dir],
check=True,
)
subprocess.run(
["cmake", "--build", build_dir, "--target", "install"],
check=True,
)
# do not include equistore libraries/headers/cmake config within
# rascaline wheel
for file in glob.glob(os.path.join(install_dir, "lib", "*equistore*")):
os.unlink(file)
shutil.rmtree(os.path.join(install_dir, "lib", "cmake", "equistore"))
for file in glob.glob(os.path.join(install_dir, "include", "equistore*")):
os.unlink(file)
def get_version():
"""
Get the version of equistore from the Cargo.toml file and git metadata.
If git is available, it is used to check if we are installing a development
version or a released version (by checking how many commits happened since
the last tag).
"""
# read version from Cargo.toml
with open("rascaline-c-api/Cargo.toml") as fd:
for line in fd:
if line.startswith("version"):
_, version = line.split(" = ")
# remove quotes
version = version[1:-2]
# take the first version in the file, this should be the right
# version
break
# Add pre-release info the version
try:
tags_list = subprocess.run(
["git", "tag"],
stderr=subprocess.DEVNULL,
stdout=subprocess.PIPE,
check=True,
)
tags_list = tags_list.stdout.decode("utf8").strip()
if tags_list == "":
first_commit = subprocess.run(
["git", "rev-list", "--max-parents=0", "HEAD"],
stderr=subprocess.DEVNULL,
stdout=subprocess.PIPE,
check=True,
)
reference = first_commit.stdout.decode("utf8").strip()
else:
last_tag = subprocess.run(
["git", "describe", "--tags", "--abbrev=0"],
stderr=subprocess.DEVNULL,
stdout=subprocess.PIPE,
check=True,
)
reference = last_tag.stdout.decode("utf8").strip()
except Exception:
reference = ""
pass
try:
n_commits_since_tag = subprocess.run(
["git", "rev-list", f"{reference}..HEAD", "--count"],
stderr=subprocess.DEVNULL,
stdout=subprocess.PIPE,
check=True,
)
n_commits_since_tag = n_commits_since_tag.stdout.decode("utf8").strip()
if n_commits_since_tag != 0:
version += ".dev" + n_commits_since_tag
except Exception:
pass
return version
setup(
version=get_version(),
author=", ".join(open(os.path.join(ROOT, "AUTHORS")).read().splitlines()),
ext_modules=[
# only declare the extension, it is built & copied as required by cmake
# in the build_ext command
Extension(name="rascaline", sources=[]),
],
cmdclass={
"build_ext": cmake_ext,
"bdist_wheel": universal_wheel,
# HACK: do not use the new setuptools install implementation, it tries
# to install the package with `easy_install`, which fails to resolve the
# freshly installed package and tries to load it from pypi.
"install": distutils_install,
},
package_data={
"rascaline": [
"rascaline/lib/*",
"rascaline/include/*",
]
},
)