Skip to content

Commit cb02197

Browse files
[PM-20597] Fix linux desktop_native script (#14428)
* Fix linux desktop_native build script * Add linux variables * Remove default * Remove unused import * Update apps/desktop/desktop_native/build.js Co-authored-by: Justin Baur <[email protected]> --------- Co-authored-by: Justin Baur <[email protected]>
1 parent 2980cd4 commit cb02197

File tree

1 file changed

+33
-38
lines changed

1 file changed

+33
-38
lines changed

apps/desktop/desktop_native/build.js

Lines changed: 33 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,21 @@ const child_process = require("child_process");
33
const fs = require("fs");
44
const path = require("path");
55
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+
621
const args = process.argv.slice(2); // Get arguments passed to the script
722
const mode = args.includes("--release") ? "release" : "debug";
823
const targetArg = args.find(arg => arg.startsWith("--target="));
@@ -13,13 +28,21 @@ let crossPlatform = process.argv.length > 2 && process.argv[2] === "cross-platfo
1328
function buildNapiModule(target, release = true) {
1429
const targetArg = target ? `--target ${target}` : "";
1530
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") });
1732
}
1833

1934
function buildProxyBin(target, release = true) {
2035
const targetArg = target ? `--target ${target}` : "";
2136
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+
}
2346
}
2447

2548
if (!crossPlatform && !target) {
@@ -36,45 +59,17 @@ if (target) {
3659
return;
3760
}
3861

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(", "));
5865

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";
6870
}
6971

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, _]) => {
7573
buildNapiModule(target);
7674
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}`));
8075
});

0 commit comments

Comments
 (0)