Browse Source

fix(cli): properly detect target architecture, closes #2040 (#2102)

* fix(cli): properly detect target architecture, closes #2040

* clippy
Lucas Fernandes Nogueira 4 years ago
parent
commit
628a53eb61

+ 6 - 0
.changes/fix-bundler-platform-detection.md

@@ -0,0 +1,6 @@
+---
+"cli.rs": patch
+"tauri-bundler": patch
+---
+
+Properly detect target platform's architecture.

+ 31 - 4
tooling/bundler/src/bundle/settings.rs

@@ -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`].
@@ -350,6 +352,7 @@ pub struct SettingsBuilder {
   package_settings: Option<PackageSettings>,
   bundle_settings: BundleSettings,
   binaries: Vec<BundleBinary>,
+  target: Option<String>,
 }
 
 impl SettingsBuilder {
@@ -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"),
@@ -413,6 +427,7 @@ impl SettingsBuilder {
         .expect("out directory is required"),
       binaries: self.binaries,
       bundle_settings,
+      target,
     })
   }
 }
@@ -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.
@@ -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) => {

+ 1 - 1
tooling/bundler/src/bundle/windows/msi/wix.rs

@@ -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(),

+ 19 - 1
tooling/cli.rs/src/build.rs

@@ -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",
@@ -207,6 +224,7 @@ impl Build {
 
       let settings = crate::interface::get_bundler_settings(
         app_settings,
+        self.target.clone(),
         &manifest,
         config_,
         &out_dir,

+ 1 - 0
tooling/cli.rs/src/dev.rs

@@ -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,

+ 5 - 0
tooling/cli.rs/src/interface/mod.rs

@@ -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,
@@ -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)
 }