-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.rs
74 lines (65 loc) · 1.72 KB
/
build.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
#[cfg(unix)]
extern crate pkg_config;
#[cfg(unix)]
use std::process::Command;
#[cfg(unix)]
fn main() {
// Fall back to libnetp-config
let output = Command::new("libnet-config")
.arg("--libs")
.output()
.expect("Failed to run libnet-config. libnet could not be linked");
parse_libs_cflags(&output.stdout);
}
/// Adapted from pkg_config
#[cfg(unix)]
fn parse_libs_cflags(output: &[u8]) {
let words = split_flags(output);
let parts = words.iter()
.filter(|l| l.len() > 2)
.map(|arg| (&arg[0..2], &arg[2..]))
.collect::<Vec<_>>();
for &(flag, val) in &parts {
match flag {
"-L" => {
println!("cargo:rustc-link-search=native={}", val);
}
"-F" => {
println!("cargo:rustc-link-search=framework={}", val);
}
"-l" => {
println!("cargo:rustc-link-lib={}", val);
}
_ => {}
}
}
}
/// Copied from pkg_config
#[cfg(unix)]
fn split_flags(output: &[u8]) -> Vec<String> {
let mut word = Vec::new();
let mut words = Vec::new();
let mut escaped = false;
for &b in output {
match b {
_ if escaped => {
escaped = false;
word.push(b);
}
b'\\' => {
escaped = true
}
b'\t' | b'\n' | b'\r' | b' ' => {
if !word.is_empty() {
words.push(String::from_utf8(word).unwrap());
word = Vec::new();
}
}
_ => word.push(b),
}
}
if !word.is_empty() {
words.push(String::from_utf8(word).unwrap());
}
words
}