From e89afeb2937e00ba1f325dbb372bcd98a34cf935 Mon Sep 17 00:00:00 2001 From: messense Date: Sat, 10 Sep 2022 23:54:01 +0800 Subject: [PATCH] Add `-mcpu` option for arm* targets --- .github/workflows/CI.yml | 4 +++- src/zig.rs | 31 ++++++++++++++++++++++++++----- 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index 576c247..f2e80d5 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -118,11 +118,13 @@ jobs: run: | rustup target add aarch64-unknown-linux-gnu rustup target add arm-unknown-linux-gnueabihf + rustup target add armv7-unknown-linux-gnueabihf cargo run zigbuild --target aarch64-unknown-linux-gnu cargo run zigbuild --target aarch64-unknown-linux-gnu.2.17 - cargo run zigbuild --target arm-unknown-linux-gnueabihf cargo run zigbuild --target aarch64-unknown-linux-gnu --manifest-path tests/hello-rustls/Cargo.toml + cargo run zigbuild --target armv7-unknown-linux-gnueabihf --manifest-path tests/hello-rustls/Cargo.toml + cargo run zigbuild --target arm-unknown-linux-gnueabihf --manifest-path tests/hello-rustls/Cargo.toml # Test building shared library cargo run zigbuild --target aarch64-unknown-linux-gnu --manifest-path tests/libhello/Cargo.toml diff --git a/src/zig.rs b/src/zig.rs index b357c5d..b718368 100644 --- a/src/zig.rs +++ b/src/zig.rs @@ -74,7 +74,7 @@ impl Zig { let is_windows_msvc = target .map(|x| x.contains("windows-msvc")) .unwrap_or_default(); - let is_arm = target.map(|x| x.contains("arm")).unwrap_or_default(); + let is_arm = target.map(|x| x.starts_with("arm")).unwrap_or_default(); let is_macos = target.map(|x| x.contains("macos")).unwrap_or_default(); let rustc_ver = rustc_version::version()?; @@ -115,6 +115,10 @@ impl Zig { return None; } } + // Ignore `-march` option for arm* targets, we use `generic` + cpu features instead + if is_arm && arg.starts_with("-march=") { + return None; + } Some(arg.to_string()) }; let has_undefined_dynamic_lookup = |args: &[String]| { @@ -546,10 +550,27 @@ pub fn prepare_zig_linker(target: &str) -> Result { let zig_cxx = format!("zigcxx-{}.{}", file_target, file_ext); let cc_args = "-g"; // prevent stripping let mut cc_args = match triple.operating_system { - OperatingSystem::Linux => format!( - "-target {}-linux-{}{} {}", - arch, target_env, abi_suffix, cc_args, - ), + OperatingSystem::Linux => { + let (zig_arch, zig_cpu) = match arch.as_str() { + // zig uses _ instead of - in cpu features + "arm" => match target_env { + Environment::Gnueabi | Environment::Musleabi => { + ("arm", "-mcpu=generic+v6+strict_align") + } + Environment::Gnueabihf | Environment::Musleabihf => { + ("arm", "-mcpu=generic+v6+strict_align+vfp2-d32") + } + _ => ("arm", ""), + }, + "armv5te" => ("arm", "-mcpu=generic+soft_float+strict_align"), + "armv7" => ("arm", "-mcpu=generic+v7a+vfp3-d32+thumb2-neon"), + _ => (arch.as_str(), ""), + }; + format!( + "-target {}-linux-{}{} {} {}", + zig_arch, target_env, abi_suffix, zig_cpu, cc_args, + ) + } OperatingSystem::MacOSX { .. } | OperatingSystem::Darwin => { let zig_version = Zig::zig_version()?; // Zig 0.10.0 switched macOS ABI to none