-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.rs
65 lines (59 loc) · 2.49 KB
/
build.rs
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
// smbclient-sys (rust bindings for libsmbclient from Samba project)
// Copyright (c) 2016 Konstantin Gribov
//
// This file is part of smbclient-sys.
//
// smbclient-sys is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// smbclient-sys is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with smbclient-sys. If not, see <http://www.gnu.org/licenses/>.
extern crate bindgen;
extern crate pkg_config;
use std::env;
use std::path::PathBuf;
fn main() {
let inc_path_name = "SMBCLIENT_INCLUDE_PATH";
let lib_path_name = "SMBCLIENT_LIBRARY_PATH";
let header = match (env::var(inc_path_name), env::var(lib_path_name)) {
(Ok(inc_path), Ok(lib_path)) => {
println!("cargo:rustc-link-lib=smbclient");
println!("cargo:rustc-link-search=native={}", lib_path);
let mut path = PathBuf::from(inc_path);
path.push("libsmbclient.h");
path.to_str().unwrap().to_string()
}
(Ok(_), Err(_)) | (Err(_), Ok(_)) =>
panic!("Either both {} and {} should be set or none of them", inc_path_name, lib_path_name),
(Err(_), Err(_)) => {
let lib = pkg_config::Config::new()
.probe("smbclient")
.expect("libsmbclient not found");
find_header(&lib).unwrap().to_str().unwrap().to_string()
}
};
let bindings = bindgen::Builder::default()
.rust_target(bindgen::RustTarget::Stable_1_25)
.header(header)
.ctypes_prefix("::libc")
.whitelist_type("(smbc_.*)|(SMBC.*)")
.whitelist_function("smbc_.*")
.whitelist_var("SMBC?.*")
.generate()
.expect("failed to generate bindings for libsmbclient");
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
bindings.write_to_file(out_dir.join("libsmbclient.rs"))
.expect("failed to write libsmbclient.rs");
}
fn find_header(lib: &pkg_config::Library) -> Option<PathBuf> {
lib.include_paths.iter()
.map(|p| p.join("libsmbclient.h"))
.find(|p| p.exists() && p.is_file())
}