Class: Hornetseye::GCCContext
Overview
Context object for creating a Ruby extension
Constant Summary collapse
- CFG =
Ruby configuration
RbConfig::CONFIG
- CFLAGS =
GCC compiler flags
"-DNDEBUG -Wno-unused-function #{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
-
.build(&action) ⇒ Object
Method for compiling Ruby to C.
Instance Method Summary collapse
-
#<<(str) ⇒ GCCContext
Add instructions to Ruby extension.
-
#build(&action) ⇒ Object
Create Ruby extension.
-
#compile ⇒ Boolean
Compile the Ruby extension.
-
#function(descriptor, *param_types) ⇒ GCCFunction
Add a new function to the Ruby extension.
-
#initialize(lib_name) ⇒ GCCContext
constructor
Initialises an empty Ruby extension.
Constructor Details
#initialize(lib_name) ⇒ GCCContext
Initialises an empty Ruby extension
116 117 118 119 120 121 |
# File 'lib/multiarray/gcccontext.rb', line 116 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
104 105 106 107 |
# File 'lib/multiarray/gcccontext.rb', line 104 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.
257 258 259 260 |
# File 'lib/multiarray/gcccontext.rb', line 257 def <<( str ) @c_instructions << str self end |
#build(&action) ⇒ Object
Create Ruby extension
130 131 132 |
# File 'lib/multiarray/gcccontext.rb', line 130 def build( &action ) action.call self end |
#compile ⇒ Boolean
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.
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 244 245 246 |
# File 'lib/multiarray/gcccontext.rb', line 182 def compile c_template = "/* This file is generated automatically. It is pointless to edit this file. */\n#include <ruby.h>\n#include <math.h>\n\ninline void *mallocToPtr( VALUE rbMalloc )\n{\n void *retVal; Data_Get_Struct( rbMalloc, void, retVal );\n return retVal;\n}\n\nstatic unsigned long make_mask( unsigned long x )\n{\n x = x | x >> 1;\n x = x | x >> 2;\n x = x | x >> 4;\n x = x | x >> 8;\n x = x | x >> 16;\n#if 4 < SIZEOF_LONG\n x = x | x >> 32;\n#endif\n return x;\n}\n\nstatic unsigned long limited_rand(unsigned long limit)\n{\n int i;\n unsigned long mask, val;\n if (limit < 2) return 0;\n mask = make_mask(limit - 1);\n retry:\n val = 0;\n for (i = SIZEOF_LONG / 4 - 1; 0 <= i; i--) {\nif ((mask >> (i * 32)) & 0xffffffff) {\n val |= (unsigned long)rb_genrand_int32() << (i * 32);\n val &= mask;\n if (limit <= val)\n goto retry;\n};\n };\n return val;\n}\n\n\#{@c_instructions}\n\n\#{@c_wrappers}\nvoid Init_\#{@lib_name}(void)\n{\n VALUE mHornetseye = rb_define_module(\"Hornetseye\");\n VALUE cGCCCache = rb_define_class_under(mHornetseye, \"GCCCache\", rb_cObject);\n\#{@c_registrations}\n}\n" # 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
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 170 171 172 |
# File 'lib/multiarray/gcccontext.rb', line 142 def function( descriptor, *param_types ) @c_instructions << "VALUE \#{descriptor}( \#{\nparam_types.collect do |t|\n t.identifiers\nend.flatten.collect_with_index do |ident,i|\n \"\#{ident} param\#{i}\"\nend.join ', '\n} )\n{\n" @c_wrappers << "VALUE wrap\#{descriptor.capitalize}( int argc, VALUE *argv, VALUE rbSelf )\n{\n \#{descriptor}( \#{\nparam_types.collect do |t|\n t.r2c\nend.flatten.collect_with_index do |conv,i|\n \"\#{conv.call \"argv[\#{i}]\"}\"\nend.join ', '\n } );\n return Qnil;\n}\n" @c_registrations << " rb_define_singleton_method(cGCCCache, \"\#{descriptor}\",\n RUBY_METHOD_FUNC( wrap\#{descriptor.capitalize} ), -1); \n" end |