Skip to content

Commit

Permalink
Add probe_sysroot_crate and emit_sysroot_crate
Browse files Browse the repository at this point in the history
  • Loading branch information
leo60228 committed Aug 16, 2019
1 parent 81ea9f3 commit 5dacd2d
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,28 @@ impl AutoCfg {
Ok(status.success())
}

/// Tests whether the given sysroot crate can be used.
///
/// The test code is subject to change, but currently looks like:
///
/// ```ignore
/// extern crate CRATE as dummy; // `as _` wasn't stabilized until Rust 1.33
/// ```
pub fn probe_sysroot_crate(&self, name: &str) -> bool {
self.probe(format!("extern crate {} as dummy;", name))
.unwrap_or(false)
}

/// Emits a config value `has_PATH` if `probe_sysroot_crate` returns true.
///
/// Any non-identifier characters in the `path` will be replaced with
/// `_` in the generated config value.
pub fn emit_sysroot_crate(&self, name: &str) {
if self.probe_sysroot_crate(name) {
emit(&format!("has_{}", mangle(name)));
}
}

/// Tests whether the given path can be used.
///
/// The test code is subject to change, but currently looks like:
Expand Down
19 changes: 19 additions & 0 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,25 @@ fn probe_sum() {
assert!(missing ^ ac.probe_type("std::iter::Sum<i32>"));
}

#[test]
fn probe_std() {
let ac = AutoCfg::with_dir("target").unwrap();
assert_eq!(ac.probe_sysroot_crate("std"), !ac.no_std);
}

#[test]
fn probe_alloc() {
let ac = AutoCfg::with_dir("target").unwrap();
let missing = !ac.probe_rustc_version(1, 36);
assert!(missing ^ ac.probe_sysroot_crate("alloc"));
}

#[test]
fn probe_bad_sysroot_crate() {
let ac = AutoCfg::with_dir("target").unwrap();
assert!(!ac.probe_sysroot_crate("doesnt_exist"));
}

#[test]
fn probe_no_std() {
let ac = AutoCfg::with_dir("target").unwrap();
Expand Down

0 comments on commit 5dacd2d

Please sign in to comment.