forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbuild.js
115 lines (100 loc) · 3.51 KB
/
build.js
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
const fs = require("fs");
const os = require("os");
const { spawnSync } = require("child_process");
const node = __dirname;
// Download the record/replay driver archive, using the latest version unless
//it was overridden via the environment.
let driverArchive = `${currentPlatform()}-recordreplay.tgz`;
let downloadArchive = driverArchive;
if (process.env.DRIVER_REVISION) {
downloadArchive = `${currentPlatform()}-recordreplay-${process.env.DRIVER_REVISION}.tgz`;
}
const driverFile = `${currentPlatform()}-recordreplay.${driverExtension()}`;
const driverJSON = `${currentPlatform()}-recordreplay.json`;
spawnChecked("curl", [`https://static.replay.io/downloads/${downloadArchive}`, "-o", driverArchive], { stdio: "inherit" });
spawnChecked("tar", ["xf", driverArchive]);
fs.unlinkSync(driverArchive);
// Embed the driver in the source.
const driverContents = fs.readFileSync(driverFile);
const { revision: driverRevision, date: driverDate } = JSON.parse(fs.readFileSync(driverJSON, "utf8"));
fs.unlinkSync(driverFile);
fs.unlinkSync(driverJSON);
let driverString = "";
for (let i = 0; i < driverContents.length; i++) {
driverString += `\\${driverContents[i].toString(8)}`;
}
fs.writeFileSync(
`${node}/src/node_record_replay_driver.cc`,
`
namespace node {
char gRecordReplayDriver[] = "${driverString}";
int gRecordReplayDriverSize = ${driverContents.length};
char gBuildId[] = "${computeBuildId()}";
}
`
);
const numCPUs = os.cpus().length;
if (process.env.CONFIGURE_NODE) {
spawnChecked(`${node}/configure`, [], { cwd: node, stdio: "inherit" });
}
spawnChecked("make", [`-j${numCPUs}`, "-C", "out", "BUILDTYPE=Release"], {
cwd: node,
stdio: "inherit",
env: {
...process.env,
// Disable recording when node runs as part of its compilation process.
RECORD_REPLAY_DONT_RECORD: "1",
},
});
function spawnChecked(cmd, args, options) {
const prettyCmd = [cmd].concat(args).join(" ");
console.error(prettyCmd);
const rv = spawnSync(cmd, args, options);
if (rv.status != 0 || rv.error) {
console.error(rv.error);
throw new Error(`Spawned process failed with exit code ${rv.status}`);
}
return rv;
}
function currentPlatform() {
switch (process.platform) {
case "darwin":
return "macOS";
case "linux":
return "linux";
default:
throw new Error(`Platform ${process.platform} not supported`);
}
}
function driverExtension() {
return currentPlatform() == "windows" ? "dll" : "so";
}
/**
* @returns {string} "YYYYMMDD" format of UTC timestamp of given revision.
*/
function getRevisionDate(
revision = "HEAD",
spawnOptions
) {
const dateString = spawnChecked(
"git",
["show", revision, "--pretty=%cd", "--date=iso-strict", "--no-patch"],
spawnOptions
)
.stdout.toString()
.trim();
// convert to UTC -> then get the date only
// explanations: https://github.com/replayio/backend/pull/7115#issue-1587869475
return new Date(dateString).toISOString().substring(0, 10).replace(/-/g, "");
}
/**
* WARNING: We have copy-and-pasted `computeBuildId` into all our runtimes and `backend`.
* When changing this: always keep all versions of this in sync, or else, builds will break.
*/
function computeBuildId() {
const runtimeRevision = spawnChecked("git", ["rev-parse", "--short=12", "HEAD"]).stdout.toString().trim();
const runtimeDate = getRevisionDate();
// Use the later of the two dates in the build ID.
const date = +runtimeDate >= +driverDate ? runtimeDate : driverDate;
return `${currentPlatform()}-node-${date}-${runtimeRevision}-${driverRevision}`;
}