Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 6 commits into from
Feb 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changes/bundler-print-cfg.md
Original file line number Diff line number Diff line change
@@ -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.
4 changes: 3 additions & 1 deletion tooling/bundler/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"

Expand Down
73 changes: 55 additions & 18 deletions tooling/bundler/src/bundle/platform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,30 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT

use std::{io::Cursor, process::Command};

#[derive(Debug, serde::Deserialize)]
struct TargetSpec {
#[serde(rename = "llvm-target")]
llvm_target: String,
}
use std::process::Command;

// 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>,
}

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.
///
/// Returns a target triple (e.g. `x86_64-unknown-linux-gnu` or `i686-pc-windows-msvc`) or an
Expand All @@ -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.",
Expand Down Expand Up @@ -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())
}
);
}
}
1 change: 1 addition & 0 deletions tooling/bundler/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down