Skip to content

Commit 0809811

Browse files
committed
Add probe_sysroot_crate and emit_sysroot_crate
1 parent 81ea9f3 commit 0809811

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

src/lib.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,27 @@ impl AutoCfg {
212212
Ok(status.success())
213213
}
214214

215+
/// Tests whether the given sysroot crate can be used.
216+
///
217+
/// The test code is subject to change, but currently looks like:
218+
///
219+
/// ```ignore
220+
/// extern crate CRATE as dummy; // `as _` wasn't stabilized until Rust 1.33
221+
/// ```
222+
pub fn probe_sysroot_crate(&self, name: &str) -> bool {
223+
self.probe(format!("extern crate {} as dummy;", name)).unwrap_or(false)
224+
}
225+
226+
/// Emits a config value `has_PATH` if `probe_sysroot_crate` returns true.
227+
///
228+
/// Any non-identifier characters in the `path` will be replaced with
229+
/// `_` in the generated config value.
230+
pub fn emit_sysroot_crate(&self, name: &str) {
231+
if self.probe_sysroot_crate(name) {
232+
emit(&format!("has_{}", mangle(name)));
233+
}
234+
}
235+
215236
/// Tests whether the given path can be used.
216237
///
217238
/// The test code is subject to change, but currently looks like:

src/tests.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,25 @@ fn probe_sum() {
5656
assert!(missing ^ ac.probe_type("std::iter::Sum<i32>"));
5757
}
5858

59+
#[test]
60+
fn probe_std() {
61+
let ac = AutoCfg::with_dir("target").unwrap();
62+
assert_eq!(ac.probe_sysroot_crate("std"), !ac.no_std);
63+
}
64+
65+
#[test]
66+
fn probe_alloc() {
67+
let ac = AutoCfg::with_dir("target").unwrap();
68+
let missing = !ac.probe_rustc_version(1, 36);
69+
assert!(missing ^ ac.probe_sysroot_crate("alloc"));
70+
}
71+
72+
#[test]
73+
fn probe_bad_sysroot_crate() {
74+
let ac = AutoCfg::with_dir("target").unwrap();
75+
assert!(!ac.probe_sysroot_crate("doesnt_exist"));
76+
}
77+
5978
#[test]
6079
fn probe_no_std() {
6180
let ac = AutoCfg::with_dir("target").unwrap();

0 commit comments

Comments
 (0)