Skip to content

Commit

Permalink
Make it easier to opt-in from extconf
Browse files Browse the repository at this point in the history
  • Loading branch information
ianks committed Jul 12, 2023
1 parent 497e5fe commit ddc8868
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 5 deletions.
7 changes: 5 additions & 2 deletions crates/rb-sys/build/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 4 additions & 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=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`
Expand All @@ -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
3 changes: 2 additions & 1 deletion examples/rust_reverse/ext/rust_reverse/extconf_bare.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 3 additions & 1 deletion gem/lib/rb_sys/mkmf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"))}
Expand All @@ -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)}
Expand Down
8 changes: 8 additions & 0 deletions gem/lib/rb_sys/mkmf/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down Expand Up @@ -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

0 comments on commit ddc8868

Please sign in to comment.