Skip to content

Commit 628a53e

Browse files
authored
fix(cli): properly detect target architecture, closes #2040 (#2102)
* fix(cli): properly detect target architecture, closes #2040 * clippy
1 parent 66efb43 commit 628a53e

File tree

6 files changed

+63
-6
lines changed

6 files changed

+63
-6
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"cli.rs": patch
3+
"tauri-bundler": patch
4+
---
5+
6+
Properly detect target platform's architecture.

tooling/bundler/src/bundle/settings.rs

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,8 @@ pub struct Settings {
339339
bundle_settings: BundleSettings,
340340
/// the binaries to bundle.
341341
binaries: Vec<BundleBinary>,
342+
/// The target triple.
343+
target: String,
342344
}
343345

344346
/// A builder for [`Settings`].
@@ -350,6 +352,7 @@ pub struct SettingsBuilder {
350352
package_settings: Option<PackageSettings>,
351353
bundle_settings: BundleSettings,
352354
binaries: Vec<BundleBinary>,
355+
target: Option<String>,
353356
}
354357

355358
impl SettingsBuilder {
@@ -396,13 +399,24 @@ impl SettingsBuilder {
396399
self
397400
}
398401

402+
/// Sets the target triple.
403+
pub fn target(mut self, target: String) -> Self {
404+
self.target.replace(target);
405+
self
406+
}
407+
399408
/// Builds a Settings from the CLI args.
400409
///
401410
/// Package settings will be read from Cargo.toml.
402411
///
403412
/// Bundle settings will be read from from $TAURI_DIR/tauri.conf.json if it exists and fallback to Cargo.toml's [package.metadata.bundle].
404413
pub fn build(self) -> crate::Result<Settings> {
405-
let bundle_settings = parse_external_bin(self.bundle_settings)?;
414+
let target = if let Some(t) = self.target {
415+
t
416+
} else {
417+
target_triple()?
418+
};
419+
let bundle_settings = parse_external_bin(&target, self.bundle_settings)?;
406420

407421
Ok(Settings {
408422
package: self.package_settings.expect("package settings is required"),
@@ -413,6 +427,7 @@ impl SettingsBuilder {
413427
.expect("out directory is required"),
414428
binaries: self.binaries,
415429
bundle_settings,
430+
target,
416431
})
417432
}
418433
}
@@ -425,7 +440,17 @@ impl Settings {
425440

426441
/// Returns the architecture for the binary being bundled (e.g. "arm", "x86" or "x86_64").
427442
pub fn binary_arch(&self) -> &str {
428-
std::env::consts::ARCH
443+
if self.target.starts_with("x86_64") {
444+
"x86_64"
445+
} else if self.target.starts_with('i') {
446+
"x86"
447+
} else if self.target.starts_with("arm") {
448+
"arm"
449+
} else if self.target.starts_with("aarch64") {
450+
"aarch64"
451+
} else {
452+
panic!("Unexpected target triple {}", self.target)
453+
}
429454
}
430455

431456
/// Returns the file name of the binary being bundled.
@@ -660,8 +685,10 @@ impl Settings {
660685
}
661686

662687
/// Parses the external binaries to bundle, adding the target triple suffix to each of them.
663-
fn parse_external_bin(bundle_settings: BundleSettings) -> crate::Result<BundleSettings> {
664-
let target_triple = target_triple()?;
688+
fn parse_external_bin(
689+
target_triple: &str,
690+
bundle_settings: BundleSettings,
691+
) -> crate::Result<BundleSettings> {
665692
let mut win_paths = Vec::new();
666693
let external_bin = match bundle_settings.external_bin {
667694
Some(paths) => {

tooling/bundler/src/bundle/windows/msi/wix.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -754,7 +754,7 @@ fn generate_resource_data(settings: &Settings) -> crate::Result<ResourceMap> {
754754
.iter()
755755
.position(|f| f.path == path);
756756
match index {
757-
Some(i) => directory_entry = directory_entry.directories.iter_mut().nth(i).unwrap(),
757+
Some(i) => directory_entry = directory_entry.directories.get_mut(i).unwrap(),
758758
None => {
759759
directory_entry.directories.push(ResourceDirectory {
760760
path: path.clone(),

tooling/cli.rs/src/build.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,24 @@ impl Build {
145145
// move merge modules to the out dir so the bundler can load it
146146
#[cfg(windows)]
147147
{
148-
let (filename, vcruntime_msm) = if cfg!(target_arch = "x86") {
148+
let arch = if let Some(t) = &self.target {
149+
if t.starts_with("x86_64") {
150+
"x86_64"
151+
} else if t.starts_with('i') {
152+
"x86"
153+
} else if t.starts_with("arm") {
154+
"arm"
155+
} else if t.starts_with("aarch64") {
156+
"aarch64"
157+
} else {
158+
panic!("Unexpected target triple {}", t)
159+
}
160+
} else if cfg!(target_arch = "x86") {
161+
"x86"
162+
} else {
163+
"x86_64"
164+
};
165+
let (filename, vcruntime_msm) = if arch == "x86" {
149166
let _ = std::fs::remove_file(out_dir.join("Microsoft_VC142_CRT_x64.msm"));
150167
(
151168
"Microsoft_VC142_CRT_x86.msm",
@@ -207,6 +224,7 @@ impl Build {
207224

208225
let settings = crate::interface::get_bundler_settings(
209226
app_settings,
227+
self.target.clone(),
210228
&manifest,
211229
config_,
212230
&out_dir,

tooling/cli.rs/src/dev.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ impl Dev {
106106
.with_context(|| "failed to get project out directory")?;
107107
let settings = crate::interface::get_bundler_settings(
108108
app_settings,
109+
self.target.clone(),
109110
&Default::default(),
110111
config_,
111112
&out_dir,

tooling/cli.rs/src/interface/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use tauri_bundler::bundle::{PackageType, Settings, SettingsBuilder};
1111

1212
pub fn get_bundler_settings(
1313
app_settings: rust::AppSettings,
14+
target: Option<String>,
1415
manifest: &Manifest,
1516
config: &Config,
1617
out_dir: &Path,
@@ -31,5 +32,9 @@ pub fn get_bundler_settings(
3132
settings_builder = settings_builder.package_types(types);
3233
}
3334

35+
if let Some(target) = target {
36+
settings_builder = settings_builder.target(target);
37+
}
38+
3439
settings_builder.build().map_err(Into::into)
3540
}

0 commit comments

Comments
 (0)