Skip to content

Commit

Permalink
Make the feature opt-in
Browse files Browse the repository at this point in the history
  • Loading branch information
ianks committed Jul 12, 2023
1 parent 8491f4b commit 497e5fe
Show file tree
Hide file tree
Showing 13 changed files with 78 additions and 29 deletions.
2 changes: 1 addition & 1 deletion bench/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ test = false
criterion = "0.5.1"
rb-sys-test-helpers = { path = "../crates/rb-sys-test-helpers" }
rb-sys = { path = "../crates/rb-sys", features = [
"stable-api-compiled",
"stable-api-compiled-testing",
"link-ruby",
"fuzz",
] }
Expand Down
5 changes: 4 additions & 1 deletion crates/rb-sys-test-helpers-macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,7 @@ quote = "1.0"

[dev-dependencies]
rb-sys-test-helpers = { path = "../rb-sys-test-helpers" }
rb-sys = { path = "../rb-sys", features = ["link-ruby", "stable-api-compiled"] }
rb-sys = { path = "../rb-sys", features = [
"link-ruby",
"stable-api-compiled-testing",
] }
5 changes: 4 additions & 1 deletion crates/rb-sys-test-helpers/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ bench = false
doctest = true

[dependencies]
rb-sys = { path = "../rb-sys", features = ["link-ruby", "stable-api-compiled"] }
rb-sys = { path = "../rb-sys", features = [
"link-ruby",
"stable-api-compiled-testing",
] }
rb-sys-test-helpers-macros = { version = "0.1.0", path = "../rb-sys-test-helpers-macros" }

[build-dependencies]
Expand Down
5 changes: 4 additions & 1 deletion crates/rb-sys-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ doctest = false
doc = false

[dependencies]
rb-sys = { path = "../rb-sys", features = ["link-ruby", "stable-api-compiled"] }
rb-sys = { path = "../rb-sys", features = [
"link-ruby",
"stable-api-compiled-testing",
] }
rb-sys-env = { path = "../rb-sys-env" }
rb-sys-test-helpers = { path = "../rb-sys-test-helpers" }
rusty-fork = "0.3.0"
Expand Down
2 changes: 1 addition & 1 deletion crates/rb-sys-tests/src/stable_api_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ macro_rules! parity_test {
#[allow(unused)]
let rust_result = unsafe { stable_api::get_default().$func(data) };
#[allow(unused_unsafe)]
let compiled_c_result = unsafe { stable_api::get_fallback().$func(data) };
let compiled_c_result = unsafe { stable_api::get_compiled().$func(data) };

assert_eq!(
compiled_c_result, rust_result,
Expand Down
10 changes: 8 additions & 2 deletions crates/rb-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ rb-sys-build = { version = "0.9.79", path = "../rb-sys-build" }

[dev-dependencies]
rb-sys-test-helpers = { path = "../rb-sys-test-helpers" }
rb-sys = { path = ".", features = ["link-ruby", "stable-api-compiled"] }
rb-sys = { path = ".", features = [
"link-ruby",
"stable-api-compiled-fallback",
] }
rusty-fork = "0.3.0"

[features]
Expand All @@ -28,7 +31,10 @@ fuzz = []
no-link-ruby = []
ruby-static = []
global-allocator = []
stable-api-compiled = []
stable-api-compiled-fallback = [] # Fallback to compiled C API
stable-api-compiled-testing = [
"stable-api-compiled-fallback",
] # For testing the fallback in rb-sys (internal)
ruby-macros = [] # deprecated
bindgen-rbimpls = ["rb-sys-build/bindgen-rbimpls"]
bindgen-deprecated-types = ["rb-sys-build/bindgen-deprecated-types"]
Expand Down
15 changes: 12 additions & 3 deletions crates/rb-sys/build/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,19 @@ pub fn is_global_allocator_enabled(rb_config: &RbConfig) -> bool {
}

pub fn is_compiled_stable_api_needed(ver: &Version) -> bool {
let needs_rust_impls = MIN_SUPPORTED_STABLE_VERSION > *ver || *ver > LATEST_STABLE_VERSION;
let is_feature_enabled = is_env_variable_defined("CARGO_FEATURE_STABLE_API_COMPILED");
let needs_c_fallback = MIN_SUPPORTED_STABLE_VERSION > *ver || *ver > LATEST_STABLE_VERSION;
let wants_c_fallback = explicitly_enabled_stable_api_compiled_fallback();

needs_rust_impls || is_feature_enabled
(needs_c_fallback && wants_c_fallback) || testing_stable_api_compiled_fallback()
}

pub fn explicitly_enabled_stable_api_compiled_fallback() -> bool {
cfg!(rb_sys_use_stable_api_compiled_fallback)
|| is_env_variable_defined("CARGO_FEATURE_STABLE_API_COMPILED_FALLBACK")
}

pub fn testing_stable_api_compiled_fallback() -> bool {
is_env_variable_defined("CARGO_FEATURE_STABLE_API_COMPILED_TESTING")
}

pub fn is_gem_enabled() -> bool {
Expand Down
7 changes: 6 additions & 1 deletion crates/rb-sys/build/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,13 @@ fn main() {
);
export_cargo_cfg(&mut rbconfig, &mut cfg_capture_file);

if explicitly_enabled_stable_api_compiled_fallback() {
println!("cargo:rustc-cfg=explicitly_enabled_stable_api_compiled_fallback");
}

if is_compiled_stable_api_needed(&current_ruby_version) {
c_glue::compile().expect("stable API C glue to compile");
println!("cargo:rustc-cfg=has_stable_api_compiled");
}

if is_link_ruby_enabled() {
Expand Down Expand Up @@ -143,7 +148,7 @@ fn export_cargo_cfg(rbconfig: &mut RbConfig, cap: &mut File) {
rustc_cfg(rbconfig, "ruby_api_version", "RUBY_API_VERSION");

if Version::current(rbconfig) <= LATEST_STABLE_VERSION {
println!("cargo:rustc-cfg=ruby_api_stable");
println!("cargo:rustc-cfg=current_ruby_is_stable");
}

if is_global_allocator_enabled(rbconfig) {
Expand Down
42 changes: 29 additions & 13 deletions crates/rb-sys/src/stable_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,42 +127,58 @@ pub trait StableApiDefinition {
unsafe fn rb_type(&self, obj: VALUE) -> crate::ruby_value_type;
}

#[cfg(any(not(ruby_api_stable), ruby_lt_2_6))]
use compiled as abi;

#[cfg(any(not(ruby_api_stable), ruby_lt_2_6, feature = "stable-api-compiled"))]
#[cfg(all(
explicitly_enabled_stable_api_compiled_fallback,
has_stable_api_compiled,
any(not(current_ruby_is_stable), ruby_lt_2_6,)
))]
use compiled as api;

#[cfg(all(
explicitly_enabled_stable_api_compiled_fallback,
has_stable_api_compiled,
))]
mod compiled;

