-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathbuild.rs
More file actions
79 lines (67 loc) · 2.33 KB
/
build.rs
File metadata and controls
79 lines (67 loc) · 2.33 KB
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
extern crate pkg_config;
use std::env;
use std::fs::File;
use std::path::Path;
use std::process::Command;
use std::io::prelude::*;
fn check_func(function_name: &str, lib: pkg_config::Library) -> bool {
let out_dir = env::var_os("OUT_DIR").unwrap();
let test_file_name = Path::new(&out_dir).join(format!("check_{}.rs", function_name));
{
let mut test_file = File::create(&test_file_name).unwrap();
writeln!(&mut test_file, "extern \"C\" {{").unwrap();
writeln!(&mut test_file, " fn {}();", function_name).unwrap();
writeln!(&mut test_file, "}}").unwrap();
writeln!(&mut test_file, "").unwrap();
writeln!(&mut test_file, "fn main() {{").unwrap();
writeln!(&mut test_file, " unsafe {{").unwrap();
writeln!(&mut test_file, " {}();", function_name).unwrap();
writeln!(&mut test_file, " }}").unwrap();
writeln!(&mut test_file, "}}").unwrap();
}
let rustc = env::var("RUSTC").unwrap();
let mut cmd = Command::new(rustc);
cmd.arg(&test_file_name).arg("--out-dir").arg(&out_dir);
for path in lib.link_paths {
cmd.arg("-L").arg(path);
}
for lib in lib.libs {
cmd.arg("-l").arg(lib);
}
cmd.args(["--target", &std::env::var("TARGET").unwrap()]);
if let Ok(linker) = std::env::var("RUSTC_LINKER") {
cmd.args(["-C", &format!("linker={linker}")]);
}
let output = cmd.output().unwrap();
if !output.status.success() {
println!(
"cargo:warning=Failed to compile test program for udev function `{}`",
function_name
);
println!("cargo:warning=Using command`{:?}`", cmd);
println!(
"cargo:warning=stdout={}",
String::from_utf8_lossy(&output.stdout)
.trim()
.replace('\n', "\ncargo:warning=")
);
println!(
"cargo:warning=stderr={}",
String::from_utf8_lossy(&output.stderr)
.trim()
.replace('\n', "\ncargo:warning=")
);
false
} else {
true
}
}
fn main() {
let lib = pkg_config::probe_library("libudev").unwrap();
if check_func("udev_hwdb_new", lib) {
println!("cargo:rustc-cfg=hwdb");
println!("cargo:hwdb=true");
} else {
println!("cargo:hwdb=false");
}
}