Skip to content

Commit 3ce34cc

Browse files
committed
Emit directives for cargo-check-cfg
1 parent 2001e76 commit 3ce34cc

File tree

1 file changed

+73
-1
lines changed

1 file changed

+73
-1
lines changed

build.rs

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
use std::{collections::BTreeMap, env, sync::atomic::Ordering};
1+
use std::{collections::BTreeMap, env, iter, sync::atomic::Ordering};
22

33
fn main() {
44
println!("cargo:rerun-if-changed=build.rs");
5+
configure_check_cfg();
56

67
let target = env::var("TARGET").unwrap();
78
let cwd = env::current_dir().unwrap();
89

910
println!("cargo:compiler-rt={}", cwd.join("compiler-rt").display());
1011

1112
// Activate libm's unstable features to make full use of Nightly.
13+
println!("cargo::rustc-check-cfg=cfg(feature, values(\"unstable\"))");
1214
println!("cargo:rustc-cfg=feature=\"unstable\"");
1315

1416
// Emscripten's runtime includes all the builtins
@@ -36,6 +38,7 @@ fn main() {
3638
}
3739

3840
// These targets have hardware unaligned access support.
41+
println!("cargo::rustc-check-cfg=cfg(feature, values(\"mem-unaligned\"))");
3942
if target.contains("x86_64")
4043
|| target.contains("i686")
4144
|| target.contains("aarch64")
@@ -64,20 +67,23 @@ fn main() {
6467
}
6568

6669
// To compile intrinsics.rs for thumb targets, where there is no libc
70+
println!("cargo::rustc-check-cfg=cfg(thumb)");
6771
if llvm_target[0].starts_with("thumb") {
6872
println!("cargo:rustc-cfg=thumb")
6973
}
7074

7175
// compiler-rt `cfg`s away some intrinsics for thumbv6m and thumbv8m.base because
7276
// these targets do not have full Thumb-2 support but only original Thumb-1.
7377
// We have to cfg our code accordingly.
78+
println!("cargo::rustc-check-cfg=cfg(thumb_1)");
7479
if llvm_target[0] == "thumbv6m" || llvm_target[0] == "thumbv8m.base" {
7580
println!("cargo:rustc-cfg=thumb_1")
7681
}
7782

7883
// Only emit the ARM Linux atomic emulation on pre-ARMv6 architectures. This
7984
// includes the old androideabi. It is deprecated but it is available as a
8085
// rustc target (arm-linux-androideabi).
86+
println!("cargo::rustc-check-cfg=cfg(kernel_user_helpers)");
8187
if llvm_target[0] == "armv4t"
8288
|| llvm_target[0] == "armv5te"
8389
|| target == "arm-linux-androideabi"
@@ -145,6 +151,71 @@ fn generate_aarch64_outlined_atomics() {
145151
std::fs::write(dst, buf).unwrap();
146152
}
147153

154+
/// Emit directives for features we expect to support that aren't in `Cargo.toml`.
155+
///
156+
/// These are mostly cfg elements emitted by this `build.rs`.
157+
fn configure_check_cfg() {
158+
// Functions where we can set the "optimized-c" flag
159+
const HAS_OPTIMIZED_C: &[&str] = &[
160+
"__ashldi3",
161+
"__ashlsi3",
162+
"__ashrdi3",
163+
"__ashrsi3",
164+
"__clzsi2",
165+
"__divdi3",
166+
"__divsi3",
167+
"__divmoddi4",
168+
"__divmodsi4",
169+
"__divmodsi4",
170+
"__divmodti4",
171+
"__lshrdi3",
172+
"__lshrsi3",
173+
"__moddi3",
174+
"__modsi3",
175+
"__muldi3",
176+
"__udivdi3",
177+
"__udivmoddi4",
178+
"__udivmodsi4",
179+
"__udivsi3",
180+
"__umoddi3",
181+
"__umodsi3",
182+
];
183+
184+
// Build a list of all aarch64 atomic operation functions
185+
let mut aarch_atomic = Vec::new();
186+
for aarch_op in ["cas", "ldadd", "ldclr", "ldeor", "ldset", "swp"] {
187+
for op_size in [1, 2, 4, 8].iter().chain(if aarch_op == "cas" {
188+
[16].as_slice()
189+
} else {
190+
[].as_slice()
191+
}) {
192+
for ordering in ["relax", "acq", "rel", "acq_rel"] {
193+
aarch_atomic.push(format!("__aarch64_{}{}_{}", aarch_op, op_size, ordering));
194+
}
195+
}
196+
}
197+
198+
for fn_name in HAS_OPTIMIZED_C
199+
.iter()
200+
.copied()
201+
.chain(aarch_atomic.iter().map(|s| s.as_str()))
202+
{
203+
println!(
204+
"cargo::rustc-check-cfg=cfg({}, values(\"optimized-c\"))",
205+
fn_name
206+
);
207+
}
208+
209+
// FIXME: this feature definitely exists in
210+
// `rustc --print target-features --target sparc64-unknown-linux-gnu`. Why does check-cfg
211+
// flag it?
212+
println!("cargo::rustc-check-cfg=cfg(target_feature, values(\"vis3\"))");
213+
214+
// FIXME: these come from libm and should be changed there
215+
println!("cargo::rustc-check-cfg=cfg(feature, values(\"checked\"))");
216+
println!("cargo::rustc-check-cfg=cfg(assert_no_panic)");
217+
}
218+
148219
#[cfg(feature = "c")]
149220
mod c {
150221
extern crate cc;
@@ -307,6 +378,7 @@ mod c {
307378
// also needs to satisfy intrinsics that jemalloc or C in general may
308379
// need, so include a few more that aren't typically needed by
309380
// LLVM/Rust.
381+
#[allow(unexpected_cfgs)]
310382
if cfg!(feature = "rustbuild") {
311383
sources.extend(&[("__ffsdi2", "ffsdi2.c")]);
312384
}

0 commit comments

Comments
 (0)