Skip to content

Commit

Permalink
Add truffleruby support (#441)
Browse files Browse the repository at this point in the history
* Add truffleruby support

* Remove panicking version of `RbConfig::get`

* Push commit to trigger CI

* More permissive version slug

* Fix various compilation issues

* Fix delta track for subtraction in tracking allocator

* Enable lazy linking for truffle on linux

* Enable lazy linking for truffle on linux

* Attempt to parse ruby version from string when MAJOR not avail

* Try harder to find ruby version

* Disable relro so lazy binding will work on linux

* Disable ruby-head for now

* Update readme
  • Loading branch information
ianks authored Nov 19, 2024
1 parent ebb9d08 commit 31c6e4e
Show file tree
Hide file tree
Showing 20 changed files with 417 additions and 182 deletions.
9 changes: 9 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@ jobs:
rust_toolchain: stable
- os: windows-2022
rust_toolchain: stable
include:
- ruby_version: "truffleruby"
sys:
os: ubuntu-latest
rust_toolchain: stable
- ruby_version: "truffleruby"
sys:
os: macos-latest
rust_toolchain: stable
exclude:
# Missing symbols for some reason, need to fix
- ruby_version: "2.6"
Expand Down
5 changes: 5 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ def cargo_test_task(name, *args, crate: name)

desc "Run cargo tests for #{name.inspect} against current Ruby"
task task_name do
if RUBY_ENGINE != "ruby"
puts "Skipping #{task_name} because it only works with MRI Ruby"
next
end

default_args = ENV["CI"] || extra_args.include?("--verbose") ? [] : ["--quiet"]
test_args = ENV["CI"] || extra_args.include?("--verbose") ? ["--", "--nocapture"] : []
sh "cargo", "test", *default_args, *extra_args, *args, "-p", crate, *test_args
Expand Down
28 changes: 15 additions & 13 deletions crates/rb-sys-build/src/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::cc::Build;
use crate::utils::is_msvc;
use crate::{debug_log, RbConfig};
use quote::ToTokens;
use stable_api::{categorize_bindings, opaqueify_bindings};
use std::fs::File;
use std::io::Write;
use std::path::{Path, PathBuf};
Expand All @@ -21,10 +22,13 @@ pub fn generate(
) -> Result<PathBuf, Box<dyn Error>> {
let out_dir = PathBuf::from(env::var("OUT_DIR")?);

let mut clang_args = vec![
format!("-I{}", rbconfig.get("rubyhdrdir")),
format!("-I{}", rbconfig.get("rubyarchhdrdir")),
];
let mut clang_args = vec![];
if let Some(ruby_include_dir) = rbconfig.get("rubyhdrdir") {
clang_args.push(format!("-I{}", ruby_include_dir));
}
if let Some(ruby_arch_include_dir) = rbconfig.get("rubyarchhdrdir") {
clang_args.push(format!("-I{}", ruby_arch_include_dir));
}

clang_args.extend(Build::default_cflags());
clang_args.extend(rbconfig.cflags.clone());
Expand All @@ -47,8 +51,10 @@ pub fn generate(
let bindings = default_bindgen(clang_args)
.allowlist_file(".*ruby.*")
.blocklist_item("ruby_abi_version")
.blocklist_function("rb_tr_abi_version")
.blocklist_function("^__.*")
.blocklist_item("RData")
.blocklist_function("rb_tr_rdata")
.parse_callbacks(Box::new(bindgen::CargoCallbacks::new()));

let bindings = if cfg!(feature = "bindgen-rbimpls") {
Expand All @@ -67,7 +73,7 @@ pub fn generate(
.blocklist_item("^_bindgen_ty_9.*")
};

let bindings = stable_api::opaqueify_bindings(rbconfig, bindings, &mut wrapper_h);
let bindings = opaqueify_bindings(rbconfig, bindings, &mut wrapper_h);

let mut tokens = {
write!(std::io::stderr(), "{}", wrapper_h)?;
Expand All @@ -76,13 +82,9 @@ pub fn generate(
syn::parse_file(&code_string)?
};

let ruby_version = rbconfig.ruby_program_version();
let ruby_platform = rbconfig.platform();
let slug = rbconfig.ruby_version_slug();
let crate_version = env!("CARGO_PKG_VERSION");
let out_path = out_dir.join(format!(
"bindings-{}-{}-{}.rs",
crate_version, ruby_platform, ruby_version
));
let out_path = out_dir.join(format!("bindings-{}-{}.rs", crate_version, slug));

let code = {
sanitizer::ensure_backwards_compatible_encoding_pointers(&mut tokens);
Expand All @@ -93,7 +95,7 @@ pub fn generate(
}

push_cargo_cfg_from_bindings(&tokens, cfg_out)?;
stable_api::categorize_bindings(&mut tokens);
categorize_bindings(&mut tokens);
tokens.into_token_stream().to_string()
};

Expand Down Expand Up @@ -121,7 +123,7 @@ fn clean_docs(rbconfig: &RbConfig, syntax: &mut syn::File) {
return;
}

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

sanitizer::cleanup_docs(syntax, &ver).unwrap_or_else(|e| {
debug_log!("WARN: failed to clean up docs, skipping: {}", e);
Expand Down
5 changes: 4 additions & 1 deletion crates/rb-sys-build/src/bindings/stable_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,10 @@ fn gen_opaque_struct(
.allowlist_type(struct_name)
}

fn get_version_specific_opaque_structs(major_minor: (u32, u32)) -> Vec<&'static str> {
fn get_version_specific_opaque_structs(major_minor: Option<(u32, u32)>) -> Vec<&'static str> {
let Some(major_minor) = major_minor else {
return vec![];
};
let mut result = vec![];
let (major, minor) = major_minor;

Expand Down
23 changes: 16 additions & 7 deletions crates/rb-sys-build/src/cc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,12 +170,21 @@ impl Build {
}

fn get_include_args(rb: &rb_config::RbConfig) -> Vec<String> {
vec![
format!("-I{}", rb.get("rubyhdrdir")),
format!("-I{}", rb.get("rubyarchhdrdir")),
format!("-I{}/include/internal", rb.get("rubyhdrdir")),
format!("-I{}/include/impl", rb.get("rubyhdrdir")),
]
let mut args = vec![];
if let Some(include_dir) = rb.get("rubyhdrdir") {
args.push(format!("-I{}", include_dir));
}
if let Some(arch_include_dir) = rb.get("rubyarchhdrdir") {
args.push(format!("-I{}", arch_include_dir));
}
if let Some(internal_include_dir) = rb.get("rubyhdrdir") {
args.push(format!("-I{}/include/internal", internal_include_dir));
}
if let Some(impl_include_dir) = rb.get("rubyhdrdir") {
args.push(format!("-I{}/include/impl", impl_include_dir));
}

args
}

fn get_common_args() -> Vec<String> {
Expand Down Expand Up @@ -292,7 +301,7 @@ fn get_tool_from_rb_config_or_env(env_var: &str) -> Option<String> {

get_tool_from_env(env_var)
.filter(|s| !s.is_empty())
.or_else(|| rb.get_optional(env_var))
.or_else(|| rb.get(env_var))
}

fn get_tool_from_env(env_var: &str) -> Option<String> {
Expand Down
Loading

0 comments on commit 31c6e4e

Please sign in to comment.