Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add rb-sys-env to ease integration pains #99

Merged
merged 8 commits into from
Nov 12, 2022
Merged

Add rb-sys-env to ease integration pains #99

merged 8 commits into from
Nov 12, 2022

Conversation

ianks
Copy link
Collaborator

@ianks ianks commented Nov 11, 2022

Why

So after working to make rb-sys seamless with magnus, there are a number of integration pain points I've stumbled across:

  1. Cargo link directives do not propagate transitively

This means that means the link-args set by rb-sys do not automatically propagated to magnus. This can cause unexpected breakage (especially on windows)

More info: (rust-lang/cargo#9554)

  1. There is some baseline information that's needed at compile time

This like the ruby_gte_3_0, and ruby_use_flonum are examples of this. Exposing this information in an easy way would alleviate a lot of headaches.

How

This PR adds a lightweight, dependency-free rb-sys-env crate to glue high level bindings crates together properly. IMPORTANT: This crate is only needed if you are wrapping rb-sys to make a high-level bindings crate (i.e. this is not needed if you are a gem author)!

At it's core, all it does is properly set cargo configuration based on the DEP_RB_* environment variables which are already set by rb-sys. This means this crate does not have to depend on rb-sys directly, and as such builds quickly.

Usage

In your high-level Ruby library that wraps rb-sys

  1. Add this to your Cargo.toml:

    [build-dependencies]
    rb-sys-env = "0.1"
  2. Then, just add this your build.rs:

    pub fn main() -> Result<(), Box<dyn std::error::Error>> {
      let _rb_env = rb_sys_env::activate()?;
    
      Ok(())
    }

What it configures

Anytime you want to see what is being set by rb-sys-env, you can run RB_SYS_ENV_DEBUG=1 cargo test...

👉 Click here to see an example output
  cargo:rustc-cfg=ruby_use_unaligned_member_access
  cargo:rustc-cfg=ruby_have_rb_ext_ractor_safe
  cargo:rustc-cfg=ruby_have_ruby_missing_h
  cargo:rustc-cfg=ruby_have_ruby_re_h
  cargo:rustc-cfg=ruby_have_ruby_debug_h
  cargo:rustc-cfg=ruby_have_ruby_oniguruma_h
  cargo:rustc-cfg=ruby_have_rb_io_t
  cargo:rustc-cfg=ruby_use_rincgc
  cargo:rustc-cfg=ruby_have_rb_data_type_t_function
  cargo:rustc-cfg=ruby_have_rb_data_type_t_parent
  cargo:rustc-cfg=ruby_have_ruby_intern_h
  cargo:rustc-cfg=ruby_have_ruby_defines_h
  cargo:rustc-cfg=ruby_have_ruby_thread_native_h
  cargo:rustc-cfg=ruby_have_rb_scan_args_optional_hash
  cargo:rustc-cfg=ruby_have_ruby_ruby_h
  cargo:rustc-cfg=ruby_have_ruby_io_h
  cargo:rustc-cfg=ruby_have_ruby_fiber_scheduler_h
  cargo:rustc-cfg=ruby_have_ruby_version_h
  cargo:rustc-cfg=ruby_have_rb_define_alloc_func
  cargo:rustc-cfg=ruby_use_transient_heap
  cargo:rustc-cfg=ruby_have_ruby_encoding_h
  cargo:rustc-cfg=ruby_have_ruby_random_h
  cargo:rustc-cfg=ruby_have_ruby_vm_h
  cargo:rustc-cfg=ruby_have_ruby_st_h
  cargo:rustc-cfg=ruby_have_ruby_ractor_h
  cargo:rustc-cfg=ruby_have_ruby_thread_h
  cargo:rustc-cfg=ruby_have_ruby_onigmo_h
  cargo:rustc-cfg=ruby_use_symbol_as_method_name
  cargo:rustc-cfg=ruby_have_rb_fd_init
  cargo:rustc-cfg=ruby_have_ruby_util_h
  cargo:rustc-cfg=ruby_have_ruby_regex_h
  cargo:rustc-cfg=ruby_have_rb_reg_new_str
  cargo:rustc-cfg=ruby_use_mjit
  cargo:rustc-cfg=ruby_use_flonum
  cargo:rustc-cfg=ruby_use_rgengc
  cargo:rustc-cfg=ruby_have_ruby_atomic_h
  cargo:rustc-cfg=ruby_have_ruby_memory_view_h
  cargo:rustc-cfg=ruby_3
  cargo:rustc-cfg=ruby_3_1
  cargo:rustc-cfg=ruby_3_1_2
  cargo:rustc-cfg=ruby_gte_2_2
  cargo:rustc-cfg=ruby_gt_2_2
  cargo:rustc-cfg=ruby_gte_2_3
  cargo:rustc-cfg=ruby_gt_2_3
  cargo:rustc-cfg=ruby_gte_2_4
  cargo:rustc-cfg=ruby_gt_2_4
  cargo:rustc-cfg=ruby_gte_2_5
  cargo:rustc-cfg=ruby_gt_2_5
  cargo:rustc-cfg=ruby_gte_2_6
  cargo:rustc-cfg=ruby_gt_2_6
  cargo:rustc-cfg=ruby_gte_2_7
  cargo:rustc-cfg=ruby_gt_2_7
  cargo:rustc-cfg=ruby_gte_3_0
  cargo:rustc-cfg=ruby_gt_3_0
  cargo:rustc-cfg=ruby_lte_3_1
  cargo:rustc-cfg=ruby_3_1
  cargo:rustc-cfg=ruby_eq_3_1
  cargo:rustc-cfg=ruby_gte_3_1
  cargo:rustc-cfg=ruby_lt_3_2
  cargo:rustc-cfg=ruby_lte_3_2
  cargo:rustc-cfg=ruby_lt_3_3
  cargo:rustc-cfg=ruby_lte_3_3
  cargo:rustc-cfg=ruby_gte_1
  cargo:rustc-cfg=ruby_gt_1
  cargo:rustc-cfg=ruby_gte_2
  cargo:rustc-cfg=ruby_gt_2
  cargo:rustc-cfg=ruby_lte_3
  cargo:rustc-cfg=ruby_3
  cargo:rustc-cfg=ruby_eq_3
  cargo:rustc-cfg=ruby_gte_3
  cargo:rustc-cfg=ruby_lt_4
  cargo:rustc-cfg=ruby_lte_4
  cargo:rustc-link-search=native=/Users/ianks/.rubies/3.1.2/lib
  cargo:rustc-link-lib=ruby.3.1
  cargo:rustc-link-arg=-Wl,-rpath,ruby.3.1
  cargo:rustc-link-arg=-Wl,-multiply_defined,suppress

@ianks ianks requested a review from matsadler November 11, 2022 20:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant