Building gem native extensions

Why does some gems have build gem native extensions to get installed by
ruby 1.8 or 1.9.1 ??
Does rubygems downloads a source and then they are compiled to be
executable?

If so, why does Jruby can’t build native C libraries for those gems?
“Many Gems will work fine in JRuby; however, some Gems build native C
libraries as part of their install process. These Gems will not work in
JRuby unless the Gem has also provided a Java equivalent to the native
library.”
in http://kenai.com/projects/jruby/pages/GettingStarted

the short answer is that JRuby runs on the Java VM which doesn’t let you
load native libraries.

the longer answer is because JRuby is running on the Java virtual
machine,
and the JVM is a bit particular about just loading and running any old
library. to call native code from the JVM you usually need to write your
own
compatibility layer, which is not trivial. the JVM was more targeted at
stability and enterprise reliabillty than rapid development, so
dynamically
loading and calling native libraries was one of the things they
excluded.

Ruby on the other hand is more egregarious and will dynamically load and
call native libraries for you without hassle, as Matz just wanted to get
things done and didn’t mind if people shot themselves in the foot.

Some Ruby gems use native code as C code is a lot faster than Ruby, plus
there are tons of existing libraries already that are easier to just
load
and run as native than re-implement in pure Ruby.

I don’t think rubygems makes gems executable after it compiles them…
most
native gems are just shared libraries that get mapped into Rubys process
space…

thanks Charles, much better answer than mine… :wink:

I’ll provide a slightly different answer…

JRuby can link in native libraries and make native calls, but the C
API exposed by Ruby demands a lot more than that. Specifically, it
requires the internal structure of objects to look a specific way,
requires direct memory access to those objects, and requires the
ability to modify object contents directly. None of these things are
provided by the JVM’s C API, and as a result there’s no way for us to
support them.

There is a possibility that in the future JRuby could support the
“safe” subset of the Ruby C API that doesn’t require direct memory
access, and there’s actually going to be a Ruby Summer of Code project
to attempt it. But we’ll never support the “unsafe” subset, so many
(most? all?) C extensions will require modification to work.

To answer your other questions:

Question: Why does some gems have build gem native extensions to get
installed by
ruby 1.8 or 1.9.1 ??

Answer: Because some of those gem authors felt they needed native (C)
code to accomplish the goals of their gem. The two usual requirements
are that the gem depends on a C library or that the C code is for
performance-intensive logic inappropriate for Ruby code.

Question: Does rubygems downloads a source and then they are compiled to
be
executable?

Answer: Yes, that’s exactly what it does. Some authors will also ship
precompiled versions for specific platforms, but ultimately all such
gems require the Ruby C API be available. Until we can support that
API, those gems will not work with JRuby.

  • Charlie

On Wed, May 12, 2010 at 3:26 AM, Matthew K. [email protected]
wrote:

things done and didn’t mind if people shot themselves in the foot.

Why does some gems have build gem native extensions to get installed by


“life is complex, it has both real and imaginary components”
http://threebrightlights.blogspot.com


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email