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
use cmake::Config;
use std::{env, path::PathBuf};
fn main() {
// Compile-time features
let generator = if cfg!(feature = "cmake-ninja") {
"Ninja"
} else if cfg!(feature = "cmake-vs2017") {
"Visual Studio 15 2017"
} else if cfg!(feature = "cmake-vs2017-win64") {
"Visual Studio 15 2017 Win64"
} else {
if cfg!(target_family = "unix") {
"Unix Makefiles"
} else {
"Ninja"
}
};
let stats = if cfg!(feature = "nng-stats") {
"ON"
} else {
"OFF"
};
let tls = if cfg!(feature = "nng-tls") {
"ON"
} else {
"OFF"
};
// Run cmake to build nng
let dst = Config::new("nng")
.generator(generator)
.define("CMAKE_BUILD_TYPE", "Release")
.define("NNG_TESTS", "OFF")
.define("NNG_TOOLS", "OFF")
.define("NNG_ENABLE_STATS", stats)
.define("NNG_ENABLE_TLS", tls)
.build();
// Check output of `cargo build --verbose`, should see something like:
// -L native=/path/runng/target/debug/build/runng-sys-abc1234/out
// That contains output from cmake
println!(
"cargo:rustc-link-search=native={}",
dst.join("lib").display()
);
println!(
"cargo:rustc-link-search=native={}",
dst.join("lib64").display()
);
// Tell rustc to use nng static library
println!("cargo:rustc-link-lib=static=nng");
// https://rust-lang-nursery.github.io/rust-bindgen
// https://docs.rs/bindgen
let mut builder = bindgen::Builder::default()
.header("wrapper.h")
// This is needed if use `#include <nng.h>` instead of `#include "path/nng.h"`
//.clang_arg("-Inng/src/")
;
if !cfg!(feature = "legacy-111-rc4") {
builder = builder
.whitelist_type("nng_.*")
.whitelist_function("nng_.*")
.whitelist_var("NNG_.*")
// Generate `pub const NNG_UNIT_EVENTS` instead of `nng_unit_enum_NNG_UNIT_EVENTS`
.prepend_enum_name(false)
// Generate `pub enum ...` instead of multiple `pub const ...`
.rustified_enum("nng_.*_enum")
// Enum special cases:
.rustified_enum("nng_pipe_ev")
.rustified_enum("nng_sockaddr_family")
.rustified_enum("nng_zt_status")
// nostd support
// https://rust-embedded.github.io/book/interoperability/c-with-rust.html#automatically-generating-the-interface
.ctypes_prefix("cty")
.use_core();
}
let bindings = builder.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");
}