Skip to content

Commit 5ebf389

Browse files
authored
feat(bundler): check target arch at runtime, closes #2286 (#2422)
1 parent 4a031ad commit 5ebf389

2 files changed

Lines changed: 42 additions & 11 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri-bundler": patch
3+
---
4+
5+
Check target architecture at runtime running `$ RUSTC_BOOTSTRAP=1 rustc -Z unstable-options --print target-spec-json` and parsing the `llvm-target` field, fixing macOS M1 sidecar check until we can compile the CLI with M1 target on GitHub Actions.

tooling/bundler/src/bundle/platform.rs

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
use std::{io::Cursor, process::Command};
2+
3+
#[derive(Debug, serde::Deserialize)]
4+
struct TargetSpec {
5+
#[serde(rename = "llvm-target")]
6+
llvm_target: String,
7+
}
8+
19
// Copyright 2019-2021 Tauri Programme within The Commons Conservancy
210
// SPDX-License-Identifier: Apache-2.0
311
// SPDX-License-Identifier: MIT
@@ -12,18 +20,36 @@
1220
/// * Errors:
1321
/// * Unexpected system config
1422
pub fn target_triple() -> Result<String, crate::Error> {
15-
let arch = if cfg!(target_arch = "x86") {
16-
"i686"
17-
} else if cfg!(target_arch = "x86_64") {
18-
"x86_64"
19-
} else if cfg!(target_arch = "arm") {
20-
"armv7"
21-
} else if cfg!(target_arch = "aarch64") {
22-
"aarch64"
23+
let output = Command::new("rustc")
24+
.args(&["-Z", "unstable-options", "--print", "target-spec-json"])
25+
.env("RUSTC_BOOTSTRAP", "1")
26+
.output()?;
27+
let arch = if output.status.success() {
28+
let target_spec: TargetSpec = serde_json::from_reader(Cursor::new(output.stdout))?;
29+
target_spec
30+
.llvm_target
31+
.split('-')
32+
.next()
33+
.unwrap()
34+
.to_string()
2335
} else {
24-
return Err(crate::Error::ArchError(String::from(
25-
"Unable to determine target-architecture",
26-
)));
36+
super::common::print_info(&format!(
37+
"failed to determine target arch using rustc, error: `{}`. The fallback is the architecture of the machine that compiled this crate.",
38+
String::from_utf8_lossy(&output.stderr),
39+
))?;
40+
if cfg!(target_arch = "x86") {
41+
"i686".into()
42+
} else if cfg!(target_arch = "x86_64") {
43+
"x86_64".into()
44+
} else if cfg!(target_arch = "arm") {
45+
"armv7".into()
46+
} else if cfg!(target_arch = "aarch64") {
47+
"aarch64".into()
48+
} else {
49+
return Err(crate::Error::ArchError(String::from(
50+
"Unable to determine target-architecture",
51+
)));
52+
}
2753
};
2854

2955
let os = if cfg!(target_os = "linux") {

0 commit comments

Comments
 (0)