]> git.proxmox.com Git - perlmod.git/blob - perlmod/build.rs
bump perlmod-bin to 0.2.0-3
[perlmod.git] / perlmod / build.rs
1 extern crate cc;
2
3 use std::path::Path;
4 use std::process::Command;
5 use std::{env, fs, io};
6
7 fn main() {
8 let out_dir = env::var("OUT_DIR").expect("expected OUT_DIR to be set by cargo");
9
10 let include_dir = format!("{out_dir}/include");
11 let ppport_h_file = format!("{include_dir}/ppport.h");
12 // quoted, without exterial double qoutes
13 let ppport_h_file_string_inner = ppport_h_file.replace('"', "\\\"");
14
15 if let Err(err) = fs::create_dir(Path::new(&include_dir)) {
16 if err.kind() != io::ErrorKind::AlreadyExists {
17 panic!("failed to make include dir in OUT_DIR");
18 }
19 }
20
21 // perl -MDevel::PPPort -e 'Devel::PPPort::WriteFile("include/ppport.h");'
22 Command::new("perl")
23 .arg("-MDevel::PPPort")
24 .arg("-e")
25 .arg(&format!(
26 r#"Devel::PPPort::WriteFile("{ppport_h_file_string_inner}");"#
27 ))
28 .output()
29 .expect("failed to create ppport.h file using perl's Devel::PPPort");
30
31 // get include path:
32 // perl -MConfig -e 'print $Config{archlib}'
33 let perl_archlib = Command::new("perl")
34 .arg("-MConfig")
35 .arg("-e")
36 .arg("print $Config{archlib}")
37 .output()
38 .expect("failed to get perl arch include directory");
39 // technically not a true Path, but we expect a system path which should be utf8-compatible
40 let archlib_include_path = format!(
41 "{}/CORE",
42 std::str::from_utf8(&perl_archlib.stdout).expect("expected perl include path to be utf8")
43 );
44
45 // get perl cflags:
46 // perl -MConfig -e 'print $Config{ccflags}'
47 let perl_ccflags = Command::new("perl")
48 .arg("-MConfig")
49 .arg("-e")
50 .arg("print $Config{ccflags}")
51 .output()
52 .expect("failed to get perl cflags");
53 // technically garbage as it may contain paths, but since this should only contain system
54 // paths and otherwise also might contain quotes and what not let's just not really care:
55 let ccflags = std::str::from_utf8(&perl_ccflags.stdout).expect("expected cflags to be utf8");
56
57 let mut cc = cc::Build::new();
58
59 cc.pic(true)
60 .shared_flag(false)
61 .opt_level(3)
62 .include(include_dir)
63 .include(archlib_include_path);
64
65 for flag in ccflags.split_ascii_whitespace() {
66 cc.flag(flag);
67 }
68
69 // now build the static library:
70 cc.file("src/glue.c").compile("libglue.a");
71
72 // get perl's MULTIPLICITY flag:
73 // perl -MConfig -e 'print $Config{usemultiplicity}'
74 let perl_multiplicity = Command::new("perl")
75 .arg("-MConfig")
76 .arg("-e")
77 .arg("print $Config{usemultiplicity}")
78 .output()
79 .expect("failed to get perl usemultiplicity flag");
80
81 // pass the multiplicity cfg flag:
82 if perl_multiplicity.stdout == b"define" {
83 println!("cargo:rustc-cfg=perlmod=\"multiplicity\"");
84 }
85
86 // the debian package should include src/glue.c
87 println!(
88 "dh-cargo:deb-built-using=glue=1={}",
89 env::var("CARGO_MANIFEST_DIR").unwrap()
90 );
91 }