diff --git a/.changes/bundler-print-cfg.md b/.changes/bundler-print-cfg.md new file mode 100644 index 000000000..d38297888 --- /dev/null +++ b/.changes/bundler-print-cfg.md @@ -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. diff --git a/tooling/bundler/Cargo.toml b/tooling/bundler/Cargo.toml index e118d53b8..66e4d006f 100644 --- a/tooling/bundler/Cargo.toml +++ b/tooling/bundler/Cargo.toml @@ -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" diff --git a/tooling/bundler/src/bundle/platform.rs b/tooling/bundler/src/bundle/platform.rs index b4b76fe36..b15610d5e 100644 --- a/tooling/bundler/src/bundle/platform.rs +++ b/tooling/bundler/src/bundle/platform.rs @@ -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, } -// 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 { - 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 { 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()) + } + ); + } +} diff --git a/tooling/bundler/src/error.rs b/tooling/bundler/src/error.rs index f751179a0..e12a4f734 100644 --- a/tooling/bundler/src/error.rs +++ b/tooling/bundler/src/error.rs @@ -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.