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