| 1 |
// SPDX-License-Identifier: Apache-2.0 OR MIT |
| 2 |
|
| 3 |
// The rustc-cfg emitted by the build script are *not* public API. |
| 4 |
|
| 5 |
#![allow(clippy::match_same_arms)] // https://github.com/rust-lang/rust-clippy/issues/12044 |
| 6 |
|
| 7 |
#[path = "src/gen/build.rs"] |
| 8 |
mod generated; |
| 9 |
#[path = "version.rs"] |
| 10 |
mod version; |
| 11 |
|
| 12 |
use std::{env, str}; |
| 13 |
|
| 14 |
use self::version::{Version, rustc_version}; |
| 15 |
|
| 16 |
fn main() { |
| 17 |
println!("cargo:rerun-if-changed=build.rs"); |
| 18 |
println!("cargo:rerun-if-changed=src/gen/build.rs"); |
| 19 |
println!("cargo:rerun-if-changed=version.rs"); |
| 20 |
|
| 21 |
#[cfg(feature = "unsafe-assume-single-core")] |
| 22 |
println!("cargo:rustc-cfg=portable_atomic_unsafe_assume_single_core"); |
| 23 |
#[cfg(feature = "unsafe-assume-privileged")] |
| 24 |
println!("cargo:rustc-cfg=portable_atomic_unsafe_assume_privileged"); |
| 25 |
#[cfg(feature = "s-mode")] |
| 26 |
println!("cargo:rustc-cfg=portable_atomic_s_mode"); |
| 27 |
#[cfg(feature = "force-amo")] |
| 28 |
println!("cargo:rustc-cfg=portable_atomic_force_amo"); |
| 29 |
#[cfg(feature = "disable-fiq")] |
| 30 |
println!("cargo:rustc-cfg=portable_atomic_disable_fiq"); |
| 31 |
|
| 32 |
let target = &*env::var("TARGET").expect("TARGET not set"); |
| 33 |
let target_arch = &*env::var("CARGO_CFG_TARGET_ARCH").expect("CARGO_CFG_TARGET_ARCH not set"); |
| 34 |
let target_os = &*env::var("CARGO_CFG_TARGET_OS").expect("CARGO_CFG_TARGET_OS not set"); |
| 35 |
|
| 36 |
let version = match rustc_version() { |
| 37 |
Some(version) => version, |
| 38 |
None => { |
| 39 |
if env::var_os("PORTABLE_ATOMIC_DENY_WARNINGS").is_some() { |
| 40 |
panic!("unable to determine rustc version") |
| 41 |
} |
| 42 |
println!( |
| 43 |
"cargo:warning={}: unable to determine rustc version; assuming latest stable rustc (1.{})", |
| 44 |
env!("CARGO_PKG_NAME"), |
| 45 |
Version::LATEST.minor |
| 46 |
); |
| 47 |
Version::LATEST |
| 48 |
} |
| 49 |
}; |
| 50 |
|
| 51 |
if version.minor >= 80 { |
| 52 |
println!( |
| 53 |
r#"cargo:rustc-check-cfg=cfg(target_feature,values("lsfe","fast-serialization","load-store-on-cond","distinct-ops","rmw"))"# |
| 54 |
); |
| 55 |
|
| 56 |
// Custom cfgs set by build script. Not public API. |
| 57 |
// grep -F 'cargo:rustc-cfg=' build.rs | grep -Ev '^ *//' | sed -E 's/^.*cargo:rustc-cfg=//; s/(=\\)?".*$//' | LC_ALL=C sort -u | tr '\n' ',' | sed -E 's/,$/\n/' |
| 58 |
println!( |
| 59 |
"cargo:rustc-check-cfg=cfg(portable_atomic_atomic_intrinsics,portable_atomic_disable_fiq,portable_atomic_force_amo,portable_atomic_ll_sc_rmw,portable_atomic_no_asm,portable_atomic_no_asm_maybe_uninit,portable_atomic_no_asm_syscall,portable_atomic_no_atomic_64,portable_atomic_no_atomic_cas,portable_atomic_no_atomic_load_store,portable_atomic_no_atomic_min_max,portable_atomic_no_cfg_target_has_atomic,portable_atomic_no_cmpxchg16b_intrinsic,portable_atomic_no_cmpxchg16b_target_feature,portable_atomic_no_const_mut_refs,portable_atomic_no_const_raw_ptr_deref,portable_atomic_no_const_transmute,portable_atomic_no_core_unwind_safe,portable_atomic_no_diagnostic_namespace,portable_atomic_no_strict_provenance,portable_atomic_no_strict_provenance_atomic_ptr,portable_atomic_no_stronger_failure_ordering,portable_atomic_no_track_caller,portable_atomic_no_unsafe_op_in_unsafe_fn,portable_atomic_pre_llvm_16,portable_atomic_pre_llvm_18,portable_atomic_pre_llvm_20,portable_atomic_s_mode,portable_atomic_sanitize_thread,portable_atomic_target_feature,portable_atomic_unsafe_assume_privileged,portable_atomic_unsafe_assume_single_core,portable_atomic_unstable_asm,portable_atomic_unstable_asm_experimental_arch,portable_atomic_unstable_cfg_target_has_atomic,portable_atomic_unstable_isa_attribute)" |
| 60 |
); |
| 61 |
// TODO: handle multi-line target_feature_fallback |
| 62 |
// grep -F 'target_feature_fallback("' build.rs | grep -Ev '^ *//' | sed -E 's/^.*target_feature_fallback\(//; s/",.*$/"/' | LC_ALL=C sort -u | tr '\n' ',' | sed -E 's/,$/\n/' |
| 63 |
println!( |
| 64 |
r#"cargo:rustc-check-cfg=cfg(portable_atomic_target_feature,values("cmpxchg16b","distinct-ops","fast-serialization","load-store-on-cond","lse","lse128","lse2","lsfe","mclass","miscellaneous-extensions-3","quadword-atomics","rcpc3","rmw","v6","v7","zaamo","zabha","zacas"))"# |
| 65 |
); |
| 66 |
} |
| 67 |
|
| 68 |
// https://github.com/rust-lang/rust/pull/123745 (includes https://github.com/rust-lang/cargo/pull/13560) merged in Rust 1.79 (nightly-2024-04-11). |
| 69 |
if !version.probe(79, 2024, 4, 10) { |
| 70 |
// HACK: If --target is specified, rustflags is not applied to the build |
| 71 |
// script itself, so the build script will not be recompiled when rustflags |
| 72 |
// is changed. That in itself is not a problem, but the old Cargo does |
| 73 |
// not rerun the build script as well, which can be problematic. |
| 74 |
// https://github.com/rust-lang/cargo/issues/13003 |
| 75 |
// This problem has been fixed in 1.79 so only older versions need a workaround. |
| 76 |
println!("cargo:rerun-if-env-changed=CARGO_ENCODED_RUSTFLAGS"); |
| 77 |
println!("cargo:rerun-if-env-changed=RUSTFLAGS"); |
| 78 |
println!("cargo:rerun-if-env-changed=CARGO_BUILD_RUSTFLAGS"); |
| 79 |
let mut target_upper = target.replace(|c: char| c == '-' || c == '.', "_"); |
| 80 |
target_upper.make_ascii_uppercase(); |
| 81 |
println!("cargo:rerun-if-env-changed=CARGO_TARGET_{}_RUSTFLAGS", target_upper); |
| 82 |
} |
| 83 |
|
| 84 |
// Note that cfgs are `no_`*, not `has_*`. This allows treating as the latest |
| 85 |
// stable rustc is used when the build script doesn't run. This is useful |
| 86 |
// for non-cargo build systems that don't run the build script. |
| 87 |
|
| 88 |
// atomic_min_max stabilized in Rust 1.45 (nightly-2020-05-30): https://github.com/rust-lang/rust/pull/72324 |
| 89 |
if !version.probe(45, 2020, 5, 29) { |
| 90 |
println!("cargo:rustc-cfg=portable_atomic_no_atomic_min_max"); |
| 91 |
} |
| 92 |
// track_caller stabilized in Rust 1.46 (nightly-2020-07-02): https://github.com/rust-lang/rust/pull/72445 |
| 93 |
if !version.probe(46, 2020, 7, 1) { |
| 94 |
println!("cargo:rustc-cfg=portable_atomic_no_track_caller"); |
| 95 |
} |
| 96 |
// unsafe_op_in_unsafe_fn stabilized in Rust 1.52 (nightly-2021-03-11): https://github.com/rust-lang/rust/pull/79208 |
| 97 |
if !version.probe(52, 2021, 3, 10) { |
| 98 |
println!("cargo:rustc-cfg=portable_atomic_no_unsafe_op_in_unsafe_fn"); |
| 99 |
} |
| 100 |
// const_transmute stabilized in Rust 1.56 (nightly-2021-07-29): https://github.com/rust-lang/rust/pull/85769 |
| 101 |
if !version.probe(56, 2021, 7, 28) { |
| 102 |
println!("cargo:rustc-cfg=portable_atomic_no_const_transmute"); |
| 103 |
} |
| 104 |
// https://github.com/rust-lang/rust/pull/84662 merged in Rust 1.56 (nightly-2021-08-02). |
| 105 |
if !version.probe(56, 2021, 8, 1) { |
| 106 |
println!("cargo:rustc-cfg=portable_atomic_no_core_unwind_safe"); |
| 107 |
} |
| 108 |
// const_raw_ptr_deref stabilized in Rust 1.58 (nightly-2021-11-15): https://github.com/rust-lang/rust/pull/89551 |
| 109 |
if !version.probe(58, 2021, 11, 14) { |
| 110 |
println!("cargo:rustc-cfg=portable_atomic_no_const_raw_ptr_deref"); |
| 111 |
} |
| 112 |
// https://github.com/rust-lang/rust/pull/98383 merged in Rust 1.64 (nightly-2022-07-19). |
| 113 |
if !version.probe(64, 2022, 7, 18) { |
| 114 |
println!("cargo:rustc-cfg=portable_atomic_no_stronger_failure_ordering"); |
| 115 |
} |
| 116 |
// https://github.com/rust-lang/rust/pull/114790 merged in nightly-2023-08-24 |
| 117 |
if !version.probe(74, 2023, 8, 23) { |
| 118 |
println!("cargo:rustc-cfg=portable_atomic_no_asm_maybe_uninit"); |
| 119 |
} |
| 120 |
// #[diagnostic] stabilized in Rust 1.78 (nightly-2024-03-09): https://github.com/rust-lang/rust/pull/119888 |
| 121 |
if !version.probe(78, 2024, 3, 8) { |
| 122 |
println!("cargo:rustc-cfg=portable_atomic_no_diagnostic_namespace"); |
| 123 |
} |
| 124 |
// const_mut_refs/const_refs_to_cell stabilized in Rust 1.83 (nightly-2024-09-16): https://github.com/rust-lang/rust/pull/129195 |
| 125 |
if !version.probe(83, 2024, 9, 15) { |
| 126 |
println!("cargo:rustc-cfg=portable_atomic_no_const_mut_refs"); |
| 127 |
} |
| 128 |
// strict_provenance/exposed_provenance APIs stabilized in Rust 1.84 (nightly-2024-10-22): https://github.com/rust-lang/rust/pull/130350 |
| 129 |
if !version.probe(84, 2024, 10, 21) { |
| 130 |
println!("cargo:rustc-cfg=portable_atomic_no_strict_provenance"); |
| 131 |
} |
| 132 |
// strict_provenance_atomic_ptr stabilized in Rust 1.91 (nightly-2024-10-22): https://github.com/rust-lang/rust/pull/145467 |
| 133 |
if !version.probe(91, 2025, 8, 30) { |
| 134 |
println!("cargo:rustc-cfg=portable_atomic_no_strict_provenance_atomic_ptr"); |
| 135 |
} |
| 136 |
|
| 137 |
// For Miri and ThreadSanitizer. (aarch64, arm64ec, s390x, powerpc64) |
| 138 |
// https://github.com/rust-lang/rust/pull/97423 merged in Rust 1.64 (nightly-2022-06-30). |
| 139 |
// https://github.com/rust-lang/rust/pull/141507 merged in Rust 1.89 (nightly-2025-05-31). |
| 140 |
if version.nightly |
| 141 |
&& version.probe(64, 2022, 6, 29) |
| 142 |
&& !version.probe(89, 2025, 5, 30) |
| 143 |
&& (target_arch != "powerpc64" || version.llvm >= 15) |
| 144 |
{ |
| 145 |
println!("cargo:rustc-cfg=portable_atomic_atomic_intrinsics"); |
| 146 |
} |
| 147 |
|
| 148 |
// asm! on AArch64, Arm, RISC-V, x86, and x86_64 stabilized in Rust 1.59 (nightly-2021-12-16): https://github.com/rust-lang/rust/pull/91728 |
| 149 |
let no_asm = !version.probe(59, 2021, 12, 15); |
| 150 |
if no_asm { |
| 151 |
if version.nightly |
| 152 |
&& version.probe(46, 2020, 6, 20) |
| 153 |
&& ((target_arch != "x86" && target_arch != "x86_64") || version.llvm >= 10) |
| 154 |
&& is_allowed_feature("asm") |
| 155 |
{ |
| 156 |
// This feature was added in Rust 1.45 (nightly-2020-05-20), but |
| 157 |
// concat! in asm! requires Rust 1.46 (nightly-2020-06-21). |
| 158 |
// x86 intel syntax requires LLVM 10 (since Rust 1.53, the minimum |
| 159 |
// external LLVM version is 10+: https://github.com/rust-lang/rust/pull/83387). |
| 160 |
// The part of this feature we use has not been changed since nightly-2020-06-21 |
| 161 |
// until it was stabilized, so it can safely be enabled in nightly for that period. |
| 162 |
println!("cargo:rustc-cfg=portable_atomic_unstable_asm"); |
| 163 |
if (target_arch == "riscv32" || target_arch == "riscv64") |
| 164 |
&& !version.probe(56, 2021, 8, 15) |
| 165 |
{ |
| 166 |
// Clobbering vector status needed for asm syscall require Rust 1.56 (nightly-2021-08-16): https://github.com/rust-lang/rust/pull/87581 |
| 167 |
println!("cargo:rustc-cfg=portable_atomic_no_asm_syscall"); |
| 168 |
} |
| 169 |
} |
| 170 |
println!("cargo:rustc-cfg=portable_atomic_no_asm"); |
| 171 |
} else { |
| 172 |
match target_arch { |
| 173 |
"arm64ec" | "s390x" => { |
| 174 |
// asm! on Arm64EC and s390x stabilized in Rust 1.84 (nightly-2024-11-11): https://github.com/rust-lang/rust/pull/131781, https://github.com/rust-lang/rust/pull/131258 |
| 175 |
if !version.probe(84, 2024, 11, 10) { |
| 176 |
if version.nightly |
| 177 |
&& (target_arch != "s390x" || version.probe(71, 2023, 5, 8)) |
| 178 |
&& is_allowed_feature("asm_experimental_arch") |
| 179 |
{ |
| 180 |
// https://github.com/rust-lang/rust/pull/111331 merged in Rust 1.71 (nightly-2023-05-09). |
| 181 |
// The part of this feature we use has not been changed since nightly-2023-05-09 |
| 182 |
// until it was stabilized, so it can safely be enabled in nightly for that period. |
| 183 |
println!("cargo:rustc-cfg=portable_atomic_unstable_asm_experimental_arch"); |
| 184 |
} else { |
| 185 |
println!("cargo:rustc-cfg=portable_atomic_no_asm"); |
| 186 |
} |
| 187 |
} |
| 188 |
} |
| 189 |
"powerpc64" => { |
| 190 |
// asm! on PowerPC stabilized in Rust 1.95 (nightly-2026-01-28): https://github.com/rust-lang/rust/pull/147996 |
| 191 |
if !version.probe(95, 2026, 1, 27) { |
| 192 |
if version.nightly |
| 193 |
&& version.probe(60, 2022, 2, 12) |
| 194 |
&& is_allowed_feature("asm_experimental_arch") |
| 195 |
{ |
| 196 |
// https://github.com/rust-lang/rust/pull/93868 merged in Rust 1.60 (nightly-2022-02-13). |
| 197 |
// The part of this feature we use has not been changed since nightly-2022-02-13 |
| 198 |
// until it was stabilized, so it can safely be enabled in nightly for that period. |
| 199 |
println!("cargo:rustc-cfg=portable_atomic_unstable_asm_experimental_arch"); |
| 200 |
} else { |
| 201 |
println!("cargo:rustc-cfg=portable_atomic_no_asm"); |
| 202 |
} |
| 203 |
if !version.probe(92, 2025, 9, 22) |
| 204 |
|| env::var("CARGO_CFG_TARGET_ABI") |
| 205 |
.unwrap_or_default() |
| 206 |
.split(',') |
| 207 |
.any(|abi| abi == "spu") |
| 208 |
{ |
| 209 |
// Clobbering ctr register needed for asm syscall require Rust 1.92 (nightly-2025-09-23): https://github.com/rust-lang/rust/pull/146831 |
| 210 |
// Cell/B.E. Linux's SPU syscalls support a subset of powerpc64 syscalls with the exact semantics: https://github.com/torvalds/linux/blob/v7.1/arch/powerpc/platforms/cell/spu_callbacks.c#L17 |
| 211 |
// but have a different syscall sequence: https://github.com/mirror/newlib-cygwin/blob/newlib-4.4.0/libgloss/spu/linux_syscalls.c#L37 |
| 212 |
// This is currently used only for testing. |
| 213 |
println!("cargo:rustc-cfg=portable_atomic_no_asm_syscall"); |
| 214 |
} |
| 215 |
} |
| 216 |
} |
| 217 |
_ => {} |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
// feature(cfg_target_has_atomic) stabilized in Rust 1.60 (nightly-2022-02-11): https://github.com/rust-lang/rust/pull/93824 |
| 222 |
if !version.probe(60, 2022, 2, 10) { |
| 223 |
if version.nightly |
| 224 |
&& version.probe(40, 2019, 10, 13) |
| 225 |
&& is_allowed_feature("cfg_target_has_atomic") |
| 226 |
{ |
| 227 |
// The part of this feature we use has not been changed since nightly-2019-10-14 |
| 228 |
// until it was stabilized, so it can safely be enabled in nightly for that period. |
| 229 |
println!("cargo:rustc-cfg=portable_atomic_unstable_cfg_target_has_atomic"); |
| 230 |
} else { |
| 231 |
println!("cargo:rustc-cfg=portable_atomic_no_cfg_target_has_atomic"); |
| 232 |
let target = &*convert_custom_linux_target(target); |
| 233 |
if generated::NO_ATOMIC_CAS.contains(&target) { |
| 234 |
println!("cargo:rustc-cfg=portable_atomic_no_atomic_cas"); |
| 235 |
} |
| 236 |
if generated::NO_ATOMIC_64.contains(&target) { |
| 237 |
println!("cargo:rustc-cfg=portable_atomic_no_atomic_64"); |
| 238 |
} else { |
| 239 |
// Otherwise, assuming `"max-atomic-width" == 64` or `"max-atomic-width" == 128`. |
| 240 |
} |
| 241 |
} |
| 242 |
} |
| 243 |
// We don't need to use convert_custom_linux_target here because all linux targets have atomics. |
| 244 |
if generated::NO_ATOMIC.contains(&target) { |
| 245 |
println!("cargo:rustc-cfg=portable_atomic_no_atomic_load_store"); |
| 246 |
} |
| 247 |
|
| 248 |
if version.llvm < 20 { |
| 249 |
println!("cargo:rustc-cfg=portable_atomic_pre_llvm_20"); |
| 250 |
if version.llvm < 18 { |
| 251 |
println!("cargo:rustc-cfg=portable_atomic_pre_llvm_18"); |
| 252 |
if version.llvm < 16 { |
| 253 |
println!("cargo:rustc-cfg=portable_atomic_pre_llvm_16"); |
| 254 |
} |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
if version.nightly { |
| 259 |
// `cfg(sanitize = "..")` is not stabilized. |
| 260 |
let sanitize = env::var("CARGO_CFG_SANITIZE").unwrap_or_default(); |
| 261 |
if sanitize.contains("thread") { |
| 262 |
// Most kinds of sanitizers are not compatible with asm |
| 263 |
// (https://github.com/google/sanitizers/issues/192), |
| 264 |
// but it seems that ThreadSanitizer is the only one that can cause |
| 265 |
// false positives in our code. |
| 266 |
println!("cargo:rustc-cfg=portable_atomic_sanitize_thread"); |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
match target_arch { |
| 271 |
"x86_64" => { |
| 272 |
// cmpxchg16b_target_feature stabilized in Rust 1.69 (nightly-2023-03-01): https://github.com/rust-lang/rust/pull/106774 |
| 273 |
if !version.probe(69, 2023, 2, 28) { |
| 274 |
println!("cargo:rustc-cfg=portable_atomic_no_cmpxchg16b_target_feature"); |
| 275 |
} |
| 276 |
// For Miri and ThreadSanitizer. |
| 277 |
// https://github.com/rust-lang/rust/pull/109359 (includes https://github.com/rust-lang/stdarch/pull/1358) merged in Rust 1.70 (nightly-2023-03-24). |
| 278 |
if version.nightly && !version.probe(70, 2023, 3, 23) { |
| 279 |
println!("cargo:rustc-cfg=portable_atomic_no_cmpxchg16b_intrinsic"); |
| 280 |
} |
| 281 |
|
| 282 |
// cmpxchg16b_target_feature stabilized in Rust 1.69. |
| 283 |
if needs_target_feature_fallback(&version, Some(69)) { |
| 284 |
// x86_64 Apple targets always support CMPXCHG16B: |
| 285 |
// https://github.com/rust-lang/rust/blob/1.68.0/compiler/rustc_target/src/spec/x86_64_apple_darwin.rs#L8 |
| 286 |
// https://github.com/rust-lang/rust/blob/1.68.0/compiler/rustc_target/src/spec/apple_base.rs#L69-L70 |
| 287 |
// (Windows (except Windows 7, since Rust 1.78) and Fuchsia (since Rust 1.87) targets |
| 288 |
// also enable CMPXCHG16B, but this branch is only used on pre-1.69 that |
| 289 |
// cmpxchg16b_target_feature is unstable.) |
| 290 |
// Script to get builtin targets that support CMPXCHG16B by default: |
| 291 |
// $ (for target in $(rustc -Z unstable-options --print all-target-specs-json | jq -r '. | to_entries[] | if .value.arch == "x86_64" then .key else empty end'); do rustc --print cfg --target "${target}" | grep -Fq '"cmpxchg16b"' && printf '%s\n' "${target}"; done) |
| 292 |
let is_apple = env::var("CARGO_CFG_TARGET_VENDOR").unwrap_or_default() == "apple"; |
| 293 |
let cmpxchg16b = is_apple; |
| 294 |
// LLVM recognizes this also as cx16 target feature: https://godbolt.org/z/KM3jz616j |
| 295 |
// However, it is unlikely that rustc will support that name, so we ignore it. |
| 296 |
target_feature_fallback("cmpxchg16b", cmpxchg16b); |
| 297 |
} |
| 298 |
} |
| 299 |
"aarch64" | "arm64ec" => { |
| 300 |
// target_feature "lse2"/"lse128"/"rcpc3" is unstable and available on rustc side since nightly-2024-08-30: https://github.com/rust-lang/rust/pull/128192 |
| 301 |
if !version.probe(82, 2024, 8, 29) || needs_target_feature_fallback(&version, None) { |
| 302 |
// FEAT_LSE2 doesn't imply FEAT_LSE. FEAT_LSE128 implies FEAT_LSE but not FEAT_LSE2. |
| 303 |
// AArch64 macOS always supports FEAT_LSE and FEAT_LSE2 because M1 is Armv8.4 with all features of Armv8.5 except FEAT_BTI: |
| 304 |
// https://github.com/llvm/llvm-project/blob/llvmorg-22.1.0-rc1/llvm/lib/Target/AArch64/AArch64Processors.td#L1558 |
| 305 |
// https://github.com/llvm/llvm-project/blob/llvmorg-22.1.0-rc1/llvm/lib/Target/AArch64/AArch64Processors.td#L1180 |
| 306 |
// Script to get builtin targets that support FEAT_LSE/FEAT_LSE2 by default: |
| 307 |
// $ (for target in $(rustc -Z unstable-options --print all-target-specs-json | jq -r '. | to_entries[] | if .value.arch == "aarch64" or .value.arch == "arm64ec" then .key else empty end'); do rustc --print cfg --target "${target}" | grep -Fq '"lse"' && printf '%s\n' "${target}"; done) |
| 308 |
// $ (for target in $(rustc -Z unstable-options --print all-target-specs-json | jq -r '. | to_entries[] | if .value.arch == "aarch64" or .value.arch == "arm64ec" then .key else empty end'); do rustc --print cfg --target "${target}" | grep -Fq '"lse2"' && printf '%s\n' "${target}"; done) |
| 309 |
let is_macos = target_os == "macos"; |
| 310 |
let mut lse = is_macos; |
| 311 |
target_feature_fallback("lse2", is_macos); |
| 312 |
lse |= target_feature_fallback("lse128", false); |
| 313 |
target_feature_fallback("rcpc3", false); |
| 314 |
// aarch64_target_feature stabilized in Rust 1.61. |
| 315 |
if needs_target_feature_fallback(&version, Some(61)) { |
| 316 |
target_feature_fallback("lse", lse); |
| 317 |
} |
| 318 |
} |
| 319 |
// As of Rust 1.85, target_feature "lsfe" is not available on rustc side: |
| 320 |
// https://github.com/rust-lang/rust/blob/1.85.0/compiler/rustc_target/src/target_features.rs |
| 321 |
target_feature_fallback("lsfe", false); |
| 322 |
|
| 323 |
// As of Apple M1/M1 Pro, on Apple hardware, CAS-loop-based RMW is much slower than |
| 324 |
// LL/SC-loop-based RMW: https://github.com/taiki-e/portable-atomic/pull/89 |
| 325 |
let is_apple = env::var("CARGO_CFG_TARGET_VENDOR").unwrap_or_default() == "apple"; |
| 326 |
if is_apple || target_cpu().map_or(false, |cpu| cpu.starts_with("apple-")) { |
| 327 |
println!("cargo:rustc-cfg=portable_atomic_ll_sc_rmw"); |
| 328 |
} |
| 329 |
} |
| 330 |
"arm" => { |
| 331 |
// For non-Linux/Android pre-v6 Arm (tier 3) with unsafe_assume_single_core enabled. |
| 332 |
// feature(isa_attribute) stabilized in Rust 1.67 (nightly-2022-11-06): https://github.com/rust-lang/rust/pull/102458 |
| 333 |
if version.nightly && !version.probe(67, 2022, 11, 5) { |
| 334 |
println!("cargo:rustc-cfg=portable_atomic_unstable_isa_attribute"); |
| 335 |
} |
| 336 |
|
| 337 |
if needs_target_feature_fallback(&version, None) { |
| 338 |
// #[cfg(target_feature = "v7")] and others don't work on stable. |
| 339 |
// armv7-unknown-linux-gnueabihf |
| 340 |
// ^^ |
| 341 |
let mut mclass = false; |
| 342 |
let mut v6 = false; |
| 343 |
let mut v7 = false; |
| 344 |
if let Some(mut subarch) = |
| 345 |
strip_prefix(target, "arm").or_else(|| strip_prefix(target, "thumb")) |
| 346 |
{ |
| 347 |
subarch = strip_prefix(subarch, "eb").unwrap_or(subarch); // ignore endianness |
| 348 |
subarch = subarch.split('-').next().unwrap(); // ignore vender/os/env |
| 349 |
let mut i = subarch.splitn(2, '.'); |
| 350 |
subarch = i.next().unwrap(); |
| 351 |
let suffix = i.next().unwrap_or_default(); // .base/.main suffix |
| 352 |
let mut known = true; |
| 353 |
// See https://github.com/taiki-e/atomic-maybe-uninit/blob/HEAD/build.rs for details |
| 354 |
match subarch { |
| 355 |
"v7" | "v7a" | "v7neon" | "v7s" | "v7k" | "v8" | "v8a" | "v9" | "v9a" => {} // aclass |
| 356 |
"v7r" | "v8r" | "v9r" => {} // rclass |
| 357 |
"v6m" | "v7em" | "v7m" | "v8m" => mclass = true, |
| 358 |
// arm-linux-androideabi is v5te |
| 359 |
// https://github.com/rust-lang/rust/blob/1.84.0/compiler/rustc_target/src/spec/targets/arm_linux_androideabi.rs#L18 |
| 360 |
_ if target == "arm-linux-androideabi" => subarch = "v5te", |
| 361 |
// armeb-unknown-linux-gnueabi is v8 & aclass |
| 362 |
// https://github.com/rust-lang/rust/blob/1.84.0/compiler/rustc_target/src/spec/targets/armeb_unknown_linux_gnueabi.rs#L18 |
| 363 |
_ if target == "armeb-unknown-linux-gnueabi" => subarch = "v8", |
| 364 |
// Legacy Arm architectures (pre-v7 except v6m) don't have *class target feature. |
| 365 |
"" => subarch = "v6", |
| 366 |
"v4t" | "v5te" | "v6" | "v6k" => {} |
| 367 |
_ => { |
| 368 |
known = false; |
| 369 |
if env::var_os("PORTABLE_ATOMIC_DENY_WARNINGS").is_some() { |
| 370 |
panic!("unrecognized Arm subarch: {}", target) |
| 371 |
} |
| 372 |
println!( |
| 373 |
"cargo:warning={}: unrecognized Arm subarch: {}", |
| 374 |
env!("CARGO_PKG_NAME"), |
| 375 |
target |
| 376 |
); |
| 377 |
} |
| 378 |
} |
| 379 |
v6 = known && subarch.starts_with("v6"); |
| 380 |
v7 = known && subarch.starts_with("v7"); |
| 381 |
if known && (subarch.starts_with("v8") || subarch.starts_with("v9")) { |
| 382 |
// Armv8-M is not considered as v8 by LLVM. |
| 383 |
// https://github.com/rust-lang/stdarch/blob/a0c30f3e3c75adcd6ee7efc94014ebcead61c507/crates/core_arch/src/arm_shared/mod.rs |
| 384 |
if mclass { |
| 385 |
// Armv8-M Mainline is a superset of Armv7-M. |
| 386 |
// Armv8-M Baseline is a superset of Armv6-M. |
| 387 |
// That said, LLVM handles thumbv8m.main without v8m like v6m, not v7m: https://godbolt.org/z/Ph96v9zae |
| 388 |
// TODO: Armv9-M has not yet been released, |
| 389 |
// so it is not clear how it will be handled here. |
| 390 |
v6 = true; |
| 391 |
v7 = suffix == "main"; |
| 392 |
} else { |
| 393 |
v7 = true; |
| 394 |
} |
| 395 |
} |
| 396 |
} |
| 397 |
v6 |= target_feature_fallback("v7", v7); |
| 398 |
target_feature_fallback("v6", v6); |
| 399 |
target_feature_fallback("mclass", mclass); |
| 400 |
} |
| 401 |
} |
| 402 |
"riscv32" | "riscv64" => { |
| 403 |
// zabha and zacas imply zaamo in GCC, LLVM 20+, and Rust, but do not in LLVM 19. |
| 404 |
// However, enabling them without zaamo or a is not allowed in LLVM 19, so we can assume |
| 405 |
// zaamo is available when zabha is enabled). |
| 406 |
// https://github.com/llvm/llvm-project/commit/956361ca080a689a96b6552d28681aaf0ad2f494 |
| 407 |
// https://github.com/gcc-mirror/gcc/commit/7b2b2e3d660edc8ef3a8cfbdfc2b0fd499459601 |
| 408 |
// https://github.com/gcc-mirror/gcc/commit/11c2453a16b725b7fb67778e1ab4636a51a1217d |
| 409 |
// https://github.com/rust-lang/rust/pull/130877 |
| 410 |
let mut zaamo = false; |
| 411 |
// target_feature "zacas" is available as unstable on rustc side |
| 412 |
// since nightly-2025-02-26 (https://github.com/rust-lang/rust/pull/137417), |
| 413 |
// and stabilized in Rust 1.94 (https://github.com/rust-lang/rust/pull/145948). |
| 414 |
if !version.probe(87, 2025, 2, 25) || needs_target_feature_fallback(&version, Some(94)) |
| 415 |
{ |
| 416 |
// amocas.{w,d,q} (and amocas.{b,h} if zabha is also available) |
| 417 |
// available as experimental since LLVM 17 https://github.com/llvm/llvm-project/commit/29f630a1ddcbb03caa31b5002f0cbc105ff3a869 |
| 418 |
// available non-experimental since LLVM 20 https://github.com/llvm/llvm-project/commit/614aeda93b2225c6eb42b00ba189ba7ca2585c60 |
| 419 |
zaamo |= target_feature_fallback("zacas", false); |
| 420 |
} |
| 421 |
// target_feature "zaamo"/"zabha" is available as unstable on rustc side |
| 422 |
// since nightly-2024-10-02 (https://github.com/rust-lang/rust/pull/130877), |
| 423 |
// and stabilized in Rust 1.94 (https://github.com/rust-lang/rust/pull/145948). |
| 424 |
if !version.probe(83, 2024, 10, 1) || needs_target_feature_fallback(&version, Some(94)) |
| 425 |
{ |
| 426 |
if version.llvm >= 19 { |
| 427 |
// amo*.{b,h} |
| 428 |
// available since LLVM 19 https://github.com/llvm/llvm-project/commit/89f87c387627150d342722b79c78cea2311cddf7 / https://github.com/llvm/llvm-project/commit/6b7444964a8d028989beee554a1f5c61d16a1cac |
| 429 |
zaamo |= target_feature_fallback("zabha", false); |
| 430 |
} |
| 431 |
// amo*.{w,d} |
| 432 |
target_feature_fallback("zaamo", zaamo); |
| 433 |
} |
| 434 |
} |
| 435 |
"powerpc64" => { |
| 436 |
// target_feature "quadword-atomics" is unstable and available on rustc side since nightly-2024-09-28: https://github.com/rust-lang/rust/pull/130873 |
| 437 |
if !version.probe(83, 2024, 9, 27) || needs_target_feature_fallback(&version, None) { |
| 438 |
let mut pwr8_features = false; |
| 439 |
if let Some(cpu) = target_cpu() { |
| 440 |
if let Some(mut cpu_version) = strip_prefix(&cpu, "pwr") { |
| 441 |
cpu_version = strip_suffix(cpu_version, "x").unwrap_or(cpu_version); // for pwr5x and pwr6x |
| 442 |
if let Ok(cpu_version) = cpu_version.parse::<u32>() { |
| 443 |
pwr8_features = cpu_version >= 8; |
| 444 |
} |
| 445 |
} else { |
| 446 |
// https://github.com/llvm/llvm-project/blob/llvmorg-22.1.0-rc1/llvm/lib/Target/PowerPC/PPC.td#L789 |
| 447 |
// https://github.com/llvm/llvm-project/blob/llvmorg-22.1.0-rc1/llvm/lib/Target/PowerPC/PPC.td#L557 |
| 448 |
// On the minimum external LLVM version of the oldest rustc version which we can use asm_experimental_arch |
| 449 |
// on this target (see CI config for more), "future" is based on pwr10 features. |
| 450 |
// https://github.com/llvm/llvm-project/blob/llvmorg-12.0.0/llvm/lib/Target/PowerPC/PPC.td#L370 |
| 451 |
pwr8_features = cpu == "future" || cpu == "ppc64le"; |
| 452 |
} |
| 453 |
} else { |
| 454 |
// powerpc64le is pwr8 by default https://github.com/llvm/llvm-project/blob/llvmorg-22.1.0-rc1/llvm/lib/Target/PowerPC/PPC.td#L789 |
| 455 |
// See also https://github.com/rust-lang/rust/issues/59932 |
| 456 |
pwr8_features = env::var("CARGO_CFG_TARGET_ENDIAN") |
| 457 |
.expect("CARGO_CFG_TARGET_ENDIAN not set") |
| 458 |
== "little"; |
| 459 |
} |
| 460 |
// power8 features: https://github.com/llvm/llvm-project/blob/llvmorg-22.1.0-rc1/llvm/lib/Target/PowerPC/PPC.td#L484 |
| 461 |
// lqarx and stqcx. |
| 462 |
target_feature_fallback("quadword-atomics", pwr8_features); |
| 463 |
} |
| 464 |
} |
| 465 |
"s390x" => { |
| 466 |
let mut arch9_features = false; // z196+ |
| 467 |
let mut arch13_features = false; // z15+ |
| 468 |
if let Some(cpu) = target_cpu() { |
| 469 |
// LLVM and GCC recognize the same names: |
| 470 |
// https://github.com/llvm/llvm-project/blob/llvmorg-22.1.0-rc1/llvm/lib/Target/SystemZ/SystemZProcessors.td |
| 471 |
// https://github.com/gcc-mirror/gcc/blob/releases/gcc-15.2.0/gcc/config/s390/s390.opt#L58-L128 |
| 472 |
if let Some(arch_version) = strip_prefix(&cpu, "arch") { |
| 473 |
if let Ok(arch_version) = arch_version.parse::<u32>() { |
| 474 |
arch9_features = arch_version >= 9; |
| 475 |
arch13_features = arch_version >= 13; |
| 476 |
} |
| 477 |
} else { |
| 478 |
match &*cpu { |
| 479 |
"z196" | "zEC12" | "z13" | "z14" => arch9_features = true, |
| 480 |
"z15" | "z16" | "z17" => { |
| 481 |
arch9_features = true; |
| 482 |
arch13_features = true; |
| 483 |
} |
| 484 |
_ => {} |
| 485 |
} |
| 486 |
} |
| 487 |
} |
| 488 |
// target_feature "miscellaneous-extensions-3" is available as unstable on rustc side |
| 489 |
// since nightly-2025-06-05 (https://github.com/rust-lang/rust/pull/141250), |
| 490 |
// and stabilized in Rust 1.93 (https://github.com/rust-lang/rust/pull/145656). |
| 491 |
if !version.probe(89, 2025, 6, 4) || needs_target_feature_fallback(&version, Some(93)) { |
| 492 |
// arch13 features: https://github.com/llvm/llvm-project/blob/llvmorg-22.1.0-rc1/llvm/lib/Target/SystemZ/SystemZFeatures.td#L303 |
| 493 |
// nand (nnr{,g}k), select (sel{,g}r), etc. |
| 494 |
target_feature_fallback("miscellaneous-extensions-3", arch13_features); |
| 495 |
} |
| 496 |
// As of Rust 1.84, target_feature "fast-serialization"/"load-store-on-cond"/"distinct-ops"/"miscellaneous-extensions-3" is not available on rustc side: |
| 497 |
// https://github.com/rust-lang/rust/blob/1.84.0/compiler/rustc_target/src/target_features.rs#L547 |
| 498 |
// arch9 features: https://github.com/llvm/llvm-project/blob/llvmorg-22.1.0-rc1/llvm/lib/Target/SystemZ/SystemZFeatures.td#L103 |
| 499 |
// bcr 14,0 |
| 500 |
target_feature_fallback("fast-serialization", arch9_features); |
| 501 |
// {l,st}oc{,g}{,r} |
| 502 |
target_feature_fallback("load-store-on-cond", arch9_features); |
| 503 |
// {al,sl,n,o,x}{,g}rk |
| 504 |
target_feature_fallback("distinct-ops", arch9_features); |
| 505 |
} |
| 506 |
"avr" => { |
| 507 |
// target_feature "rmw" is unstable and available on rustc side since nightly-2026-02-08: https://github.com/rust-lang/rust/pull/146900 |
| 508 |
if !version.probe(95, 2026, 2, 7) || needs_target_feature_fallback(&version, None) { |
| 509 |
// https://github.com/llvm/llvm-project/blob/llvmorg-22.1.0-rc1/llvm/lib/Target/AVR/AVRDevices.td |
| 510 |
let mut xmegau = false; // FamilyXMEGAU |
| 511 |
if let Some(cpu) = target_cpu() { |
| 512 |
match &*cpu { |
| 513 |
"atxmega16a4u" | "atxmega16c4" | "atxmega32a4u" | "atxmega32c3" |
| 514 |
| "atxmega32c4" | "atxmega32e5" | "atxmega16e5" | "atxmega8e5" |
| 515 |
| "atxmega64a3u" | "atxmega64a4u" | "atxmega64b1" | "atxmega64b3" |
| 516 |
| "atxmega64c3" | "atxmega64a1u" | "atxmega128a3u" | "atxmega128b1" |
| 517 |
| "atxmega128b3" | "atxmega128c3" | "atxmega192a3u" | "atxmega192c3" |
| 518 |
| "atxmega256a3u" | "atxmega256a3bu" | "atxmega256c3" | "atxmega384c3" |
| 519 |
| "atxmega128a1u" | "atxmega128a4u" => xmegau = true, |
| 520 |
_ => {} |
| 521 |
} |
| 522 |
} |
| 523 |
target_feature_fallback("rmw", xmegau); |
| 524 |
} |
| 525 |
} |
| 526 |
_ => {} |
| 527 |
} |
| 528 |
} |
| 529 |
|
| 530 |
// HACK: Currently, it seems that the only way to handle unstable target |
| 531 |
// features on the stable is to parse the `-C target-feature` in RUSTFLAGS. |
| 532 |
// |
| 533 |
// - #[cfg(target_feature = "unstable_target_feature")] doesn't work on stable. |
| 534 |
// - CARGO_CFG_TARGET_FEATURE excludes unstable target features on stable. |
| 535 |
// |
| 536 |
// As mentioned in the [RFC2045], unstable target features are also passed to LLVM |
| 537 |
// (e.g., https://godbolt.org/z/4rr7rMcfG), so this hack works properly on stable. |
| 538 |
// |
| 539 |
// [RFC2045]: https://rust-lang.github.io/rfcs/2045-target-feature.html#backend-compilation-options |
| 540 |
fn needs_target_feature_fallback(version: &Version, stable: Option<u32>) -> bool { |
| 541 |
match stable { |
| 542 |
// In these cases, cfg(target_feature = "...") would work, so skip emitting our own fallback target_feature cfg. |
| 543 |
_ if version.nightly => false, |
| 544 |
Some(stabilized) if version.minor >= stabilized => false, |
| 545 |
_ => true, |
| 546 |
} |
| 547 |
} |
| 548 |
fn target_feature_fallback(name: &str, mut has_target_feature: bool) -> bool { |
| 549 |
if let Some(rustflags) = env::var_os("CARGO_ENCODED_RUSTFLAGS") { |
| 550 |
for mut flag in rustflags.to_string_lossy().split('\x1f') { |
| 551 |
flag = strip_prefix(flag, "-C").unwrap_or(flag); |
| 552 |
if let Some(flag) = strip_prefix(flag, "target-feature=") { |
| 553 |
for s in flag.split(',') { |
| 554 |
// TODO: Handles cases where a specific target feature |
| 555 |
// implicitly enables another target feature. |
| 556 |
match (s.as_bytes().first(), s.as_bytes().get(1..)) { |
| 557 |
(Some(b'+'), Some(f)) if f == name.as_bytes() => has_target_feature = true, |
| 558 |
(Some(b'-'), Some(f)) if f == name.as_bytes() => has_target_feature = false, |
| 559 |
_ => {} |
| 560 |
} |
| 561 |
} |
| 562 |
} |
| 563 |
} |
| 564 |
} |
| 565 |
if has_target_feature { |
| 566 |
println!("cargo:rustc-cfg=portable_atomic_target_feature=\"{}\"", name); |
| 567 |
} |
| 568 |
has_target_feature |
| 569 |
} |
| 570 |
|
| 571 |
fn target_cpu() -> Option<String> { |
| 572 |
let rustflags = env::var_os("CARGO_ENCODED_RUSTFLAGS")?; |
| 573 |
let rustflags = rustflags.to_string_lossy(); |
| 574 |
let mut cpu = None; |
| 575 |
for mut flag in rustflags.split('\x1f') { |
| 576 |
flag = strip_prefix(flag, "-C").unwrap_or(flag); |
| 577 |
if let Some(flag) = strip_prefix(flag, "target-cpu=") { |
| 578 |
cpu = Some(flag); |
| 579 |
} |
| 580 |
} |
| 581 |
cpu.map(str::to_owned) |
| 582 |
} |
| 583 |
|
| 584 |
fn is_allowed_feature(name: &str) -> bool { |
| 585 |
// https://github.com/dtolnay/thiserror/pull/248 |
| 586 |
if env::var_os("RUSTC_STAGE").is_some() { |
| 587 |
return false; |
| 588 |
} |
| 589 |
|
| 590 |
// allowed by default |
| 591 |
let mut allowed = true; |
| 592 |
if let Some(rustflags) = env::var_os("CARGO_ENCODED_RUSTFLAGS") { |
| 593 |
for mut flag in rustflags.to_string_lossy().split('\x1f') { |
| 594 |
flag = strip_prefix(flag, "-Z").unwrap_or(flag); |
| 595 |
if let Some(flag) = strip_prefix(flag, "allow-features=") { |
| 596 |
// If it is specified multiple times, the last value will be preferred. |
| 597 |
allowed = flag.split(',').any(|allowed| allowed == name); |
| 598 |
} |
| 599 |
} |
| 600 |
} |
| 601 |
allowed |
| 602 |
} |
| 603 |
|
| 604 |
// Adapted from https://github.com/crossbeam-rs/crossbeam/blob/crossbeam-utils-0.8.21/build-common.rs. |
| 605 |
// |
| 606 |
// The target triplets have the form of 'arch-vendor-system'. |
| 607 |
// |
| 608 |
// When building for Linux (e.g. the 'system' part is |
| 609 |
// 'linux-something'), replace the vendor with 'unknown' |
| 610 |
// so that mapping to rust standard targets happens correctly. |
| 611 |
fn convert_custom_linux_target(target: &str) -> String { |
| 612 |
let mut parts: Vec<&str> = target.split('-').collect(); |
| 613 |
let system = parts.get(2); |
| 614 |
if system == Some(&"linux") { |
| 615 |
parts[1] = "unknown"; |
| 616 |
} |
| 617 |
parts.join("-") |
| 618 |
} |
| 619 |
|
| 620 |
// str::strip_prefix requires Rust 1.45 |
| 621 |
#[must_use] |
| 622 |
fn strip_prefix<'a>(s: &'a str, pat: &str) -> Option<&'a str> { |
| 623 |
if s.starts_with(pat) { Some(&s[pat.len()..]) } else { None } |
| 624 |
} |
| 625 |
// str::strip_suffix requires Rust 1.45 |
| 626 |
#[must_use] |
| 627 |
fn strip_suffix<'a>(s: &'a str, pat: &str) -> Option<&'a str> { |
| 628 |
if s.ends_with(pat) { Some(&s[..s.len() - pat.len()]) } else { None } |
| 629 |
} |