Skip to content

Commit

Permalink
Bump bindgen to 0.62 (#208)
Browse files Browse the repository at this point in the history
* Bump bindgen to 0.62

* Add magnus-head to integration test matrix

* make clippy happy
  • Loading branch information
ianks authored May 10, 2023
1 parent fb7d7b6 commit 45ac331
Show file tree
Hide file tree
Showing 16 changed files with 87 additions and 59 deletions.
8 changes: 6 additions & 2 deletions .github/workflows/integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,12 @@ jobs:
ref: main
run: rake
- name: "matsadler/magnus"
slug: magnus
ref: "0.5.0"
slug: magnus-0.5
ref: "0.5.3"
run: cargo test
- name: "matsadler/magnus"
slug: magnus-head
ref: "51d154ce32a3ef06542454fc720ad4f291dba86a"
run: cargo test
os: ["ubuntu-latest", "windows-latest", "macos-latest"]
rust: ["stable"]
Expand Down
9 changes: 5 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions crates/rb-sys-build/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ description = "Build system for rb-sys"
homepage = "https://github.com/oxidize-rb/rb-sys"
license = "MIT OR Apache-2.0"
repository = "https://github.com/oxidize-rb/rb-sys"
rust-version = "1.54"
rust-version = "1.57"

[lib]
bench = false
Expand All @@ -15,7 +15,7 @@ doctest = false
[dependencies]
regex = "1"
shell-words = "1.1"
bindgen = { version = "0.60", default-features = false, features = ["runtime"] }
bindgen = { version = "0.62", default-features = false, features = ["runtime"] }
cc-impl = { version = "1.0", optional = true, package = "cc" }
syn = { version = "1.0", features = ["parsing", "full", "extra-traits"] }
quote = "1.0"
Expand All @@ -29,3 +29,6 @@ bindgen-rbimpls = []
bindgen-deprecated-types = []
bindgen-layout-tests = []
bindgen-impl-debug = []
bindgen-sizet-is-usize = []
bindgen-return-const-encoding-pointers = []
bindgen-enable-function-attribute-detection = []
23 changes: 16 additions & 7 deletions crates/rb-sys-build/src/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::RbConfig;
use quote::ToTokens;
use std::fs::File;
use std::io::Write;
use std::path::PathBuf;
use std::path::{Path, PathBuf};
use std::{env, error::Error};
use syn::{Expr, ExprLit, ItemConst, Lit};

Expand Down Expand Up @@ -73,7 +73,7 @@ pub fn generate(
syn::parse_file(&code_string)?
};

let ruby_version = rbconfig.ruby_version();
let ruby_version = rbconfig.ruby_program_version();
let ruby_platform = rbconfig.platform();
let crate_version = env!("CARGO_PKG_VERSION");
let out_path = out_dir.join(format!(
Expand All @@ -82,11 +82,13 @@ pub fn generate(
));

let code = {
sanitizer::ensure_backwards_compatible_encoding_pointers(&mut tokens);
clean_docs(rbconfig, &mut tokens);

if is_msvc() {
qualify_symbols_for_msvc(&mut tokens, static_ruby, rbconfig);
}

push_cargo_cfg_from_bindings(&tokens, cfg_out).expect("write cfg");
tokens.into_token_stream().to_string()
};
Expand All @@ -98,7 +100,7 @@ pub fn generate(
Ok(out_path)
}

fn run_rustfmt(path: &PathBuf) {
fn run_rustfmt(path: &Path) {
let mut cmd = std::process::Command::new("rustfmt");
cmd.stderr(std::process::Stdio::inherit());
cmd.stdout(std::process::Stdio::inherit());
Expand All @@ -115,16 +117,15 @@ fn clean_docs(rbconfig: &RbConfig, syntax: &mut syn::File) {
return;
}

let ver = rbconfig.ruby_version();
let ver = rbconfig.ruby_program_version();

sanitizer::cleanup_docs(syntax, &ver).unwrap_or_else(|e| {
eprintln!("Failed to clean up docs, skipping: {}", e);
})
}

fn default_bindgen(clang_args: Vec<String>) -> bindgen::Builder {
bindgen::Builder::default()
.use_core()
let bindings = bindgen::Builder::default()
.rustfmt_bindings(false) // We use syn so this is pointless
.rustified_enum(".*")
.no_copy("rb_data_type_struct")
Expand All @@ -136,8 +137,16 @@ fn default_bindgen(clang_args: Vec<String>) -> bindgen::Builder {
.blocklist_item("^_opaque_pthread.*")
.blocklist_item("^pthread_.*")
.blocklist_item("^rb_native.*")
.merge_extern_blocks(true)
.size_t_is_usize(env::var("CARGO_FEATURE_BINDGEN_SIZE_T_IS_USIZE").is_ok())
.impl_debug(cfg!(feature = "bindgen-impl-debug"))
.parse_callbacks(Box::new(bindgen::CargoCallbacks))
.parse_callbacks(Box::new(bindgen::CargoCallbacks));

if env::var("CARGO_FEATURE_BINDGEN_ENABLE_FUNCTION_ATTRIBUTE_DETECTION").is_ok() {
bindings.enable_function_attribute_detection()
} else {
bindings
}
}

// This is needed because bindgen doesn't support the `__declspec(dllimport)` on
Expand Down
25 changes: 25 additions & 0 deletions crates/rb-sys-build/src/bindings/sanitizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,31 @@ pub fn add_link_ruby_directives(
Ok(())
}

/// Converts all `*const rb_encoding` and `*const OnigEncodingTypeST` to *mut
/// _` to keep backwards compatibility with bindgen < 0.62.
pub fn ensure_backwards_compatible_encoding_pointers(syntax: &mut syn::File) {
for item in syntax.items.iter_mut() {
if let Item::ForeignMod(fmod) = item {
for item in fmod.items.iter_mut() {
if let syn::ForeignItem::Fn(f) = item {
if let syn::ReturnType::Type(_, ty) = &mut f.sig.output {
if let syn::Type::Ptr(ptr) = &mut **ty {
if let syn::Type::Path(path) = &*ptr.elem {
if path.path.segments.len() == 1
&& path.path.segments[0].ident == "OnigEncodingTypeST"
|| path.path.segments[0].ident == "rb_encoding"
{
ptr.mutability = Some(syn::token::Mut::default());
}
}
}
}
}
}
}
}
}

/// Turn the cruby comments into rustdoc comments.
pub fn cleanup_docs(syntax: &mut syn::File, ruby_version: &str) -> Result<(), Box<dyn Error>> {
let footer = doc_footer(ruby_version);
Expand Down
40 changes: 13 additions & 27 deletions crates/rb-sys-build/src/rb_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use std::ffi::OsString;

use crate::{
memoize,
utils::{is_msvc, is_mswin_or_mingw, shellsplit},
utils::{is_msvc, shellsplit},
};

use self::flags::Flags;
Expand Down Expand Up @@ -167,9 +167,18 @@ impl RbConfig {
self
}

/// Returns the current ruby version.
pub fn ruby_version(&self) -> String {
self.get("ruby_version")
/// Returns the current ruby program version.
pub fn ruby_program_version(&self) -> String {
if let Some(progv) = self.get_optional("RUBY_PROGRAM_VERSION") {
progv
} else {
format!(
"{}.{}.{}",
self.get("MAJOR"),
self.get("MINOR"),
self.get("TEENY")
)
}
}

/// Get the CPPFLAGS from the RbConfig, making sure to subsitute variables.
Expand Down Expand Up @@ -247,8 +256,6 @@ impl RbConfig {
search_paths.push(search_path.name.as_str());
}

append_ld_library_path(search_paths);

for lib in &self.libs {
if !self.blocklist_lib.iter().any(|b| lib.name.contains(b)) {
result.push(format!("cargo:rustc-link-lib={}", lib));
Expand Down Expand Up @@ -415,27 +422,6 @@ fn capture_name(regex: &Regex, arg: &str) -> Option<String> {
.map(|cap| cap.name("name").unwrap().as_str().trim().to_owned())
}

// Needed because Rust 1.54 does not support link-arg, and thus rpath
// See <https://doc.rust-lang.org/cargo/reference/environment-variables.html#dynamic-library-paths
fn append_ld_library_path(search_paths: Vec<&str>) {
let env_var_name = if is_mswin_or_mingw() {
"PATH"
} else if cfg!(target_os = "macos") {
"DYLD_FALLBACK_LIBRARY_PATH"
} else {
"LD_LIBRARY_PATH"
};

let new_path = match std::env::var_os(env_var_name) {
Some(val) => {
format!("{}:{}", val.to_str().unwrap(), search_paths.join(":"))
}
None => search_paths.join(":"),
};

println!("cargo:rustc-env={}={}", env_var_name, new_path);
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
2 changes: 1 addition & 1 deletion crates/rb-sys-env/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ homepage = "https://github.com/oxidize-rb/rb-sys"
license = "MIT OR Apache-2.0"
repository = "https://github.com/oxidize-rb/rb-sys"
readme = "readme.md"
rust-version = "1.54"
rust-version = "1.57"

[lib]
bench = false
Expand Down
2 changes: 1 addition & 1 deletion crates/rb-sys-test-helpers-macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ homepage = "https://github.com/oxidize-rb/rb-sys"
license = "MIT OR Apache-2.0"
repository = "https://github.com/oxidize-rb/rb-sys"
readme = "readme.md"
rust-version = "1.54"
rust-version = "1.57"

[lib]
proc-macro = true
Expand Down
2 changes: 1 addition & 1 deletion crates/rb-sys-test-helpers/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ homepage = "https://github.com/oxidize-rb/rb-sys"
license = "MIT OR Apache-2.0"
repository = "https://github.com/oxidize-rb/rb-sys"
readme = "readme.md"
rust-version = "1.54"
rust-version = "1.57"

[lib]
bench = false
Expand Down
2 changes: 1 addition & 1 deletion crates/rb-sys-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ version = "0.9.76"
edition = "2018"
autotests = false
publish = false
rust-version = "1.54"
rust-version = "1.57"

[lib]
bench = false
Expand Down
5 changes: 4 additions & 1 deletion crates/rb-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ homepage = "https://github.com/oxidize-rb/rb-sys"
license = "MIT OR Apache-2.0"
links = "rb"
repository = "https://github.com/oxidize-rb/rb-sys"
rust-version = "1.54"
rust-version = "1.57"

[build-dependencies]
rb-sys-build = { version = "0.9.76", path = "../rb-sys-build" }
Expand All @@ -30,6 +30,9 @@ bindgen-rbimpls = ["rb-sys-build/bindgen-rbimpls"]
bindgen-deprecated-types = ["rb-sys-build/bindgen-deprecated-types"]
bindgen-layout-tests = ["rb-sys-build/bindgen-layout-tests"]
bindgen-impl-debug = ["rb-sys-build/bindgen-impl-debug"]
bindgen-sizet-is-usize = ["rb-sys-build/bindgen-sizet-is-usize"]
bindgen-return-const-encoding-pointers = ["rb-sys-build/bindgen-return-const-encoding-pointers"]
bindgen-enable-function-attribute-detection = ["rb-sys-build/bindgen-enable-function-attribute-detection"]

[lib]
doctest = false
2 changes: 1 addition & 1 deletion crates/rb-sys/build/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ fn main() {
let mut rbconfig = RbConfig::current();
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());

let ruby_version = rbconfig.ruby_version();
let ruby_version = rbconfig.ruby_program_version();
let ruby_platform = rbconfig.platform();
let crate_version = env!("CARGO_PKG_VERSION");
let cfg_capture_path = out_dir.join(format!(
Expand Down
2 changes: 1 addition & 1 deletion data/toolchains.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"policy": {
"minimum-supported-ruby-version": "2.4",
"minimum-supported-rust-version": "1.54"
"minimum-supported-rust-version": "1.57"
},
"toolchains": [
{
Expand Down
5 changes: 3 additions & 2 deletions examples/rust_reverse/ext/rust_reverse/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 3 additions & 7 deletions gem/lib/rb_sys/mkmf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,8 @@ def create_rust_makefile(target, &blk)
#{conditional_assign("RB_SYS_CARGO_PROFILE_DIR", "$(RB_SYS_CARGO_PROFILE)", indent: 1)}
#{endif_stmt}
# Set the build profile (dev, release, etc.) Compat with Rust 1.54.
#{if_eq_stmt("$(RB_SYS_CARGO_PROFILE)", "release")}
#{assign_stmt("RB_SYS_CARGO_PROFILE_FLAG", "--release", indent: 1)}
#{else_stmt}
# Set the build profile (dev, release, etc.).
#{assign_stmt("RB_SYS_CARGO_PROFILE_FLAG", "--profile $(RB_SYS_CARGO_PROFILE)", indent: 1)}
#{endif_stmt}
# Account for sub-directories when using `--target` argument with Cargo
#{conditional_assign("RB_SYS_CARGO_TARGET_DIR", "target")}
Expand Down Expand Up @@ -332,8 +328,8 @@ def assert_libclang_version_valid!
raise "libclang version 5.0.0 or greater is required (current #{libclang_version})"
end

if libclang_version >= Gem::Version.new("15.0.0")
raise "libclang version > 14.0.0 or greater is required (current #{libclang_version})"
if libclang_version >= Gem::Version.new("17.0.0")
raise "libclang version < 17.0.0 or greater is required (current #{libclang_version})"
end
end

Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ building your own gem.

- Ruby: <!--toolchains .policy.minimum-supported-ruby-version -->2.4<!--/toolchains-->+ (for full compatibility with
Rubygems)
- Rust: <!--toolchains .policy.minimum-supported-rust-version -->1.54<!--/toolchains-->+ (for old versions of rust
- Rust: <!--toolchains .policy.minimum-supported-rust-version -->1.57<!--/toolchains-->+ (for old versions of rust
toolchains ubuntu)

## Supported Platforms
Expand Down

0 comments on commit 45ac331

Please sign in to comment.