mirror of
https://github.com/bitwarden/browser
synced 2025-12-13 14:53:33 +00:00
[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 <19896123+justindbaur@users.noreply.github.com> --------- Co-authored-by: Justin Baur <19896123+justindbaur@users.noreply.github.com>
This commit is contained in:
@@ -3,6 +3,21 @@ const child_process = require("child_process");
|
|||||||
const fs = require("fs");
|
const fs = require("fs");
|
||||||
const path = require("path");
|
const path = require("path");
|
||||||
const process = require("process");
|
const process = require("process");
|
||||||
|
|
||||||
|
// Map of the Node arch equivalents for the rust target triplets, used to move the file to the correct location
|
||||||
|
const rustTargetsMap = {
|
||||||
|
"i686-pc-windows-msvc": { nodeArch: 'ia32', platform: 'win32' },
|
||||||
|
"x86_64-pc-windows-msvc": { nodeArch: 'x64', platform: 'win32' },
|
||||||
|
"aarch64-pc-windows-msvc": { nodeArch: 'arm64', platform: 'win32' },
|
||||||
|
"x86_64-apple-darwin": { nodeArch: 'x64', platform: 'darwin' },
|
||||||
|
"aarch64-apple-darwin": { nodeArch: 'arm64', platform: 'darwin' },
|
||||||
|
'x86_64-unknown-linux-musl': { nodeArch: 'x64', platform: 'linux' },
|
||||||
|
'aarch64-unknown-linux-musl': { nodeArch: 'arm64', platform: 'linux' },
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ensure the dist directory exists
|
||||||
|
fs.mkdirSync(path.join(__dirname, "dist"), { recursive: true });
|
||||||
|
|
||||||
const args = process.argv.slice(2); // Get arguments passed to the script
|
const args = process.argv.slice(2); // Get arguments passed to the script
|
||||||
const mode = args.includes("--release") ? "release" : "debug";
|
const mode = args.includes("--release") ? "release" : "debug";
|
||||||
const targetArg = args.find(arg => arg.startsWith("--target="));
|
const targetArg = args.find(arg => arg.startsWith("--target="));
|
||||||
@@ -13,13 +28,21 @@ let crossPlatform = process.argv.length > 2 && process.argv[2] === "cross-platfo
|
|||||||
function buildNapiModule(target, release = true) {
|
function buildNapiModule(target, release = true) {
|
||||||
const targetArg = target ? `--target ${target}` : "";
|
const targetArg = target ? `--target ${target}` : "";
|
||||||
const releaseArg = release ? "--release" : "";
|
const releaseArg = release ? "--release" : "";
|
||||||
return child_process.execSync(`npm run build -- ${releaseArg} ${targetArg}`, { stdio: 'inherit', cwd: path.join(__dirname, "napi") });
|
child_process.execSync(`npm run build -- ${releaseArg} ${targetArg}`, { stdio: 'inherit', cwd: path.join(__dirname, "napi") });
|
||||||
}
|
}
|
||||||
|
|
||||||
function buildProxyBin(target, release = true) {
|
function buildProxyBin(target, release = true) {
|
||||||
const targetArg = target ? `--target ${target}` : "";
|
const targetArg = target ? `--target ${target}` : "";
|
||||||
const releaseArg = release ? "--release" : "";
|
const releaseArg = release ? "--release" : "";
|
||||||
return child_process.execSync(`cargo build --bin desktop_proxy ${releaseArg} ${targetArg}`, {stdio: 'inherit', cwd: path.join(__dirname, "proxy")});
|
child_process.execSync(`cargo build --bin desktop_proxy ${releaseArg} ${targetArg}`, {stdio: 'inherit', cwd: path.join(__dirname, "proxy")});
|
||||||
|
|
||||||
|
if (target) {
|
||||||
|
// Copy the resulting binary to the dist folder
|
||||||
|
const targetFolder = release ? "release" : "debug";
|
||||||
|
const ext = process.platform === "win32" ? ".exe" : "";
|
||||||
|
const nodeArch = rustTargetsMap[target].nodeArch;
|
||||||
|
fs.copyFileSync(path.join(__dirname, "target", target, targetFolder, `desktop_proxy${ext}`), path.join(__dirname, "dist", `desktop_proxy.${process.platform}-${nodeArch}${ext}`));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!crossPlatform && !target) {
|
if (!crossPlatform && !target) {
|
||||||
@@ -36,45 +59,17 @@ if (target) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Note that targets contains pairs of [rust target, node arch]
|
// Filter the targets based on the current platform, and build for each of them
|
||||||
// We do this to move the output binaries to a location that can
|
let platformTargets = Object.entries(rustTargetsMap).filter(([_, { platform: p }]) => p === process.platform);
|
||||||
// be easily accessed from electron-builder using ${os} and ${arch}
|
console.log("Cross building native modules for the targets: ", platformTargets.map(([target, _]) => target).join(", "));
|
||||||
let targets = [];
|
|
||||||
switch (process.platform) {
|
|
||||||
case "win32":
|
|
||||||
targets = [
|
|
||||||
["i686-pc-windows-msvc", 'ia32'],
|
|
||||||
["x86_64-pc-windows-msvc", 'x64'],
|
|
||||||
["aarch64-pc-windows-msvc", 'arm64']
|
|
||||||
];
|
|
||||||
break;
|
|
||||||
|
|
||||||
case "darwin":
|
|
||||||
targets = [
|
|
||||||
["x86_64-apple-darwin", 'x64'],
|
|
||||||
["aarch64-apple-darwin", 'arm64']
|
|
||||||
];
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
|
||||||
targets = [
|
|
||||||
['x86_64-unknown-linux-musl', 'x64'],
|
|
||||||
['aarch64-unknown-linux-musl', 'arm64']
|
|
||||||
];
|
|
||||||
|
|
||||||
|
// When building for Linux, we need to set some environment variables to allow cross-compilation
|
||||||
|
if (process.platform === "linux") {
|
||||||
process.env["PKG_CONFIG_ALLOW_CROSS"] = "1";
|
process.env["PKG_CONFIG_ALLOW_CROSS"] = "1";
|
||||||
process.env["PKG_CONFIG_ALL_STATIC"] = "1";
|
process.env["PKG_CONFIG_ALL_STATIC"] = "1";
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log("Cross building native modules for the targets: ", targets.map(([target, _]) => target).join(", "));
|
platformTargets.forEach(([target, _]) => {
|
||||||
|
|
||||||
fs.mkdirSync(path.join(__dirname, "dist"), { recursive: true });
|
|
||||||
|
|
||||||
targets.forEach(([target, nodeArch]) => {
|
|
||||||
buildNapiModule(target);
|
buildNapiModule(target);
|
||||||
buildProxyBin(target);
|
buildProxyBin(target);
|
||||||
|
|
||||||
const ext = process.platform === "win32" ? ".exe" : "";
|
|
||||||
fs.copyFileSync(path.join(__dirname, "target", target, "release", `desktop_proxy${ext}`), path.join(__dirname, "dist", `desktop_proxy.${process.platform}-${nodeArch}${ext}`));
|
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user