#[cfg(all(
not(has_stable_api_compiled),
any(not(current_ruby_is_stable), ruby_lt_2_6)
))]
compile_error!("Compiled stable API is needed but could not find it.");

#[cfg(ruby_eq_2_6)]
#[path = "stable_api/ruby_2_6.rs"]
mod abi;
mod api;

#[cfg(ruby_eq_2_7)]
#[path = "stable_api/ruby_2_7.rs"]
mod abi;
mod api;

#[cfg(ruby_eq_3_0)]
#[path = "stable_api/ruby_3_0.rs"]
mod abi;
mod api;

#[cfg(ruby_eq_3_1)]
#[path = "stable_api/ruby_3_1.rs"]
mod abi;
mod api;

#[cfg(ruby_eq_3_2)]
#[path = "stable_api/ruby_3_2.rs"]
mod abi;
mod api;

/// Get the default stable API definition for the current Ruby version.
pub const fn get_default() -> &'static abi::Definition {
const API: abi::Definition = abi::Definition {};
pub const fn get_default() -> &'static api::Definition {
const API: api::Definition = api::Definition {};
&API
}

/// Get the fallback stable API definition for the current Ruby version, which
/// is compiled C code that is linked into to this crate.
#[cfg(feature = "stable-api-compiled")]
pub const fn get_fallback() -> &'static compiled::Definition {
#[cfg(all(
explicitly_enabled_stable_api_compiled_fallback,
has_stable_api_compiled,
))]
pub const fn get_compiled() -> &'static compiled::Definition {
const COMPILED_API: compiled::Definition = compiled::Definition {};
&COMPILED_API
}
6 changes: 4 additions & 2 deletions examples/rust_reverse/ext/rust_reverse/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
[package]
name = "rust_reverse"
version = "0.9.79"
autotests = true # set true if you want to use "cargo test"
autotests = true # set true if you want to use "cargo test"
edition = "2018"

[dependencies]
rb-sys = { version = "0.9.79", path = "./../../../../crates/rb-sys", features = ["global-allocator"] }
rb-sys = { version = "0.9.79", path = "./../../../../crates/rb-sys", features = [
"global-allocator",
] }

[lib]
crate-type = ["cdylib"]
Expand Down
2 changes: 1 addition & 1 deletion examples/rust_reverse/ext/rust_reverse/extconf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
r.ext_dir = "."

# Extra flags to pass to the $RUSTFLAGS environment variable (optional)
r.extra_rustflags << "--cfg=some_nested_config_var_for_crate"
r.extra_rustflags << "--cfg=rb_sys_use_stable_api_compiled_fallback"

# Force a rust toolchain to be installed via rustup (optional)
# You can also set the env var `RB_SYS_FORCE_INSTALL_RUST_TOOLCHAIN=true`
Expand Down
4 changes: 3 additions & 1 deletion examples/rust_reverse/ext/rust_reverse/extconf_bare.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@
require "mkmf"
require "rb_sys/mkmf"

create_rust_makefile("rust_reverse")
create_rust_makefile("rust_reverse") do |r|
r.extra_rustflags << "--cfg=rb_sys_use_stable_api_compiled_fallback"
end
2 changes: 1 addition & 1 deletion fuzz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ cargo-fuzz = true
libfuzzer-sys = "0.4"
rb-sys-test-helpers = { path = "../crates/rb-sys-test-helpers" }
rb-sys = { path = "../crates/rb-sys", features = [
"stable-api-compiled",
"stable-api-compiled-testing",
"link-ruby",
"fuzz",
] }
Expand Down

0 comments on commit 497e5fe

Please sign in to comment.