libperl_config/
perl_config.rs

1use 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            if ver >= v {
70                println!("cargo:rustc-cfg=perlapi_ver{}", v);
71            }
72        }
73    }
74
75    pub fn emit_features(&self, configs: &[&str]) {
76        for &cfg in configs.iter() {
77            println!("# perl config {} = {:?}", cfg, self.dict.get(&String::from(cfg)));
78            if self.is_defined(cfg).unwrap() {
79                println!("cargo:rustc-cfg=perl_{}", cfg);
80            }
81        }
82    }
83}
84
85fn read_config(cmd: &PerlCommand, configs: &[&str]) -> Result<ConfigDict, Error> {
86    let config = cmd.read_raw_config(configs)?;
87    let lines = config.lines().map(String::from).collect();
88    Ok(lines_to_hashmap(lines))
89}
90
91fn lines_to_hashmap(lines: Vec<String>) -> ConfigDict {
92    let mut dict = HashMap::new();
93    for line in lines.iter() {
94        let kv: Vec<String> = line.splitn(2, '\t').map(String::from).collect();
95        if kv.len() == 2 {
96            dict.insert(kv[0].clone(), kv[1].clone());
97        }
98    }
99    dict
100}