Skip to content

Commit 839daec

Browse files
fix(bundler): Use arch instead of llvm_target. fix #3285 (#3286)
Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
1 parent 8d39741 commit 839daec

File tree

4 files changed

+64
-19
lines changed

4 files changed

+64
-19
lines changed

.changes/bundler-print-cfg.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri-bundler": patch
3+
---
4+
5+
Replaces usage of the nightly command `RUSTC_BOOTSTRAP=1 rustc -Z unstable-options --print target-spec-json` with the stable command `rustc --print cfg`, improving target triple detection.

tooling/bundler/Cargo.toml

+3-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ walkdir = "2"
4040
handlebars = { version = "4.2" }
4141
zip = { version = "0.5" }
4242
tempfile = "3.3.0"
43-
regex = "1"
4443

4544
[target."cfg(target_os = \"windows\")".dependencies]
4645
attohttpc = "0.18"
@@ -54,6 +53,9 @@ hex = "0.4"
5453
chrono = "0.4"
5554
dirs-next = "2.0"
5655

56+
[target."cfg(any(target_os = \"macos\", target_os = \"windows\"))".dependencies]
57+
regex = "1"
58+
5759
[target."cfg(target_os = \"linux\")".dependencies]
5860
heck = "0.4"
5961

tooling/bundler/src/bundle/platform.rs

+55-18
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,30 @@
22
// SPDX-License-Identifier: Apache-2.0
33
// SPDX-License-Identifier: MIT
44

5-
use std::{io::Cursor, process::Command};
6-
7-
#[derive(Debug, serde::Deserialize)]
8-
struct TargetSpec {
9-
#[serde(rename = "llvm-target")]
10-
llvm_target: String,
11-
}
5+
use std::process::Command;
126

137
// Copyright 2019-2021 Tauri Programme within The Commons Conservancy
148
// SPDX-License-Identifier: Apache-2.0
159
// SPDX-License-Identifier: MIT
1610

11+
#[derive(Debug, PartialEq, Eq)]
12+
struct RustCfg {
13+
target_arch: Option<String>,
14+
}
15+
16+
fn parse_rust_cfg(cfg: String) -> RustCfg {
17+
let target_line = "target_arch=\"";
18+
let mut target_arch = None;
19+
for line in cfg.split('\n') {
20+
if line.starts_with(target_line) {
21+
let len = target_line.len();
22+
let arch = line.chars().skip(len).take(line.len() - len - 1).collect();
23+
target_arch.replace(arch);
24+
}
25+
}
26+
RustCfg { target_arch }
27+
}
28+
1729
/// Try to determine the current target triple.
1830
///
1931
/// Returns a target triple (e.g. `x86_64-unknown-linux-gnu` or `i686-pc-windows-msvc`) or an
@@ -24,18 +36,12 @@ struct TargetSpec {
2436
/// * Errors:
2537
/// * Unexpected system config
2638
pub fn target_triple() -> Result<String, crate::Error> {
27-
let output = Command::new("rustc")
28-
.args(&["-Z", "unstable-options", "--print", "target-spec-json"])
29-
.env("RUSTC_BOOTSTRAP", "1")
30-
.output()?;
39+
let output = Command::new("rustc").args(&["--print", "cfg"]).output()?;
40+
3141
let arch = if output.status.success() {
32-
let target_spec: TargetSpec = serde_json::from_reader(Cursor::new(output.stdout))?;
33-
target_spec
34-
.llvm_target
35-
.split('-')
36-
.next()
37-
.unwrap()
38-
.to_string()
42+
parse_rust_cfg(String::from_utf8_lossy(&output.stdout).into_owned())
43+
.target_arch
44+
.expect("could not find `target_arch` when running `rustc --print cfg`.")
3945
} else {
4046
super::common::print_info(&format!(
4147
"failed to determine target arch using rustc, error: `{}`. The fallback is the architecture of the machine that compiled this crate.",
@@ -90,3 +96,34 @@ pub fn target_triple() -> Result<String, crate::Error> {
9096

9197
Ok(format!("{}-{}", arch, os))
9298
}
99+
100+
#[cfg(test)]
101+
mod tests {
102+
use super::RustCfg;
103+
104+
#[test]
105+
fn parse_rust_cfg() {
106+
assert_eq!(
107+
super::parse_rust_cfg("target_arch".into()),
108+
RustCfg { target_arch: None }
109+
);
110+
111+
assert_eq!(
112+
super::parse_rust_cfg(
113+
r#"debug_assertions
114+
target_arch="aarch64"
115+
target_endian="little"
116+
target_env=""
117+
target_family="unix"
118+
target_os="macos"
119+
target_pointer_width="64"
120+
target_vendor="apple"
121+
unix"#
122+
.into()
123+
),
124+
RustCfg {
125+
target_arch: Some("aarch64".into())
126+
}
127+
);
128+
}
129+
}

tooling/bundler/src/error.rs

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ pub enum Error {
5050
#[error("`{0}`")]
5151
JsonError(#[from] serde_json::error::Error),
5252
/// Regex error.
53+
#[cfg(any(target_os = "macos", windows))]
5354
#[error("`{0}`")]
5455
RegexError(#[from] regex::Error),
5556
/// Failed to perform HTTP request.

0 commit comments

Comments
 (0)