-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbuild.js
64 lines (51 loc) · 2.05 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
/**
* Script to build the native NodeJS addon for the current host platform.
* Useful so a universal `npm install` command can work in development environments,
* and to reduce the amount of CI/CD config needed to generate artifacts.
*/
const path = require('path');
const fs = require('fs');
const cargoCpArtifact = require('cargo-cp-artifact');
const isDevBuild = process.argv[2] === 'dev';
console.log(`IS DEV BUILD: ${isDevBuild}`);
const targetMap = {
'darwin-x64': ['darwin-x64.node', 'x86_64-apple-darwin'],
'darwin-arm64': ['darwin-arm64.node', 'aarch64-apple-darwin'],
'win32-x64': ['win32-x64.node', 'x86_64-pc-windows-msvc'],
'linux-x64-glibc': ['linux-x64-glibc.node', 'x86_64-unknown-linux-gnu'],
'linux-x64-musl': ['linux-x64-musl.node', 'x86_64-unknown-linux-musl'],
'linux-arm64-glibc': ['linux-arm64-glibc.node', 'aarch64-unknown-linux-gnu'],
'linux-arm64-musl': ['linux-arm64-musl.node', 'aarch64-unknown-linux-musl'],
};
const targetPlatform = process.env['TARGET_PLATFORM'] ?? process.platform;
const targetArch = process.env['TARGET_ARCH'] ?? process.arch;
let configuredTarget = `${targetPlatform}-${targetArch}`;
if (targetPlatform === 'linux') {
const targetLibc = process.env['TARGET_LIBC'] ?? require('detect-libc').familySync() ?? 'glibc';
configuredTarget += '-' + targetLibc;
}
const [outputFileName, cargoTarget] = targetMap[configuredTarget]
if (!outputFileName || !cargoTarget) {
throw new Error(`Unsupported target ${configuredTarget}`);
}
const outputFilePath = path.join('native', outputFileName);
if (fs.existsSync(outputFilePath)) {
fs.unlinkSync(outputFilePath);
}
let runArgs = [
'-nc', outputFilePath,
'--',
'cargo',
'build',
'--message-format=json-render-diagnostics',
'--target', cargoTarget
];
if (isDevBuild) {
runArgs.push('--profile=release-dev', '--features=profiling');
} else {
runArgs.push('--release')
}
console.log(`Building target: ${cargoTarget}`);
console.log(`Building artifact: ${outputFilePath}`);
console.log(`Build command: ${runArgs.join(' ')}`);
cargoCpArtifact(runArgs, process.env);