-
Notifications
You must be signed in to change notification settings - Fork 41
/
do_prebuild.js
349 lines (289 loc) · 9.64 KB
/
do_prebuild.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
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
const path = require('path');
const pkg = require(path.resolve('package.json'));
const { exec } = require('child_process');
const os = require('os');
const { exit } = require('process');
const fs = require('fs');
const nodeAbi = require('node-abi');
('use strict');
function run_cmd(cmd) {
return new Promise((resolve, reject) => {
exec(cmd, (error, stdout, stderr) => {
const output = {
error,
stdout,
stderr,
};
if (error) {
reject(output);
}
resolve(output);
});
});
}
async function run_prebuild(options) {
console.log(`Prebuilding for versions: ${options.versions}.`);
// Could use npx here, but Windows find it difficult to find arguments.
const bin_path_cmd = await run_cmd('npm bin').catch(output => output);
if (bin_path_cmd.error) {
throw new Error(
`Could not find prebuild. Error: ${bin_path_cmd.error}`
);
}
const bin_path = bin_path_cmd.stdout.trim();
const prebuild_path = path.join(bin_path, 'prebuild');
let prebuild_options = `--backend cmake-js -r ${options.runtime} --prepack "node do_prebuild.js --prepack-only"`;
if (options.include_regex) {
prebuild_options += ` --include-regex "${options.include_regex}"`;
}
if (options.target) {
prebuild_options += ` -t ${options.target}`;
}
if (options.arch) {
prebuild_options += ` --arch="${options.arch}"`;
}
if (options.all_versions) {
prebuild_options += ' --all';
}
let cmake_options = `-G="${options.generator}"`;
if (options.prefer_gnu) {
cmake_options += ' --prefer-gnu';
}
if (options.CC) {
cmake_options += ` --cc="${options.CC}"`;
}
if (options.CXX) {
cmake_options += ` --cxx="${options.CXX}"`;
}
const prebuild_cmd_input = `${prebuild_path} ${prebuild_options} -- ${cmake_options}`;
console.log(`Prebuild command input: ${prebuild_cmd_input}`);
const prebuild_cmd = await run_cmd(prebuild_cmd_input).catch(
output => output
);
console.log(prebuild_cmd.stdout);
console.log(prebuild_cmd.stderr);
if (prebuild_cmd.error) {
throw new Error(prebuild_cmd.error);
}
}
function moveFilesToPlatformSpecificFolder(platform, arch, currentFolder) {
if (!fs.existsSync(currentFolder)) {
fs.mkdirSync(currentFolder);
}
const newFolder = path.join(currentFolder, `${platform}-${arch}`);
if (!fs.existsSync(newFolder)) {
fs.mkdirSync(newFolder);
}
const files = fs.readdirSync(currentFolder);
for (const file of files) {
const oldFilePath = path.join(currentFolder, file);
const isDirectory = fs.lstatSync(oldFilePath).isDirectory();
if (!isDirectory) {
const newFilePath = path.join(newFolder, file);
fs.renameSync(oldFilePath, newFilePath);
}
}
}
async function decompress_local(options) {
if (options.build_from_source) {
throw new Error(
'Build from source is requested. Skipping decompress of file.'
);
}
let version = nodeAbi.getAbi(options.target, options.runtime);
let { arch } = { options };
if (!arch) {
arch = os.arch();
}
const local_filename = path.join(
options.prebuilds_dir,
`${pkg.name}-v${pkg.version}-${options.runtime}-v${version}-${options.platform}-${arch}.tar.gz`
);
if (!fs.existsSync(local_filename)) {
throw new Error(
`Unable to decompress local prebuild. File does not exist: ${local_filename}`
);
}
console.log(`Decompressing local filename: ${local_filename}.`);
const decompress_cmd = await run_cmd(`tar xvf ${local_filename}`).catch(
output => output
);
console.log(decompress_cmd.stdout);
console.log(decompress_cmd.stderr);
if (decompress_cmd.error) {
throw new Error(decompress_cmd.error);
}
moveFilesToPlatformSpecificFolder(
options.platform,
arch,
options.shared_install_dir
);
}
const PLATFORMS = { darwin: ['x64'], linux: ['x64'], win32: ['ia32', 'x64'] };
async function run_install(options) {
if (options.build_from_source) {
throw new Error(
'Build from source is requested. Skipping install from Github.'
);
}
console.log('Trying to install prebuild from developer.nordicsemi.no...');
console.log(`Runtime: ${options.runtime}`);
let version = options.target;
console.log(`Target: ${version}`);
for (const platform of Object.keys(PLATFORMS)) {
for (const arch of PLATFORMS[platform]) {
const cmd_input = `npx prebuild-install -r ${options.runtime} -t ${version} --platform ${platform} --arch ${arch} --verbose`;
console.log(`Running command: ${cmd_input}`);
/* eslint-disable no-await-in-loop */
const install_cmd = await run_cmd(cmd_input).catch(
output => output
);
console.log(install_cmd.stdout);
console.log(install_cmd.stderr)
if (install_cmd.error) {
throw new Error(install_cmd.error);
}
moveFilesToPlatformSpecificFolder(
platform,
arch,
options.shared_install_dir
);
}
}
}
function get_versions(runtime, from_abi, to_abi) {
console.log(`runtime: ${runtime}, from_abi:${from_abi}`);
if (!runtime) {
throw new Error('runtime must be specified to get_versions.');
}
const retval = [];
nodeAbi.allTargets.forEach(target => {
let filter_res = target.runtime === runtime;
if (from_abi) {
filter_res =
filter_res &&
parseInt(target.abi, 10) >= parseInt(from_abi, 10);
}
if (to_abi) {
filter_res =
filter_res && parseInt(target.abi, 10) <= parseInt(to_abi, 10);
}
if (filter_res) {
retval.push(target.target);
}
});
return retval;
}
const {
CXX,
CC,
npm_config_arch,
npm_config_build_from_source,
npm_config_runtime,
npm_config_target,
} = process.env;
const arch =
npm_config_arch || (process.platform === 'win32' ? os.arch() : undefined);
const options = {
generator: 'Ninja',
shared_install_dir: path.join(process.cwd(), 'Release'),
prefer_gnu: true,
CXX,
CC,
versions: [],
include_regex: '(.dll|.so|.dylib|.dll|.node|.hex|.json)',
arch,
platform: process.platform,
prebuilds_dir: 'prebuilds',
build_from_source: npm_config_build_from_source,
runtime: npm_config_runtime,
target: npm_config_target,
all_versions: false,
};
console.log("options.shared_install_dir:" + options.shared_install_dir);
if (!options.runtime)
{
throw new Error("npm_config_runtime needs to be specified");
}
if (!options.target)
{
throw new Error("npm_config_target needs to be specified");
}
if (process.platform === 'win32') {
if (arch === 'ia32') {
options.generator = 'Visual Studio 15 2017';
} else if (arch === 'x64') {
options.generator = 'Visual Studio 15 2017 Win64';
} else {
console.log(`${arch} is not supported on Windows`);
return 1;
}
}
const args = process.argv;
let do_prebuild = true;
args.forEach(arg => {
arg = arg.trim();
if (arg === '--prepack-only') {
console.log('Pre-packing prebuild.');
do_prebuild = false;
exit(0)
}
if (arg === '--decompress-only') {
do_prebuild = false;
console.log('Decompressing local prebuild.');
decompress_local(options)
.then(() => {
exit(0);
})
.catch(err => {
console.log(err.message);
exit(1);
});
}
if (arg === '--install-only') {
do_prebuild = false;
console.log('Installing prebuild from developer.nordicsemi.no.');
run_install(options)
.then(() => {
console.log(
'Install from developer.nordicsemi.no was successful!'
);
exit(0);
})
.catch(err => {
console.log(
'================================================================='
);
console.log('Install from developer.nordicsemi.no failed.');
console.log('');
console.log('NOTE:');
console.log(
'The prebuild-install module will use a different strategy for fetching prebuilt binaries and will try to download from Github instead if it finds a token in ~/.prebuild-installrc'
);
console.log(
'Ensure that this token is not present by either deleting or temporarily moving it: ~/.prebuild-installrc'
);
console.log(
'================================================================='
);
console.log(`Error message was: ${err.message}`);
exit(1);
});
}
});
if (do_prebuild) {
const from_abi = nodeAbi.getAbi(options.target, options.runtime);
const got_versions = get_versions(options.runtime, from_abi, from_abi);
options.versions = options.versions.concat(got_versions);
run_prebuild(options)
.then(() => {
console.log('Done');
exit(0);
})
.catch(err => {
console.log('Done with error:');
console.log(err);
exit(1);
});
}
return 0;