-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcflags.ts
More file actions
executable file
·118 lines (104 loc) · 3.66 KB
/
cflags.ts
File metadata and controls
executable file
·118 lines (104 loc) · 3.66 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
#!/usr/bin/env bun
// Print the concatenated, comment-stripped clang/lld flag set for a given
// (os, arch) pair, optionally including the *-bin final-link layer.
//
// Usage:
// bun run ./cli/cflags.ts # host os, host arch, compile profile
// bun run ./cli/cflags.ts <os> <arch> # explicit (os, arch), compile profile
// bun run ./cli/cflags.ts <os> <arch> --bin # explicit (os, arch), binary profile
// bun run ./cli/cflags.ts --bin # host os, host arch, binary profile
//
// Output is whitespace-separated on a single line, suitable for:
// export CFLAGS="$(bun run ./cli/cflags.ts)"
// export LDFLAGS="$(bun run ./cli/cflags.ts --bin)"
//
// Recognized values:
// <os>: linux | darwin
// <arch>: amd64 | arm64
import { existsSync, readFileSync } from "node:fs";
import { dirname, resolve } from "node:path";
import { fileURLToPath } from "node:url";
const SUPPORTED_OS = ["linux", "darwin"] as const;
const SUPPORTED_ARCH = ["amd64", "arm64"] as const;
type Os = (typeof SUPPORTED_OS)[number];
type Arch = (typeof SUPPORTED_ARCH)[number];
const scriptDir = dirname(fileURLToPath(import.meta.url));
const root = resolve(scriptDir, "..");
function die(msg: string, code = 2): never {
process.stderr.write(`cflags: ${msg}\n`);
process.exit(code);
}
function usage(): never {
// Walk every consecutive `//` comment line after the shebang and emit
// them stripped of the leading marker. Splitting on blank lines (the
// earlier approach) drops every paragraph after the first because
// intra-block separator lines are written as `//` followed by nothing,
// which becomes an empty line once the marker is stripped.
const lines = readFileSync(fileURLToPath(import.meta.url), "utf8").split("\n");
const out: string[] = [];
for (let i = 1; i < lines.length; i++) {
if (!lines[i].startsWith("//")) break;
out.push(lines[i].replace(/^\/\/ ?/, ""));
}
process.stderr.write(out.join("\n") + "\n");
process.exit(0);
}
function hostOs(): Os {
switch (process.platform) {
case "linux":
return "linux";
case "darwin":
return "darwin";
default:
die(`unsupported host os: ${process.platform}`);
}
}
function hostArch(): Arch {
switch (process.arch) {
case "x64":
return "amd64";
case "arm64":
return "arm64";
default:
die(`unsupported host arch: ${process.arch}`);
}
}
const args = process.argv.slice(2);
let bin = false;
const positional: string[] = [];
for (const a of args) {
if (a === "--bin") bin = true;
else if (a === "-h" || a === "--help") usage();
else if (a === "--") continue;
else if (a.startsWith("-")) die(`unknown option: ${a}`);
else positional.push(a);
}
if (positional.length > 2) {
die("too many positional arguments (expected at most 2: <os> <arch>)");
}
const os = (positional[0] ?? hostOs()) as Os;
const arch = (positional[1] ?? hostArch()) as Arch;
if (!SUPPORTED_OS.includes(os)) {
die(`unsupported os: ${os} (expected: ${SUPPORTED_OS.join(" | ")})`);
}
if (!SUPPORTED_ARCH.includes(arch)) {
die(`unsupported arch: ${arch} (expected: ${SUPPORTED_ARCH.join(" | ")})`);
}
const files = [
`${root}/base.txt`,
`${root}/${os}.txt`,
`${root}/${os}-${arch}.txt`,
];
if (bin) {
files.push(`${root}/${os}-bin.txt`, `${root}/${os}-${arch}-bin.txt`);
}
const flags: string[] = [];
for (const f of files) {
if (!existsSync(f)) die(`missing profile file: ${f}`, 1);
for (const rawLine of readFileSync(f, "utf8").split("\n")) {
// Strip end-of-line comments + surrounding whitespace.
const stripped = rawLine.replace(/#.*$/, "").trim();
if (stripped) flags.push(stripped);
}
}
process.stdout.write(flags.join(" ") + "\n");