forked from capnproto/node-capnp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.js
More file actions
executable file
·414 lines (350 loc) · 13 KB
/
build.js
File metadata and controls
executable file
·414 lines (350 loc) · 13 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
#!/usr/bin/env node
// build.js copied from node-fibers package under MIT license.
// License for node-fibers is as follows:
//
// Copyright 2011 Marcel Laverdet
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
const childProcess = require("node:child_process");
const fs = require("node:fs");
const path = require("node:path");
const yargs = require("yargs");
/*
Parse command-line arguments using `yargs`. Parsed values are stored in the `args` global.
*/
const args = yargs
.option("target-platform", {
description: "The target platform for cross-compilation.",
type: "string",
choices: ["win32", "darwin", "linux"],
// The default platform is the `npm_config_platform` environment variable if it's set, or
// `process.platform` otherwise. That environment variable will be set when a `--platform`
// argument is passed to `npm install` while installing this package as a dependency; this is
// the same cross-compilation CLI provided by Electron.
default: process.env.npm_config_platform || process.platform,
})
.option("target-arch", {
description: "The target architecture for cross-compilation.",
type: "string",
choices: ["ia32", "x64", "arm64"],
default: process.env.npm_config_arch || process.arch,
})
.option("debug", {
description: "Request a debug build from node-gyp.",
type: "boolean",
default: false,
})
.option("dist-url", {
description: "Passed unchanged to node-gyp.",
type: "string",
})
.option("target", {
description: "Passed unchanged to node-gyp.",
type: "string",
})
.strict()
.help().argv;
/*
We only support certain combinations of host and target.
(TODO: Could a Darwin host also run `build-capnp.sh`? Do we care about that?)
*/
const hostAndTargetSupported =
(process.platform === "linux" && args.targetPlatform === "linux") ||
(process.platform === "linux" && args.targetPlatform === "darwin") ||
(process.platform === "linux" && args.targetPlatform === "win32") ||
(process.platform === "win32" && args.targetPlatform === "win32");
if (!hostAndTargetSupported) {
console.error(
`We can't build this package for a ${args.targetPlatform} target ` +
`from a ${process.platform} host.`
);
process.exit(1);
}
/*
Build the capnp libraries and include files into a `./build-capnp` subdirectory.
For simplicity, when building `capnp.node`, we always start by rebuilding the capnp library from
scratch.
*/
function buildCapnp() {
let buildCapnpResult;
if (process.platform === "linux") {
/*
Build the capnp library on Linux, perhaps cross-compiling to Darwin.
*/
if (args.targetPlatform !== "linux" && args.targetPlatform !== "darwin") {
throw new Error(
`Unexpected target ${args.targetPlatform} when building on a Linux host.`
);
}
buildCapnpResult = childProcess.spawnSync(
"./build-capnp.sh",
[args.targetPlatform, args.targetArch],
{
stdio: "inherit",
}
);
} else if (process.platform === "win32") {
/*
Build the capnp library on Win32, for a Win32 target.
*/
if (args.targetPlatform !== "win32") {
throw new Error(
`Unexpected target ${args.targetPlatform} when building on a Win32 host.`
);
}
buildCapnpResult = childProcess.spawnSync("build-capnp.bat", [], {
stdio: "inherit",
// shell: true is required on Windows since node v20.12.2:
// https://nodejs.org/en/blog/vulnerability/april-2024-security-releases-2
shell: true
});
} else {
throw new Error(
`Unexpected host platform ${process.platform}; expected linux or win32.`
);
}
if (buildCapnpResult.error) {
throw buildCapnpResult.error;
}
if (buildCapnpResult.status !== 0) {
process.exit(buildCapnpResult.status);
}
}
// TODO: Ideally these paths shouldn't be hardcoded, but I can't see a clean workaround...
let capnpLibPath, capnpIncPath, patchLibPath;
if (process.platform === "linux") {
capnpLibPath = path.resolve("./build-capnp/.libs");
capnpIncPath = path.resolve("./build-capnp/capnp-root/usr/local/include");
patchLibPath = ".libs";
} else if (process.platform === "win32") {
capnpLibPath = path.resolve("./build-capnp/.libs");
capnpIncPath = path.resolve("./build-capnp/src/");
patchLibPath = undefined; // Unused, since we don't support win32->darwin builds
} else {
throw new Error(
`Building on platform ${JSON.stringify(
process.platform
)} is not yet supported.`
);
}
/*
When we expect to run the `node-gyp` compiler, we configure it by passing in additional environment
variables.
*/
let buildEnvironment = Object.assign({}, process.env);
if (process.platform === "linux" && args.targetPlatform === "linux") {
buildEnvironment.CC = "clang";
buildEnvironment.CXX = "clang++";
buildEnvironment.CFLAGS = "-I" + capnpIncPath;
buildEnvironment.CXXFLAGS = buildEnvironment.CFLAGS + " -std=c++20";
buildEnvironment.LDFLAGS = "-L" + capnpLibPath;
}
if (process.platform === "linux" && args.targetPlatform === "darwin") {
if (args.targetArch === "x64") {
buildEnvironment.CC = "o64-clang";
buildEnvironment.CXX = "o64-clang++";
buildEnvironment.PATCH_TOOL = "x86_64-apple-darwin20.4-install_name_tool";
} else if (args.targetArch == "arm64") {
buildEnvironment.CC = "oa64-clang";
buildEnvironment.CXX = "oa64-clang++";
buildEnvironment.PATCH_TOOL = "arm64-apple-darwin20.4-install_name_tool";
} else {
throw new Error(`Bad target architecture ${args.targetArch} for darwin cross-compilation`);
}
buildEnvironment.CFLAGS =
"-mmacosx-version-min=10.7 -std=c++20 " +
"-stdlib=libc++ -I" +
capnpIncPath;
buildEnvironment.CXXFLAGS = buildEnvironment.CFLAGS;
buildEnvironment.LDFLAGS = "-L" + capnpLibPath;
}
if (process.platform === "win32" && args.targetPlatform === "win32") {
buildEnvironment.CL = `/I ${capnpIncPath}`;
buildEnvironment.LINK = `/LIBPATH:${capnpLibPath}`;
}
/**
* Build `capnp.node` using `node-gyp`.
*
* @param buildEnvironment - the set of environment variables to pass to `node-gyp`
*/
function build(buildEnvironment) {
buildCapnp();
console.log(
`Building with the following environment:\n${JSON.stringify(
buildEnvironment,
null,
"\t"
)}`
);
// Collate arguments for `node-gyp configure` and `node-gyp build`.
let buildArgs = ["--verbose"];
if (args.targetArch !== process.arch) {
buildArgs.push(`--arch=${args.targetArch}`);
}
if (args.debug) {
buildArgs.push("--debug");
}
if (args.distUrl) {
buildArgs.push(`--dist-url=${args.distUrl}`);
}
if (args.target) {
buildArgs.push(`--target=${args.target}`);
}
// The arguments for `node-gyp configure` may differ slightly from those for `node-gyp build`.
let configureArgs = [...buildArgs];
if (args.targetPlatform === "darwin") {
// Pass all subsequent arguments through to `gyp` rather than `node-gyp`.
configureArgs.push("--");
// If we are cross-compiling for darwin, we must instruct gyp to generate a Makefile which is
// compatible with the MacOS tooling. We must use the -f variant (not --format=) because it
// overrides -f set by node-gyp, and node-gyp isn't clever enough to know that they're aliases.
configureArgs.push("-f", "make-mac");
}
const runNodeGyp = (taskName, taskArgs) => {
const command = process.platform === "win32" ? "node-gyp.cmd" : "node-gyp";
const commandArgs = [taskName, ...taskArgs];
console.log(
`Executing ${command} with the following arguments:\n${commandArgs}`
);
const spawnSyncOptions = {
stdio: "inherit",
env: buildEnvironment,
};
if (process.platform === "win32") {
// shell: true is required on Windows since node v20.12.2:
// https://nodejs.org/en/blog/vulnerability/april-2024-security-releases-2
spawnSyncOptions.shell = true;
}
const nodeGypResult = childProcess.spawnSync(command, commandArgs, spawnSyncOptions);
if (nodeGypResult.error) {
throw nodeGypResult.error;
}
if (nodeGypResult.status === 127) {
console.error(
"node-gyp not found! Please upgrade your install of npm! You need at least 1.1.5 " +
"(I think) and preferably 1.1.30."
);
process.exit(1);
}
if (nodeGypResult.status !== 0) {
console.error(
`Build step ${taskName} failed with exit code ${nodeGypResult.status}.`
);
process.exit(1);
}
};
runNodeGyp("clean", []);
runNodeGyp("configure", configureArgs);
runNodeGyp("build", buildArgs);
}
/**
* Move the build result to the expected location.
*
* @param builtPath - a path to the compiled `capnp.node` file
* @param buildEnvironment - a set of environment variables, perhaps including PATCH_TOOL
*/
function moveBuildResult(builtPath, buildEnvironment) {
// We do not use the V8 version in the target dir, as the V8 version we are running with
// currently may not be the same as that we are building for.
const dirName = args.targetPlatform + "-" + args.targetArch;
const installPath = path.join(__dirname, "bin", dirName, "capnp.node");
try {
fs.mkdirSync(path.join(__dirname, "bin", dirName), {
recursive: true,
});
} catch (ex) {}
try {
fs.statSync(builtPath);
} catch (ex) {
console.error("Build succeeded but target not found");
process.exit(1);
}
if (args.targetPlatform === "darwin") {
patchLibs(buildEnvironment.PATCH_TOOL, patchLibPath, builtPath);
}
try {
fs.copyFileSync(builtPath, installPath);
console.log("Installed in `" + installPath + "`");
} catch (ex) {
console.error(`All went wrong when we tried to move the target: ${ex}`);
}
}
/**
* Rewrite the dynamic library references in the produced bundle to have appropriate paths for
* relative layout within the final application bundle.
*
* @param patchTool - command name to use for editing the binary
* @param patchPath - the path to be stripped out of the binary
* @param target - The target binary to be modified
*/
function patchLibs(patchTool, patchPath, target) {
var libVersion = "1.1.0";
var libExt = "so";
var libs = ["libkj", "libkj-async", "libcapnp", "libcapnpc", "libcapnp-rpc"];
var patchArgs = libs
.flatMap((lib) => {
return [
"-change",
`${patchPath}/${lib}-${libVersion}.${libExt}`,
`@executable_path/${lib}-${libVersion}.dylib`,
];
})
.concat([target]);
console.log(
`Invoking binary patch tool ${patchTool} with the following arguments:`
);
console.log(patchArgs);
// In future if this errors, we may need to set headerpad_max_install_names in LDFLAGS too...
// (this provides more space for the dylib names in the object so that they can be rewritten
// post-link) - looks like we don't need to do this for now though?
var patchProcess = childProcess.spawnSync(patchTool, patchArgs, {
stdio: "inherit",
});
if (patchProcess.error) {
console.error(`Failed to patch binary: ${patchProcess.error}`);
process.exit(1);
} else if (patchProcess.status !== 0) {
console.error(
`Failed to patch binary: ${patchTool} exited with code ${patchProcess.status}`
);
process.exit(1);
} else {
console.log("Binary patched successfully.");
}
}
/*
If we're using a host->target configuration other than linux->win32, run the above functions to
build capnproto and `capnp.node`, then move `capnp.node` to the expected location.
In the linux->win32 case, we're stuck using a precompiled binary, because (speaking from hard
experience) `node-gyp` doesn't support that type of cross-compilation. We simply copy it from our
`prebuilt/win32-x64` subdirectory.
*/
if (process.platform === "linux" && args.targetPlatform === "win32") {
const prebuiltPath = "prebuilt/win32-x64/capnp.node";
console.log(`Using prebuilt binary ${prebuiltPath}...`);
moveBuildResult(prebuiltPath, buildEnvironment);
} else {
build(buildEnvironment);
const builtPath = path.join(
__dirname,
"build",
args.debug ? "Debug" : "Release",
"capnp.node"
);
moveBuildResult(builtPath, buildEnvironment);
}