Open History to see the last commit
1// @effect-diagnostics nodeBuiltinImport:off globalConsole:off - Runs in electron-builder's Node afterPack hook.
2import * as NodeChildProcess from "node:child_process";
3import * as NodeFSP from "node:fs/promises";
4import * as NodePath from "node:path";
5import * as NodeUtil from "node:util";
6
7// Both Linux release jobs build on Ubuntu 24.04; this is a build ceiling, not a distro support promise.
8const GLIBC_BUILD_CEILING = "2.39";
9const readelf = NodeUtil.promisify(NodeChildProcess.execFile);
10
11function compareVersions(left: string, right: string): number {
12 const a = left.split(".").map(Number);
13 const b = right.split(".").map(Number);
14 for (let i = 0; i < Math.max(a.length, b.length); i++) {
15 const difference = (a[i] ?? 0) - (b[i] ?? 0);
16 if (difference !== 0) return difference;
17 }
18 return 0;
19}
20
21export function parseGlibcNeeds(output: string): string | undefined {
22 let inNeeds = false;
23 let minimum: string | undefined;
24 for (const line of output.split("\n")) {
25 if (line.startsWith("Version ")) inNeeds = line.startsWith("Version needs section ");
26 if (!inNeeds) continue;
27 const name = /\bName:\s+(GLIBC_\S+)/.exec(line)?.[1];
28 if (!name) continue;
29 // DT_RELR's named ABI requirement was introduced in glibc 2.36.
30 const version =
31 name === "GLIBC_ABI_DT_RELR" ? "2.36" : /^GLIBC_(\d+(?:\.\d+)+)$/.exec(name)?.[1];
32 if (!version) throw new Error(`Unhandled glibc requirement: ${name}`);
33 if (!minimum || compareVersions(version, minimum) > 0) minimum = version;
34 }
35 return minimum;
36}
37
38export async function scanGlibcNeeds(appOutDir: string): Promise<{
39 minimum: string;
40 elfCount: number;
41}> {
42 let minimum: string | undefined;
43 let elfCount = 0;
44 async function walk(directory: string): Promise<void> {
45 for (const entry of await NodeFSP.readdir(directory, { withFileTypes: true })) {
46 const file = NodePath.join(directory, entry.name);
47 if (entry.isDirectory()) {
48 await walk(file);
49 continue;
50 }
51 if (!entry.isFile()) continue;
52 const handle = await NodeFSP.open(file, "r");
53 let isElf: boolean;
54 try {
55 const magic = Buffer.alloc(4);
56 await handle.read(magic, 0, 4, 0);
57 isElf = magic.equals(Buffer.from([0x7f, 0x45, 0x4c, 0x46]));
58 } finally {
59 await handle.close();
60 }
61 if (!isElf) continue;
62 elfCount++;
63 try {
64 // readelf inspects either release architecture without executing the binary.
65 const { stdout, stderr } = await readelf("readelf", ["--wide", "--version-info", file], {
66 env: { ...process.env, LC_ALL: "C" },
67 maxBuffer: 16 * 1024 * 1024,
68 });
69 if (stderr.trim()) throw new Error(stderr.trim());
70 const required = parseGlibcNeeds(stdout);
71 if (required && compareVersions(required, GLIBC_BUILD_CEILING) > 0) {
72 throw new Error(
73 `requires GLIBC_${required}, exceeding build ceiling ${GLIBC_BUILD_CEILING}`,
74 );
75 }
76 if (required && (!minimum || compareVersions(required, minimum) > 0)) minimum = required;
77 } catch (cause) {
78 throw new Error(
79 `Cannot validate glibc compatibility of ${file}: ${cause instanceof Error ? cause.message : String(cause)}`,
80 { cause },
81 );
82 }
83 }
84 }
85 await walk(appOutDir);
86 if (elfCount === 0 || !minimum) throw new Error(`No glibc requirements found in ${appOutDir}`);
87 console.log(
88 `[linux-glibc] ${elfCount} ELF files; requires glibc >= ${minimum}; build ceiling ${GLIBC_BUILD_CEILING}`,
89 );
90 return { minimum, elfCount };
91}