-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnative_library.py
More file actions
334 lines (288 loc) · 12.4 KB
/
Copy pathnative_library.py
File metadata and controls
334 lines (288 loc) · 12.4 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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
"""Build one reusable native BLAS or LAPACK library from the copied examples."""
from __future__ import annotations
import argparse
import concurrent.futures
from dataclasses import dataclass
import hashlib
import os
from pathlib import Path
import shutil
import subprocess
import sys
import sysconfig
from collections.abc import Sequence
EXAMPLES_ROOT = Path(__file__).resolve().parent
BLAS_SOURCE_ROOT = EXAMPLES_ROOT / "blas" / "native"
LAPACK_SOURCE_ROOT = EXAMPLES_ROOT / "lapack" / "native"
NATIVE_CACHE_ENV = "PRIK_REAL_LIBRARY_NATIVE_CACHE_DIR"
NATIVE_JOBS_ENV = "PRIK_REAL_LIBRARY_NATIVE_JOBS"
NATIVE_CACHE_VERSION = "copyable-examples-v3-link-dependencies"
NATIVE_MODULE_SOURCE_STEMS = frozenset({"la_constants", "la_xisnan"})
NATIVE_LINK_DEPENDENCIES = {
"blas": (),
"lapack": ("-llapack", "-lblas"),
}
DEFAULT_NATIVE_COMPILE_JOB_LIMIT = 8
FORTRAN_SUFFIXES = frozenset({".f", ".f90", ".f95", ".f03", ".f08", ".for", ".f77", ".ftn"})
SUPPORTED_LIBRARIES = ("blas", "lapack")
@dataclass(frozen=True)
class NativeLibrary:
"""A complete native library compiled once for both wrapper generators."""
name: str
shared_library: Path
archive: Path
cache_dir: Path
module_dir: Path
sources: tuple[Path, ...]
compiler: str
def require_tool(name: str) -> str:
"""Return an executable path or explain the missing example prerequisite."""
executable = shutil.which(name)
if executable is None:
raise RuntimeError(f"{name} is required to build the native-library examples")
return executable
def compiler_identity(compiler: str) -> str:
"""Return the resolved compiler path and its first version line."""
result = subprocess.run( # nosec B603 - explicit local compiler identity probe
(compiler, "--version"),
capture_output=True,
text=True,
check=False,
)
first_line = result.stdout.splitlines()[0] if result.stdout else compiler
return f"{Path(compiler).resolve()}: {first_line}"
def library_sources(library: str) -> tuple[Path, ...]:
"""Return the authoritative implementation sources for one named library."""
if library not in SUPPORTED_LIBRARIES:
raise ValueError(f"unknown reference library {library!r}; choose from {', '.join(SUPPORTED_LIBRARIES)}")
root = BLAS_SOURCE_ROOT if library == "blas" else LAPACK_SOURCE_ROOT
return tuple(sorted(path for path in root.iterdir() if path.is_file() and path.suffix.lower() in FORTRAN_SUFFIXES))
def native_sources(library: str) -> tuple[Path, ...]:
"""Return sources in a safe compilation order, including LAPACK's BLAS dependencies."""
if library not in SUPPORTED_LIBRARIES:
return library_sources(library)
if library == "blas":
return library_sources("blas")
lapack_sources = library_sources("lapack")
module_sources = tuple(
source
for source in (
LAPACK_SOURCE_ROOT / "la_constants.f90",
LAPACK_SOURCE_ROOT / "la_xisnan.F90",
)
if source.is_file()
)
module_source_set = set(module_sources)
lapack_rest = tuple(source for source in lapack_sources if source not in module_source_set)
lapack_stems = {source.stem.lower() for source in lapack_sources}
blas_dependencies = tuple(source for source in library_sources("blas") if source.stem.lower() not in lapack_stems)
return (*module_sources, *lapack_rest, *blas_dependencies)
def native_cache_root() -> Path:
"""Return the configured cache, or a build directory inside the copied examples."""
configured = os.environ.get(NATIVE_CACHE_ENV)
if configured:
return Path(configured).expanduser().resolve()
return EXAMPLES_ROOT / ".build" / "native"
def native_compile_jobs() -> int:
"""Return the validated native compilation parallelism."""
configured = os.environ.get(NATIVE_JOBS_ENV)
if configured:
try:
jobs = int(configured)
except ValueError as error:
raise ValueError(f"{NATIVE_JOBS_ENV} must be a positive integer, got {configured!r}") from error
if jobs < 1:
raise ValueError(f"{NATIVE_JOBS_ENV} must be a positive integer, got {configured!r}")
return jobs
return max(1, min(os.cpu_count() or 1, DEFAULT_NATIVE_COMPILE_JOB_LIMIT))
def _native_platform_identity() -> str:
return f"{sysconfig.get_platform()}:{os.name}:{sys.maxsize}"
def _source_digest(source: Path) -> str:
digest = hashlib.sha256()
with source.open("rb") as stream:
for chunk in iter(lambda: stream.read(1024 * 1024), b""):
digest.update(chunk)
return digest.hexdigest()
def _native_cache_key(library: str, compiler: str, sources: tuple[Path, ...]) -> str:
digest = hashlib.sha256()
for value in (NATIVE_CACHE_VERSION, library, compiler_identity(compiler), _native_platform_identity()):
digest.update(value.encode())
digest.update(b"\0")
for source in sources:
digest.update(source.relative_to(EXAMPLES_ROOT).as_posix().encode())
digest.update(b":")
digest.update(_source_digest(source).encode())
digest.update(b"\0")
return digest.hexdigest()[:24]
def _cached_object_path(objects_dir: Path, source: Path) -> Path:
return objects_dir / source.relative_to(EXAMPLES_ROOT).with_suffix(".o")
def _compile_source(compiler: str, source: Path, native_object: Path, module_dir: Path) -> None:
native_object.parent.mkdir(parents=True, exist_ok=True)
subprocess.run( # nosec B603 - explicit compiler and copied example source
(
compiler,
"-O0",
"-fPIC",
"-c",
str(source),
"-o",
str(native_object),
"-J",
str(module_dir),
"-I",
str(module_dir),
),
check=True,
)
def _compile_independent_sources(
compiler: str,
sources: tuple[Path, ...],
objects_dir: Path,
module_dir: Path,
jobs: int,
) -> None:
if not sources:
return
with concurrent.futures.ThreadPoolExecutor(max_workers=min(jobs, len(sources))) as executor:
futures = [
executor.submit(
_compile_source,
compiler,
source,
_cached_object_path(objects_dir, source),
module_dir,
)
for source in sources
]
for future in concurrent.futures.as_completed(futures):
future.result()
def _cached_objects(
cache_dir: Path,
sources: tuple[Path, ...],
compiler: str,
jobs: int,
) -> tuple[Path, ...]:
objects_dir = cache_dir / "objects"
modules_dir = cache_dir / "modules"
complete = cache_dir / "objects.complete"
objects = tuple(_cached_object_path(objects_dir, source) for source in sources)
module_sources = tuple(source for source in sources if source.stem.lower() in NATIVE_MODULE_SOURCE_STEMS)
modules = tuple(modules_dir / f"{source.stem.lower()}.mod" for source in module_sources)
if (
complete.is_file()
and modules_dir.is_dir()
and all(native_object.is_file() for native_object in objects)
and all(module.is_file() for module in modules)
):
return objects
process_suffix = str(os.getpid())
temporary_objects = cache_dir / f"objects.{process_suffix}.tmp"
temporary_modules = cache_dir / f"modules.{process_suffix}.tmp"
shutil.rmtree(temporary_objects, ignore_errors=True)
shutil.rmtree(temporary_modules, ignore_errors=True)
temporary_objects.mkdir(parents=True)
temporary_modules.mkdir(parents=True)
module_source_set = set(module_sources)
independent_sources = tuple(source for source in sources if source not in module_source_set)
for source in module_sources:
_compile_source(compiler, source, _cached_object_path(temporary_objects, source), temporary_modules)
_compile_independent_sources(compiler, independent_sources, temporary_objects, temporary_modules, jobs)
shutil.rmtree(objects_dir, ignore_errors=True)
temporary_objects.rename(objects_dir)
shutil.rmtree(modules_dir, ignore_errors=True)
temporary_modules.rename(modules_dir)
complete.write_text(f"{NATIVE_CACHE_VERSION}\n", encoding="utf-8")
(cache_dir / "archive.complete").unlink(missing_ok=True)
(cache_dir / "shared.complete").unlink(missing_ok=True)
return tuple(_cached_object_path(objects_dir, source) for source in sources)
def _cached_archive(cache_dir: Path, library: str, objects: tuple[Path, ...], archiver: str) -> Path:
archive = cache_dir / f"libprik_full_{library}.a"
complete = cache_dir / "archive.complete"
if complete.is_file() and archive.is_file():
return archive
temporary_archive = cache_dir / f"{archive.name}.{os.getpid()}.tmp"
temporary_archive.unlink(missing_ok=True)
subprocess.run( # nosec B603 - explicit archiver and compiled example objects
(archiver, "rcs", str(temporary_archive), *(str(native_object) for native_object in objects)),
check=True,
)
os.replace(temporary_archive, archive)
complete.write_text(f"{NATIVE_CACHE_VERSION}\n", encoding="utf-8")
return archive
def _cached_shared_library(cache_dir: Path, library: str, archive: Path, compiler: str) -> Path:
shared_library = cache_dir / f"libprik_full_{library}.so"
complete = cache_dir / "shared.complete"
if complete.is_file() and shared_library.is_file():
return shared_library
temporary_shared = cache_dir / f"{shared_library.name}.{os.getpid()}.tmp"
temporary_shared.unlink(missing_ok=True)
subprocess.run( # nosec B603 - explicit compiler and compiled example archive
(
compiler,
"-shared",
"-o",
str(temporary_shared),
"-Wl,--whole-archive",
str(archive),
"-Wl,--no-whole-archive",
*NATIVE_LINK_DEPENDENCIES[library],
),
check=True,
)
os.replace(temporary_shared, shared_library)
complete.write_text(f"{NATIVE_CACHE_VERSION}\n", encoding="utf-8")
return shared_library
def build_reference_library(
library: str,
*,
cache_root: Path | None = None,
compiler: str | None = None,
archiver: str | None = None,
jobs: int | None = None,
) -> NativeLibrary:
"""Build on a cache miss and return one complete reusable native library."""
selected_compiler = compiler or require_tool("gfortran")
selected_archiver = archiver or require_tool("ar")
selected_sources = native_sources(library)
selected_jobs = jobs if jobs is not None else native_compile_jobs()
if selected_jobs < 1:
raise ValueError(f"jobs must be a positive integer, got {selected_jobs!r}")
selected_cache_root = (cache_root or native_cache_root()).resolve()
cache_dir = selected_cache_root / f"{library}-{_native_cache_key(library, selected_compiler, selected_sources)}"
cache_dir.mkdir(parents=True, exist_ok=True)
objects = _cached_objects(cache_dir, selected_sources, selected_compiler, selected_jobs)
archive = _cached_archive(cache_dir, library, objects, selected_archiver)
shared_library = _cached_shared_library(cache_dir, library, archive, selected_compiler)
return NativeLibrary(
name=library,
shared_library=shared_library,
archive=archive,
cache_dir=cache_dir,
module_dir=cache_dir / "modules",
sources=selected_sources,
compiler=selected_compiler,
)
def linker_name(shared_library: Path) -> str:
"""Return the `-l` name for a shared library produced by this module."""
name = shared_library.name
if not name.startswith("lib") or ".so" not in name:
raise ValueError(f"expected a lib*.so native library, got {shared_library}")
return name[3 : name.index(".so")]
def main(argv: Sequence[str] | None = None) -> int:
"""Build a copied example's native library and print its reusable path."""
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("library", choices=SUPPORTED_LIBRARIES)
parser.add_argument("--cache-dir", type=Path, default=None)
parser.add_argument("--compiler", default=None)
parser.add_argument("--jobs", type=int, default=None)
args = parser.parse_args(argv)
build = build_reference_library(
args.library,
cache_root=args.cache_dir,
compiler=args.compiler,
jobs=args.jobs,
)
print(build.shared_library)
return 0
if __name__ == "__main__":
raise SystemExit(main())