libperl_config/lib.rs
1//! # libperl-config
2//!
3//! Build-time helper for crates that link against `libperl`. Reads the
4//! local Perl installation's `Config.pm` (via `perl -V:...`) and turns
5//! the answers into `cargo:` directives + `cfg(...)` flags.
6//!
7//! Used by [`libperl-sys`](https://docs.rs/libperl-sys) and
8//! [`libperl-rs`](https://docs.rs/libperl-rs) build scripts to:
9//!
10//! - emit `cargo:rustc-link-search` / `rustc-link-lib` / `rustc-link-arg`
11//! from `$Config{ccopts}` and `$Config{ldopts}`,
12//! - expose feature toggles like `cfg(perl_useithreads)` based on
13//! `$Config{useithreads}`,
14//! - emit per-API-version cfgs (`cfg(perlapi_ver26)` ...
15//! `cfg(perlapi_ver42)`) so source can branch on Perl version.
16//!
17//! Typical usage in a downstream `build.rs`:
18//!
19//! ```no_run
20//! use libperl_config::PerlConfig;
21//!
22//! fn main() {
23//! let config = PerlConfig::default();
24//! config.emit_cargo_ldopts();
25//! config.emit_features(&["useithreads"]);
26//! config.emit_all_perlapi_versions(10);
27//! }
28//! ```
29//!
30//! See [`PerlConfig`] for the full API.
31
32mod perl_command;
33pub use perl_command::*;
34
35mod perl_config;
36pub use perl_config::*;
37
38pub mod process_util;
39
40#[cfg(test)]
41mod tests {
42 #[test]
43 fn it_works() {
44 let cfg = super::PerlConfig::default();
45 assert!(cfg.read_ccopts().unwrap().len() > 0);
46 assert!(cfg.read_ldopts().unwrap().len() > 0);
47 }
48
49 #[test]
50 fn can_read_config() {
51 let cfg = super::PerlConfig::default();
52 let perl_version = cfg.dict.get("PERL_VERSION");
53 assert_ne!(perl_version, None);
54 if let Some(ver) = perl_version {
55 let script = r#"
56use strict;
57use Config;
58print "PERL_VERSION\t", $Config{PERL_VERSION};
59"#;
60 assert_eq!(super::process_util::process_command_output(
61 cfg.command(&["-e", script]).output().unwrap()
62 ).unwrap(), ["PERL_VERSION", ver].join("\t"))
63 }
64 }
65}