@@ -2,6 +2,59 @@ use cmake::Config;
2
2
use std:: { env, path:: PathBuf } ;
3
3
4
4
fn main ( ) {
5
+ if cfg ! ( feature = "build-nng" ) {
6
+ let dst = cmake ( ) ;
7
+ // Check output of `cargo build --verbose`, should see something like:
8
+ // -L native=/path/runng/target/debug/build/runng-sys-abc1234/out
9
+ // That contains output from cmake
10
+ println ! (
11
+ "cargo:rustc-link-search=native={}" ,
12
+ dst. join( "lib" ) . display( )
13
+ ) ;
14
+ println ! (
15
+ "cargo:rustc-link-search=native={}" ,
16
+ dst. join( "lib64" ) . display( )
17
+ ) ;
18
+
19
+ // Tell rustc to use nng static library
20
+ println ! ( "cargo:rustc-link-lib=static=nng" ) ;
21
+ } else {
22
+ println ! ( "cargo:rustc-link-lib=dylib=nng" ) ;
23
+ }
24
+
25
+ // https://rust-lang-nursery.github.io/rust-bindgen
26
+ // https://docs.rs/bindgen
27
+ let mut builder = bindgen:: Builder :: default ( )
28
+ // This is needed if use `#include <nng.h>` instead of `#include "path/nng.h"` in wrapper.h
29
+ //.clang_arg("-Inng/src/")
30
+ . header ( "wrapper.h" ) ;
31
+
32
+ if !cfg ! ( feature = "legacy-111-rc4" ) {
33
+ builder = builder
34
+ . whitelist_type ( "nng_.*" )
35
+ . whitelist_function ( "nng_.*" )
36
+ . whitelist_var ( "NNG_.*" )
37
+ // Generate `pub const NNG_UNIT_EVENTS` instead of `nng_unit_enum_NNG_UNIT_EVENTS`
38
+ . prepend_enum_name ( false )
39
+ // Generate `pub enum ...` instead of multiple `pub const ...`
40
+ . rustified_enum ( "nng_.*_enum" )
41
+ // Enum special cases:
42
+ . rustified_enum ( "nng_pipe_ev" )
43
+ . rustified_enum ( "nng_sockaddr_family" )
44
+ . rustified_enum ( "nng_zt_status" )
45
+ // no_std support
46
+ // https://rust-embedded.github.io/book/interoperability/c-with-rust.html#automatically-generating-the-interface
47
+ . ctypes_prefix ( "cty" )
48
+ . use_core ( ) ;
49
+ }
50
+ let bindings = builder. generate ( ) . expect ( "Unable to generate bindings" ) ;
51
+ let out_path = PathBuf :: from ( env:: var ( "OUT_DIR" ) . unwrap ( ) ) ;
52
+ bindings
53
+ . write_to_file ( out_path. join ( "bindings.rs" ) )
54
+ . expect ( "Couldn't write bindings" ) ;
55
+ }
56
+
57
+ fn cmake ( ) -> PathBuf {
5
58
// Compile-time features
6
59
let generator = if cfg ! ( feature = "cmake-ninja" ) {
7
60
"Ninja"
@@ -10,6 +63,7 @@ fn main() {
10
63
} else if cfg ! ( feature = "cmake-vs2017-win64" ) {
11
64
"Visual Studio 15 2017 Win64"
12
65
} else {
66
+ // Default generators
13
67
if cfg ! ( target_family = "unix" ) {
14
68
"Unix Makefiles"
15
69
} else {
@@ -28,58 +82,12 @@ fn main() {
28
82
} ;
29
83
30
84
// Run cmake to build nng
31
- let dst = Config :: new ( "nng" )
85
+ Config :: new ( "nng" )
32
86
. generator ( generator)
33
87
. define ( "CMAKE_BUILD_TYPE" , "Release" )
34
88
. define ( "NNG_TESTS" , "OFF" )
35
89
. define ( "NNG_TOOLS" , "OFF" )
36
90
. define ( "NNG_ENABLE_STATS" , stats)
37
91
. define ( "NNG_ENABLE_TLS" , tls)
38
- . build ( ) ;
39
-
40
- // Check output of `cargo build --verbose`, should see something like:
41
- // -L native=/path/runng/target/debug/build/runng-sys-abc1234/out
42
- // That contains output from cmake
43
- println ! (
44
- "cargo:rustc-link-search=native={}" ,
45
- dst. join( "lib" ) . display( )
46
- ) ;
47
- println ! (
48
- "cargo:rustc-link-search=native={}" ,
49
- dst. join( "lib64" ) . display( )
50
- ) ;
51
-
52
- // Tell rustc to use nng static library
53
- println ! ( "cargo:rustc-link-lib=static=nng" ) ;
54
-
55
- // https://rust-lang-nursery.github.io/rust-bindgen
56
- // https://docs.rs/bindgen
57
- let mut builder = bindgen:: Builder :: default ( )
58
- . header ( "wrapper.h" )
59
- // This is needed if use `#include <nng.h>` instead of `#include "path/nng.h"`
60
- //.clang_arg("-Inng/src/")
61
- ;
62
- if !cfg ! ( feature = "legacy-111-rc4" ) {
63
- builder = builder
64
- . whitelist_type ( "nng_.*" )
65
- . whitelist_function ( "nng_.*" )
66
- . whitelist_var ( "NNG_.*" )
67
- // Generate `pub const NNG_UNIT_EVENTS` instead of `nng_unit_enum_NNG_UNIT_EVENTS`
68
- . prepend_enum_name ( false )
69
- // Generate `pub enum ...` instead of multiple `pub const ...`
70
- . rustified_enum ( "nng_.*_enum" )
71
- // Enum special cases:
72
- . rustified_enum ( "nng_pipe_ev" )
73
- . rustified_enum ( "nng_sockaddr_family" )
74
- . rustified_enum ( "nng_zt_status" )
75
- // nostd support
76
- // https://rust-embedded.github.io/book/interoperability/c-with-rust.html#automatically-generating-the-interface
77
- . ctypes_prefix ( "cty" )
78
- . use_core ( ) ;
79
- }
80
- let bindings = builder. generate ( ) . expect ( "Unable to generate bindings" ) ;
81
- let out_path = PathBuf :: from ( env:: var ( "OUT_DIR" ) . unwrap ( ) ) ;
82
- bindings
83
- . write_to_file ( out_path. join ( "bindings.rs" ) )
84
- . expect ( "Couldn't write bindings" ) ;
92
+ . build ( )
85
93
}
0 commit comments