Using C Extensions for Ruby to create a Ruby wrapper around a C library

·FOSS, GSoC, GSoC2018, Ruby

First published on my old blog, The Silent Monk’s Retreat.

It is often the case when you are sipping your tea at the middle of the night. You are suddenly forced to readjust your fancy monocle because you just came across a highly optimised Library written in C. But alas, it is not yet available in Ruby, your favourite language.

You wait for weeks hoping that the developer would port it. But they are busy having a life. Then one day when you hear your “colleagues” ranting about how someone created a Python wrapper for it and that makes you lose it.

That night a thought dawns in your mind:

myself.gif

Since you love control you decide to tackle this at a fundamental level and create C extensions rather than using Fiddle/ffi. And hence you find yourself here.

The journey ahead is difficult yet interesting, when you go through it you will gain a lot of valuable insights about the Ruby Interpreter and get yourself a shiny Library that you can use.

You need to prepare well. There are a lot of brilliant sacred texts about creating C extensions that can be found here,here (both parts) and here

I would advise you to go through these before you continue reading this post.

Because in this post I will not be reiterating the entire procedure that has already been well explained in the referenced blog posts, rather I will develop upon the problems that I tackled while creating a Ruby wrapper around GR Framework

To write these extensions we use the Ruby.h (Ruby C API) to directly communicate with  theRuby Interpreter via C code using ruby.h.

Specifying the compilation flags

The first issue that I faced was configuring the extconf.rb file (which creates the makefile) to include the required compilation flags

To compile a C code which uses GR Framework  (lets say test.c) I need to pass the following:

gcc test.c -I/usr/local/gr/include -L/usr/local/gr/lib -lGR -lm -Wl,-rpath,/usr/local/gr/lib

Here the -I/usr/local/gr/include gives the location of the gr.h include file, while -L/usr/local/gr/lib -lGR -lm -Wl,-rpath,/usr/local/gr/lib points to the location of the .so (shared object file) the include file uses.

Hence we modify our extconf.rb file to include the flags:

require 'mkmf'
$CFLAGS << " -I/usr/local/gr/include "
$LDFLAGS << " -L/usr/local/gr/lib -lGR -lm -Wl,-rpath,/usr/local/gr/lib"
create_makefile('grruby/grruby')

Wrapping a C Library function in Ruby

Now lets say that we have to create an extension for a C function with the following declaration.

void gr_axes(double x_tick, double y_tick, double x_org, double y_org, int major_x, int major_y, double tick_size)

The extension will look like :

#include "gr.h"
#include "ruby.h"

static VALUE axes(VALUE self, VALUE x_tick, VALUE y_tick, VALUE x_org, VALUE y_org, VALUE major_x, VALUE major_y, VALUE tick_size){

double x_tickc=NUM2DBL(x_tick);
double y_tickc=NUM2DBL(y_tick);
double x_orgc=NUM2DBL(x_org);
double y_orgc=NUM2DBL(y_org);
int major_xc=NUM2INT(major_x);
int major_yc=NUM2INT(major_y);
double tick_sizec=NUM2DBL(tick_size);
gr_axes(x_tickc,y_tickc,x_orgc,y_orgc,major_xc,major_yc,tick_sizec);
return Qtrue;
}

void Init_grruby(){
VALUE mGRruby=rb_define_module("GRruby");
rb_define_singleton_method(mGRruby,"axes",axes,7);
}

Let us look into the function that we have wrapped

static VALUE axes(VALUE self, VALUE x_tick, VALUE y_tick, VALUE x_org, VALUE y_org, VALUE major_x, VALUE major_y, VALUE tick_size){

double x_tickc=NUM2DBL(x_tick);
double y_tickc=NUM2DBL(y_tick);
double x_orgc=NUM2DBL(x_org);
double y_orgc=NUM2DBL(y_org);
int major_xc=NUM2INT(major_x);
int major_yc=NUM2INT(major_y);
double tick_sizec=NUM2DBL(tick_size);
gr_axes(x_tickc,y_tickc,x_orgc,y_orgc,major_xc,major_yc,tick_sizec);
return Qtrue;
}

The function “axes” is going to get its parameters from Ruby.

The Ruby Values need to be parsed into double and int C values so that they can be passed to the C function.

Lucky for us ruby.h provides us with a lot of methods ( This post is really helpful) like NUM2DBL (You can use RFLOAT_VALUE() too but that can cause the interpreter to crash if non float values are passed) and NUM2INT that we can use.

After which we can create the C counterparts of the Ruby values and pass them to the C function.

Since the wrapped C function returns nothing we return an arbitrary Ruby value (Qtrue in this case, which is boolean true for Ruby) as a Ruby function needs to return something.

I would suggest that you look into the C extensions code for Arrayfire  and fast_blank for example code.

In the upcoming post, I will discuss parsing and passing Ruby arrays and strings to a C program and how to make certain C functions more “Rubylike”.