-
Notifications
You must be signed in to change notification settings - Fork 73
/
goma.js
241 lines (210 loc) · 7.16 KB
/
goma.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
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
const childProcess = require('child_process');
const crypto = require('crypto');
const fs = require('fs');
const path = require('path');
const { unzipSync } = require('cross-zip');
const { color, fatal } = require('./logging');
const depot = require('./depot-tools');
const os = require('os');
const { getIsArm } = require('./arm');
const { deleteDir } = require('./paths');
const gomaDir = path.resolve(__dirname, '..', '..', 'third_party', 'goma');
const gomaGnFile = path.resolve(__dirname, '..', '..', 'third_party', 'goma.gn');
const gomaShaFile = path.resolve(__dirname, '..', '..', 'third_party', 'goma', '.sha');
const gomaBaseURL = 'https://dev-cdn.electronjs.org/goma-clients';
const gomaLoginFile = path.resolve(gomaDir, 'last-known-login');
let gomaPlatform = process.platform;
if (gomaPlatform === 'darwin' && getIsArm()) {
gomaPlatform = 'darwin-arm64';
}
if (gomaPlatform === 'linux' && process.arch === 'arm64') {
gomaPlatform = 'linux-arm64';
}
const GOMA_PLATFORM_SHAS = {
darwin: 'acdddfb6346d3fce1a0630e9880669082bd64f095a0be81eb3ad54cc14e4ea4c',
'darwin-arm64': 'd7b44eb9988c81e8b69c14e8c1ad6458bd9fc3f6ee0b360812d605da62d5721a',
linux: '293d1768c4f1fca239aaad2a347f76dab1856415512e24085afb4b1aeadcdcbf',
'linux-arm64': '5d1c3bf476097de6db9176e4d88ad3549661c1544bdc14a1701fa46fd70df2e2',
win32: '8644a9443d03675cc113a26b42a20e97e41e4f4d152ff84140a9a93ea6830a08',
};
const MSFT_GOMA_PLATFORM_SHAS = {
darwin: '143a1357c7d308d77400901cc4e8b64733f2205564408dfb6131c8718ee56f7a',
'darwin-arm64': '9b507a022d62112fbfd904e50f01f447d7894d31cc6a676bec2bb85ea8d3a336',
linux: 'b0e257d401d4dec8079988bba99a63e8425f40b046ce841527dd1dc6de54b28b',
'linux-arm64': '66e250374b6f5cfb73c31ed53ed92f28e4dbbd98ca58a5aad8063bc8f83f9bb5',
win32: 'ebb9279f47fef6417340f9d0e0ec4a1a39cf69c5712168d5976fe1829f192531',
};
const isSupportedPlatform = !!GOMA_PLATFORM_SHAS[gomaPlatform];
function downloadAndPrepareGoma(config) {
if (!isSupportedPlatform) return;
if (!fs.existsSync(path.dirname(gomaDir))) {
fs.mkdirSync(path.dirname(gomaDir));
}
const gomaGnContents = `goma_dir = "${gomaDir}"\nuse_goma = true`;
if (!fs.existsSync(gomaGnFile) || fs.readFileSync(gomaGnFile, 'utf8') !== gomaGnContents) {
console.log(`Writing new goma.gn file ${color.path(gomaGnFile)}`);
fs.writeFileSync(gomaGnFile, gomaGnContents);
}
let sha = GOMA_PLATFORM_SHAS[gomaPlatform];
if (config && config.gomaSource === 'msft') {
sha = MSFT_GOMA_PLATFORM_SHAS[gomaPlatform];
}
if (
fs.existsSync(gomaShaFile) &&
fs.readFileSync(gomaShaFile, 'utf8') === sha &&
!process.env.ELECTRON_FORGE_GOMA_REDOWNLOAD
)
return sha;
const filename = {
darwin: 'goma-mac.tgz',
'darwin-arm64': 'goma-mac-arm64.tgz',
linux: 'goma-linux.tgz',
'linux-arm64': 'goma-linux-arm64.tgz',
win32: 'goma-win.zip',
}[gomaPlatform];
if (fs.existsSync(path.resolve(gomaDir, 'goma_ctl.py'))) {
depot.execFileSync(config, 'python3', ['goma_ctl.py', 'stop'], {
cwd: gomaDir,
stdio: ['ignore'],
});
}
const tmpDownload = path.resolve(gomaDir, '..', filename);
// Clean Up
deleteDir(gomaDir);
deleteDir(tmpDownload);
const downloadURL = `${gomaBaseURL}/${sha}/${filename}`;
console.log(`Downloading ${color.cmd(downloadURL)} into ${color.path(tmpDownload)}`);
const { status } = childProcess.spawnSync(
process.execPath,
[path.resolve(__dirname, '..', 'download.js'), downloadURL, tmpDownload],
{
stdio: 'inherit',
},
);
if (status !== 0) {
deleteDir(tmpDownload);
fatal(`Failure while downloading goma`);
}
const hash = crypto
.createHash('SHA256')
.update(fs.readFileSync(tmpDownload))
.digest('hex');
if (hash !== sha) {
console.error(
`${color.err} Got hash for downloaded file ${color.cmd(hash)} which did not match ${color.cmd(
sha,
)}. Halting now`,
);
deleteDir(tmpDownload);
process.exit(1);
}
const targetDir = path.resolve(tmpDownload, '..');
if (filename.endsWith('.tgz')) {
const result = childProcess.spawnSync('tar', ['zxvf', filename], {
cwd: targetDir,
});
if (result.status !== 0) {
fatal('Failed to extract goma');
}
} else {
unzipSync(tmpDownload, targetDir);
}
deleteDir(tmpDownload);
fs.writeFileSync(gomaShaFile, sha);
return sha;
}
function gomaIsAuthenticated(config) {
const lastKnownLogin = getLastKnownLoginTime();
// Assume if we authed in the last 12 hours it is still valid
if (lastKnownLogin && Date.now() - lastKnownLogin.getTime() < 1000 * 60 * 60 * 12) return true;
let loggedInInfo;
try {
loggedInInfo = depot.execFileSync(config, 'python3', ['goma_auth.py', 'info'], {
cwd: gomaDir,
stdio: ['ignore'],
});
} catch {
return false;
}
return /^Login as (\w+\s\w+)$/.test(loggedInInfo.toString().trim());
}
function authenticateGoma(config) {
if (!isSupportedPlatform) return;
downloadAndPrepareGoma(config);
if (!gomaIsAuthenticated(config)) {
const { status, error } = depot.spawnSync(config, 'python3', ['goma_auth.py', 'login'], {
cwd: gomaDir,
stdio: 'inherit',
env: {
AGREE_NOTGOMA_TOS: '1',
},
});
if (status !== 0) {
let errorMsg = `Failed to run command:`;
if (status !== null) errorMsg += `\n Exit Code: "${status}"`;
if (error) errorMsg += `\n ${error}`;
fatal(errorMsg, status);
}
recordGomaLoginTime();
}
}
function getLastKnownLoginTime() {
if (!fs.existsSync(gomaLoginFile)) return null;
const contents = fs.readFileSync(gomaLoginFile);
return new Date(parseInt(contents, 10));
}
function clearGomaLoginTime() {
if (!fs.existsSync(gomaLoginFile)) return;
fs.unlinkSync(gomaLoginFile);
}
function recordGomaLoginTime() {
fs.writeFileSync(gomaLoginFile, `${Date.now()}`);
}
function ensureGomaStart(config) {
// GomaCC is super fast and we can assume that a 0 exit code means we are good-to-go
const gomacc = path.resolve(gomaDir, process.platform === 'win32' ? 'gomacc.exe' : 'gomacc');
const { status } = childProcess.spawnSync(gomacc, ['port', '2']);
if (status === 0) return;
// Set number of subprocs to equal number of CPUs for MacOS
let subprocs = {};
if (process.platform === 'darwin') {
const { length: cpus } = os.cpus();
subprocs = {
GOMA_MAX_SUBPROCS: cpus.toString(),
GOMA_MAX_SUBPROCS_LOW: cpus.toString(),
};
}
depot.execFileSync(config, 'python3', ['goma_ctl.py', 'ensure_start'], {
cwd: gomaDir,
env: {
...gomaEnv(config),
...subprocs,
},
// Inherit stdio on Windows because otherwise this never terminates
stdio: process.platform === 'win32' ? 'inherit' : ['ignore'],
});
}
function gomaCIEnv(config) {
if (!config && process.env.CI) {
return {
// Automatically start the compiler proxy when it dies in CI, random flakes be random
GOMA_START_COMPILER_PROXY: 'true',
};
}
return {};
}
function gomaEnv(config) {
return {
...gomaCIEnv(config),
};
}
module.exports = {
auth: authenticateGoma,
ensure: ensureGomaStart,
dir: gomaDir,
downloadAndPrepare: downloadAndPrepareGoma,
gnFilePath: gomaGnFile,
env: gomaEnv,
clearGomaLoginTime,
recordGomaLoginTime,
};