From 5dacd2df27a9666ba4f322fe7a802acf8ecdae3c Mon Sep 17 00:00:00 2001 From: leo60228 Date: Fri, 16 Aug 2019 17:20:22 -0400 Subject: [PATCH] Add probe_sysroot_crate and emit_sysroot_crate --- src/lib.rs | 22 ++++++++++++++++++++++ src/tests.rs | 19 +++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 7a9532c..0a689c4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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: diff --git a/src/tests.rs b/src/tests.rs index f22e0e0..ec33d05 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -56,6 +56,25 @@ fn probe_sum() { assert!(missing ^ ac.probe_type("std::iter::Sum")); } +#[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();