|
| 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 | + |
1 | 9 | // Copyright 2019-2021 Tauri Programme within The Commons Conservancy |
2 | 10 | // SPDX-License-Identifier: Apache-2.0 |
3 | 11 | // SPDX-License-Identifier: MIT |
|
12 | 20 | /// * Errors: |
13 | 21 | /// * Unexpected system config |
14 | 22 | 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() |
23 | 35 | } 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 | + } |
27 | 53 | }; |
28 | 54 |
|
29 | 55 | let os = if cfg!(target_os = "linux") { |
|
0 commit comments