From ddc8868470b2e20f8cd1bc0194576970dd7329c2 Mon Sep 17 00:00:00 2001 From: Ian Ker-Seymer Date: Wed, 12 Jul 2023 02:15:33 -0400 Subject: [PATCH] Make it easier to opt-in from extconf --- crates/rb-sys/build/features.rs | 7 +++++-- examples/rust_reverse/ext/rust_reverse/extconf.rb | 5 ++++- examples/rust_reverse/ext/rust_reverse/extconf_bare.rb | 3 ++- gem/lib/rb_sys/mkmf.rb | 4 +++- gem/lib/rb_sys/mkmf/config.rb | 8 ++++++++ 5 files changed, 22 insertions(+), 5 deletions(-) diff --git a/crates/rb-sys/build/features.rs b/crates/rb-sys/build/features.rs index 44a251fc..72f2e156 100644 --- a/crates/rb-sys/build/features.rs +++ b/crates/rb-sys/build/features.rs @@ -26,8 +26,11 @@ pub fn is_compiled_stable_api_needed(ver: &Version) -> bool { } 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") + println!("cargo:rerun-if-env-changed=RB_SYS_USE_STABLE_API_COMPILED_FALLBACK"); + + is_env_variable_defined("CARGO_FEATURE_STABLE_API_COMPILED_FALLBACK") + || cfg!(rb_sys_use_stable_api_compiled_fallback) + || is_env_variable_defined("RB_SYS_USE_STABLE_API_COMPILED_FALLBACK") } pub fn testing_stable_api_compiled_fallback() -> bool { diff --git a/examples/rust_reverse/ext/rust_reverse/extconf.rb b/examples/rust_reverse/ext/rust_reverse/extconf.rb index 7b986bf2..f28e51f6 100644 --- a/examples/rust_reverse/ext/rust_reverse/extconf.rb +++ b/examples/rust_reverse/ext/rust_reverse/extconf.rb @@ -20,7 +20,7 @@ r.ext_dir = "." # Extra flags to pass to the $RUSTFLAGS environment variable (optional) - r.extra_rustflags << "--cfg=rb_sys_use_stable_api_compiled_fallback" + r.extra_rustflags << "--cfg=some_nested_config_var_for_crate" # Force a rust toolchain to be installed via rustup (optional) # You can also set the env var `RB_SYS_FORCE_INSTALL_RUST_TOOLCHAIN=true` @@ -34,4 +34,7 @@ # Extra targets to install via rustup (optional) r.extra_rustup_targets = ["wasm32-unknown-unknown"] + + # Enable stable API compiled fallback for ruby-head (optional) + r.use_stable_api_compiled_fallback = true end diff --git a/examples/rust_reverse/ext/rust_reverse/extconf_bare.rb b/examples/rust_reverse/ext/rust_reverse/extconf_bare.rb index 66607082..6379af0e 100644 --- a/examples/rust_reverse/ext/rust_reverse/extconf_bare.rb +++ b/examples/rust_reverse/ext/rust_reverse/extconf_bare.rb @@ -6,5 +6,6 @@ require "rb_sys/mkmf" create_rust_makefile("rust_reverse") do |r| - r.extra_rustflags << "--cfg=rb_sys_use_stable_api_compiled_fallback" + # Enable stable API compiled fallback for ruby-head (optional) + r.use_stable_api_compiled_fallback = true end diff --git a/gem/lib/rb_sys/mkmf.rb b/gem/lib/rb_sys/mkmf.rb index 232788fd..79b989e4 100644 --- a/gem/lib/rb_sys/mkmf.rb +++ b/gem/lib/rb_sys/mkmf.rb @@ -53,6 +53,8 @@ def create_rust_makefile(target, &blk) RbConfig.expand(srcdir = srcprefix.dup) full_cargo_command = cargo_command(srcdir, builder) + global_rustflags = GLOBAL_RUSTFLAGS.dup + global_rustflags << "--cfg=rb_sys_use_stable_api_compiled_fallback" if builder.use_stable_api_compiled_fallback? make_install = +<<~MAKE #{conditional_assign("RB_SYS_BUILD_DIR", File.join(Dir.pwd, ".rb-sys"))} @@ -68,7 +70,7 @@ def create_rust_makefile(target, &blk) #{set_cargo_profile(builder)} #{conditional_assign("RB_SYS_CARGO_FEATURES", builder.features.join(","))} - #{conditional_assign("RB_SYS_GLOBAL_RUSTFLAGS", GLOBAL_RUSTFLAGS.join(" "))} + #{conditional_assign("RB_SYS_GLOBAL_RUSTFLAGS", global_rustflags.join(" "))} #{conditional_assign("RB_SYS_EXTRA_RUSTFLAGS", builder.extra_rustflags.join(" "))} #{conditional_assign("RB_SYS_EXTRA_CARGO_ARGS", builder.extra_cargo_args.join(" "))} #{conditional_assign("RB_SYS_CARGO_MANIFEST_DIR", builder.manifest_dir)} diff --git a/gem/lib/rb_sys/mkmf/config.rb b/gem/lib/rb_sys/mkmf/config.rb index 1d669a73..4999ebd1 100644 --- a/gem/lib/rb_sys/mkmf/config.rb +++ b/gem/lib/rb_sys/mkmf/config.rb @@ -22,10 +22,14 @@ class Config # Extra targets to install attr_accessor :extra_rustup_targets + # Use compiled C code fallback for stable API for ruby-head (default: false) + attr_accessor :use_stable_api_compiled_fallback + def initialize(builder) @builder = builder @force_install_rust_toolchain = false @auto_install_rust_toolchain = true + @use_stable_api_compiled_fallback = false @clean_after_install = rubygems_invoked? @rubygems_clean_dirs = ["./cargo-vendor"] @extra_rustup_targets = [] @@ -53,6 +57,10 @@ def respond_to_missing?(name, include_private = false) def rubygems_invoked? ENV.key?("SOURCE_DATE_EPOCH") end + + def use_stable_api_compiled_fallback? + @use_stable_api_compiled_fallback + end end end end