Skip to main content

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    /// Uses the perl named by the `PERL` environment variable when set
11    /// (and non-empty), falling back to `perl` on `PATH`. Build tools
12    /// like ExtUtils::MakeMaker postambles pass `PERL=$(FULLPERL)` so
13    /// that the perl running Makefile.PL and the perl being linked
14    /// against are the same.
15    fn default() -> Self {
16        // Instruct cargo to re-run the calling build script when the
17        // selected perl changes. Safe here: this crate is only used
18        // from build scripts.
19        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                        // Embed rpath into ALL link products of the calling
92                        // crate — `cargo:rustc-link-arg=` covers cdylibs,
93                        // bins, examples, AND `cargo test` binaries. The
94                        // earlier `cargo:rustc-cdylib-link-arg=` form only
95                        // covered cdylibs, so test binaries on perls
96                        // installed in non-default locations (e.g. via
97                        // `shogo82148/actions-setup-perl@v1`) failed at
98                        // runtime with `libperl.so: cannot open shared
99                        // object file`.
100                        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}