Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto generate rust bindings using rust bindgen #4723

Merged
merged 2 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .github/workflows/cargo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macos-latest, macos-latest-xlarge]
features: ["", "--features static", "--features schannel", "--features schannel,static"]
features: ["", "--features static", "--features schannel", "--features schannel,static", "--features overwrite"]
exclude:
- os: ubuntu-latest
features: "--features schannel"
Expand Down Expand Up @@ -60,6 +60,8 @@ jobs:
run: cargo clippy --all-targets -- -D warnings
- name: Cargo build
run: cargo build --all ${{ matrix.features }}
- name: Check all generated files with git
run: git diff --exit-code
- name: Cargo test
run: cargo test --all ${{ matrix.features }}
- name: Cargo Publish (dry run)
Expand Down
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
43 changes: 43 additions & 0 deletions scripts/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,47 @@ fn main() {
}
println!("cargo:rustc-link-lib=static=msquic");
}

#[cfg(all(feature = "overwrite", not(target_os = "macos")))]
overwrite_bindgen();
}

/// Read the c header and generate rust bindings.
/// TODO: macos currently uses linux bindings.
#[cfg(all(feature = "overwrite", not(target_os = "macos")))]
fn overwrite_bindgen() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we update the GitHub action to ensure the bindings are up to date?

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!");
}
Loading
Loading