-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.rs
More file actions
58 lines (50 loc) · 1.96 KB
/
build.rs
File metadata and controls
58 lines (50 loc) · 1.96 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
use std::env;
use std::path::PathBuf;
fn main() {
let include_paths = if env::var_os("CARGO_FEATURE_VENDORED").is_some() {
build_vendored();
vec![PathBuf::from("generated"), PathBuf::from("libversion")]
} else {
find_system_lib()
};
let mut bindings = bindgen::Builder::default()
.header("wrapper.h")
.default_enum_style(bindgen::EnumVariation::Consts)
.allowlist_function("version_compare.*")
.allowlist_var("VERSIONFLAG_.*")
.allowlist_var("LIBVERSION_VERSION.*")
.parse_callbacks(Box::new(bindgen::CargoCallbacks::new()))
.clang_args(
include_paths
.iter()
.map(|path| format!("-I{}", path.display())),
);
if env::var_os("CARGO_FEATURE_VENDORED").is_some() {
bindings = bindings.clang_arg("-DLIBVERSION_STATIC_DEFINE");
}
let bindings = bindings.generate().expect("Unable to generate bindings");
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
bindings
.write_to_file(out_path.join("bindings.rs"))
.expect("Couldn't write bindings!");
}
fn build_vendored() {
// Build libversion from vendored C sources. The cmake-generated headers
// (config.h, export.h) are pre-committed under generated/ so end users do
// not need cmake installed for the default build.
cc::Build::new()
.file("libversion/libversion/compare.c")
.file("libversion/libversion/private/compare.c")
.file("libversion/libversion/private/parse.c")
.include("libversion")
.include("generated")
.define("LIBVERSION_STATIC_DEFINE", None)
.compile("version");
}
fn find_system_lib() -> Vec<PathBuf> {
let library = pkg_config::Config::new()
.atleast_version("3.0.0")
.probe("libversion")
.expect("failed to find system libversion via pkg-config; enable the vendored feature or install libversion.pc");
library.include_paths
}