Skip to content

Commit 942ab9f

Browse files
committed
Move examples/intrinsics.rs to its own crate
Currently there is an interesting situation with the way features get enabled; `testcrate` enables `mangled-names`, but the `intrinsics.rs` example requires this feature be disabled (otherwise the test fails with missing symbols, as expected). This is also the reason that `testcrate` is not a default workspace member, meaning `cargo test` doesn't actually run `testcrate`'s tests; making it a default member would mean that `compiler-builtins/mangled-names` gets enabled when `examples/intrinsics.rs` gets built, due to the way features get unified. Simplify the situation by making moving the example to its own crate as `builtins-test-intrinsics`. This also means `testcrate` can become a default member so it is included in `cargo check` or `cargo test` when run at the workspace root. `testcrate` and `builtins-test-intrinsics` still can't be built at the same time since there isn't a straightforward way to have Cargo build `compiler-builtins` twice with different features. This is a side effect of us using non-additive features, but there isn't really a better option since enabling both mangled and unmangled names would render `builtins-test-intrinsics` useless.
1 parent 04ec5de commit 942ab9f

File tree

9 files changed

+69
-32
lines changed

9 files changed

+69
-32
lines changed

Cargo.toml

+12-5
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,20 @@ rustc-dep-of-std = ['compiler-builtins', 'core']
7171
# are not normally public but are required by the `testcrate`
7272
public-test-deps = []
7373

74-
[[example]]
75-
name = "intrinsics"
76-
required-features = ["compiler-builtins"]
77-
7874
[workspace]
7975
resolver = "2"
80-
members = ["testcrate"]
76+
members = [
77+
# Note that builtins-test-intrinsics cannot be a default member because it
78+
# needs the `mangled-names` feature disabled, while `testcrate` needs it
79+
# enabled.
80+
"builtins-test-intrinsics",
81+
"testcrate",
82+
]
83+
84+
default-members = [
85+
".",
86+
"testcrate",
87+
]
8188

8289
[profile.release]
8390
panic = 'abort'

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ It is distributed as part of Rust's sysroot.
1616
[C implementation][2] to Rust.
1717
4. Add a test to compare the behavior of the ported intrinsic(s) with their
1818
implementation on the testing host.
19-
5. Add the intrinsic to `examples/intrinsics.rs` to verify it can be linked on
20-
all targets.
19+
5. Add the intrinsic to `builtins-test-intrinsics/src/main.rs` to verify it
20+
can be linked on all targets.
2121
6. Send a Pull Request (PR).
2222
7. Once the PR passes our extensive testing infrastructure, we'll merge it!
2323
8. Celebrate :tada:

build.rs

+2-15
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::{collections::BTreeMap, env, path::PathBuf, sync::atomic::Ordering};
22

33
mod configure;
44

5-
use configure::{configure_f16_f128, Target};
5+
use configure::{configure_aliases, configure_f16_f128, Target};
66

