Skip to content

Commit 4686f42

Browse files
committed
feat: Remove bindgen dependency for MTU crate
Fixes quinn-rs/quinn#2465 (comment)
1 parent 4288d22 commit 4686f42

14 files changed

Lines changed: 126 additions & 114 deletions

File tree

.github/actions/check-vm/action.yml

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ inputs:
1111
codecov-token:
1212
description: "Codecov token, if Codecov upload is desired."
1313
default: ""
14+
generate-bindings:
15+
description: "Generate and output bindgen bindings for BSD platforms."
16+
default: "false"
1417

1518
runs:
1619
using: composite
@@ -21,6 +24,7 @@ runs:
2124
WD: ${{ inputs.working-directory }}
2225
PLATFORM: ${{ inputs.platform }}
2326
WORKSPACE: ${{ inputs.working-directory == '.' && '--workspace' || '' }}
27+
GENERATE_BINDINGS: ${{ inputs.generate-bindings }}
2428
run: |
2529
cat <<EOF > prepare.sh
2630
# This executes as root
@@ -91,6 +95,18 @@ runs:
9195
;;
9296
esac
9397
cargo test --locked --no-fail-fast --release
98+
99+
# Generate bindings if requested
100+
if [ "$GENERATE_BINDINGS" = "true" ]; then
101+
cargo install bindgen-cli --locked
102+
echo "::group::Generated bindings for $PLATFORM"
103+
bindgen --allowlist-type 'rt_msghdr|rt_metrics|if_data' \
104+
--allowlist-item 'RTAX_MAX|RTM_GET|RTM_VERSION|RTA_DST|RTA_IFP' \
105+
--generate-cstr --explicit-padding --with-derive-default \
106+
src/bindings/bsd.h
107+
echo "::endgroup::"
108+
fi
109+
94110
rm -rf target # Do not sync this back to host
95111
EOF
96112
{
@@ -100,7 +116,7 @@ runs:
100116
} >> "$GITHUB_OUTPUT"
101117
102118
curl -o "$WD/rustup.sh" --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs
103-
echo "envs=CARGO_TERM_COLOR RUST_BACKTRACE RUST_LOG RUST_TEST_TIME_UNIT RUST_TEST_TIME_INTEGRATION RUST_TEST_TIME_DOCTEST WD" >> "$GITHUB_OUTPUT"
119+
echo "envs=CARGO_TERM_COLOR RUST_BACKTRACE RUST_LOG RUST_TEST_TIME_UNIT RUST_TEST_TIME_INTEGRATION RUST_TEST_TIME_DOCTEST WD GENERATE_BINDINGS" >> "$GITHUB_OUTPUT"
104120
105121
- if: ${{ inputs.platform == 'freebsd' }}
106122
uses: vmactions/freebsd-vm@4807432c7cab1c3f97688665332c0b932062d31f # v1.4.3

.github/workflows/check-mtu.yml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,38 @@ jobs:
2626
- name: Run network namespace tests
2727
run: sudo -E env "PATH=$PATH" cargo test --package mtu --test netns -- --nocapture
2828

29+
generate-bindings:
30+
name: Generate ${{ matrix.os }} bindings
31+
strategy:
32+
fail-fast: false
33+
matrix:
34+
include:
35+
- os: linux
36+
runner: ubuntu-24.04
37+
header: linux.h
38+
args: --allowlist-type 'rtattr|rtmsg|ifinfomsg|nlmsghdr'
39+
- os: macos
40+
runner: macos-15
41+
header: bsd.h
42+
args: --allowlist-type 'rt_msghdr|rt_metrics|if_data' --allowlist-item 'RTAX_MAX|RTM_GET|RTM_VERSION|RTA_DST|RTA_IFP'
43+
runs-on: ${{ matrix.runner }}
44+
steps:
45+
- uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1
46+
with:
47+
persist-credentials: false
48+
- name: Install bindgen-cli
49+
run: cargo install bindgen-cli --locked
50+
- name: Generate bindings
51+
env:
52+
OS: ${{ matrix.os }}
53+
ARGS: ${{ matrix.args }}
54+
HEADER: ${{ matrix.header }}
55+
run: |
56+
echo "::group::Generated bindings for $OS"
57+
bindgen $ARGS --generate-cstr --explicit-padding --with-derive-default "mtu/src/bindings/$HEADER"
58+
echo "::endgroup::"
59+
60+
2961
check-android:
3062
name: Check Android
3163
runs-on: ubuntu-24.04
@@ -65,4 +97,5 @@ jobs:
6597
working-directory: mtu
6698
platform: ${{ matrix.os }}
6799
codecov-token: ${{ secrets.CODECOV_TOKEN }} # zizmor: ignore[secrets-outside-env]
100+
generate-bindings: true
68101

