Repository navigation
Expand file tree
/
Copy pathlinux_build_python_instance.py
More file actions
320 lines (286 loc) · 12.3 KB
/
Copy pathlinux_build_python_instance.py
File metadata and controls
320 lines (286 loc) · 12.3 KB
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
from __future__ import (
annotations,
) # Needed for python 3.9 to support python 3.10 style typehints
import copy
import os
from os import environ
from pathlib import Path
from build_python_instance_base import BuildPythonInstanceBase
import shutil
from wheel_builder_utils import _remove_tree
class LinuxBuildPythonInstance(BuildPythonInstanceBase):
def clone(self):
# Pattern for generating a deep copy of the current object state as a new build instance
cls = self.__class__
new = cls.__new__(cls)
new.__dict__ = copy.deepcopy(self.__dict__)
return new
def get_pixi_environment_name(self):
# The pixi environment name is the same as the manylinux version
# and is related to the environment setups defined in pixi.toml
# in the root of this git directory that contains these scripts.
return self.platform_env
def prepare_build_env(self) -> None:
# #############################################
# ### Setup build tools
self.package_env_config["USE_TBB"] = "ON"
self.package_env_config["TBB_DIR"] = (
self.build_dir_root / "build" / "oneTBB-prefix" / "lib" / "cmake" / "TBB"
)
# The interpreter is provided; ensure basic tools are available
self.venv_paths()
self.update_venv_itk_build_configurations()
if self.package_env_config["ARCH"] == "x64":
target_triple = "x86_64-linux-gnu"
elif self.package_env_config["ARCH"] in ("aarch64", "arm64"):
target_triple = "aarch64-linux-gnu"
elif self.package_env_config["ARCH"] == "x86":
target_triple = "i686-linux-gnu"
else:
target_triple = f"{self.package_env_config['ARCH']}-linux-gnu"
target_arch = self.package_env_config["ARCH"]
self.cmake_compiler_configurations.set(
"CMAKE_CXX_COMPILER_TARGET:STRING", target_triple
)
itk_binary_build_name: Path = (
self.build_dir_root
/ "build"
/ f"ITK-{self.platform_env}-{self.get_pixi_environment_name()}_{target_arch}"
)
self.cmake_itk_source_build_configurations.set(
"ITK_BINARY_DIR:PATH", itk_binary_build_name
)
# Keep values consistent with prior quoting behavior
# self.cmake_compiler_configurations.set("CMAKE_CXX_FLAGS:STRING", "-O3 -DNDEBUG")
# self.cmake_compiler_configurations.set("CMAKE_C_FLAGS:STRING", "-O3 -DNDEBUG")
def post_build_fixup(self) -> None:
manylinux_ver: str | None = self.package_env_config.get(
"MANYLINUX_VERSION", None
)
if manylinux_ver:
# Repair all produced wheels with auditwheel for packages with so elements (starts with itk_)
whl = None
# cp39-cp39-linux itk_segmentation-6.0.0b2-cp39-cp39-linux_x86_64.whl
cp_prefix: str = self.platform_env.replace("py", "cp").replace(".", "")
binary_wheel_glob_pattern: str = "itk_*-*{cp_prefix}*-linux_*.whl"
dist_path: Path = self.build_dir_root / "dist"
for whl in dist_path.glob(binary_wheel_glob_pattern):
if str(whl).startswith("itk-"):
print(
f"Skipping the itk-meta wheel that has nothing to fixup {whl}"
)
continue
self.fixup_wheel(str(whl))
del whl
# Retag meta-wheel: Special handling for the itk meta wheel to adjust tag
# auditwheel does not process this "metawheel" correctly since it does not
# have any native SO's.
meta_wheel_glob_pattern: str = "itk-*-*{cp_prefix}*-linux_*.whl"
for metawhl in dist_path.glob(meta_wheel_glob_pattern):
# Unpack, edit WHEEL tag, repack
metawheel_dir = self.build_dir_root / "metawheel"
metawheel_dir.mkdir(parents=True, exist_ok=True)
self.echo_check_call(
[
self.package_env_config["PYTHON_EXECUTABLE"],
"-m",
"wheel",
"unpack",
"--dest",
str(metawheel_dir),
str(metawhl),
]
)
# Find unpacked dir
unpacked_dirs = list(metawheel_dir.glob("itk-*/itk*.dist-info/WHEEL"))
for wheel_file in unpacked_dirs:
content = wheel_file.read_text(encoding="utf-8").splitlines()
base = metawhl.name
if len(manylinux_ver) > 0:
base = metawhl.name.replace(
"linux", f"manylinux{manylinux_ver}"
)
tag = Path(base).stem
new = []
for line in content:
if line.startswith("Tag: "):
new.append(f"Tag: {tag}")
else:
new.append(line)
wheel_file.write_text("\n".join(new) + "\n", encoding="utf-8")
for fixed_dir in metawheel_dir.glob("itk-*"):
metawheel_dist = self.build_dir_root / "metawheel-dist"
metawheel_dist.mkdir(parents=True, exist_ok=True)
self.echo_check_call(
[
self.package_env_config["PYTHON_EXECUTABLE"],
"-m",
"wheel",
"pack",
"--dest",
str(metawheel_dist),
str(fixed_dir),
]
)
# Move and clean
for new_whl in metawheel_dist.glob("*.whl"):
shutil.move(
str(new_whl),
str((self.build_dir_root / "dist") / new_whl.name),
)
# Remove old and temp
try:
metawhl.unlink()
except OSError:
pass
_remove_tree(metawheel_dir)
_remove_tree(metawheel_dist)
def final_import_test(self) -> None:
self._final_import_test_fn(self.platform_env, Path(self.dist_dir))
def fixup_wheel(self, filepath, lib_paths: str = "") -> None:
# Use auditwheel to repair wheels and set manylinux tags
manylinux_ver = self.package_env_config.get("MANYLINUX_VERSION", "")
if len(manylinux_ver) > 1:
plat = None
if self.package_env_config["ARCH"] == "x64" and manylinux_ver:
plat = f"manylinux{manylinux_ver}_x86_64"
cmd = [
self.package_env_config["PYTHON_EXECUTABLE"],
"-m",
"auditwheel",
"repair",
]
if plat:
cmd += ["--plat", plat]
cmd += [
str(filepath),
"-w",
str(self.build_dir_root / "dist"),
]
# Provide LD_LIBRARY_PATH for oneTBB and common system paths
extra_lib = str(
self.package_env_config["IPP_SUPERBUILD_BINARY_DIR"].parent
/ "oneTBB-prefix"
/ "lib"
)
env = dict(self.package_env_config)
env["LD_LIBRARY_PATH"] = ":".join(
[
env.get("LD_LIBRARY_PATH", ""),
extra_lib,
"/usr/lib64",
"/usr/lib",
]
)
print(f'RUNNING WITH PATH {os.environ["PATH"]}')
env["PATH"] = os.environ["PATH"]
self.echo_check_call(cmd, env=env)
print(
"Building outside of manylinux environment does not require wheel fixups."
)
return
def post_build_cleanup(self) -> None:
"""Clean build artifacts
Actions (leaving dist/ intact):
- remove oneTBB-prefix (symlink or dir)
- remove ITKPythonPackage/, tools/, _skbuild/, build/
- remove top-level *.egg-info
- remove ITK-* build tree and tarballs
- if ITK_MODULE_PREQ is set ("org/name@ref:org2/name2@ref2"), remove cloned module dirs
"""
base = Path(self.package_env_config["IPP_SOURCE_DIR"])
# Helper to remove arbitrary paths (files/dirs) quietly
def rm(tree_path: Path):
try:
_remove_tree(tree_path)
except Exception:
pass
# 1) unlink oneTBB-prefix if it's a symlink or file
tbb_prefix_dir = base / "oneTBB-prefix"
try:
if tbb_prefix_dir.is_symlink() or tbb_prefix_dir.is_file():
tbb_prefix_dir.unlink(missing_ok=True) # type: ignore[arg-type]
elif tbb_prefix_dir.exists():
# If it is a directory (not expected), remove tree
rm(tbb_prefix_dir)
except Exception:
pass
# 2) standard build directories
for rel in ("ITKPythonPackage", "tools", "_skbuild", "build"):
rm(base / rel)
# 3) egg-info folders at top-level
for p in base.glob("*.egg-info"):
rm(p)
# 4) ITK build tree and tarballs
target_arch = self.package_env_config["ARCH"]
for p in base.glob(f"ITK-*-{self.package_env_config}_{target_arch}"):
rm(p)
# Tarballs
for p in base.glob(f"ITKPythonBuilds-{self.package_env_config}*.tar.zst"):
rm(p)
# 5) Optional module prerequisites cleanup (ITK_MODULE_PREQ)
# Format: "InsightSoftwareConsortium/ITKModuleA@v1.0:Kitware/ITKModuleB@sha" -> remove repo names
itk_preq = self.package_env_config.get("ITK_MODULE_PREQ") or environ.get(
"ITK_MODULE_PREQ", ""
)
if itk_preq:
for entry in itk_preq.split(":"):
entry = entry.strip()
if not entry:
continue
try:
module_name = entry.split("@", 1)[0].split("/", 1)[1]
except Exception:
continue
rm(base / module_name)
def build_tarball(self):
self.create_posix_tarball()
def venv_paths(self) -> None:
"""Resolve virtualenv tool paths.
platform_env may be a name under IPP_SOURCE_DIR/venvs or an absolute/relative path to a venv.
"""
primary_python_base_dir = Path(
self.package_env_config["PYTHON_EXECUTABLE"]
).parent.parent
# if True:
# self._pip_uninstall_itk_wildcard(self.package_env_config["PYTHON_EXECUTABLE"])
(
python_executable,
python_include_dir,
python_library,
venv_bin_path,
venv_base_dir,
) = self.find_unix_exectable_paths(primary_python_base_dir)
self.venv_info_dict = {
"python_executable": python_executable,
"python_include_dir": python_include_dir,
"python_library": python_library,
"venv_bin_path": venv_bin_path,
"venv_base_dir": venv_base_dir,
"python_root_dir": primary_python_base_dir,
}
def discover_python_venvs(
self, platform_os_name: str, platform_architechure: str
) -> list[str]:
names = []
# Discover virtualenvs under project 'venvs' folder
def _discover_ipp_venvs() -> list[str]:
venvs_dir = self.build_dir_root / "venvs"
if not venvs_dir.exists():
return []
names.extend([p.name for p in venvs_dir.iterdir() if p.is_dir()])
# Sort for stable order
return sorted(names)
# Discover available manylinux CPython installs under /opt/python
def _discover_manylinuxlocal_pythons() -> list[str]:
base = Path("/opt/python")
if not base.exists():
return []
names.extend([p.name for p in base.iterdir() if p.is_dir()])
return sorted(names)
default_platform_envs = (
_discover_manylinuxlocal_pythons() + _discover_ipp_venvs()
)
return default_platform_envs
def _final_import_test_fn(self, platform_env, param):
pass