forked from voipir/rust-xmlsec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bindings.rs
85 lines (64 loc) · 2.17 KB
/
bindings.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
//!
//! XmlSec Bindings Generation
//!
use bindgen::Builder as BindgenBuilder;
use bindgen::Formatter as BindgenFormatter;
use pkg_config::Config as PkgConfig;
use std::env;
use std::path::PathBuf;
use std::process::Command;
const BINDINGS: &str = "bindings.rs";
fn main()
{
println!("cargo:rustc-link-lib=xmlsec1-openssl"); // -lxmlsec1-openssl
println!("cargo:rustc-link-lib=xmlsec1"); // -lxmlsec1
println!("cargo:rustc-link-lib=xml2"); // -lxml2
println!("cargo:rustc-link-lib=ssl"); // -lssl
println!("cargo:rustc-link-lib=crypto"); // -lcrypto
let path_out = PathBuf::from(env::var("OUT_DIR").unwrap());
let path_bindings = path_out.join(BINDINGS);
if !path_bindings.exists()
{
PkgConfig::new()
.probe("xmlsec1")
.expect("Could not find xmlsec1 using pkg-config");
let bindbuild = BindgenBuilder::default()
.header("bindings.h")
.clang_args(fetch_xmlsec_config_flags())
.clang_args(fetch_xmlsec_config_libs())
.layout_tests(true)
.formatter(BindgenFormatter::default())
.generate_comments(true);
let bindings = bindbuild.generate()
.expect("Unable to generate bindings");
bindings.write_to_file(path_bindings)
.expect("Couldn't write bindings!");
}
}
fn fetch_xmlsec_config_flags() -> Vec<String>
{
let out = Command::new("xmlsec1-config")
.arg("--cflags")
.output()
.expect("Failed to get --cflags from xmlsec1-config. Is xmlsec1 installed?")
.stdout;
args_from_output(out)
}
fn fetch_xmlsec_config_libs() -> Vec<String>
{
let out = Command::new("xmlsec1-config")
.arg("--libs")
.output()
.expect("Failed to get --libs from xmlsec1-config. Is xmlsec1 installed?")
.stdout;
args_from_output(out)
}
fn args_from_output(args: Vec<u8>) -> Vec<String>
{
let decoded = String::from_utf8(args)
.expect("Got invalid UTF8 from xmlsec1-config");
let args = decoded.split_whitespace()
.map(|p| p.to_owned())
.collect::<Vec<String>>();
args
}