Skip to content

Commit 4562e67

Browse files
fix(build): append .exe binary based on target triple instead of running OS, closes #3870 (#4032)
Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
1 parent a0ecd81 commit 4562e67

File tree

6 files changed

+75
-35
lines changed

6 files changed

+75
-35
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+
"cli.js": patch
4+
---
5+
6+
Resolve binary file extension from target triple instead of compile-time checks to allow cross compilation.

core/tauri-utils/src/resources.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,11 @@ pub fn external_binaries(external_binaries: &[String], target_triple: &str) -> V
2929
"{}-{}{}",
3030
curr_path,
3131
target_triple,
32-
if cfg!(windows) { ".exe" } else { "" }
32+
if target_triple.contains("windows") {
33+
".exe"
34+
} else {
35+
""
36+
}
3337
));
3438
}
3539
paths

tooling/bundler/src/bundle/settings.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -322,11 +322,7 @@ impl BundleBinary {
322322
/// Creates a new bundle binary.
323323
pub fn new(name: String, main: bool) -> Self {
324324
Self {
325-
name: if cfg!(windows) {
326-
format!("{}.exe", name)
327-
} else {
328-
name
329-
},
325+
name,
330326
src_path: None,
331327
main,
332328
}

tooling/cli/src/build.rs

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -168,10 +168,20 @@ pub fn command(options: Options) -> Result<()> {
168168
.name
169169
.clone()
170170
.expect("Cargo manifest must have the `package.name` field");
171-
#[cfg(windows)]
172-
let bin_path = out_dir.join(format!("{}.exe", bin_name));
173-
#[cfg(not(windows))]
174-
let bin_path = out_dir.join(&bin_name);
171+
172+
let target: String = if let Some(target) = options.target.clone() {
173+
target
174+
} else {
175+
tauri_utils::platform::target_triple()?
176+
};
177+
let binary_extension: String = if target.contains("windows") {
178+
".exe"
179+
} else {
180+
""
181+
}
182+
.into();
183+
184+
let bin_path = out_dir.join(&bin_name).with_extension(&binary_extension);
175185

176186
let no_default_features = args.contains(&"--no-default-features".into());
177187

@@ -211,12 +221,13 @@ pub fn command(options: Options) -> Result<()> {
211221
}
212222

213223
if let Some(product_name) = config_.package.product_name.clone() {
214-
#[cfg(windows)]
215-
let product_path = out_dir.join(format!("{}.exe", product_name));
216-
#[cfg(target_os = "macos")]
217-
let product_path = out_dir.join(product_name);
218224
#[cfg(target_os = "linux")]
219-
let product_path = out_dir.join(product_name.to_kebab_case());
225+
let product_name = product_name.to_kebab_case();
226+
227+
let product_path = out_dir
228+
.join(&product_name)
229+
.with_extension(&binary_extension);
230+
220231
rename(&bin_path, &product_path).with_context(|| {
221232
format!(
222233
"failed to rename `{}` to `{}`",
@@ -314,7 +325,7 @@ pub fn command(options: Options) -> Result<()> {
314325
}
315326
let settings = crate::interface::get_bundler_settings(
316327
app_settings,
317-
options.target.clone(),
328+
target,
318329
&enabled_features,
319330
&manifest,
320331
config_,

tooling/cli/src/interface/mod.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use tauri_bundler::bundle::{PackageType, Settings, SettingsBuilder};
1212
#[allow(clippy::too_many_arguments)]
1313
pub fn get_bundler_settings(
1414
app_settings: rust::AppSettings,
15-
target: Option<String>,
15+
target: String,
1616
features: &[String],
1717
manifest: &Manifest,
1818
config: &Config,
@@ -23,8 +23,9 @@ pub fn get_bundler_settings(
2323
let mut settings_builder = SettingsBuilder::new()
2424
.package_settings(app_settings.get_package_settings())
2525
.bundle_settings(app_settings.get_bundle_settings(config, manifest, features)?)
26-
.binaries(app_settings.get_binaries(config)?)
27-
.project_out_directory(out_dir);
26+
.binaries(app_settings.get_binaries(config, &target)?)
27+
.project_out_directory(out_dir)
28+
.target(target);
2829

2930
if verbose {
3031
settings_builder = settings_builder.verbose();
@@ -34,9 +35,5 @@ pub fn get_bundler_settings(
3435
settings_builder = settings_builder.package_types(types);
3536
}
3637

37-
if let Some(target) = target {
38-
settings_builder = settings_builder.target(target);
39-
}
40-
4138
settings_builder.build().map_err(Into::into)
4239
}

tooling/cli/src/interface/rust.rs

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,16 @@ impl AppSettings {
198198
self.package_settings.clone()
199199
}
200200

201-
pub fn get_binaries(&self, config: &Config) -> crate::Result<Vec<BundleBinary>> {
201+
pub fn get_binaries(&self, config: &Config, target: &str) -> crate::Result<Vec<BundleBinary>> {
202202
let mut binaries: Vec<BundleBinary> = vec![];
203+
204+
let binary_extension: String = if target.contains("windows") {
205+
".exe"
206+
} else {
207+
""
208+
}
209+
.into();
210+
203211
if let Some(bin) = &self.cargo_settings.bin {
204212
let default_run = self
205213
.package_settings
@@ -212,14 +220,21 @@ impl AppSettings {
212220
|| binary.name.as_str() == default_run
213221
{
214222
BundleBinary::new(
215-
config
216-
.package
217-
.binary_name()
218-
.unwrap_or_else(|| binary.name.clone()),
223+
format!(
224+
"{}{}",
225+
config
226+
.package
227+
.binary_name()
228+
.unwrap_or_else(|| binary.name.clone()),
229+
&binary_extension
230+
),
219231
true,
220232
)
221233
} else {
222-
BundleBinary::new(binary.name.clone(), false)
234+
BundleBinary::new(
235+
format!("{}{}", binary.name.clone(), &binary_extension),
236+
false,
237+
)
223238
}
224239
.set_src_path(binary.path.clone()),
225240
)
@@ -236,7 +251,10 @@ impl AppSettings {
236251
bin.name() == name || path.ends_with(bin.src_path().unwrap_or(&"".to_string()))
237252
});
238253
if !bin_exists {
239-
binaries.push(BundleBinary::new(name.to_string_lossy().to_string(), false))
254+
binaries.push(BundleBinary::new(
255+
format!("{}{}", name.to_string_lossy(), &binary_extension),
256+
false,
257+
))
240258
}
241259
}
242260
}
@@ -251,10 +269,14 @@ impl AppSettings {
251269
}
252270
None => {
253271
binaries.push(BundleBinary::new(
254-
config
255-
.package
256-
.binary_name()
257-
.unwrap_or_else(|| default_run.to_string()),
272+
format!(
273+
"{}{}",
274+
config
275+
.package
276+
.binary_name()
277+
.unwrap_or_else(|| default_run.to_string()),
278+
&binary_extension
279+
),
258280
true,
259281
));
260282
}
@@ -266,7 +288,11 @@ impl AppSettings {
266288
#[cfg(target_os = "linux")]
267289
self.package_settings.product_name.to_kebab_case(),
268290
#[cfg(not(target_os = "linux"))]
269-
self.package_settings.product_name.clone(),
291+
format!(
292+
"{}{}",
293+
self.package_settings.product_name.clone(),
294+
&binary_extension
295+
),
270296
true,
271297
)),
272298
1 => binaries.get_mut(0).unwrap().set_main(true),

0 commit comments

Comments
 (0)