Skip to content

Commit

Permalink
squash bingen work
Browse files Browse the repository at this point in the history
  • Loading branch information
youyuanwu committed Jan 7, 2025
1 parent 1f248f9 commit 4124e45
Show file tree
Hide file tree
Showing 7 changed files with 11,526 additions and 1 deletion.
5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ include = [
"/submodules/openssl/VMS",
"/submodules/xdp-for-windows/published/external",
"/scripts/build.rs",
"/src/*.rs",
"/src/**/*.rs",
"/src/bin",
"/src/core",
"/src/inc",
Expand All @@ -49,9 +49,12 @@ default = []
schannel = []
static = []
preview-api = []
# Overwrite generated binding by reruning the bindgen
overwrite = [ "dep:bindgen" ]

[build-dependencies]
cmake = "0.1"
bindgen = { version = "0.71", optional = true }

[dependencies]
bitfield = "0.17.0"
Expand Down
42 changes: 42 additions & 0 deletions scripts/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,46 @@ fn main() {
}
println!("cargo:rustc-link-lib=static=msquic");
}

#[cfg(feature = "overwrite")]
overwrite_bindgen();
}

/// Read the c header and generate rust bindings.
#[cfg(feature = "overwrite")]
fn overwrite_bindgen() {
let manifest_dir = std::path::PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
let root_dir = manifest_dir;
// include msquic headers
let inc_dir = root_dir.join("src").join("inc");

// The bindgen::Builder is the main entry point
// to bindgen, and lets you build up options for
// the resulting bindings.
let bindings = bindgen::Builder::default()
// The input header we would like to generate
// bindings for.
.header(root_dir.join("src/ffi/wrapper.hpp").to_str().unwrap())
.clang_arg(format!("-I{}", inc_dir.to_string_lossy()))
.allowlist_recursively(false)
.allowlist_item("QUIC.*|BOOLEAN|BYTE|HQUIC|HRESULT")
.blocklist_type("QUIC_ADDR")
// Tell cargo to invalidate the built crate whenever any of the
// included header files changed.
.parse_callbacks(Box::new(bindgen::CargoCallbacks::new()))
// Finish the builder and generate the bindings.
.generate()
// Unwrap the Result and panic on failure.
.expect("Unable to generate bindings");

// Write bindings to the sys mod.
let out_path = root_dir.join("src/ffi");
#[cfg(target_os = "windows")]
let binding_file = "win_bindings.rs";
#[cfg(target_os = "linux")]
let binding_file = "linux_bindings.rs";
// TODO: support macos.
bindings
.write_to_file(out_path.join(binding_file))
.expect("Couldn't write bindings!");
}
5,695 changes: 5,695 additions & 0 deletions src/ffi/linux_bindings.rs

Large diffs are not rendered by default.

20 changes: 20 additions & 0 deletions src/ffi/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#![allow(
non_snake_case,
non_upper_case_globals,
non_camel_case_types,
dead_code,
clippy::all
)]

pub type QUIC_ADDR = std::ffi::c_void;

// TODO: macos currently is using the linux bindings.
#[cfg(not(target_os = "windows"))]
pub type sa_family_t = u16;
#[cfg(not(target_os = "windows"))]
include!("linux_bindings.rs");

#[cfg(target_os = "windows")]
pub type ADDRESS_FAMILY = u16;
#[cfg(target_os = "windows")]
include!("win_bindings.rs");
Loading

0 comments on commit 4124e45

Please sign in to comment.