fix(bundler): Use arch instead of llvm_target. fix #3285 (#3286)

Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
This commit is contained in:
Jonas Kruckenberg 2022-02-05 04:40:13 +01:00 committed by GitHub
parent 8d39741aa1
commit 839daec7ab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 65 additions and 20 deletions

View File

@ -0,0 +1,5 @@
---
"tauri-bundler": patch
---
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.

View File

@ -40,7 +40,6 @@ walkdir = "2"
handlebars = { version = "4.2" }
zip = { version = "0.5" }
tempfile = "3.3.0"
regex = "1"
[target."cfg(target_os = \"windows\")".dependencies]
attohttpc = "0.18"
@ -54,6 +53,9 @@ hex = "0.4"
chrono = "0.4"
dirs-next = "2.0"
[target."cfg(any(target_os = \"macos\", target_os = \"windows\"))".dependencies]
regex = "1"
[target."cfg(target_os = \"linux\")".dependencies]
heck = "0.4"

View File

@ -2,17 +2,29 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
use std::{io::Cursor, process::Command};
use std::process::Command;
#[derive(Debug, serde::Deserialize)]
struct TargetSpec {
#[serde(rename = "llvm-target")]
llvm_target: String,
// Copyright 2019-2021 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
#[derive(Debug, PartialEq, Eq)]
struct RustCfg {
target_arch: Option<String>,
}
// Copyright 2019-2021 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
fn parse_rust_cfg(cfg: String) -> RustCfg {
let target_line = "target_arch=\"";
let mut target_arch = None;
for line in cfg.split('\n') {
if line.starts_with(target_line) {
let len = target_line.len();
let arch = line.chars().skip(len).take(line.len() - len - 1).collect();
target_arch.replace(arch);
}
}
RustCfg { target_arch }
}
/// Try to determine the current target triple.
///
@ -24,18 +36,12 @@ struct TargetSpec {
/// * Errors:
/// * Unexpected system config
pub fn target_triple() -> Result<String, crate::Error> {
let output = Command::new("rustc")
.args(&["-Z", "unstable-options", "--print", "target-spec-json"])
.env("RUSTC_BOOTSTRAP", "1")
.output()?;
let output = Command::new("rustc").args(&["--print", "cfg"]).output()?;
let arch = if output.status.success() {
let target_spec: TargetSpec = serde_json::from_reader(Cursor::new(output.stdout))?;
target_spec
.llvm_target
.split('-')
.next()
.unwrap()
.to_string()
parse_rust_cfg(String::from_utf8_lossy(&output.stdout).into_owned())
.target_arch
.expect("could not find `target_arch` when running `rustc --print cfg`.")
} else {
super::common::print_info(&format!(
"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> {
Ok(format!("{}-{}", arch, os))
}
#[cfg(test)]
mod tests {
use super::RustCfg;
#[test]
fn parse_rust_cfg() {
assert_eq!(
super::parse_rust_cfg("target_arch".into()),
RustCfg { target_arch: None }
);
assert_eq!(
super::parse_rust_cfg(
r#"debug_assertions
target_arch="aarch64"
target_endian="little"
target_env=""
target_family="unix"
target_os="macos"
target_pointer_width="64"
target_vendor="apple"
unix"#
.into()
),
RustCfg {
target_arch: Some("aarch64".into())
}
);
}
}

View File

@ -50,6 +50,7 @@ pub enum Error {
#[error("`{0}`")]
JsonError(#[from] serde_json::error::Error),
/// Regex error.
#[cfg(any(target_os = "macos", windows))]
#[error("`{0}`")]
RegexError(#[from] regex::Error),
/// Failed to perform HTTP request.