libperl_config/
perl_command.rs

1use super::process_util::*;
2
3use regex::Regex;
4
5pub struct PerlCommand {
6    perl: String,    
7}
8
9impl Default for PerlCommand {
10    fn default() -> Self {
11        Self {
12            perl: String::from("perl")
13        }
14    }
15}
16
17impl PerlCommand {
18
19    pub fn new(perl: &str) -> Self {
20        Self {
21            perl: String::from(perl),
22        }
23    }
24
25    pub fn command(&self, args: &[&str]) -> Command {
26        make_command(self.perl.as_str(), args)
27    }
28
29    pub fn read_raw_config(&self, configs: &[&str]) -> Result<String, Error> {
30        let script = ["-wle", r#"
31    use strict;
32    use Config;
33    print join "\t", $_, ($Config{$_} // '')
34      for @ARGV ? @ARGV : sort keys %Config;
35    "#
36        ];
37        let mut cmd = self.command(&[&script[..], configs].concat());
38        
39        process_command_output(cmd.output()?)
40    }
41
42    pub fn read_ccopts(&self) -> Result<Vec<String>, Error> {
43        self.read_embed_opts("ccopts", r"^-[ID]")
44    }
45
46    pub fn read_ldopts(&self) -> Result<Vec<String>, Error> {
47        self.read_embed_opts("ldopts", r"^-[lL]")
48    }
49
50    pub fn read_raw_embed_opts(&self, cmd: &str) -> Result<String, Error> {
51        let mut cmd = self.command(
52            &["-MExtUtils::Embed", "-e", cmd],
53        );
54
55        process_command_output(cmd.output()?)
56    }
57
58    pub fn read_embed_opts(&self, cmd: &str, prefix: &str) -> Result<Vec<String>, Error> {
59        let out_str = self.read_raw_embed_opts(cmd)?;
60
61        let re = Regex::new(prefix).unwrap();
62        Ok(out_str
63           .split_whitespace()
64           .map(String::from)
65           .filter(|s| re.is_match(s))
66           .collect())
67    }
68
69    pub fn emit_cargo_ldopts(&self) {
70        let ldopts = self.read_ldopts().unwrap();
71        println!("# perl ldopts = {:?}, ", ldopts);
72
73        for opt in self.read_ldopts().unwrap().iter() {
74            if opt.starts_with("-L") {
75                let libpath = opt.get(2..).unwrap();
76                println!("cargo:rustc-link-search={}", libpath);
77                if std::path::Path::new(libpath).file_name()
78                    == Some(std::ffi::OsStr::new("CORE")) {
79                        println!("cargo:rustc-cdylib-link-arg=-Wl,-rpath,{}", libpath);
80                    }
81            }
82            else if opt.starts_with("-l") {
83                println!("cargo:rustc-link-lib={}", opt.get(2..).unwrap());
84            }
85        }
86    }
87}