libperl_config/
perl_config.rs1use super::process_util::*;
2
3use super::PerlCommand;
4
5use std::collections::HashMap;
6
7type ConfigDict = HashMap<String, String>;
8
9pub struct PerlConfig {
10 command: PerlCommand,
11 pub dict: ConfigDict,
12}
13
14impl Default for PerlConfig {
15 fn default() -> Self {
16 let cmd = PerlCommand::default();
17 let dict = read_config(&cmd, &[]).expect("Failed to read Config.pm");
18 Self {
19 command: cmd,
20 dict: dict,
21 }
22 }
23}
24
25impl PerlConfig {
26
27 pub fn new(perl: &str) -> Self {
28 let cmd = PerlCommand::new(perl);
29 let dict = read_config(&cmd, &[]).expect("Failed to read Config.pm");
30 Self {
31 command: cmd,
32 dict: dict,
33 }
34 }
35
36 pub fn command(&self, args: &[&str]) -> Command {
37 self.command.command(args)
38 }
39
40 pub fn read_ccopts(&self) -> Result<Vec<String>, Error> {
41 self.command.read_ccopts()
42 }
43
44 pub fn read_ldopts(&self) -> Result<Vec<String>, Error> {
45 self.command.read_ldopts()
46 }
47
48 pub fn is_defined(&self, name: &str) -> Result<bool, Error> {
49 if let Some(value) = self.dict.get(name) {
50 Ok(value == "define")
51 } else {
52 Err(other_error("No such entry".to_string()))
53 }
54 }
55
56 pub fn emit_cargo_ldopts(&self) {
57 self.command.emit_cargo_ldopts()
58 }
59
60 pub fn emit_perlapi_vers(&self, min: i32, max: i32) {
61 let config = &self.dict["PERL_API_VERSION"];
62 let config = config.trim();
63 println!("# PERL_API_VERSION={}", config);
64 let ver = i32::from_str_radix(String::from(config).trim(), 10).unwrap();
65 for v in min..=max {
66 if v % 2 == 1 {
67 continue;
68 }
69 println!("cargo::rustc-check-cfg=cfg(perlapi_ver{})", v);
71 if ver >= v {
72 println!("cargo:rustc-cfg=perlapi_ver{}", v);
73 }
74 }
75 }
76
77 pub fn emit_features(&self, configs: &[&str]) {
78 for &cfg in configs.iter() {
79 println!("# perl config {} = {:?}", cfg, self.dict.get(&String::from(cfg)));
80 println!("cargo::rustc-check-cfg=cfg(perl_{})", cfg);
82 if self.is_defined(cfg).unwrap() {
83 println!("cargo:rustc-cfg=perl_{}", cfg);
84 }
85 }
86 }
87}
88
89fn read_config(cmd: &PerlCommand, configs: &[&str]) -> Result<ConfigDict, Error> {
90 let config = cmd.read_raw_config(configs)?;
91 let lines = config.lines().map(String::from).collect();
92 Ok(lines_to_hashmap(lines))
93}
94
95fn lines_to_hashmap(lines: Vec<String>) -> ConfigDict {
96 let mut dict = HashMap::new();
97 for line in lines.iter() {
98 let kv: Vec<String> = line.splitn(2, '\t').map(String::from).collect();
99 if kv.len() == 2 {
100 dict.insert(kv[0].clone(), kv[1].clone());
101 }
102 }
103 dict
104}