Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use other build systems with rubygems?

My C code is getting harder to manage due to the inflexibility of mkmf. For this reason, I'd like to use another build system.

What does rubygems need in order to build a C extension? How can I integrate a build system like autotools/configure into the workflow?

Gem::Specification.new 'my_gem' do |gem|
  # Will this work?
  gem.extensions = %w(ext/my_gem/configure)
end
like image 931
Matheus Moreira Avatar asked Jan 18 '23 07:01

Matheus Moreira


1 Answers

There are some tools that help is such situation (e.g. rake-compiler gem), but I prefer to use RubyInline gem. It was designed to replace slow, performance critical sections of Ruby code with implementations in other languages (e.g. C is supported out of the box), but it is also used to inline code that calls external C libraries.

A RubyInline example looks as follows:

class MyClass
  inline(:C) do |builder|
    builder.include '<stdio.h>'
    builder.c <<-END
      void my_printf(char * string){
        printf("%s\\n",string);
      }
    END
  end
end
MyClass.new.my_printf("Abc") 
# prints 'Abc'

The nice feature of RubyInline is that you don't have to keep separate files for C and Ruby, some basic argument conversions are supported out of the box and you don't have to write the gluing code. The bad parts are that you don't have the full control over compilation, etc. Personally I find RubyInline a very powerful solution.

like image 110
Aleksander Pohl Avatar answered Jan 28 '23 20:01

Aleksander Pohl