-
Notifications
You must be signed in to change notification settings - Fork 14
/
setup.py
203 lines (169 loc) · 6.42 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
#!/usr/bin/env python
# Copyright (c) 2021, Aryan Roy
#
# Distributed under the 3-clause BSD license, see accompanying file LICENSE
# or https://github.com/scikit-hep/fastjet for details.
from setuptools import setup # isort:skip
# Available at setup time due to pyproject.toml
from pybind11.setup_helpers import Pybind11Extension # isort:skip
import os
import pathlib
import shutil
import subprocess
import sys
import sysconfig
import urllib.request
import zipfile
import setuptools.command.build_ext
import setuptools.command.install
CGAL_ZIP = "https://github.com/CGAL/cgal/releases/download/v5.6/CGAL-5.6-library.zip"
DIR = pathlib.Path(__file__).parent.resolve()
FASTJET = DIR / "extern" / "fastjet-core"
FASTJET_CONTRIB = DIR / "extern" / "fastjet-contrib"
PYTHON = DIR / "src" / "fastjet"
OUTPUT = PYTHON / "_fastjet_core"
# Clean up transient directories to allow for rebuilds during development
if (DIR / "build").exists():
shutil.rmtree(DIR / "build")
if OUTPUT.exists():
shutil.rmtree(OUTPUT)
LIBS = [
"fastjet",
"fastjettools",
"siscone",
"siscone_spherical",
"fastjetplugins",
"fastjetcontribfragile",
]
def get_version() -> str:
g = {}
with open(PYTHON / "version.py") as f:
exec(f.read(), g)
return g["__version__"]
class FastJetBuild(setuptools.command.build_ext.build_ext):
def build_extensions(self):
if not OUTPUT.exists():
zip_filename = DIR / pathlib.Path(CGAL_ZIP).parts[-1]
with urllib.request.urlopen(CGAL_ZIP) as http_obj:
with open(zip_filename, "wb") as file_obj:
shutil.copyfileobj(http_obj, file_obj)
with zipfile.ZipFile(zip_filename) as zip_obj:
cgal_dir = DIR / zip_obj.namelist()[0]
zip_obj.extractall(DIR)
# Patch for segfault of LimitedWarning
# For more info see https://github.com/scikit-hep/fastjet/pull/131
subprocess.run(
["patch", "src/ClusterSequence.cc", DIR / "patch_clustersequence.txt"],
cwd=FASTJET,
)
# RPATH is set for shared libraries in the following locations:
# * fastjet/
# * fastjet/_fastjet_core/lib/
# * fastjet/_fastjet_core/lib/python*/site-packages/
_rpath = "'$$ORIGIN/_fastjet_core/lib:$$ORIGIN:$$ORIGIN/../..'"
env = os.environ.copy()
env["PYTHON"] = sys.executable
env["PYTHON_INCLUDE"] = f'-I{sysconfig.get_path("include")}'
env["CXXFLAGS"] = "-O3 -Bstatic -lgmp -Bdynamic -std=c++17"
env["LDFLAGS"] = env.get("LDFLAGS", "") + f" -Wl,-rpath,{_rpath}"
env["ORIGIN"] = "$ORIGIN" # if evaluated, it will still be '$ORIGIN'
args = [
f"--prefix={OUTPUT}",
"--enable-thread-safety",
"--disable-auto-ptr",
"--enable-allcxxplugins",
"--enable-cgal-header-only",
"--enable-cgal",
f"--with-cgaldir={cgal_dir}",
"--enable-swig",
"--enable-pyext",
f'LDFLAGS={env["LDFLAGS"]}',
]
try:
subprocess.run(
["./autogen.sh"] + args,
cwd=FASTJET,
env=env,
check=True,
)
except Exception:
subprocess.run(["cat", "config.log"], cwd=FASTJET, check=True)
raise
env = os.environ.copy()
env["CXX"] = env.get("CXX", "g++")
env["LDFLAGS"] = env.get("LDFLAGS", "") + f" -Wl,-rpath,{_rpath}"
env["ORIGIN"] = "$ORIGIN" # if evaluated, it will still be '$ORIGIN'
subprocess.run(["make", "-j"], cwd=FASTJET, env=env, check=True)
subprocess.run(["make", "install"], cwd=FASTJET, env=env, check=True)
subprocess.run(
[
"./configure",
f"--fastjet-config={FASTJET}/fastjet-config",
f'CXX={env["CXX"]}',
"CXXFLAGS=-O3 -Bstatic -Bdynamic -std=c++17",
f'LDFLAGS={env["LDFLAGS"]}',
],
cwd=FASTJET_CONTRIB,
env=env,
check=True,
)
subprocess.run(["make", "-j"], cwd=FASTJET_CONTRIB, env=env, check=True)
subprocess.run(
["make", "install"], cwd=FASTJET_CONTRIB, env=env, check=True
)
subprocess.run(
["make", "fragile-shared"], cwd=FASTJET_CONTRIB, env=env, check=True
)
subprocess.run(
["make", "fragile-shared-install"],
cwd=FASTJET_CONTRIB,
env=env,
check=True,
)
setuptools.command.build_ext.build_ext.build_extensions(self)
class FastJetInstall(setuptools.command.install.install):
def run(self):
fastjetdir = pathlib.Path(f"{self.build_lib}/fastjet")
shutil.copytree(OUTPUT, fastjetdir / "_fastjet_core", symlinks=True)
make = "make"
if sys.platform == "darwin":
make = "gmake"
pythondir = pathlib.Path(
subprocess.check_output(
f"""{make} -f Makefile --eval='print-pythondir:
\t@echo $(pythondir)
' print-pythondir""",
shell=True,
cwd=FASTJET / "pyinterface",
universal_newlines=True,
).strip()
)
pyexecdir = pathlib.Path(
subprocess.check_output(
f"""{make} -f Makefile --eval='print-pyexecdir:
\t@echo $(pyexecdir)
' print-pyexecdir""",
shell=True,
cwd=FASTJET / "pyinterface",
universal_newlines=True,
).strip()
)
shutil.copyfile(pythondir / "fastjet.py", fastjetdir / "_swig.py")
shutil.copyfile(pyexecdir / "_fastjet.so", fastjetdir / "_fastjet.so")
setuptools.command.install.install.run(self)
ext_modules = [
Pybind11Extension(
"fastjet._ext",
["src/_ext.cpp"],
cxx_std=11,
include_dirs=[str(OUTPUT / "include")],
library_dirs=[str(OUTPUT / "lib")],
runtime_library_dirs=["$ORIGIN/_fastjet_core/lib"],
libraries=LIBS,
),
]
setup(
version=get_version(),
ext_modules=ext_modules,
cmdclass={"build_ext": FastJetBuild, "install": FastJetInstall},
)