@@ -63,4 +63,46 @@ fn main() {
63
63
}
64
64
println ! ( "cargo:rustc-link-lib=static=msquic" ) ;
65
65
}
66
+
67
+ #[ cfg( feature = "overwrite" ) ]
68
+ overwrite_bindgen ( ) ;
69
+ }
70
+
71
+ /// Read the c header and generate rust bindings.
72
+ #[ cfg( feature = "overwrite" ) ]
73
+ fn overwrite_bindgen ( ) {
74
+ let manifest_dir = std:: path:: PathBuf :: from ( env:: var ( "CARGO_MANIFEST_DIR" ) . unwrap ( ) ) ;
75
+ let root_dir = manifest_dir;
76
+ // include msquic headers
77
+ let inc_dir = root_dir. join ( "src" ) . join ( "inc" ) ;
78
+
79
+ // The bindgen::Builder is the main entry point
80
+ // to bindgen, and lets you build up options for
81
+ // the resulting bindings.
82
+ let bindings = bindgen:: Builder :: default ( )
83
+ // The input header we would like to generate
84
+ // bindings for.
85
+ . header ( root_dir. join ( "src/ffi/wrapper.hpp" ) . to_str ( ) . unwrap ( ) )
86
+ . clang_arg ( format ! ( "-I{}" , inc_dir. to_string_lossy( ) ) )
87
+ . allowlist_recursively ( false )
88
+ . allowlist_item ( "QUIC.*|BOOLEAN|BYTE|HQUIC|HRESULT" )
89
+ . blocklist_type ( "QUIC_ADDR" )
90
+ // Tell cargo to invalidate the built crate whenever any of the
91
+ // included header files changed.
92
+ . parse_callbacks ( Box :: new ( bindgen:: CargoCallbacks :: new ( ) ) )
93
+ // Finish the builder and generate the bindings.
94
+ . generate ( )
95
+ // Unwrap the Result and panic on failure.
96
+ . expect ( "Unable to generate bindings" ) ;
97
+
98
+ // Write bindings to the sys mod.
99
+ let out_path = root_dir. join ( "src/ffi" ) ;
100
+ #[ cfg( target_os = "windows" ) ]
101
+ let binding_file = "win_bindings.rs" ;
102
+ #[ cfg( target_os = "linux" ) ]
103
+ let binding_file = "linux_bindings.rs" ;
104
+ // TODO: support macos.
105
+ bindings
106
+ . write_to_file ( out_path. join ( binding_file) )
107
+ . expect ( "Couldn't write bindings!" ) ;
66
108
}
0 commit comments