-
Notifications
You must be signed in to change notification settings - Fork 2
/
amalgamate.mjs
executable file
·67 lines (56 loc) · 2.18 KB
/
amalgamate.mjs
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
#!/usr/bin/env node
import * as fsp from "fs/promises";
import * as pathLib from "path";
import * as assert from "assert/strict";
function amalgamatedPath(extension) {
return pathLib.resolve(`lighttpd.amalgamated.${extension}`);
}
const commands = JSON.parse((await fsp.readFile("compile_commands.json")).toString())
.filter(e => pathLib.relative(".", e.directory) === "build")
.filter(e => ![
// These are for other binaries and so have their own `main`s, which conflict.
"lemon.c",
// "lighttpd-angel.c",
].includes(pathLib.relative("./src", e.file)))
;
const includes = [
// For some reason, this needs to be first in order to define `XXH32`.
"src/algo_xxhash.c",
...commands.map(e => pathLib.relative(".", e.file))
]
.map(path => `#include "${path}"`)
.join("\n")
;
const flags = [...new Set(
commands.flatMap(e => (e.arguments ?? e.command.split(/ +/g))
.slice(
["cc"].length, // Slice off the compiler invocation (not a flag).
-["-o", "obj", "-c", "src"].length, // Slice off the file-specific args that aren't flags.
)
)
)];
const directories = [...new Set(commands.map(e => e.directory))];
assert.equal(directories.length, 1);
const directory = directories[0];
const amalgamated = {
exe: {
directory,
arguments: ["cc", ...flags, "-o", amalgamatedPath("exe"), amalgamatedPath("c"), "-lpcre2-8", "-ldl", "-Wl,-export-dynamic"],
file: amalgamatedPath("c"),
},
ii: {
// A post-preprocessing file, just to check exactly what's included or not.
directory,
arguments: ["cc", ...flags, "-o", amalgamatedPath("ii"), "-E", amalgamatedPath("c")],
file: amalgamatedPath("c"),
},
};
const amalgamatedCommands = [amalgamated.exe];
await fsp.writeFile(amalgamatedPath("c"), includes);
await fsp.writeFile("amalgamated.compile_commands.json", JSON.stringify(amalgamatedCommands, null, 4));
function toShellCommand(cmd) {
const command = cmd.arguments && cmd.arguments.join(" ") || cmd.command;
return `(cd "${cmd.directory}" && ${command})`;
}
console.log(toShellCommand(amalgamated.ii));
console.log(toShellCommand(amalgamated.exe));