The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 03:37 UTC
| 1 | // @effect-diagnostics nodeBuiltinImport:off globalConsole:off - Runs in electron-builder's Node afterPack hook. |
| 2 | import * as NodeChildProcess from "node:child_process"; |
| 3 | import * as NodeFSP from "node:fs/promises"; |
| 4 | import * as NodePath from "node:path"; |
| 5 | import * 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. |
| 8 | const GLIBC_BUILD_CEILING = "2.39"; |
| 9 | const readelf = NodeUtil.promisify(NodeChildProcess.execFile); |
| 10 | |
| 11 | function 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 | |
| 21 | export 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 | // The loader only warns when a WEAK version is missing, so it does not raise the floor. |
| 29 | if (!name || /\bFlags:[^:]*\bWEAK\b/.test(line)) continue; |
| 30 | // DT_RELR's named ABI requirement was introduced in glibc 2.36. |
| 31 | const version = |
| 32 | name === "GLIBC_ABI_DT_RELR" ? "2.36" : /^GLIBC_(\d+(?:\.\d+)+)$/.exec(name)?.[1]; |
| 33 | if (!version) throw new Error(`Unhandled glibc requirement: ${name}`); |
| 34 | if (!minimum || compareVersions(version, minimum) > 0) minimum = version; |
| 35 | } |
| 36 | return minimum; |
| 37 | } |
| 38 | |
| 39 | export async function scanGlibcNeeds(appOutDir: string): Promise<{ |
| 40 | minimum: string; |
| 41 | elfCount: number; |
| 42 | }> { |
| 43 | let minimum: string | undefined; |
| 44 | let elfCount = 0; |
| 45 | async function walk(directory: string): Promise<void> { |
| 46 | for (const entry of await NodeFSP.readdir(directory, { withFileTypes: true })) { |
| 47 | const file = NodePath.join(directory, entry.name); |
| 48 | if (entry.isDirectory()) { |
| 49 | await walk(file); |
| 50 | continue; |
| 51 | } |
| 52 | if (!entry.isFile()) continue; |
| 53 | const handle = await NodeFSP.open(file, "r"); |
| 54 | let isElf: boolean; |
| 55 | try { |
| 56 | const magic = Buffer.alloc(4); |
| 57 | await handle.read(magic, 0, 4, 0); |
| 58 | isElf = magic.equals(Buffer.from([0x7f, 0x45, 0x4c, 0x46])); |
| 59 | } finally { |
| 60 | await handle.close(); |
| 61 | } |
| 62 | if (!isElf) continue; |
| 63 | elfCount++; |
| 64 | try { |
| 65 | // readelf inspects either release architecture without executing the binary. |
| 66 | const { stdout, stderr } = await readelf("readelf", ["--wide", "--version-info", file], { |
| 67 | env: { ...process.env, LC_ALL: "C" }, |
| 68 | maxBuffer: 16 * 1024 * 1024, |
| 69 | }); |
| 70 | if (stderr.trim()) throw new Error(stderr.trim()); |
| 71 | const required = parseGlibcNeeds(stdout); |
| 72 | if (required && compareVersions(required, GLIBC_BUILD_CEILING) > 0) { |
| 73 | throw new Error( |
| 74 | `requires GLIBC_${required}, exceeding build ceiling ${GLIBC_BUILD_CEILING}`, |
| 75 | ); |
| 76 | } |
| 77 | if (required && (!minimum || compareVersions(required, minimum) > 0)) minimum = required; |
| 78 | } catch (cause) { |
| 79 | throw new Error( |
| 80 | `Cannot validate glibc compatibility of ${file}: ${cause instanceof Error ? cause.message : String(cause)}`, |
| 81 | { cause }, |
| 82 | ); |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | await walk(appOutDir); |
| 87 | if (elfCount === 0 || !minimum) throw new Error(`No glibc requirements found in ${appOutDir}`); |
| 88 | console.log( |
| 89 | `[linux-glibc] ${elfCount} ELF files; requires glibc >= ${minimum}; build ceiling ${GLIBC_BUILD_CEILING}`, |
| 90 | ); |
| 91 | return { minimum, elfCount }; |
| 92 | } |