Skip to content

Commit

Permalink
fix(cli): properly detect target architecture, closes #2040 (#2102)
Browse files Browse the repository at this point in the history
* fix(cli): properly detect target architecture, closes #2040

* clippy
  • Loading branch information
lucasfernog authored Jun 28, 2021
1 parent 66efb43 commit 628a53e
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 6 deletions.
6 changes: 6 additions & 0 deletions .changes/fix-bundler-platform-detection.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"cli.rs": patch
"tauri-bundler": patch
---

Properly detect target platform's architecture.
35 changes: 31 additions & 4 deletions tooling/bundler/src/bundle/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,8 @@ pub struct Settings {
bundle_settings: BundleSettings,
/// the binaries to bundle.
binaries: Vec<BundleBinary>,
/// The target triple.
target: String,
}

/// A builder for [`Settings`].
Expand All @@ -350,6 +352,7 @@ pub struct SettingsBuilder {
package_settings: Option<PackageSettings>,
bundle_settings: BundleSettings,
binaries: Vec<BundleBinary>,
target: Option<String>,
}

impl SettingsBuilder {
Expand Down Expand Up @@ -396,13 +399,24 @@ impl SettingsBuilder {
self
}

/// Sets the target triple.
pub fn target(mut self, target: String) -> Self {
self.target.replace(target);
self
}

/// Builds a Settings from the CLI args.
///
/// Package settings will be read from Cargo.toml.
///
/// Bundle settings will be read from from $TAURI_DIR/tauri.conf.json if it exists and fallback to Cargo.toml's [package.metadata.bundle].
pub fn build(self) -> crate::Result<Settings> {
let bundle_settings = parse_external_bin(self.bundle_settings)?;
let target = if let Some(t) = self.target {
t
} else {
target_triple()?
};
let bundle_settings = parse_external_bin(&target, self.bundle_settings)?;

Ok(Settings {
package: self.package_settings.expect("package settings is required"),
Expand All @@ -413,6 +427,7 @@ impl SettingsBuilder {
.expect("out directory is required"),
binaries: self.binaries,
bundle_settings,
target,
})
}
}
Expand All @@ -425,7 +440,17 @@ impl Settings {

/// Returns the architecture for the binary being bundled (e.g. "arm", "x86" or "x86_64").
pub fn binary_arch(&self) -> &str {
std::env::consts::ARCH
if self.target.starts_with("x86_64") {
"x86_64"
} else if self.target.starts_with('i') {
"x86"
} else if self.target.starts_with("arm") {
"arm"
} else if self.target.starts_with("aarch64") {
"aarch64"
} else {
panic!("Unexpected target triple {}", self.target)
}
}

/// Returns the file name of the binary being bundled.
Expand Down Expand Up @@ -660,8 +685,10 @@ impl Settings {
}

/// Parses the external binaries to bundle, adding the target triple suffix to each of them.
fn parse_external_bin(bundle_settings: BundleSettings) -> crate::Result<BundleSettings> {
let target_triple = target_triple()?;
fn parse_external_bin(
target_triple: &str,
bundle_settings: BundleSettings,
) -> crate::Result<BundleSettings> {
let mut win_paths = Vec::new();
let external_bin = match bundle_settings.external_bin {
Some(paths) => {
Expand Down
2 changes: 1 addition & 1 deletion tooling/bundler/src/bundle/windows/msi/wix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -754,7 +754,7 @@ fn generate_resource_data(settings: &Settings) -> crate::Result<ResourceMap> {
.iter()
.position(|f| f.path == path);
match index {
Some(i) => directory_entry = directory_entry.directories.iter_mut().nth(i).unwrap(),
Some(i) => directory_entry = directory_entry.directories.get_mut(i).unwrap(),
None => {
directory_entry.directories.push(ResourceDirectory {
path: path.clone(),
Expand Down
20 changes: 19 additions & 1 deletion tooling/cli.rs/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,24 @@ impl Build {
// move merge modules to the out dir so the bundler can load it
#[cfg(windows)]
{
let (filename, vcruntime_msm) = if cfg!(target_arch = "x86") {
let arch = if let Some(t) = &self.target {
if t.starts_with("x86_64") {
"x86_64"
} else if t.starts_with('i') {
"x86"
} else if t.starts_with("arm") {
"arm"
} else if t.starts_with("aarch64") {
"aarch64"
} else {
panic!("Unexpected target triple {}", t)
}
} else if cfg!(target_arch = "x86") {
"x86"
} else {
"x86_64"
};
let (filename, vcruntime_msm) = if arch == "x86" {
let _ = std::fs::remove_file(out_dir.join("Microsoft_VC142_CRT_x64.msm"));
(
"Microsoft_VC142_CRT_x86.msm",
Expand Down Expand Up @@ -207,6 +224,7 @@ impl Build {

let settings = crate::interface::get_bundler_settings(
app_settings,
self.target.clone(),
&manifest,
config_,
&out_dir,
Expand Down
1 change: 1 addition & 0 deletions tooling/cli.rs/src/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ impl Dev {
.with_context(|| "failed to get project out directory")?;
let settings = crate::interface::get_bundler_settings(
app_settings,
self.target.clone(),
&Default::default(),
config_,
&out_dir,
Expand Down
5 changes: 5 additions & 0 deletions tooling/cli.rs/src/interface/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use tauri_bundler::bundle::{PackageType, Settings, SettingsBuilder};

pub fn get_bundler_settings(
app_settings: rust::AppSettings,
target: Option<String>,
manifest: &Manifest,
config: &Config,
out_dir: &Path,
Expand All @@ -31,5 +32,9 @@ pub fn get_bundler_settings(
settings_builder = settings_builder.package_types(types);
}

if let Some(target) = target {
settings_builder = settings_builder.target(target);
}

settings_builder.build().map_err(Into::into)
}

0 comments on commit 628a53e

Please sign in to comment.