浏览代码

fix(build): append .exe binary based on target triple instead of running OS, closes #3870 (#4032)

Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
Alex Rock 3 年之前
父节点
当前提交
4562e671e4

+ 6 - 0
.changes/cli-bin-ext-from-target-triple.md

@@ -0,0 +1,6 @@
+---
+"cli.rs": patch
+"cli.js": patch
+---
+
+Resolve binary file extension from target triple instead of compile-time checks to allow cross compilation.

+ 5 - 1
core/tauri-utils/src/resources.rs

@@ -29,7 +29,11 @@ pub fn external_binaries(external_binaries: &[String], target_triple: &str) -> V
       "{}-{}{}",
       curr_path,
       target_triple,
-      if cfg!(windows) { ".exe" } else { "" }
+      if target_triple.contains("windows") {
+        ".exe"
+      } else {
+        ""
+      }
     ));
   }
   paths

+ 1 - 5
tooling/bundler/src/bundle/settings.rs

@@ -322,11 +322,7 @@ impl BundleBinary {
   /// Creates a new bundle binary.
   pub fn new(name: String, main: bool) -> Self {
     Self {
-      name: if cfg!(windows) {
-        format!("{}.exe", name)
-      } else {
-        name
-      },
+      name,
       src_path: None,
       main,
     }

+ 21 - 10
tooling/cli/src/build.rs

@@ -168,10 +168,20 @@ pub fn command(options: Options) -> Result<()> {
     .name
     .clone()
     .expect("Cargo manifest must have the `package.name` field");
-  #[cfg(windows)]
-  let bin_path = out_dir.join(format!("{}.exe", bin_name));
-  #[cfg(not(windows))]
-  let bin_path = out_dir.join(&bin_name);
+
+  let target: String = if let Some(target) = options.target.clone() {
+    target
+  } else {
+    tauri_utils::platform::target_triple()?
+  };
+  let binary_extension: String = if target.contains("windows") {
+    ".exe"
+  } else {
+    ""
+  }
+  .into();
+
+  let bin_path = out_dir.join(&bin_name).with_extension(&binary_extension);
 
   let no_default_features = args.contains(&"--no-default-features".into());
 
@@ -211,12 +221,13 @@ pub fn command(options: Options) -> Result<()> {
   }
 
   if let Some(product_name) = config_.package.product_name.clone() {
-    #[cfg(windows)]
-    let product_path = out_dir.join(format!("{}.exe", product_name));
-    #[cfg(target_os = "macos")]
-    let product_path = out_dir.join(product_name);
     #[cfg(target_os = "linux")]
-    let product_path = out_dir.join(product_name.to_kebab_case());
+    let product_name = product_name.to_kebab_case();
+
+    let product_path = out_dir
+      .join(&product_name)
+      .with_extension(&binary_extension);
+
     rename(&bin_path, &product_path).with_context(|| {
       format!(
         "failed to rename `{}` to `{}`",
@@ -314,7 +325,7 @@ pub fn command(options: Options) -> Result<()> {
     }
     let settings = crate::interface::get_bundler_settings(
       app_settings,
-      options.target.clone(),
+      target,
       &enabled_features,
       &manifest,
       config_,

+ 4 - 7
tooling/cli/src/interface/mod.rs

@@ -12,7 +12,7 @@ use tauri_bundler::bundle::{PackageType, Settings, SettingsBuilder};
 #[allow(clippy::too_many_arguments)]
 pub fn get_bundler_settings(
   app_settings: rust::AppSettings,
-  target: Option<String>,
+  target: String,
   features: &[String],
   manifest: &Manifest,
   config: &Config,
@@ -23,8 +23,9 @@ pub fn get_bundler_settings(
   let mut settings_builder = SettingsBuilder::new()
     .package_settings(app_settings.get_package_settings())
     .bundle_settings(app_settings.get_bundle_settings(config, manifest, features)?)
-    .binaries(app_settings.get_binaries(config)?)
-    .project_out_directory(out_dir);
+    .binaries(app_settings.get_binaries(config, &target)?)
+    .project_out_directory(out_dir)
+    .target(target);
 
   if verbose {
     settings_builder = settings_builder.verbose();
@@ -34,9 +35,5 @@ 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)
 }

+ 38 - 12
tooling/cli/src/interface/rust.rs

@@ -198,8 +198,16 @@ impl AppSettings {
     self.package_settings.clone()
   }
 
-  pub fn get_binaries(&self, config: &Config) -> crate::Result<Vec<BundleBinary>> {
+  pub fn get_binaries(&self, config: &Config, target: &str) -> crate::Result<Vec<BundleBinary>> {
     let mut binaries: Vec<BundleBinary> = vec![];
+
+    let binary_extension: String = if target.contains("windows") {
+      ".exe"
+    } else {
+      ""
+    }
+    .into();
+
     if let Some(bin) = &self.cargo_settings.bin {
       let default_run = self
         .package_settings
@@ -212,14 +220,21 @@ impl AppSettings {
             || binary.name.as_str() == default_run
           {
             BundleBinary::new(
-              config
-                .package
-                .binary_name()
-                .unwrap_or_else(|| binary.name.clone()),
+              format!(
+                "{}{}",
+                config
+                  .package
+                  .binary_name()
+                  .unwrap_or_else(|| binary.name.clone()),
+                &binary_extension
+              ),
               true,
             )
           } else {
-            BundleBinary::new(binary.name.clone(), false)
+            BundleBinary::new(
+              format!("{}{}", binary.name.clone(), &binary_extension),
+              false,
+            )
           }
           .set_src_path(binary.path.clone()),
         )
@@ -236,7 +251,10 @@ impl AppSettings {
             bin.name() == name || path.ends_with(bin.src_path().unwrap_or(&"".to_string()))
           });
           if !bin_exists {
-            binaries.push(BundleBinary::new(name.to_string_lossy().to_string(), false))
+            binaries.push(BundleBinary::new(
+              format!("{}{}", name.to_string_lossy(), &binary_extension),
+              false,
+            ))
           }
         }
       }
@@ -251,10 +269,14 @@ impl AppSettings {
         }
         None => {
           binaries.push(BundleBinary::new(
-            config
-              .package
-              .binary_name()
-              .unwrap_or_else(|| default_run.to_string()),
+            format!(
+              "{}{}",
+              config
+                .package
+                .binary_name()
+                .unwrap_or_else(|| default_run.to_string()),
+              &binary_extension
+            ),
             true,
           ));
         }
@@ -266,7 +288,11 @@ impl AppSettings {
         #[cfg(target_os = "linux")]
         self.package_settings.product_name.to_kebab_case(),
         #[cfg(not(target_os = "linux"))]
-        self.package_settings.product_name.clone(),
+        format!(
+          "{}{}",
+          self.package_settings.product_name.clone(),
+          &binary_extension
+        ),
         true,
       )),
       1 => binaries.get_mut(0).unwrap().set_main(true),