Skip to content

Commit 4124e45

Browse files
committed
squash bingen work
1 parent 1f248f9 commit 4124e45

File tree

7 files changed

+11526
-1
lines changed

7 files changed

+11526
-1
lines changed

Cargo.toml

+4-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ include = [
3434
"/submodules/openssl/VMS",
3535
"/submodules/xdp-for-windows/published/external",
3636
"/scripts/build.rs",
37-
"/src/*.rs",
37+
"/src/**/*.rs",
3838
"/src/bin",
3939
"/src/core",
4040
"/src/inc",
@@ -49,9 +49,12 @@ default = []
4949
schannel = []
5050
static = []
5151
preview-api = []
52+
# Overwrite generated binding by reruning the bindgen
53+
overwrite = [ "dep:bindgen" ]
5254

5355
[build-dependencies]
5456
cmake = "0.1"
57+
bindgen = { version = "0.71", optional = true }
5558

5659
[dependencies]
5760
bitfield = "0.17.0"

scripts/build.rs

+42
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,46 @@ fn main() {
6363
}
6464
println!("cargo:rustc-link-lib=static=msquic");
6565
}
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!");
66108
}

src/ffi/linux_bindings.rs

+5,695
Large diffs are not rendered by default.

src/ffi/mod.rs

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#![allow(
2+
non_snake_case,
3+
non_upper_case_globals,
4+
non_camel_case_types,
5+
dead_code,
6+
clippy::all
7+
)]
8+
9+
pub type QUIC_ADDR = std::ffi::c_void;
10+
11+
// TODO: macos currently is using the linux bindings.
12+
#[cfg(not(target_os = "windows"))]
13+
pub type sa_family_t = u16;
14+
#[cfg(not(target_os = "windows"))]
15+
include!("linux_bindings.rs");
16+
17+
#[cfg(target_os = "windows")]
18+
pub type ADDRESS_FAMILY = u16;
19+
#[cfg(target_os = "windows")]
20+
include!("win_bindings.rs");

0 commit comments

Comments
 (0)