mtu/Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,9 @@ windows = { workspace = true, features = [
3333
[build-dependencies]
3434
cfg_aliases = { version = "0.2", default-features = false }
3535
mozbuild = { version = "0.1", default-features = false, optional = true }
36-
bindgen = { version = "0.72", default-features = false, features = ["runtime"] }
3736

3837
[package.metadata.cargo-machete]
39-
ignored = ["bindgen", "cfg_aliases"]
38+
ignored = ["cfg_aliases"]
4039

4140
[features]
4241
gecko = ["dep:mozbuild"]

mtu/build.rs

Lines changed: 0 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -4,92 +4,7 @@
44
// option. This file may not be copied, modified, or distributed
55
// except according to those terms.
66

7-
#![expect(clippy::unwrap_used, reason = "OK in build scripts.")]
8-
9-
use std::env;
10-
11-
const BINDINGS: &str = "bindings.rs";
12-
13-
#[cfg(feature = "gecko")]
14-
fn clang_args() -> Vec<String> {
15-
use mozbuild::{TOPOBJDIR, config::BINDGEN_SYSTEM_FLAGS};
16-
17-
let mut flags: Vec<String> = BINDGEN_SYSTEM_FLAGS.iter().map(|s| s.to_string()).collect();
18-
19-
flags.push(String::from("-include"));
20-
flags.push(
21-
TOPOBJDIR
22-
.join("dist")
23-
.join("include")
24-
.join("mozilla-config.h")
25-
.to_str()
26-
.unwrap()
27-
.to_string(),
28-
);
29-
flags
30-
}
31-
32-
#[cfg(not(feature = "gecko"))]
33-
const fn clang_args() -> Vec<String> {
34-
Vec::new()
35-
}
36-
37-
fn bindgen() {
38-
let target_os = env::var("CARGO_CFG_TARGET_OS").expect("CARGO_CFG_TARGET_OS was not set");
39-
40-
// Platforms currently not supported.
41-
//
42-
// See <https://github.com/mozilla/mtu/issues/82>.
43-
if matches!(target_os.as_str(), "ios" | "tvos" | "visionos") {
44-
return;
45-
}
46-
47-
if target_os == "windows" {
48-
return;
49-
}
50-
51-
let bindings = if matches!(target_os.as_str(), "linux" | "android") {
52-
bindgen::Builder::default()
53-
.header_contents("rtnetlink.h", "#include <linux/rtnetlink.h>")
54-
// Only generate bindings for the following types
55-
.allowlist_type("rtattr|rtmsg|ifinfomsg|nlmsghdr")
56-
} else {
57-
bindgen::Builder::default()
58-
.header_contents(
59-
"route.h",
60-
"#include <sys/types.h>\n#include <sys/socket.h>\n#include <net/route.h>\n#include <net/if.h>",
61-
)
62-
// Only generate bindings for the following types and items
63-
.allowlist_type("rt_msghdr|rt_metrics|if_data")
64-
.allowlist_item("RTAX_MAX|RTM_GET|RTM_VERSION|RTA_DST|RTA_IFP")
65-
};
66-
67-
let bindings = bindings
68-
.clang_args(clang_args())
69-
// Tell cargo to invalidate the built crate whenever any of the
70-
// included header files changed.
71-
.parse_callbacks(Box::new(bindgen::CargoCallbacks::new()))
72-
// Constants should be generated as &CStr instead of &[u8].
73-
.generate_cstr(true)
74-
// Always emit explicit padding fields.
75-
.explicit_padding(true)
76-
// Default trait should be derived when possible
77-
.derive_default(true)
78-
// Finish the builder and generate the bindings.
79-
.generate()
80-
// Unwrap the Result and panic on failure.
81-
.expect("Unable to generate bindings");
82-
83-
// Write the bindings to the $OUT_DIR/$BINDINGS file.
84-
let out_path = std::path::PathBuf::from(env::var("OUT_DIR").unwrap()).join(BINDINGS);
85-
bindings
86-
.write_to_file(out_path.clone())
87-
.expect("Couldn't write bindings!");
88-
println!("cargo:rustc-env=BINDINGS={}", out_path.display());
89-
}
90-
917
fn main() {
92-
// Setup cfg aliases
938
cfg_aliases::cfg_aliases! {
949
bsd: {
9510
any(
@@ -100,6 +15,4 @@ fn main() {
10015
)
10116
}
10217
}
103-
104-
bindgen();
10518
}

mtu/src/bindings/bsd.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#include <sys/types.h>
2+
#include <sys/socket.h>
3+
#include <net/route.h>
4+
#include <net/if.h>

mtu/src/bindings/freebsd.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
2+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
3+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
4+
// option. This file may not be copied, modified, or distributed
5+
// except according to those terms.
6+
7+
// Generated by bindgen. Do not edit.
8+
// Regenerate and verify with CI workflow.
9+
10+
// TODO: Replace with actual bindings from CI output.

mtu/src/bindings/linux.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#include <linux/rtnetlink.h>

mtu/src/bindings/linux.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
2+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
3+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
4+
// option. This file may not be copied, modified, or distributed
5+
// except according to those terms.
6+
7+
// Generated by bindgen. Do not edit.
8+
// Regenerate and verify with CI workflow.
9+
10+
// TODO: Replace with actual bindings from CI output.

mtu/src/bindings/macos.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
2+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
3+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
4+
// option. This file may not be copied, modified, or distributed
5+
// except according to those terms.
6+
7+
// Generated by bindgen. Do not edit.
8+
// Regenerate and verify with CI workflow.
9+
10+
// TODO: Replace with actual bindings from CI output.

mtu/src/bindings/netbsd.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
2+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
3+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
4+
// option. This file may not be copied, modified, or distributed
5+
// except according to those terms.
6+
7+
// Generated by bindgen. Do not edit.
8+
// Regenerate and verify with CI workflow.
9+
10+
// TODO: Replace with actual bindings from CI output.

0 commit comments

Comments
 (0)