Class: Hornetseye::GCCContext

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

Constant Summary collapse

LDSHARED =

c:mingwbingcc

Config::CONFIG[ 'LDSHARED' ]
STRIP =
Config::CONFIG[ 'STRIP' ]
RUBYHDRDIR =
Config::CONFIG.member?( 'rubyhdrdir' ) ?
"-I#{Config::CONFIG['rubyhdrdir']} " +
"-I#{Config::CONFIG['rubyhdrdir']}/#{Config::CONFIG['arch']}" :
"-I#{Config::CONFIG['archdir']}"
LIBRUBYARG =
Config::CONFIG[ 'LIBRUBYARG' ]
DIRNAME =
"#{Dir.tmpdir}/hornetseye-ruby#{RUBY_VERSION}-" +
"#{ENV[ 'USER' ] || ENV[ 'USERNAME' ]}"
LOCKFILE =
"#{DIRNAME}/lock"
@@lock =
File.new LOCKFILE, 'w', 0600
@@lib_name =
'hornetseye_aaaaaaaa'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lib_name) ⇒ GCCContext



56
57
58
59
60
61
# File 'lib/multiarray/gcccontext.rb', line 56

def initialize( lib_name )
  @lib_name = lib_name
  @instructions = ''
  @wrappers = ''
  @registrations = ''
end

Class Method Details

.build(&action) ⇒ Object



49
50
51
52
# File 'lib/multiarray/gcccontext.rb', line 49

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

Instance Method Details

#<<(str) ⇒ Object



141
142
143
144
# File 'lib/multiarray/gcccontext.rb', line 141

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

#build(&action) ⇒ Object



63
64
65
# File 'lib/multiarray/gcccontext.rb', line 63

def build( &action )
  action.call self
end

#compileObject



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/multiarray/gcccontext.rb', line 105

def compile
  template = "#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\n\#{@instructions}\n\n\#{@wrappers}\nvoid Init_\#{@lib_name}(void)\n{\n  VALUE mHornetseye = rb_define_module( \"Hornetseye\" );\n  VALUE cGCCCache = rb_define_class_under( mHornetseye, \"GCCCache\",\n                                       rb_cObject );\n\#{@registrations}\n}\n"
  # File::EXCL no overwrite
  File.open "#{DIRNAME}/#{@lib_name}.c", 'w', 0600 do |f|
    f << template
  end
  gcc = "#{LDSHARED} -fPIC #{RUBYHDRDIR} -O " +
        "-o #{DIRNAME}/#{@lib_name}.so " +
        "#{DIRNAME}/#{@lib_name}.c #{LIBRUBYARG}"
  strip = "#{STRIP} #{DIRNAME}/#{@lib_name}.so"
  # puts template
  raise "Error compiling #{DIRNAME}/#{@lib_name}.c" unless system gcc
  raise "Error stripping #{DIRNAME}/#{@lib_name}.so" unless system strip
  require "#{DIRNAME}/#{@lib_name}.so"
end

#function(descriptor, *param_types) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/multiarray/gcccontext.rb', line 67

def function( descriptor, *param_types )
  @instructions << "void \#{descriptor}(\#{\nif param_types.empty?\n  ''\nelse\n  ' ' + param_types.collect do |t|\nt.identifiers\n  end.flatten.collect_with_index do |ident,i|\n\"\#{ident} param\#{i}\"\n  end.join( ', ' ) + ' '\nend\n}) {\n"

  @wrappers << "VALUE wrap\#{descriptor.capitalize}( int argc, VALUE *argv, VALUE rbSelf )\n{\n  \#{descriptor}(\#{\nif param_types.empty?\n  ''\nelse\n  s = ' ' + param_types.collect do |t|\nt.r2c\n  end.flatten.collect_with_index do |conv,i|\nconv.call \"argv[ \#{i} ]\"\n  end.join( ', ' ) + ' '\nend\n});\n  return Qnil;\n}\n"

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