detect-targets: Add fallback to x86_64-apple-darwin (#1233)

Fallback to `x86_64h-apple-darwin` if supported.

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
Jiahao XU 2023-07-28 17:59:23 +10:00 committed by GitHub
parent 333c1805e1
commit d5549ce99e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,4 +1,4 @@
use std::{io, process::Stdio}; use std::process::Stdio;
use tokio::process::Command; use tokio::process::Command;
@ -21,22 +21,22 @@ const X86H: &str = "x86_64h-apple-darwin";
const UNIVERSAL: &str = "universal-apple-darwin"; const UNIVERSAL: &str = "universal-apple-darwin";
const UNIVERSAL2: &str = "universal2-apple-darwin"; const UNIVERSAL2: &str = "universal2-apple-darwin";
async fn is_x86_64_supported() -> io::Result<bool> { async fn is_arch_supported(arch_name: &str) -> bool {
let exit_status = Command::new("arch") Command::new("arch")
.args(["-arch", "x86_64", "/usr/bin/true"]) .args(["-arch", arch_name, "/usr/bin/true"])
.stdin(Stdio::null()) .stdin(Stdio::null())
.stdout(Stdio::null()) .stdout(Stdio::null())
.stderr(Stdio::null()) .stderr(Stdio::null())
.status() .status()
.await?; .await
.map(|exit_status| exit_status.success())
Ok(exit_status.success()) .unwrap_or(false)
} }
pub(super) async fn detect_alternative_targets(target: &str) -> impl Iterator<Item = String> { pub(super) async fn detect_alternative_targets(target: &str) -> impl Iterator<Item = String> {
match target { match target {
AARCH64 => { AARCH64 => {
let is_x86_64_supported = is_x86_64_supported().await.unwrap_or(false); let is_x86_64_supported = is_arch_supported("x86_64").await;
[ [
// Prefer universal as it provides native arm executable // Prefer universal as it provides native arm executable
Some(UNIVERSAL), Some(UNIVERSAL),
@ -46,7 +46,12 @@ pub(super) async fn detect_alternative_targets(target: &str) -> impl Iterator<It
is_x86_64_supported.then_some(X86), is_x86_64_supported.then_some(X86),
] ]
} }
X86 => [Some(UNIVERSAL), Some(UNIVERSAL2), None, None], X86 => [
is_arch_supported("x86_64h").await.then_some(X86H),
Some(UNIVERSAL),
Some(UNIVERSAL2),
None,
],
X86H => [Some(X86), Some(UNIVERSAL), Some(UNIVERSAL2), None], X86H => [Some(X86), Some(UNIVERSAL), Some(UNIVERSAL2), None],
_ => [None, None, None, None], _ => [None, None, None, None],
} }