Skip to content

Commit 8e0fa2e

Browse files
authored
fix(cli): bundle with the target triple resolved from cargo config (#16097)
1 parent 9642b30 commit 8e0fa2e

4 files changed

Lines changed: 98 additions & 12 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"tauri-cli": patch:bug
3+
"@tauri-apps/cli": patch:bug
4+
---
5+
6+
Fix the bundler using the host target triple when `--target` is not passed but `build.target` is set in `.cargo/config.toml`. The CLI now also accepts `build.target` as an array and honors the `CARGO_BUILD_TARGET` environment variable.

‎crates/tauri-cli/src/interface/mod.rs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ pub trait AppSettings {
3939
) -> crate::Result<Vec<tauri_bundler::BundleBinary>>;
4040
fn app_name(&self) -> Option<String>;
4141
fn lib_name(&self) -> Option<String>;
42+
/// The resolved target triple (`--target`, cargo's `build.target` or the host triple).
43+
fn target_triple(&self) -> &str;
4244

4345
fn get_bundler_settings(
4446
&self,
@@ -57,7 +59,7 @@ pub trait AppSettings {
5759
let target: String = if let Some(target) = options.target.clone() {
5860
target
5961
} else {
60-
tauri_utils::platform::target_triple().context("failed to get target triple")?
62+
self.target_triple().to_string()
6163
};
6264

6365
let mut bins = self.get_binaries(&options, tauri_dir)?;

‎crates/tauri-cli/src/interface/rust.rs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,6 +1013,10 @@ impl AppSettings for RustAppSettings {
10131013
Ok(binaries)
10141014
}
10151015

1016+
fn target_triple(&self) -> &str {
1017+
&self.target_triple
1018+
}
1019+
10161020
fn app_name(&self) -> Option<String> {
10171021
self
10181022
.manifest

‎crates/tauri-cli/src/interface/rust/cargo_config.rs‎

Lines changed: 85 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,51 @@ impl<'a> Iterator for PathAncestors<'a> {
4141
}
4242
}
4343

44-
#[derive(Default, Deserialize)]
44+
/// `build.target` can be a single target or a list of targets.
45+
#[derive(Deserialize)]
46+
#[serde(untagged)]
47+
enum BuildTarget {
48+
Single(String),
49+
Multiple(Vec<String>),
50+
}
51+
52+
impl BuildTarget {
53+
fn into_target(self) -> Option<String> {
54+
match self {
55+
Self::Single(target) => Some(target),
56+
Self::Multiple(targets) => {
57+
if targets.len() > 1 {
58+
log::warn!(
59+
"Multiple targets configured in `build.target` ({}), using the first one: {}",
60+
targets.join(", "),
61+
targets[0]
62+
);
63+
}
64+
targets.into_iter().next()
65+
}
66+
}
67+
}
68+
}
69+
70+
#[derive(Deserialize)]
71+
struct BuildConfigSchema {
72+
target: Option<BuildTarget>,
73+
}
74+
75+
#[derive(Default)]
4576
pub struct BuildConfig {
4677
target: Option<String>,
4778
}
4879

4980
#[derive(Deserialize)]
50-
pub struct ConfigSchema {
51-
build: Option<BuildConfig>,
81+
struct ConfigSchema {
82+
build: Option<BuildConfigSchema>,
83+
}
84+
85+
impl ConfigSchema {
86+
fn build_target(self) -> Option<String> {
87+
self.build?.target?.into_target()
88+
}
5289
}
5390

5491
#[derive(Default)]
@@ -69,21 +106,30 @@ impl Config {
69106
))
70107
};
71108

72-
for current in PathAncestors::new(path) {
73-
if let Some(path) = get_file_path(&current.join(".cargo"), "config", true)? {
74-
let toml = get_config(path)?;
75-
if let Some(target) = toml.build.and_then(|b| b.target) {
76-
config.build.target = Some(target);
77-
break;
109+
// the environment variable takes precedence over the configuration files
110+
if let Ok(target) = std::env::var("CARGO_BUILD_TARGET") {
111+
let targets = target
112+
.split_whitespace()
113+
.map(ToString::to_string)
114+
.collect::<Vec<_>>();
115+
config.build.target = BuildTarget::Multiple(targets).into_target();
116+
}
117+
118+
if config.build.target.is_none() {
119+
for current in PathAncestors::new(path) {
120+
if let Some(path) = get_file_path(&current.join(".cargo"), "config", true)? {
121+
if let Some(target) = get_config(path)?.build_target() {
122+
config.build.target = Some(target);
123+
break;
124+
}
78125
}
79126
}
80127
}
81128

82129
if config.build.target.is_none() {
83130
if let Ok(cargo_home) = std::env::var("CARGO_HOME") {
84131
if let Some(path) = get_file_path(&PathBuf::from(cargo_home), "config", true)? {
85-
let toml = get_config(path)?;
86-
if let Some(target) = toml.build.and_then(|b| b.target) {
132+
if let Some(target) = get_config(path)?.build_target() {
87133
config.build.target = Some(target);
88134
}
89135
}
@@ -147,3 +193,31 @@ fn get_file_path(
147193
Ok(None)
148194
}
149195
}
196+
197+
#[cfg(test)]
198+
mod tests {
199+
use super::ConfigSchema;
200+
201+
fn build_target(toml: &str) -> Option<String> {
202+
toml::from_str::<ConfigSchema>(toml).unwrap().build_target()
203+
}
204+
205+
#[test]
206+
fn parse_build_target() {
207+
assert_eq!(build_target(""), None);
208+
assert_eq!(build_target("[build]\njobs = 2"), None);
209+
assert_eq!(
210+
build_target("[build]\ntarget = \"x86_64-pc-windows-msvc\""),
211+
Some("x86_64-pc-windows-msvc".into())
212+
);
213+
assert_eq!(
214+
build_target("[build]\ntarget = [\"aarch64-apple-darwin\"]"),
215+
Some("aarch64-apple-darwin".into())
216+
);
217+
assert_eq!(
218+
build_target("[build]\ntarget = [\"aarch64-apple-darwin\", \"x86_64-apple-darwin\"]"),
219+
Some("aarch64-apple-darwin".into())
220+
);
221+
assert_eq!(build_target("[build]\ntarget = []"), None);
222+
}
223+
}

0 commit comments

Comments
 (0)