forked from utf26/iohook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
335 lines (312 loc) · 9 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
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
const spawn = require('child_process').spawn;
const fs = require('fs-extra');
const path = require('path');
const tar = require('tar');
const argv = require('minimist')(process.argv.slice(2), {
// Specify that these arguments should be a string
string: ['version', 'runtime', 'abi'],
});
const pkg = require('./package.json');
const nodeAbi = require('node-abi');
const { optionsFromPackage } = require('./helpers');
let arch = process.env.ARCH
? process.env.ARCH.replace('i686', 'ia32').replace('x86_64', 'x64')
: process.arch;
// // Additional handling for arm64 architecture
// if (process.platform === 'darwin' && process.arch === 'arm64') {
// arch = 'arm64';
// }
let gypJsPath = path.join(
__dirname,
'node_modules',
'.bin',
process.platform === 'win32' ? 'node-gyp.cmd' : 'node-gyp'
);
let files = [];
let targets;
let chain = Promise.resolve();
initBuild();
function initBuild() {
// Check if a specific runtime has been specified from the command line
if ('runtime' in argv && 'version' in argv && 'abi' in argv) {
targets = [[argv['runtime'], argv['version'], argv['abi']]];
} else if ('all' in argv) {
// If "--all", use those defined in package.json
targets = require('./package.json').supportedTargets;
} else {
const options = optionsFromPackage();
if (process.env.npm_config_targets) {
options.targets = options.targets.concat(
process.env.npm_config_targets.split(',')
);
}
console.log(options.targets);
options.targets = options.targets.map((targetStr) => targetStr.split('-'));
if (process.env.npm_config_targets === 'all') {
options.targets = require('./package.json').supportedTargets.map((arr) => [arr[0], arr[2]]);
options.platforms = ['win32', 'darwin', 'linux'];
options.arches = ['x64', 'ia32'];
}
if (process.env.npm_config_platforms) {
options.platforms = options.platforms.concat(
process.env.npm_config_platforms.split(',')
);
}
if (process.env.npm_config_arches) {
options.arches = options.arches.concat(
process.env.npm_config_arches.split(',')
);
}
console.log({options})
if (options.targets.length > 0) {
targets = options.targets.map((e) => [
e[0],
nodeAbi.getTarget(e[1], e[0]),
e[1],
]);
} else {
const runtime = process.versions['electron'] ? 'electron' : 'node';
const version = process.versions.node;
const abi = process.versions.modules;
targets = [[runtime, version, abi]];
}
}
console.log({targets})
targets.forEach((parts) => {
let runtime = parts[0];
let version = parts[1];
let abi = parts[2];
chain = chain
.then(function () {
console.log(runtime, version, abi);
return build(runtime, version, abi);
})
.then(function () {
cpBuilds(runtime, abi);
return tarGz(runtime, abi);
})
.catch((err) => {
console.error(err);
process.exit(1);
});
});
chain = chain.then(function () {
if ('upload' in argv && argv['upload'] === 'false') {
// If no upload has been specified, don't attempt to upload
return;
}
return uploadFiles(files);
});
cpGyp();
}
function cpBuilds(runtime, abi) {
let buildsPath = path.join(__dirname, 'builds', runtime +
'-v' +
abi +
'-' +
process.platform +
'-' +
arch,
'build',
'Release'
);
try {
fs.unlinkSync(path.join(buildsPath, 'iohook.node'));
} catch (e) {}
fs.copySync(
path.join(__dirname, 'build', 'Release', 'iohook.node'),
path.join(buildsPath, 'iohook.node')
);
switch (process.platform) {
case 'win32':
try {
fs.unlinkSync(path.join(buildsPath, 'uiohook.dll'));
} catch (e) {}
fs.copySync(
path.join(__dirname, 'build', 'Release', 'uiohook.dll'),
path.join(buildsPath, 'uiohook.dll')
);
break;
case 'darwin':
try {
fs.unlinkSync(path.join(buildsPath, 'uiohook.dylib'));
} catch (e) {}
fs.copySync(
path.join(__dirname, 'build', 'Release', 'uiohook.dylib'),
path.join(buildsPath, 'uiohook.dylib')
);
break;
default:
try {
fs.unlinkSync(path.join(buildsPath, 'uiohook.so'));
} catch (e) {}
fs.copySync(
path.join(__dirname, 'build', 'Release', 'uiohook.so'),
path.join(buildsPath, 'uiohook.so')
);
break;
}
}
function cpGyp() {
try {
fs.unlinkSync(path.join(__dirname, 'binding.gyp'));
fs.unlinkSync(path.join(__dirname, 'uiohook.gyp'));
} catch (e) {}
switch (process.platform) {
case 'win32':
case 'darwin':
fs.copySync(
path.join(__dirname, 'build_def', process.platform, 'binding.gyp'),
path.join(__dirname, 'binding.gyp')
);
fs.copySync(
path.join(__dirname, 'build_def', process.platform, 'uiohook.gyp'),
path.join(__dirname, 'uiohook.gyp')
);
break;
default:
fs.copySync(
path.join(__dirname, 'build_def', 'linux', 'binding.gyp'),
path.join(__dirname, 'binding.gyp')
);
fs.copySync(
path.join(__dirname, 'build_def', 'linux', 'uiohook.gyp'),
path.join(__dirname, 'uiohook.gyp')
);
break;
}
}
function build(runtime, version, abi) {
return new Promise(function (resolve, reject) {
let args = [
'configure',
'rebuild',
'--target=' + version,
'--arch=' + arch,
];
console.log("runtime", runtime, version, arch, abi);
if (/^electron/i.test(runtime)) {
args.push('--dist-url=https://artifacts.electronjs.org/headers/dist');
}
if (parseInt(abi) >= 80) {
if (arch === 'x64') {
args.push('--v8_enable_pointer_compression=1');
} else {
args.push('--v8_enable_pointer_compression=0');
args.push('--v8_enable_31bit_smis_on_64bit_arch=1');
}
}
if (process.platform !== 'win32') {
if (parseInt(abi) >= 64) {
args.push('--build_v8_with_gn=false');
}
if (parseInt(abi) >= 67) {
args.push('--enable_lto=false');
}
} else {
if (parseInt(abi) >= 64 && parseInt(abi) < 121) {
args.push('--build_v8_with_gn=false');
}
}
if (arch === 'arm64' && process.platform === 'darwin') {
// Log the architecture for verification
console.log('Building for Apple Silicon (ARM64)');
}
console.log('Building iohook for ' + runtime + ' v' + version + '>>>>');
if (process.platform === 'win32') {
if (version.split('.')[0] >= 4) {
process.env.msvs_toolset = 15;
process.env.msvs_version = argv.msvs_version || 2024;
} else {
process.env.msvs_toolset = 15;
process.env.msvs_version = 2024;
}
args.push('--msvs_version=' + process.env.msvs_version);
} else {
process.env.gyp_iohook_runtime = runtime;
process.env.gyp_iohook_abi = abi;
process.env.gyp_iohook_platform = process.platform;
process.env.gyp_iohook_arch = arch;
}
let proc = spawn(gypJsPath, args, {
env: process.env,
});
proc.stdout.pipe(process.stdout);
proc.stderr.pipe(process.stderr);
proc.on('error', (err) => {
console.error('Spawn process error:', err);
return reject(err);
});
proc.on('exit', function (code, sig) {
if (code === 1) {
return reject(new Error('Failed to build...'));
}
resolve();
});
});
}
function tarGz(runtime, abi) {
const FILES_TO_ARCHIVE = {
win32: ['build/Release/iohook.node', 'build/Release/uiohook.dll'],
linux: ['build/Release/iohook.node', 'build/Release/uiohook.so'],
darwin: ['build/Release/iohook.node', 'build/Release/uiohook.dylib'],
};
const tarPath =
'prebuilds/iohook-v' +
pkg.version +
'-' +
runtime +
'-v' +
abi +
'-' +
process.platform +
'-' +
arch +
'.tar.gz';
files.push(tarPath);
if (!fs.existsSync(path.dirname(tarPath))) {
fs.mkdirSync(path.dirname(tarPath));
}
tar.c(
{
gzip: true,
file: tarPath,
sync: true,
},
FILES_TO_ARCHIVE[process.platform]
);
}
function uploadFiles(files) {
const upload = require('prebuild/upload');
return new Promise(function (resolve, reject) {
console.log(
'Uploading ' + files.length + ' prebuilds(s) to Github releases'
);
let opts = {
pkg: pkg,
files: files,
'tag-prefix': 'v',
upload: process.env.GITHUB_ACCESS_TOKEN,
};
upload(opts, function (err, result) {
if (err) {
return reject(err);
}
console.log('Found ' + result.old.length + ' prebuild(s) on Github');
if (result.old.length) {
result.old.forEach(function (build) {
console.log('-> ' + build);
});
}
console.log(
'Uploaded ' + result.new.length + ' new prebuild(s) to Github'
);
if (result.new.length) {
result.new.forEach(function (build) {
console.log('-> ' + build);
});
}
resolve();
});
});
}