Skip to content

Commit ed02227

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

File tree

14 files changed

+125
-114
lines changed

14 files changed

+125
-114
lines changed

.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
@@ -93,6 +97,18 @@ runs:
9397
;;
9498
esac
9599
cargo test --locked --no-fail-fast --release
100+
101+
# Generate bindings if requested
102+
if [ "$GENERATE_BINDINGS" = "true" ]; then
103+
cargo install bindgen-cli --locked
104+
echo "::group::Generated bindings for $PLATFORM"
105+
bindgen --allowlist-type 'rt_msghdr|rt_metrics|if_data' \
106+
--allowlist-item 'RTAX_MAX|RTM_GET|RTM_VERSION|RTA_DST|RTA_IFP' \
107+
--generate-cstr --explicit-padding --with-derive-default \
108+
src/bindings/bsd.h
109+
echo "::endgroup::"
110+
fi
111+
96112
rm -rf target # Do not sync this back to host
97113
EOF
98114
{
@@ -102,7 +118,7 @@ runs:
102118
} >> "$GITHUB_OUTPUT"
103119
104120
curl -o "$WD/rustup.sh" --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs
105-
echo "envs=CARGO_TERM_COLOR RUST_BACKTRACE RUST_LOG RUST_TEST_TIME_UNIT RUST_TEST_TIME_INTEGRATION RUST_TEST_TIME_DOCTEST WD" >> "$GITHUB_OUTPUT"
121+
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"
106122
107123
- if: ${{ inputs.platform == 'freebsd' }}
108124
uses: vmactions/freebsd-vm@670398e4236735b8b65805c3da44b7a511fb8b27 # v1.3.0

.github/workflows/check-mtu.yml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,37 @@ permissions:
1212
contents: read
1313

1414
jobs:
15+
generate-bindings:
16+
name: Generate ${{ matrix.os }} bindings
17+
strategy:
18+
fail-fast: false
19+
matrix:
20+
include:
21+
- os: linux
22+
runner: ubuntu-24.04
23+
header: linux.h
24+
args: --allowlist-type 'rtattr|rtmsg|ifinfomsg|nlmsghdr'
25+
- os: macos
26+
runner: macos-15
27+
header: bsd.h
28+
args: --allowlist-type 'rt_msghdr|rt_metrics|if_data' --allowlist-item 'RTAX_MAX|RTM_GET|RTM_VERSION|RTA_DST|RTA_IFP'
29+
runs-on: ${{ matrix.runner }}
30+
steps:
31+
- uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1
32+
with:
33+
persist-credentials: false
34+
- name: Install bindgen-cli
35+
run: cargo install bindgen-cli --locked
36+
- name: Generate bindings
37+
env:
38+
OS: ${{ matrix.os }}
39+
ARGS: ${{ matrix.args }}
40+
HEADER: ${{ matrix.header }}
41+
run: |
42+
echo "::group::Generated bindings for $OS"
43+
bindgen $ARGS --generate-cstr --explicit-padding --with-derive-default "mtu/src/bindings/$HEADER"
44+
echo "::endgroup::"
45+
1546
check-android:
1647
name: Check Android
1748
runs-on: ubuntu-24.04
@@ -44,4 +75,5 @@ jobs:
4475
working-directory: mtu
4576
platform: ${{ matrix.os }}
4677
codecov-token: ${{ secrets.CODECOV_TOKEN }}
78+
generate-bindings: true
4779

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::{config::BINDGEN_SYSTEM_FLAGS, TOPOBJDIR};
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)