helpers.rs
1 /// Print to `stdout` a build-script instruction for Cargo. 2 /// 3 /// # Panics 4 /// If either argument is an empty string. 5 /// 6 /// (Actually private to the crate, not part of public API. Is only `pub` for old Rust versions.) 7 pub fn emit_cargo_instruction( 8 instruction: &str, 9 arg: Option<&str>, 10 ) 11 { 12 assert!(!instruction.is_empty()); 13 if let Some(arg) = arg { 14 assert!(!arg.is_empty()); 15 } 16 println!( 17 "cargo:{}{}", 18 instruction, 19 arg.map(|s| format!("={}", s)).unwrap_or_else(String::new) 20 ); 21 } 22 23 /// Tell Cargo to display the given warning message after a build script has finished running. 24 pub fn emit_warning(message: &str) 25 { 26 emit_cargo_instruction("warning", Some(message)); 27 } 28 29 /// Tell Cargo to pass a key-value configuration option to the compiler to be set for conditional 30 /// compilation, for features of the Rust compiler, language, or standard library. 31 /// 32 /// This enables using [the standard conditional-compilation 33 /// forms](https://doc.rust-lang.org/reference/conditional-compilation.html) (i.e. the `cfg` 34 /// attribute, et al) for features of Rust itself, in a way that is more similar to Cargo package 35 /// features. 36 /// 37 /// `category`: One of `"comp"`, `"lang"`, or `"lib"`. 38 /// 39 /// `value`: The feature name, which should follow [The Unstable 40 /// Book](https://doc.rust-lang.org/nightly/unstable-book/index.html) where appropriate. 41 /// 42 /// # Examples 43 /// 44 /// Doing `emit_rust_feature("lib", "step_trait")` in a package's build script enables the 45 /// package's source code to use `#[cfg(rust_lib_feature = "step_trait")]`. 46 /// 47 /// # Panics 48 /// 49 /// If `category` is not one of the acceptable categories. 50 /// 51 /// (Actually private to the crate, not part of public API. Is only `pub` for old Rust versions.) 52 pub fn emit_rust_feature( 53 category: &str, 54 name: &str, 55 ) 56 { 57 assert!(["comp", "lang", "lib"].contains(&category)); 58 emit_cargo_instruction("rustc-cfg", Some(&format!("rust_{}_feature={:?}", category, name))); 59 }