77
fn main() {
88
println!("cargo::rerun-if-changed=build.rs");
@@ -13,6 +13,7 @@ fn main() {
1313

1414
configure_check_cfg();
1515
configure_f16_f128(&target);
16+
configure_aliases(&target);
1617

1718
configure_libm(&target);
1819

@@ -71,20 +72,6 @@ fn main() {
7172
}
7273
}
7374

74-
// To compile intrinsics.rs for thumb targets, where there is no libc
75-
println!("cargo::rustc-check-cfg=cfg(thumb)");
76-
if llvm_target[0].starts_with("thumb") {
77-
println!("cargo:rustc-cfg=thumb")
78-
}
79-
80-
// compiler-rt `cfg`s away some intrinsics for thumbv6m and thumbv8m.base because
81-
// these targets do not have full Thumb-2 support but only original Thumb-1.
82-
// We have to cfg our code accordingly.
83-
println!("cargo::rustc-check-cfg=cfg(thumb_1)");
84-
if llvm_target[0] == "thumbv6m" || llvm_target[0] == "thumbv8m.base" {
85-
println!("cargo:rustc-cfg=thumb_1")
86-
}
87-
8875
// Only emit the ARM Linux atomic emulation on pre-ARMv6 architectures. This
8976
// includes the old androideabi. It is deprecated but it is available as a
9077
// rustc target (arm-linux-androideabi).

builtins-test-intrinsics/Cargo.toml

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[package]
2+
name = "builtins-test-intrinsics"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
compiler_builtins = { path = "../", features = ["compiler-builtins"]}
8+
panic-handler = { path = '../crates/panic-handler' }
9+
10+
[features]
11+
c = ["compiler_builtins/c"]

builtins-test-intrinsics/build.rs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
mod builtins_configure {
2+
include!("../configure.rs");
3+
}
4+
5+
fn main() {
6+
println!("cargo::rerun-if-changed=../configure.rs");
7+
8+
let target = builtins_configure::Target::from_env();
9+
builtins_configure::configure_f16_f128(&target);
10+
builtins_configure::configure_aliases(&target);
11+
}
File renamed without changes.

ci/run.sh

+9-9
Original file line numberDiff line numberDiff line change
@@ -120,22 +120,22 @@ done
120120

121121
rm -f "${rlib_paths[@]}"
122122

123-
build_intrinsics() {
124-
cargo build --target "$target" -v --example intrinsics "$@"
123+
build_intrinsics_test() {
124+
cargo build --target "$target" -v --package builtins-test-intrinsics "$@"
125125
}
126126

127-
# Verify that we haven't drop any intrinsic/symbol
128-
build_intrinsics
129-
build_intrinsics --release
130-
build_intrinsics --features c
131-
build_intrinsics --features c --release
127+
# Verify that we haven't dropped any intrinsics/symbols
128+
build_intrinsics_test
129+
build_intrinsics_test --release
130+
build_intrinsics_test --features c
131+
build_intrinsics_test --features c --release
132132

133133
# Verify that there are no undefined symbols to `panic` within our
134134
# implementations
135135
CARGO_PROFILE_DEV_LTO=true \
136-
cargo build --target "$target" --example intrinsics
136+
cargo build --target "$target" --package builtins-test-intrinsics
137137
CARGO_PROFILE_RELEASE_LTO=true \
138-
cargo build --target "$target" --example intrinsics --release
138+
cargo build --target "$target" --package builtins-test-intrinsics --release
139139

140140
# Ensure no references to any symbols from core
141141
update_rlib_paths

configure.rs

+21-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use std::env;
66
#[allow(dead_code)]
77
pub struct Target {
88
pub triple: String,
9+
pub triple_split: Vec<String>,
910
pub opt_level: String,
1011
pub cargo_features: Vec<String>,
1112
pub os: String,
@@ -19,6 +20,8 @@ pub struct Target {
1920

2021
impl Target {
2122
pub fn from_env() -> Self {
23+
let triple = env::var("TARGET").unwrap();
24+
let triple_split = triple.split('-').map(ToOwned::to_owned).collect();
2225
let little_endian = match env::var("CARGO_CFG_TARGET_ENDIAN").unwrap().as_str() {
2326
"little" => true,
2427
"big" => false,
@@ -30,7 +33,8 @@ impl Target {
3033
.collect();
3134

3235
Self {
33-
triple: env::var("TARGET").unwrap(),
36+
triple,
37+
triple_split,
3438
os: env::var("CARGO_CFG_TARGET_OS").unwrap(),
3539
opt_level: env::var("OPT_LEVEL").unwrap(),
3640
cargo_features,
@@ -56,6 +60,22 @@ impl Target {
5660
}
5761
}
5862

63+
pub fn configure_aliases(target: &Target) {
64+
// To compile builtins-test-intrinsics for thumb targets, where there is no libc
65+
println!("cargo::rustc-check-cfg=cfg(thumb)");
66+
if target.triple_split[0].starts_with("thumb") {
67+
println!("cargo:rustc-cfg=thumb")
68+
}
69+
70+
// compiler-rt `cfg`s away some intrinsics for thumbv6m and thumbv8m.base because
71+
// these targets do not have full Thumb-2 support but only original Thumb-1.
72+
// We have to cfg our code accordingly.
73+
println!("cargo::rustc-check-cfg=cfg(thumb_1)");
74+
if target.triple_split[0] == "thumbv6m" || target.triple_split[0] == "thumbv8m.base" {
75+
println!("cargo:rustc-cfg=thumb_1")
76+
}
77+
}
78+
5979
/// Configure whether or not `f16` and `f128` support should be enabled.
6080
pub fn configure_f16_f128(target: &Target) {
6181
// Set whether or not `f16` and `f128` are supported at a basic level by LLVM. This only means

testcrate/build.rs

+1
Original file line numberDiff line numberDiff line change
@@ -115,5 +115,6 @@ fn main() {
115115
println!("cargo:rustc-cfg=feature=\"{name}\"");
116116
}
117117

118+
builtins_configure::configure_aliases(&target);
118119
builtins_configure::configure_f16_f128(&target);
119120
}

0 commit comments

Comments
 (0)