-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbuild.rs
More file actions
62 lines (51 loc) · 1.88 KB
/
build.rs
File metadata and controls
62 lines (51 loc) · 1.88 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
// MIT/Apache2/Zlib License
//! Finds the path of Xlib and Xlib-XCB.
//!
//! Normally, we should be able to link directly to these packages. However, on some atypical
//! Linux configurations (like NixOS), they might be in other directories. As far as I know
//! `pkg-config` is the only blessed way to find and link to these libraries. So unfortunately,
//! we have to have a heavy build script and build-time dependency.
use std::io::{self, prelude::*};
use std::path::PathBuf;
use std::{env, fs};
fn find_link_deps(deps: &[(&str, &str)]) -> Result<(), Box<dyn std::error::Error>> {
for (_name, pkg_config_name) in deps {
pkg_config::Config::new().probe(pkg_config_name)?;
}
Ok(())
}
fn find_dlopen_dirs(deps: &[(&str, &str)]) -> Result<(), Box<dyn std::error::Error>> {
let mut libdir_file = {
let path = PathBuf::from(env::var_os("OUT_DIR").unwrap()).join("libdir.rs");
let libdir_file = fs::File::create(path)?;
io::BufWriter::new(libdir_file)
};
for (name, pkg_config_name) in deps {
let dir = match pkg_config::get_variable(pkg_config_name, "libdir") {
Ok(libdir) => format!("Some(\"{}\")", libdir),
Err(err) => {
println!(
"cargo:warning=failed to get libdir for library {}: {}",
pkg_config_name, err
);
"None".to_string()
}
};
writeln!(
libdir_file,
"const {}_LIBDIR: Option<&str> = {};",
name, dir
)?;
}
libdir_file.flush()?;
Ok(())
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
const DEPENDENCIES: &[(&str, &str)] = &[("XLIB", "x11"), ("XLIB_XCB", "x11-xcb")];
if env::var_os("CARGO_FEATURE_DLOPEN").is_some() {
find_dlopen_dirs(DEPENDENCIES)?;
} else {
find_link_deps(DEPENDENCIES)?;
}
Ok(())
}