-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.rs
More file actions
89 lines (81 loc) · 3.55 KB
/
build.rs
File metadata and controls
89 lines (81 loc) · 3.55 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
80
81
82
83
84
85
86
87
88
89
/****************************************************************
* *
* Copyright (c) 2019-2024 YottaDB LLC and/or its subsidiaries. *
* All rights reserved. *
* *
* This source code contains the intellectual property *
* of its copyright holder(s), and is made available *
* under a license. If you do not know the terms of *
* the license, please stop and do not read further. *
* *
****************************************************************/
use std::env;
use std::path::{PathBuf, Path};
fn main() {
let yottadb = pkg_config::probe_library("yottadb").unwrap();
let mut include_path = String::from("-I");
for path in yottadb.include_paths {
let s = path.to_str().unwrap();
include_path.push_str(s);
}
let mut library_path = String::from("");
for path in yottadb.link_paths {
let s = path.to_str().unwrap();
library_path.push_str(s);
}
println!("cargo:rust-link-search={}", library_path);
println!("cargo:rerun-if-changed=wrapper.h");
println!("cargo:rerun-if-changed=build.rs");
println!("cargo:rerun-if-changed=Cargo.lock");
// Write the bindings to the $OUT_DIR/bindings.rs file.
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
let out_path = out_path.join("bindings.rs");
#[cfg(feature = "vendor")]
generate_bindings_via_library(include_path, &out_path);
#[cfg(not(feature = "vendor"))]
generate_bindings_via_cli(include_path, &out_path);
}
#[cfg(feature = "vendor")]
fn generate_bindings_via_library(include_path: String, out_path: &Path) {
bindgen::Builder::default()
.header("wrapper.h")
.clang_arg(include_path)
.allowlist_type("ydb_.*")
.allowlist_function("ydb_.*")
.allowlist_var("YDB_.*")
// for `ydb_lock`
.allowlist_type("gparam_list.*")
.allowlist_var("MAX_GPARAM_LIST_ARGS")
.blocklist_item("YDB_NOTTP")
// Finish the builder and generate the bindings.
.generate()
// Unwrap the Result and panic on failure.
.expect("Unable to generate bindings")
.write_to_file(out_path)
.expect("Couldn't write bindings!");
}
#[cfg(not(feature = "vendor"))]
fn generate_bindings_via_cli(include_path: String, out_path: &Path) {
use std::process::Command;
#[rustfmt::skip] // rustfmt doesn't know the arguments are related
let args = [
"wrapper.h",
"--whitelist-type", "ydb_.*",
"--whitelist-function", "ydb_.*",
"--whitelist-var", "YDB_.*",
"--whitelist-type", "gparam_list.*",
"--whitelist-var", "MAX_GPARAM_LIST_ARGS",
"--blacklist-item", "YDB_NOTTP",
// This was a String originally, so it's safe to unwrap.
// It only went through `Path` so that the file separator would be right.
"--output", out_path.to_str().unwrap(),
"--",
&include_path,
];
match Command::new("bindgen").args(args).status() {
Ok(status) => assert!(status.success(), "error: failed to generate bindings with bindgen\nhelp: there may be more error output above"),
Err(err) => panic!("error: failed to run bindgen: {}\n\
help: make sure `bindgen` is installed and in $PATH\n\
help: consider enabling the `vendor` feature, which compiles `bindgen` from source", err),
}
}