]> git.proxmox.com Git - perlmod.git/blob - perlmod/build.rs
bump perlmod to 0.13.1-1
[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!("{}/include", out_dir);
11 let ppport_h_file = format!("{}/ppport.h", include_dir);
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("{}");"#,
27 ppport_h_file_string_inner
28 ))
29 .output()
30 .expect("failed to create ppport.h file using perl's Devel::PPPort");
31
32 // get include path:
33 // perl -MConfig -e 'print $Config{archlib}'
34 let perl_archlib = Command::new("perl")
35 .arg("-MConfig")
36 .arg("-e")
37 .arg("print $Config{archlib}")
38 .output()
39 .expect("failed to get perl arch include directory");
40 // technically not a true Path, but we expect a system path which should be utf8-compatible
41 let archlib_include_path = format!(
42 "{}/CORE",
43 std::str::from_utf8(&perl_archlib.stdout).expect("expected perl include path to be utf8")
44 );
45
46 // get perl cflags:
47 // perl -MConfig -e 'print $Config{ccflags}'
48 let perl_ccflags = Command::new("perl")
49 .arg("-MConfig")
50 .arg("-e")
51 .arg("print $Config{ccflags}")
52 .output()
53 .expect("failed to get perl cflags");
54 // technically garbage as it may contain paths, but since this should only contain system
55 // paths and otherwise also might contain quotes and what not let's just not really care:
56 let ccflags = std::str::from_utf8(&perl_ccflags.stdout).expect("expected cflags to be utf8");
57
58 let mut cc = cc::Build::new();
59
60 cc.pic(true)
61 .shared_flag(false)
62 .opt_level(3)
63 .include(include_dir)
64 .include(archlib_include_path);
65
66 for flag in ccflags.split_ascii_whitespace() {
67 cc.flag(flag);
68 }
69
70 // now build the static library:
71 cc.file("src/glue.c").compile("libglue.a");
72
73 // get perl's MULTIPLICITY flag:
74 // perl -MConfig -e 'print $Config{usemultiplicity}'
75 let perl_multiplicity = Command::new("perl")
76 .arg("-MConfig")
77 .arg("-e")
78 .arg("print $Config{usemultiplicity}")
79 .output()
80 .expect("failed to get perl usemultiplicity flag");
81
82 // pass the multiplicity cfg flag:
83 if perl_multiplicity.stdout == b"define" {
84 println!("cargo:rustc-cfg=perlmod=\"multiplicity\"");
85 }
86
87 // the debian package should include src/glue.c
88 println!(
89 "dh-cargo:deb-built-using=glue=1={}",
90 env::var("CARGO_MANIFEST_DIR").unwrap()
91 );
92 }