Class: Hornetseye::GCCContext

Inherits:
Object
  • Object
show all
Defined in:
lib/multiarray/gcccontext.rb

Overview

Context object for creating a Ruby extension

Constant Summary collapse

CFG =

Ruby configuration

RbConfig::CONFIG
CFLAGS =

GCC compiler flags

"-DNDEBUG #{CFG[ 'CFLAGS' ]} " +
"-I#{CFG['archdir']}"
LIBRUBYARG =

Arguments for linking the Ruby extension

"-L#{CFG[ 'libdir' ]} #{CFG[ 'LIBRUBYARG' ]} #{CFG[ 'LDFLAGS' ]} "
LDSHARED =

Command for linking the Ruby extension

CFG[ 'LDSHARED' ]
DLEXT =

Shared library file extension under current operating system

CFG[ 'DLEXT' ]
DIRNAME =

Directory for storing the Ruby extensions

"#{Dir.tmpdir}/hornetseye-ruby#{RUBY_VERSION}-" +
"#{ENV[ 'USER' ] || ENV[ 'USERNAME' ]}"
LOCKFILE =

Lock file to prevent conflicts

"#{DIRNAME}/lock"
@@lock =

The actual file lock

File.new LOCKFILE, 'w', 0600
@@lib_name =

Next available base name for a Ruby extension

'hornetseye_aaaaaaaa'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lib_name) ⇒ GCCContext

Initialises an empty Ruby extension

Parameters:

  • lib_name (String)

    Base name of library to create later.



113
114
115
116
117
118
# File 'lib/multiarray/gcccontext.rb', line 113

def initialize( lib_name )
  @lib_name = lib_name
  @c_instructions = ''
  @c_wrappers = ''
  @c_registrations = ''
end

Class Method Details

.build(&action) ⇒ Object

Method for compiling Ruby to C

Parameters:

  • action (Proc)

    The code block needs to accept a GCCContext object.

Returns:

  • (Object)

    Returns result of code block.

See Also:



101
102
103
104
# File 'lib/multiarray/gcccontext.rb', line 101

def build( &action )
  lib_name, @@lib_name = @@lib_name, @@lib_name.succ
  new( lib_name ).build &action
end

Instance Method Details

#<<(str) ⇒ GCCContext

Add instructions to Ruby extension

The given string is appended to the source code of the Ruby extension.

Parameters:

  • str (String)

    String with source code fragment of Ruby extension.

Returns:



254
255
256
257
# File 'lib/multiarray/gcccontext.rb', line 254

def <<( str )
  @c_instructions << str
  self
end

#build(&action) ⇒ Object

Create Ruby extension

Parameters:

  • action (Proc)

    Code block accepting a GCCContext object.

Returns:

  • (Object)

    Returns result of code block.



127
128
129
# File 'lib/multiarray/gcccontext.rb', line 127

def build( &action )
  action.call self
end

#compileBoolean

Compile the Ruby extension

This method writes the source code to a file and calls GCC to compile it. Finally the Ruby extension is loaded.

Returns:

  • (Boolean)

    Returns of loading the Ruby extension.



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/multiarray/gcccontext.rb', line 179

def compile
  c_template = <<EOS
/* This file is generated automatically. It is pointless to edit this file. */
#include <ruby.h>
#include <math.h>

inline void *mallocToPtr( VALUE rbMalloc )
{
  void *retVal; Data_Get_Struct( rbMalloc, void, retVal );
  return retVal;
}

static unsigned long make_mask( unsigned long x )
{
  x = x | x >> 1;
  x = x | x >> 2;
  x = x | x >> 4;
  x = x | x >> 8;
  x = x | x >> 16;
#if 4 < SIZEOF_LONG
  x = x | x >> 32;
#endif
  return x;
}

static unsigned long limited_rand(unsigned long limit)
{
  int i;
  unsigned long mask, val;
  if (limit < 2) return 0;
  mask = make_mask(limit - 1);
  retry:
  val = 0;
  for (i = SIZEOF_LONG / 4 - 1; 0 <= i; i--) {
if ((mask >> (i * 32)) & 0xffffffff) {
  val |= (unsigned long)rb_genrand_int32() << (i * 32);
  val &= mask;
  if (limit <= val)
    goto retry;
};
  };
  return val;
}

#{@c_instructions}

#{@c_wrappers}
void Init_#{@lib_name}(void)
{
  VALUE mHornetseye = rb_define_module("Hornetseye");
  VALUE cGCCCache = rb_define_class_under(mHornetseye, "GCCCache", rb_cObject);
#{@c_registrations}
}
EOS
  # File::EXCL no overwrite
  File.open "#{DIRNAME}/#{@lib_name}.c", 'w', 0600 do |f|
    f << c_template
  end
  gcc = "#{LDSHARED} #{CFLAGS} -o #{DIRNAME}/#{@lib_name}.#{DLEXT} " +
        "#{DIRNAME}/#{@lib_name}.c #{LIBRUBYARG}"
  # puts c_template
  # puts gcc
  raise "The following command failed: #{gcc}" unless system gcc
  require "#{DIRNAME}/#{@lib_name}"
end

#function(descriptor, *param_types) ⇒ GCCFunction

Add a new function to the Ruby extension

Parameters:

  • descriptor (String)

    Method name of function.

  • param_types (Array<GCCType>)

    Array with parameter types.

Returns:



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/multiarray/gcccontext.rb', line 139

def function( descriptor, *param_types )
  @c_instructions << <<EOS
VALUE #{descriptor}( #{
param_types.collect do |t|
  t.identifiers
end.flatten.collect_with_index do |ident,i|
  "#{ident} param#{i}"
end.join ', '
} )
{
EOS

  @c_wrappers << <<EOS
VALUE wrap#{descriptor.capitalize}( int argc, VALUE *argv, VALUE rbSelf )
{
  #{descriptor}( #{
param_types.collect do |t|
  t.r2c
end.flatten.collect_with_index do |conv,i|
  "#{conv.call "argv[#{i}]"}"
end.join ', '
  } );
  return Qnil;
}
EOS

  @c_registrations << <<EOS
  rb_define_singleton_method(cGCCCache, "#{descriptor}",
                         RUBY_METHOD_FUNC( wrap#{descriptor.capitalize} ), -1); 
EOS
end