Repository navigation
Expand file tree
/
Copy pathpackage-linux-gpu-stack.py
More file actions
executable file
·848 lines (736 loc) · 28.5 KB
/
Copy pathpackage-linux-gpu-stack.py
File metadata and controls
executable file
·848 lines (736 loc) · 28.5 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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
#!/usr/bin/env python3
"""Package the standalone Linux GPU and desktop dependency stack for Navop.
The archive is assembled from unpacked distribution RPMs rather than from the
running system. That keeps packaging architecture independent: a single
x86_64 CI runner can produce both the x86_64 and the aarch64 archive, because
nothing in here executes the collected libraries, it only reads their ELF
headers.
"""
from __future__ import annotations
import argparse
from dataclasses import dataclass
import fnmatch
import glob
import hashlib
import json
import os
from pathlib import Path
import re
import shlex
import shutil
import subprocess
import sys
from typing import Iterable, NoReturn
# Navop reaches EGL through dlopen, so nothing in navop's own DT_NEEDED closes
# over the graphics stack. These entry points are collected explicitly; the
# recursive expansion then pulls in the rest of Mesa (the swrast driver NEEDs
# libLLVM, for example).
GPU_STACK_ENTRY_LIBRARIES = (
"libEGL.so.1",
"libEGL_mesa.so.0",
)
# Mesa loads its DRI driver by file name from a compile-time search directory,
# which is why the driver is resolved by directory scan instead of by SONAME.
GPU_STACK_DRI_DRIVERS = (
"swrast_dri.so",
"kms_swrast_dri.so",
)
# GLVND reads this to discover the Mesa vendor library installed alongside it.
GPU_STACK_EGL_VENDOR_CONFIGURATION = "/usr/share/glvnd/egl_vendor.d/50_mesa.json"
# Navop runs on the host dynamic loader, so the C runtime must come from the
# host. Bundling these would shadow the system glibc with a foreign copy, which
# is precisely what this archive must never do.
HOST_PROVIDED_LIBRARIES = (
"ld-linux-aarch64.so.1",
"ld-linux-x86-64.so.2",
"libc.so.6",
"libdl.so.2",
"libm.so.6",
"libpthread.so.0",
"libresolv.so.2",
"librt.so.1",
)
HOST_PROVIDED_PATTERNS = (
"libnss_*.so.2",
"linux-vdso.so.1",
)
# GTK, WebKitGTK and the GLib family integrate with the running desktop: themes,
# the accessibility bus, dconf, input methods, font configuration. Shipping one
# distribution's copy of that stack onto another distribution's desktop is not
# supportable, and it would dwarf the renderer this archive exists to deliver.
# These names are only reached by a binary built with `embedded-webview`, which is
# off by default on every platform -- see crates/navop_app/Cargo.toml -- so keeping them listed
# is what makes the classification hold for a self-built binary that opts the
# feature back in.
HOST_DESKTOP_PATTERNS = (
"libwebkit2gtk-4*.so.*",
"libjavascriptcoregtk-4*.so.*",
"libgtk-3.so.*",
"libgtk-4.so.*",
"libgdk-3.so.*",
"libgdk-4.so.*",
"libsoup-2.4.so.*",
"libsoup-3.0.so.*",
"libglib-2.0.so.*",
"libgobject-2.0.so.*",
"libgio-2.0.so.*",
"libgmodule-2.0.so.*",
"libgthread-2.0.so.*",
"libpango-1.0.so.*",
"libpangocairo-1.0.so.*",
"libcairo.so.*",
"libcairo-gobject.so.*",
"libharfbuzz.so.*",
"libatk-1.0.so.*",
"libatk-bridge-2.0.so.*",
"libgdk_pixbuf-2.0.so.*",
"libepoxy.so.*",
)
@dataclass(frozen=True)
class TargetConfig:
machine: str
architecture_label: str
libdir: str
dri_dir: str
library_directories: tuple[str, ...]
dri_driver_directories: tuple[str, ...]
# The archive is built from a RHEL 8 generation distribution, where both
# architectures use /usr/lib64. That path is what Mesa's compile-time DRI
# directory points at, so the payload keeps it verbatim; the installer
# registers the directory with the loader on hosts that do not search it.
TARGET_CONFIGS = {
"aarch64-unknown-linux-gnu": TargetConfig(
machine="AArch64",
architecture_label="aarch64",
libdir="/usr/lib64",
dri_dir="/usr/lib64/dri",
library_directories=("/usr/lib64", "/lib64", "/usr/lib", "/usr/local/lib"),
dri_driver_directories=(
"/usr/lib64/dri",
"/usr/lib/dri",
"/usr/lib/aarch64-linux-gnu/dri",
),
),
"x86_64-unknown-linux-gnu": TargetConfig(
machine="Advanced Micro Devices X86-64",
architecture_label="x86_64",
libdir="/usr/lib64",
dri_dir="/usr/lib64/dri",
library_directories=("/usr/lib64", "/lib64", "/usr/lib", "/usr/local/lib"),
dri_driver_directories=(
"/usr/lib64/dri",
"/usr/lib/dri",
"/usr/lib/x86_64-linux-gnu/dri",
),
),
}
GL_GROUP = "gl"
CLIENT_GROUP = "client"
@dataclass
class CollectedLibrary:
soname: str
source: Path
group: str
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(
description=(
"Collect the Mesa software renderer and the desktop client "
"libraries Navop needs on a host without a usable GPU driver, and "
"lay them out as an installable dependency archive."
)
)
parser.add_argument(
"--binary",
required=True,
type=Path,
help=(
"release Navop binary; its DT_NEEDED closure determines the "
"desktop client libraries that must be shipped"
),
)
parser.add_argument(
"--library-root",
required=True,
type=Path,
help="root of the unpacked distribution file tree to collect from",
)
parser.add_argument(
"--rpm-directory",
required=True,
type=Path,
help="directory holding the downloaded RPMs, used for package metadata",
)
parser.add_argument("--output", required=True, type=Path)
parser.add_argument(
"--installer-source",
type=Path,
default=Path("script/linux-gpu-stack-install.sh"),
help="installer script copied to the archive root as install.sh",
)
parser.add_argument(
"--target",
default="aarch64-unknown-linux-gnu",
choices=tuple(TARGET_CONFIGS),
)
parser.add_argument(
"--glibc-baseline",
default="2.28",
help=(
"maximum GLIBC symbol version allowed in any bundled library; the "
"archive must stay loadable on the oldest host Navop itself runs on"
),
)
parser.add_argument(
"--source-distribution",
default="",
help="human readable build host description recorded in the manifest",
)
return parser.parse_args()
def fail(message: str) -> NoReturn:
raise SystemExit(f"Error: {message}")
def run(command: list[str], *, check: bool = True) -> subprocess.CompletedProcess[str]:
return subprocess.run(
command,
check=check,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
)
def require_command(command: str) -> None:
if shutil.which(command) is None:
fail(f"required command is not installed: {command}")
def readelf(path: Path, *arguments: str) -> str:
result = run(["readelf", *arguments, str(path)], check=False)
if result.returncode != 0:
fail(f"readelf failed for {path}: {result.stderr.strip()}")
return result.stdout
def elf_machine(path: Path) -> str:
header = readelf(path, "-hW")
match = re.search(r"^\s*Machine:\s*(.+?)\s*$", header, re.MULTILINE)
if match is None:
fail(f"cannot determine ELF machine for {path}")
return match.group(1)
def version_tuple(version: str) -> tuple[int, ...]:
return tuple(int(component) for component in version.split("."))
def highest_glibc_version(path: Path) -> str | None:
versions = re.findall(
r"\bGLIBC_(\d+(?:\.\d+)+)\b",
readelf(path, "--version-info", "-W"),
)
if not versions:
return None
return max(versions, key=version_tuple)
def verify_glibc_baseline(path: Path, maximum: str) -> str | None:
highest = highest_glibc_version(path)
if highest is None:
return None
if version_tuple(highest) > version_tuple(maximum):
fail(
f"{path} requires GLIBC_{highest}, above the supported "
f"GLIBC_{maximum} archive baseline; the dependency stack must come "
"from a distribution at or below that baseline"
)
return highest
def dynamic_metadata(path: Path) -> list[str]:
return re.findall(
r"\(NEEDED\).*?Shared library:\s*\[([^\]]+)\]",
readelf(path, "-dW"),
)
def is_host_provided(soname: str) -> bool:
if soname in HOST_PROVIDED_LIBRARIES:
return True
return any(fnmatch.fnmatch(soname, pattern) for pattern in HOST_PROVIDED_PATTERNS)
def is_host_desktop_library(soname: str) -> bool:
return any(
fnmatch.fnmatch(soname, pattern) for pattern in HOST_DESKTOP_PATTERNS
)
def inside(root: Path, path: Path) -> bool:
try:
path.relative_to(root)
except ValueError:
return False
return True
class LibraryIndex:
"""Indexes every shared object below an unpacked distribution tree.
A fixed list of search directories is not sufficient. RHEL 8 keeps its
compat LLVM runtime in /usr/lib64/llvm17/lib, a directory no dynamic loader
configuration searches, yet Mesa's single gallium driver links against the
libLLVM-17.so it holds. Indexing the tree by file name keeps the packager
independent of wherever a distribution decides to park a library, while the
configured directories still decide which copy wins when a name exists in
more than one place.
"""
def __init__(self, root: Path) -> None:
self.root = root
self._by_name: dict[str, list[Path]] = {}
for path in root.rglob("*"):
name = path.name
if ".so" not in name or not path.is_file():
continue
resolved = path.resolve()
if not inside(root, resolved) or not resolved.is_file():
continue
# Keep the path as packaged rather than its target: RPM file lists
# name the symlink (/usr/lib64/libgcc_s.so.1), so ownership lookup
# and the archive layout both have to use the same spelling.
self._by_name.setdefault(name, []).append(path)
def candidates(self, name: str, directories: Iterable[str]) -> list[Path]:
found = self._by_name.get(name)
if not found:
return []
wanted = list(directories)
preferred: list[Path] = []
remaining: list[Path] = []
for path in found:
parent = "/" + path.relative_to(self.root).parent.as_posix()
if parent in wanted:
preferred.append(path)
else:
remaining.append(path)
preferred.sort(
key=lambda path: wanted.index(
"/" + path.relative_to(self.root).parent.as_posix()
)
)
remaining.sort(
key=lambda path: (len(path.relative_to(self.root).parts), str(path))
)
return preferred + remaining
def resolve_library(
soname: str,
*,
index: LibraryIndex,
directories: Iterable[str],
machine: str,
) -> Path | None:
for candidate in index.candidates(soname, directories):
if elf_machine(candidate) == machine:
return candidate
return None
def resolve_dri_driver(
name: str,
*,
index: LibraryIndex,
directories: Iterable[str],
) -> Path | None:
# Driver directories hold one real module plus per-driver symlinks, so the
# index resolves each name through to the file it points at.
for candidate in index.candidates(name, directories):
return candidate
return None
# RHEL 8 is a merged-/usr distribution: /lib64 is a symlink to /usr/lib64, and
# the RPM file lists still name the pre-merge path. libgcc is the visible case,
# declaring /lib64/libgcc_s.so.1 while the extracted tree stores
# /usr/lib64/libgcc_s.so.1. Ownership lookup has to accept both spellings.
USR_MERGE_PREFIXES = (
("/lib64/", "/usr/lib64/"),
("/lib/", "/usr/lib/"),
)
def path_spellings(path: str) -> list[str]:
variants = {path}
for merged, canonical in USR_MERGE_PREFIXES:
if path.startswith(merged):
variants.add(canonical + path[len(merged):])
elif path.startswith(canonical):
variants.add(merged + path[len(canonical):])
return sorted(variants)
class RpmIndex:
"""Maps unpacked file paths back to the RPM that owns them."""
def __init__(self, directory: Path) -> None:
self.owners: dict[str, str] = {}
self.archives: dict[str, Path] = {}
for rpm in sorted(directory.glob("*.rpm")):
name = self.field(rpm, "%{NAME}")
if not name:
continue
self.archives[name] = rpm
for path in self.paths(rpm):
for spelling in path_spellings(path):
self.owners.setdefault(spelling, name)
@staticmethod
def field(rpm: Path, query_format: str) -> str | None:
result = run(["rpm", "-qp", "--qf", query_format, str(rpm)], check=False)
if result.returncode != 0:
return None
value = result.stdout.strip()
return value or None
@staticmethod
def paths(rpm: Path) -> list[str]:
result = run(["rpm", "-qlp", str(rpm)], check=False)
if result.returncode != 0:
return []
return [line.strip() for line in result.stdout.splitlines() if line.strip()]
def package_of(self, library_root: Path, path: Path) -> str | None:
relative = path.relative_to(library_root)
for spelling in path_spellings(f"/{relative.as_posix()}"):
owner = self.owners.get(spelling)
if owner is not None:
return owner
return None
def sha256(path: Path) -> str:
digest = hashlib.sha256()
with path.open("rb") as stream:
for chunk in iter(lambda: stream.read(1024 * 1024), b""):
digest.update(chunk)
return digest.hexdigest()
def collect_closure(
roots: list[tuple[Path, str]],
*,
library_root: Path,
index: LibraryIndex,
target: TargetConfig,
host_requirements: dict[str, str] | None = None,
) -> dict[str, CollectedLibrary]:
"""Expand DT_NEEDED from every root, remembering which group reached it.
Client roots are processed first so a library needed by both Navop and
Mesa is classified as a client library. That matters because client
libraries are always reconciled against the host, while the graphics group
is skipped wholesale when the host already renders.
"""
collected: dict[str, CollectedLibrary] = {}
scanned: set[Path] = set()
queue: list[tuple[Path, str]] = list(roots)
while queue:
consumer, group = queue.pop(0)
resolved_consumer = consumer.resolve()
if resolved_consumer in scanned:
continue
scanned.add(resolved_consumer)
if elf_machine(resolved_consumer) != target.machine:
fail(f"architecture mismatch in dependency closure: {resolved_consumer}")
for soname in dynamic_metadata(resolved_consumer):
if is_host_provided(soname) or soname in collected:
continue
if is_host_desktop_library(soname):
if host_requirements is not None:
host_requirements.setdefault(soname, str(resolved_consumer))
continue
source = resolve_library(
soname,
index=index,
directories=target.library_directories,
machine=target.machine,
)
if source is None:
fail(
f"missing required shared library {soname} for "
f"{resolved_consumer}; download the RPM that provides it"
)
collected[soname] = CollectedLibrary(
soname=soname,
source=source,
group=group,
)
queue.append((source, group))
return collected
# Distributions disagree about where a package keeps its license text. RHEL 8
# uses /usr/share/licenses/<name> for some packages and /usr/share/doc/<name>
# for others, and either may carry a version suffix.
LICENSE_DIRECTORY_CANDIDATES = ("usr/share/licenses", "usr/share/doc")
def license_files_for(library_root: Path, package: str) -> list[Path]:
found: dict[str, Path] = {}
for relative in LICENSE_DIRECTORY_CANDIDATES:
base = library_root / relative
if not base.is_dir():
continue
for directory in sorted(base.glob(f"{glob.escape(package)}*")):
if not directory.is_dir():
continue
for path in sorted(directory.rglob("*")):
if path.is_file() and path.name not in found:
found[path.name] = path
return [found[name] for name in sorted(found)]
def package_records(
packaged: dict[str, Path],
*,
library_root: Path,
index: RpmIndex,
license_directory: Path,
) -> list[dict[str, object]]:
by_package: dict[str, set[str]] = {}
for relative, source in packaged.items():
owner = index.package_of(library_root, source)
if owner is None:
fail(
"cannot publish a bundled library without RPM ownership "
f"metadata: {source}"
)
by_package.setdefault(owner, set()).add(relative)
license_directory.mkdir(parents=True, exist_ok=True)
records: list[dict[str, object]] = []
for package in sorted(by_package):
declared = index.archives.get(package)
license_files = license_files_for(library_root, package)
if license_files:
destination = license_directory / package
destination.mkdir(parents=True, exist_ok=True)
for source in license_files:
shutil.copy2(source, destination / source.name)
else:
print(
f"warning: {package} ships no license files under "
"/usr/share/licenses or /usr/share/doc; recording the "
"declared license only",
file=sys.stderr,
)
records.append(
{
"package": package,
"version": (
RpmIndex.field(declared, "%{VERSION}-%{RELEASE}")
if declared
else "unknown"
),
"license": (
RpmIndex.field(declared, "%{LICENSE}") if declared else "unknown"
),
"license_files": sorted(p.name for p in license_files),
"files": sorted(by_package[package]),
}
)
return records
def manifest_files(output: Path, excluded: set[Path]) -> list[dict[str, object]]:
records: list[dict[str, object]] = []
for path in sorted(output.rglob("*")):
if not path.is_file() or path in excluded:
continue
records.append(
{
"path": path.relative_to(output).as_posix(),
"size": path.stat().st_size,
"sha256": sha256(path),
}
)
return records
def main() -> None:
args = parse_args()
target = TARGET_CONFIGS[args.target]
for command in ("readelf", "rpm"):
require_command(command)
library_root = args.library_root.resolve()
if not library_root.is_dir():
fail(f"library root does not exist: {library_root}")
rpm_directory = args.rpm_directory.resolve()
if not rpm_directory.is_dir():
fail(f"RPM directory does not exist: {rpm_directory}")
binary = args.binary.resolve()
if not binary.is_file():
fail(f"release binary does not exist: {binary}")
if elf_machine(binary) != target.machine:
fail(
f"dependency archive for {args.target} expects {target.machine}, "
f"got {elf_machine(binary)}"
)
verify_glibc_baseline(binary, args.glibc_baseline)
output = args.output.resolve()
repository_root = Path(__file__).resolve().parent.parent
if output in (Path("/"), repository_root):
fail(f"refusing to replace unsafe output directory: {output}")
installer_source = args.installer_source
if not installer_source.is_absolute():
installer_source = repository_root / installer_source
if not installer_source.is_file():
fail(f"installer script does not exist: {installer_source}")
index = RpmIndex(rpm_directory)
library_index = LibraryIndex(library_root)
host_requirements: dict[str, str] = {}
# Client libraries first: they are always reconciled against the host.
libraries = collect_closure(
[(binary, CLIENT_GROUP)],
library_root=library_root,
index=library_index,
target=target,
host_requirements=host_requirements,
)
graphics_roots: list[tuple[Path, str]] = []
graphics_entries: dict[str, Path] = {}
for soname in GPU_STACK_ENTRY_LIBRARIES:
if soname in libraries:
continue
source = resolve_library(
soname,
index=library_index,
directories=target.library_directories,
machine=target.machine,
)
if source is None:
fail(
f"missing {soname} required by the graphics stack; download "
"the RPM that provides the Mesa EGL runtime"
)
graphics_entries[soname] = source
graphics_roots.append((source, GL_GROUP))
drivers: dict[str, Path] = {}
for name in GPU_STACK_DRI_DRIVERS:
source = resolve_dri_driver(
name,
index=library_index,
directories=target.dri_driver_directories,
)
if source is None:
if name == GPU_STACK_DRI_DRIVERS[0]:
fail(
f"missing {name} required by the software renderer; "
"download the RPM that provides the Mesa DRI drivers"
)
continue
drivers[name] = source
graphics_roots.append((source, GL_GROUP))
for soname, entry in collect_closure(
graphics_roots,
library_root=library_root,
index=library_index,
target=target,
host_requirements=host_requirements,
).items():
libraries.setdefault(soname, entry)
# The entry points are payload in their own right. Navop reaches EGL through
# dlopen, so nothing in the binary's own DT_NEEDED closure pulls them in;
# the walk above therefore only ever visits them as roots and would never
# ship them.
for soname, source in graphics_entries.items():
libraries.setdefault(
soname,
CollectedLibrary(soname=soname, source=source, group=GL_GROUP),
)
vendor_source = library_root / GPU_STACK_EGL_VENDOR_CONFIGURATION.lstrip("/")
if not vendor_source.is_file():
fail(
"missing EGL vendor configuration for the software renderer: "
f"{GPU_STACK_EGL_VENDOR_CONFIGURATION}"
)
payload = output / "payload"
packaged: dict[str, Path] = {}
manifest_rows: list[tuple[str, str, str, str]] = []
stored_by_digest: dict[str, str] = {}
def stage(relative: str, source: Path, kind: str, group: str) -> None:
destination = payload / relative
destination.parent.mkdir(parents=True, exist_ok=True)
digest = sha256(source)
owner = stored_by_digest.get(digest)
if owner is not None:
# Distributions hard-link one real module under several names: RHEL
# 8 exposes six DRI drivers that are all the same 19 MiB inode.
# Filing the duplicate as a relative symlink keeps the archive from
# carrying that payload twice, and the installer dereferences it.
destination.symlink_to(os.path.relpath(payload / owner, destination.parent))
else:
shutil.copy2(source, destination)
destination.chmod(0o644)
stored_by_digest[digest] = relative
packaged[relative] = source
manifest_rows.append((kind, relative, group, digest))
for soname, entry in sorted(libraries.items()):
stage(f"{target.libdir.lstrip('/')}/{soname}", entry.source, "library", entry.group)
for name, source in sorted(drivers.items()):
stage(f"{target.dri_dir.lstrip('/')}/{name}", source, "driver", GL_GROUP)
vendor_relative = GPU_STACK_EGL_VENDOR_CONFIGURATION.lstrip("/")
stage(vendor_relative, vendor_source, "vendor", GL_GROUP)
# Every bundled shared object must stay inside the glibc baseline, because
# the host loader resolves them directly now.
baselines: dict[str, str] = {}
for relative, source in sorted(packaged.items()):
if relative == vendor_relative:
continue
highest = verify_glibc_baseline(source, args.glibc_baseline)
if highest is not None:
baselines[relative] = highest
shutil.copy2(installer_source, output / "install.sh")
(output / "install.sh").chmod(0o755)
distribution = args.source_distribution or "RHEL 8 generation distribution"
# install.sh sources this file, so values are shell-quoted: the source
# distribution string carries spaces and parentheses.
(output / "install.env").write_text(
"".join(
f"{key}={shlex.quote(value)}\n"
for key, value in (
("NAVOP_GPU_STACK_ARCH", target.architecture_label),
("NAVOP_GPU_STACK_TARGET", args.target),
("NAVOP_GPU_STACK_LIBDIR", target.libdir),
("NAVOP_GPU_STACK_DRI_DIR", target.dri_dir),
(
"NAVOP_GPU_STACK_EGL_VENDOR_DIR",
str(Path(GPU_STACK_EGL_VENDOR_CONFIGURATION).parent),
),
("NAVOP_GPU_STACK_GLIBC_BASELINE", args.glibc_baseline),
("NAVOP_GPU_STACK_SOURCE_DISTRIBUTION", distribution),
)
),
encoding="utf-8",
)
(output / "manifest.tsv").write_text(
"".join(
f"{kind}\t{relative}\t{group}\t{digest}\n"
for kind, relative, group, digest in manifest_rows
),
encoding="utf-8",
)
records = package_records(
packaged,
library_root=library_root,
index=index,
license_directory=output / "licenses",
)
(output / "runtime-packages.txt").write_text(
"".join(
f"{record['package']}\t{record['version']}\t{','.join(record['files'])}\n"
for record in records
),
encoding="utf-8",
)
manifest_path = output / "manifest.json"
graphics_libraries = sorted(
soname for soname, entry in libraries.items() if entry.group == GL_GROUP
)
client_libraries = sorted(
soname for soname, entry in libraries.items() if entry.group == CLIENT_GROUP
)
manifest = {
"schema_version": 1,
"product": "navop",
"component": "linux-gpu-stack",
"target": args.target,
"architecture": target.architecture_label,
"source_distribution": distribution,
"binary_glibc_baseline": args.glibc_baseline,
"libdir": target.libdir,
"dri_dir": target.dri_dir,
"egl_vendor_dir": str(Path(GPU_STACK_EGL_VENDOR_CONFIGURATION).parent),
"graphics_libraries": graphics_libraries,
"client_libraries": client_libraries,
"host_required_libraries": sorted(host_requirements),
"dri_drivers": sorted(drivers),
"egl_vendor_configurations": [Path(GPU_STACK_EGL_VENDOR_CONFIGURATION).name],
"glibc_requirements": baselines,
"packages": records,
"host_interfaces": [
"Linux kernel and procfs",
"X11 or Wayland display sockets",
"GPU devices and host vendor drivers",
"system fonts and font configuration",
"the distribution's GTK stack when the binary links it, plus "
"WebKitGTK for a binary built with the opt-in embedded webview "
"feature",
],
"files": manifest_files(output, {manifest_path}),
}
manifest_path.write_text(
json.dumps(manifest, indent=2, sort_keys=True) + "\n",
encoding="utf-8",
)
payload_size = sum(
path.stat().st_size for path in payload.rglob("*") if path.is_file()
)
print(
f"Packaged {len(libraries)} libraries ({len(graphics_libraries)} graphics, "
f"{len(client_libraries)} client) and {len(drivers)} DRI drivers into {output}"
)
if host_requirements:
print(
f"Left to the host ({len(host_requirements)} desktop libraries): "
+ ", ".join(sorted(host_requirements))
)
print(f"Uncompressed payload: {payload_size / (1024 * 1024):.1f} MiB")
print(f"Built on: {distribution} (GLIBC_{args.glibc_baseline} baseline)")
if __name__ == "__main__":
main()