Debian Codegraph @

build.rs

raw symbols SCIP
1 #[cfg(unix)]
2 extern crate pkg_config;
3
4 #[cfg(windows)]
5 use std::path::PathBuf;
6 #[cfg(unix)]
7 use std::process::Command;
8
9 #[cfg(windows)]
10 fn main() {
11 let dir = std::env::var("CARGO_MANIFEST_DIR").unwrap();
12 let mut lib_path = PathBuf::from(&dir).join("Lib");
13 if cfg!(target_arch = "x86_64") {
14 lib_path.push("x64")
15 }
16 println!("cargo:rustc-link-search=native={}", lib_path.display());
17 println!("cargo:rustc-link-lib=packet");
18 println!("cargo:rustc-link-lib=wpcap");
19 }
20
21 #[cfg(unix)]
22 fn main() {
23 // First, try pkg_config (available in libpcap 1.9.0+)
24 if pkg_config::probe_library("libpcap").is_ok() {
25 return;
26 }
27
28 // Fall back to pcap-config
29 match Command::new("pcap-config").arg("--libs").output() {
30 Ok(output) => {
31 parse_libs_cflags(&output.stdout)
32 },
33 _ => (),
34 }
35
36 // on macOS, pcap-config returns /usr/local/lib, but libpcap is actually in /usr/lib
37 println!("cargo:rustc-link-search=native=/usr/lib");
38 }
39
40 /// Adapted from pkg_config
41 #[cfg(unix)]
42 fn parse_libs_cflags(output: &[u8]) {
43 let words = split_flags(output);
44 let parts = words
45 .iter()
46 .filter(|l| l.len() > 2)
47 .map(|arg| (&arg[0..2], &arg[2..]))
48 .collect::<Vec<_>>();
49
50 for &(flag, val) in &parts {
51 match flag {
52 "-L" => {
53 println!("cargo:rustc-link-search=native={}", val);
54 }
55 "-F" => {
56 println!("cargo:rustc-link-search=framework={}", val);
57 }
58 "-l" => {
59 println!("cargo:rustc-link-lib={}", val);
60 }
61 _ => {}
62 }
63 }
64 }
65
66 /// Copied from pkg_config
67 #[cfg(unix)]
68 fn split_flags(output: &[u8]) -> Vec<String> {
69 let mut word = Vec::new();
70 let mut words = Vec::new();
71 let mut escaped = false;
72
73 for &b in output {
74 match b {
75 _ if escaped => {
76 escaped = false;
77 word.push(b);
78 }
79 b'\\' => escaped = true,
80 b'\t' | b'\n' | b'\r' | b' ' => {
81 if !word.is_empty() {
82 words.push(String::from_utf8(word).unwrap());
83 word = Vec::new();
84 }
85 }
86 _ => word.push(b),
87 }
88 }
89
90 if !word.is_empty() {
91 words.push(String::from_utf8(word).unwrap());
92 }
93
94 words
95 }