@@ -3,6 +3,21 @@ const child_process = require("child_process");
3
3
const fs = require ( "fs" ) ;
4
4
const path = require ( "path" ) ;
5
5
const process = require ( "process" ) ;
6
+
7
+ // Map of the Node arch equivalents for the rust target triplets, used to move the file to the correct location
8
+ const rustTargetsMap = {
9
+ "i686-pc-windows-msvc" : { nodeArch : 'ia32' , platform : 'win32' } ,
10
+ "x86_64-pc-windows-msvc" : { nodeArch : 'x64' , platform : 'win32' } ,
11
+ "aarch64-pc-windows-msvc" : { nodeArch : 'arm64' , platform : 'win32' } ,
12
+ "x86_64-apple-darwin" : { nodeArch : 'x64' , platform : 'darwin' } ,
13
+ "aarch64-apple-darwin" : { nodeArch : 'arm64' , platform : 'darwin' } ,
14
+ 'x86_64-unknown-linux-musl' : { nodeArch : 'x64' , platform : 'linux' } ,
15
+ 'aarch64-unknown-linux-musl' : { nodeArch : 'arm64' , platform : 'linux' } ,
16
+ }
17
+
18
+ // Ensure the dist directory exists
19
+ fs . mkdirSync ( path . join ( __dirname , "dist" ) , { recursive : true } ) ;
20
+
6
21
const args = process . argv . slice ( 2 ) ; // Get arguments passed to the script
7
22
const mode = args . includes ( "--release" ) ? "release" : "debug" ;
8
23
const targetArg = args . find ( arg => arg . startsWith ( "--target=" ) ) ;
@@ -13,13 +28,21 @@ let crossPlatform = process.argv.length > 2 && process.argv[2] === "cross-platfo
13
28
function buildNapiModule ( target , release = true ) {
14
29
const targetArg = target ? `--target ${ target } ` : "" ;
15
30
const releaseArg = release ? "--release" : "" ;
16
- return child_process . execSync ( `npm run build -- ${ releaseArg } ${ targetArg } ` , { stdio : 'inherit' , cwd : path . join ( __dirname , "napi" ) } ) ;
31
+ child_process . execSync ( `npm run build -- ${ releaseArg } ${ targetArg } ` , { stdio : 'inherit' , cwd : path . join ( __dirname , "napi" ) } ) ;
17
32
}
18
33
19
34
function buildProxyBin ( target , release = true ) {
20
35
const targetArg = target ? `--target ${ target } ` : "" ;
21
36
const releaseArg = release ? "--release" : "" ;
22
- return child_process . execSync ( `cargo build --bin desktop_proxy ${ releaseArg } ${ targetArg } ` , { stdio : 'inherit' , cwd : path . join ( __dirname , "proxy" ) } ) ;
37
+ child_process . execSync ( `cargo build --bin desktop_proxy ${ releaseArg } ${ targetArg } ` , { stdio : 'inherit' , cwd : path . join ( __dirname , "proxy" ) } ) ;
38
+
39
+ if ( target ) {
40
+ // Copy the resulting binary to the dist folder
41
+ const targetFolder = release ? "release" : "debug" ;
42
+ const ext = process . platform === "win32" ? ".exe" : "" ;
43
+ const nodeArch = rustTargetsMap [ target ] . nodeArch ;
44
+ fs . copyFileSync ( path . join ( __dirname , "target" , target , targetFolder , `desktop_proxy${ ext } ` ) , path . join ( __dirname , "dist" , `desktop_proxy.${ process . platform } -${ nodeArch } ${ ext } ` ) ) ;
45
+ }
23
46
}
24
47
25
48
if ( ! crossPlatform && ! target ) {
@@ -36,45 +59,17 @@ if (target) {
36
59
return ;
37
60
}
38
61
39
- // Note that targets contains pairs of [rust target, node arch]
40
- // We do this to move the output binaries to a location that can
41
- // be easily accessed from electron-builder using ${os} and ${arch}
42
- let targets = [ ] ;
43
- switch ( process . platform ) {
44
- case "win32" :
45
- targets = [
46
- [ "i686-pc-windows-msvc" , 'ia32' ] ,
47
- [ "x86_64-pc-windows-msvc" , 'x64' ] ,
48
- [ "aarch64-pc-windows-msvc" , 'arm64' ]
49
- ] ;
50
- break ;
51
-
52
- case "darwin" :
53
- targets = [
54
- [ "x86_64-apple-darwin" , 'x64' ] ,
55
- [ "aarch64-apple-darwin" , 'arm64' ]
56
- ] ;
57
- break ;
62
+ // Filter the targets based on the current platform, and build for each of them
63
+ let platformTargets = Object . entries ( rustTargetsMap ) . filter ( ( [ _ , { platform : p } ] ) => p === process . platform ) ;
64
+ console . log ( "Cross building native modules for the targets: " , platformTargets . map ( ( [ target , _ ] ) => target ) . join ( ", " ) ) ;
58
65
59
- default :
60
- targets = [
61
- [ 'x86_64-unknown-linux-musl' , 'x64' ] ,
62
- [ 'aarch64-unknown-linux-musl' , 'arm64' ]
63
- ] ;
64
-
65
- process . env [ "PKG_CONFIG_ALLOW_CROSS" ] = "1" ;
66
- process . env [ "PKG_CONFIG_ALL_STATIC" ] = "1" ;
67
- break ;
66
+ // When building for Linux, we need to set some environment variables to allow cross-compilation
67
+ if ( process . platform === "linux" ) {
68
+ process . env [ "PKG_CONFIG_ALLOW_CROSS" ] = "1" ;
69
+ process . env [ "PKG_CONFIG_ALL_STATIC" ] = "1" ;
68
70
}
69
71
70
- console . log ( "Cross building native modules for the targets: " , targets . map ( ( [ target , _ ] ) => target ) . join ( ", " ) ) ;
71
-
72
- fs . mkdirSync ( path . join ( __dirname , "dist" ) , { recursive : true } ) ;
73
-
74
- targets . forEach ( ( [ target , nodeArch ] ) => {
72
+ platformTargets . forEach ( ( [ target , _ ] ) => {
75
73
buildNapiModule ( target ) ;
76
74
buildProxyBin ( target ) ;
77
-
78
- const ext = process . platform === "win32" ? ".exe" : "" ;
79
- fs . copyFileSync ( path . join ( __dirname , "target" , target , "release" , `desktop_proxy${ ext } ` ) , path . join ( __dirname , "dist" , `desktop_proxy.${ process . platform } -${ nodeArch } ${ ext } ` ) ) ;
80
75
} ) ;
0 commit comments