Native extensions fallback to pure Ruby if not supported on gem install

后端 未结 3 1777
走了就别回头了
走了就别回头了 2021-02-04 10:38

I am developing a gem, which is currently pure Ruby, but I have also been developing a faster C variant for one of the features. The feature is usable, but sometimes slow, in pu

3条回答
  •  攒了一身酷
    2021-02-04 11:00

    Here is a thought, based on info from http://guides.rubygems.org/c-extensions/ and http://yorickpeterse.com/articles/hacking-extconf-rb/.

    Looks like you can put the logic in extconf.rb. For example, query the RUBY_DESCRIPTION constant and determine if you are in a Ruby that supports native extensions:

    $ irb
    jruby-1.6.8 :001 > RUBY_DESCRIPTION
    => "jruby 1.6.8 (ruby-1.8.7-p357) (2012-09-18 1772b40) (Java HotSpot(TM) 64-Bit Server VM       
        1.6.0_51) [darwin-x86_64-java]"
    

    So you could try something like wrap the code in extconf.rb in a conditional (in extconf.rb):

    unless RUBY_DESCRIPTION =~ /jruby/ do
    
      require 'mkmf'
    
      # stuff    
      create_makefile('my_extension/my_extension')
    
    end
    

    Obviously, you will want more sophisticated logic, grabbing parameters passed on "gem install", etc.

提交回复
热议问题