forked from xsuite/xsuite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.py
69 lines (56 loc) · 2.59 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
# copyright ############################### #
# This file is part of the Xsuite project. #
# Copyright (c) CERN, 2024. #
# ######################################### #
import os
from pathlib import Path
from setuptools import setup
from setuptools.command.build_ext import build_ext
from setuptools.dist import Distribution
class BinaryDistribution(Distribution):
"""Force a binary distribution when building wheels.
Since we don't use extensions in the package, the wheel would be built as a
source wheel, without the platform tags, by default. This way we force the
wheel to be built as a binary wheel.
"""
def has_ext_modules(_): # noqa
return True
class CustomBuildExtCommand(build_ext):
"""Custom build_ext that generates kernel binaries to be added to bdist.
The building of the kernels can be skipped by setting the environment
variable SKIP_KERNEL_BUILD to a non-empty value. This is useful when
performing an editable install, as the kernels pip runs the build script in
a temporary environment and the kernels in such case might not be compatible
with the currently installed packages (e.g. packages with potentially
compatible version numbers but different as they are under development).
"""
def run(self):
super().run()
if os.environ.get('SKIP_KERNEL_BUILD', False):
print('Skipping kernel build as requested by environment variable.')
return
# Override number of threads for parallel building
if os.environ.get('XSK_N_THREADS', None) is not None:
n_threads = int(os.environ['XSK_N_THREADS'])
else:
n_threads = None
# Modern setuptools/pip don't guarantee that building happens in the
# package directory. As we need the kernel generation code here, we
# add the package directory to the path to be able to import it.
import sys
sys.path.append(str(Path(__file__).parent))
from xsuite.prebuild_kernels import regenerate_kernels
# Regenerate the kernels in the build directory so that they are
# included in the binary wheel, unless we're installing in editable
# mode, in which case the kernels are generated in the source (default).
if not self.editable_mode:
location = str(Path(self.build_lib) / 'xsuite/lib')
regenerate_kernels(location=location, n_threads=n_threads)
else:
regenerate_kernels(n_threads=n_threads)
setup(
distclass=BinaryDistribution,
cmdclass={
'build_ext': CustomBuildExtCommand,
}
)