Skip to content

Commit

Permalink
fix(build): append .exe binary based on target triple instead of runn…
Browse files Browse the repository at this point in the history
…ing OS, closes #3870 (#4032)

Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
  • Loading branch information
Pierstoval and lucasfernog authored May 3, 2022
1 parent a0ecd81 commit 4562e67
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 35 deletions.
6 changes: 6 additions & 0 deletions .changes/cli-bin-ext-from-target-triple.md
Original file line number Diff line number Diff line change
@@ -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.
6 changes: 5 additions & 1 deletion core/tauri-utils/src/resources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 1 addition & 5 deletions tooling/bundler/src/bundle/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down
31 changes: 21 additions & 10 deletions tooling/cli/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down Expand Up @@ -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 `{}`",
Expand Down Expand Up @@ -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_,
Expand Down
11 changes: 4 additions & 7 deletions tooling/cli/src/interface/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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();
Expand All @@ -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)
}
50 changes: 38 additions & 12 deletions tooling/cli/src/interface/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()),
)
Expand All @@ -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,
))
}
}
}
Expand All @@ -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,
));
}
Expand All @@ -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),
Expand Down

0 comments on commit 4562e67

Please sign in to comment.