-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathbuildBindings.ts
66 lines (58 loc) · 1.93 KB
/
buildBindings.ts
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
import fs from "fs";
import path from "path";
import { exec } from "./exec";
import { testBindings } from "./testBindings";
import { assertSupportedSwigVersion } from "./swig";
import {
ensureDirFromFilepath,
findBindingsFile,
BINDINGS_DIR,
BLST_WRAP_CPP_PREBUILD,
} from "./paths";
export async function buildBindings(binaryPath: string) {
if (
process.env.BLST_WRAP_CPP_FORCE_BUILD &&
fs.existsSync(BLST_WRAP_CPP_PREBUILD)
) {
console.log(
`BLST_WRAP_CPP_FORCE_BUILD=true, cleaning existing BLST_WRAP_CPP_PREBUILD ${BLST_WRAP_CPP_PREBUILD}`
);
fs.unlinkSync(BLST_WRAP_CPP_PREBUILD);
}
// Make sure SWIG generated bindings are available or download from release assets
if (fs.existsSync(BLST_WRAP_CPP_PREBUILD)) {
console.log(
`BLST_WRAP_CPP_PREBUILD ${BLST_WRAP_CPP_PREBUILD} exists, SWIG will be skipped`
);
} else {
if (process.env.SWIG_SKIP_RUN) {
throw Error(`Prebuild SWIG not found ${BLST_WRAP_CPP_PREBUILD}`);
} else {
await assertSupportedSwigVersion();
console.log("Building bindings from src");
}
}
// From https://github.com/sass/node-sass/blob/769f3a6f5a3949bd8e69c6b0a5d385a9c07924b4/scripts/build.js#L59
const nodeJsExec = process.execPath;
const nodeGypExec = require.resolve(
path.join("node-gyp", "bin", "node-gyp.js")
);
console.log("Launching node-gyp", {
nodeJsExec,
nodeGypExec,
cwd: BINDINGS_DIR,
BLST_WRAP_CPP_PREBUILD,
});
await exec(nodeJsExec, [nodeGypExec, "rebuild"], {
cwd: BINDINGS_DIR,
env: { ...process.env, BLST_WRAP_CPP_PREBUILD },
});
// The output of node-gyp is not at a predictable path but various
// depending on the OS.
const bindingsFileOutput = findBindingsFile(BINDINGS_DIR);
// Copy built .node file to expected path
ensureDirFromFilepath(binaryPath);
fs.copyFileSync(bindingsFileOutput, binaryPath);
// Make sure downloaded bindings work
await testBindings(binaryPath);
}