Skip to content

Commit

Permalink
Rework how bindings are generated
Browse files Browse the repository at this point in the history
It has been a few years since I have touched the xed-sys crate and
advancements to the rust ecosystem have made some of the hacks we used
to need obsolete. This commit reworks how and when bindings are
generated.

The new approach has two main advantages:
- With no extra features enabled the build script doesn't even have to
  run bindgen at all. It can just use the bundled bindings directly.
- We no longer have to maintain a separate c2rust translation of a
  subset of the functions exposed by XED. A new experimental feature in
  bindgen can now do this (mostly) automatically.

I have also thrown in the enc2 feature that is in the process of being
introduced by rust-xed#65. We do end up needing bindgen when the enc2 feature is
enabled as the generated bindings would be too large to submit to
crates.io. (They are >40MB!).

With that said, here's how things work now:
1. Bindings for the default featureset are generated in advance by
   running `./generate.sh`. XED's headers don't contain any
   platform-dependent code so this should remain portable to any
   platforms where XED is supported.
2. When the bindgen feature is enabled we ignore the bindings from step
   1 and instead generate them in build.rs.

With the new experimental --wrap-static-fns bindgen option we can drop
the janky c2rust step that translates versions of these functions to
rust so that they can be included. Instead bindgen generates an
additional C file that declares a set of new functions that forward to
the static functions, the generated bindings refer to these forwarding
functions instead of the original ones.
  • Loading branch information
Phantomical committed Jan 16, 2024
1 parent 46ca346 commit 3803776
Show file tree
Hide file tree
Showing 15 changed files with 25,963 additions and 2,847 deletions.
25 changes: 17 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,29 @@ edition = "2018"
repository = "https://github.com/rust-xed/xed-sys"
readme = "README.md"
keywords = ["xed", "intel", "x86", "x86_64"]
categories = ["encoding", "external-ffi-bindings", "hardware-support", "parsing"]
categories = ["encoding", "external-ffi-bindings", "hardware-support", "parsing", "no_std"]

[badges]
appveyor = { repository = "rust-xed/xed-sys" }
travis-ci = { repository = "rust-xed/xed-sys" }

[features]
# Enable the enc2 module of XED.
#
# Note that this will somewhat dramatically slow down compilation of xed-sys.
# The enc2 module involves a large amount of generated code and the resulting
# bindgen definitions contain 40+MB of rust code.
enc2 = ["bindgen"]

# Generate bindings with bindgen at build-time instead of using the
# pregenerated bindings.
#
# This will be slower but is required for certain feature combinations.
bindgen = ["dep:bindgen"]

[dependencies]

[build-dependencies]
bindgen = "0.69"
bindgen = { version = "0.69", optional = true, features = ["experimental"] }
cc = "1.0"
target-lexicon = "0.12"

[workspace]
members = [
".",
"build-tools"
]
1 change: 0 additions & 1 deletion build-tools/.gitignore

This file was deleted.

15 changes: 0 additions & 15 deletions build-tools/Cargo.toml

This file was deleted.

43 changes: 0 additions & 43 deletions build-tools/build-c2rust-bindings

This file was deleted.

97 changes: 0 additions & 97 deletions build-tools/src/bin/build-xed.rs

This file was deleted.

42 changes: 0 additions & 42 deletions build-tools/src/bin/remove-methods.rs

This file was deleted.

2 changes: 0 additions & 2 deletions build-tools/xed.c

This file was deleted.

Loading

0 comments on commit 3803776

Please sign in to comment.