From 2feeb8c8d889dc3b4566141f38a429d272e8443e Mon Sep 17 00:00:00 2001 From: Guillaume Desmottes Date: Mon, 8 Feb 2021 12:30:37 +0100 Subject: [PATCH] export feature for each found dependency This can be used to check if an optional dep has been found or not. Fix #27 --- src/lib.rs | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 650e42b..a80ce99 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -43,10 +43,19 @@ //! //! ```toml //! [package.metadata.system-deps] -//! testdata = { version = "4.5", optional = true } +//! test-data = { version = "4.5", optional = true } //! testmore = { version = "2", v3 = { version = "3.0", optional = true }} //! ``` //! +//! `system-deps` will automatically export for each dependency a feature `system_deps_have_$DEP` where `$DEP` +//! is the `toml` key defining the dependency in [snake_case](https://en.wikipedia.org/wiki/Snake_case). +//! This can be used to check if an optional dependency has been found or not: +//! +//! ``` +//! #[cfg(system_deps_have_testdata)] +//! println!("found test-data"); +//! ``` +//! //! # Overriding library name //! `toml` keys cannot contain dot characters so if your library name does you can define it using the `name` field: //! @@ -131,7 +140,7 @@ extern crate lazy_static; #[cfg(test)] mod test; -use heck::ShoutySnakeCase; +use heck::{ShoutySnakeCase, SnakeCase}; use std::collections::HashMap; use std::env; use std::fmt; @@ -314,6 +323,10 @@ impl Config { // Output cargo flags println!("{}", flags); + for (name, _) in libraries.iter() { + println!("cargo:rustc-cfg=system_deps_have_{}", name.to_snake_case()); + } + Ok(libraries) }