2007-11-10 19:33:30 +03:00
|
|
|
# This file is interpreted by $(BASERUBY) and miniruby.
|
2007-11-15 05:54:55 +03:00
|
|
|
# $(BASERUBY) is used for miniprelude.c.
|
|
|
|
# miniruby is used for prelude.c.
|
2007-11-10 19:33:30 +03:00
|
|
|
# Since $(BASERUBY) may be older than Ruby 1.9,
|
|
|
|
# Ruby 1.9 feature should not be used.
|
|
|
|
|
2010-03-12 01:15:11 +03:00
|
|
|
require 'erb'
|
2007-12-17 15:41:27 +03:00
|
|
|
|
2010-03-12 01:15:11 +03:00
|
|
|
class Prelude
|
|
|
|
SRCDIR = File.dirname(File.dirname(__FILE__))
|
|
|
|
$:.unshift(SRCDIR)
|
2007-08-24 19:26:28 +04:00
|
|
|
|
2010-03-12 01:15:11 +03:00
|
|
|
C_ESC = {
|
|
|
|
"\\" => "\\\\",
|
|
|
|
'"' => '\"',
|
|
|
|
"\n" => '\n',
|
|
|
|
}
|
2007-11-10 12:22:59 +03:00
|
|
|
|
2010-03-12 01:15:11 +03:00
|
|
|
0x00.upto(0x1f) {|ch| C_ESC[[ch].pack("C")] ||= "\\%03o" % ch }
|
|
|
|
0x7f.upto(0xff) {|ch| C_ESC[[ch].pack("C")] = "\\%03o" % ch }
|
|
|
|
C_ESC_PAT = Regexp.union(*C_ESC.keys)
|
2007-11-10 12:22:59 +03:00
|
|
|
|
2010-03-12 01:15:11 +03:00
|
|
|
def c_esc(str)
|
|
|
|
'"' + str.gsub(C_ESC_PAT) { C_ESC[$&] } + '"'
|
|
|
|
end
|
|
|
|
def prelude_base(filename)
|
|
|
|
filename[/\A#{Regexp.quote(SRCDIR)}\/(.*?)(\.rb)?\z/om, 1]
|
|
|
|
end
|
|
|
|
def prelude_name(filename)
|
|
|
|
"<internal:" + prelude_base(filename) + ">"
|
|
|
|
end
|
2007-08-24 19:26:28 +04:00
|
|
|
|
2010-03-12 01:15:11 +03:00
|
|
|
def initialize(preludes)
|
|
|
|
@mkconf = nil
|
|
|
|
@have_sublib = false
|
|
|
|
@need_ruby_prefix = false
|
|
|
|
@preludes = {}
|
|
|
|
@mains = preludes.map {|filename| translate(filename)[0]}
|
|
|
|
end
|
|
|
|
|
|
|
|
def translate(filename, sub = false)
|
|
|
|
idx = @preludes[filename]
|
|
|
|
return idx if idx
|
|
|
|
lines = []
|
|
|
|
@preludes[filename] = result = [@preludes.size, filename, lines, sub]
|
|
|
|
File.readlines(filename).each do |line|
|
|
|
|
line.gsub!(/RbConfig::CONFIG\["(\w+)"\]/) {
|
|
|
|
key = $1
|
|
|
|
unless @mkconf
|
|
|
|
require './rbconfig'
|
2010-03-12 18:11:10 +03:00
|
|
|
@mkconf = RbConfig::MAKEFILE_CONFIG.merge('prefix'=>'#{TMP_RUBY_PREFIX}')
|
2010-03-12 01:15:11 +03:00
|
|
|
end
|
|
|
|
if RbConfig::MAKEFILE_CONFIG.has_key? key
|
|
|
|
val = RbConfig.expand("$(#{key})", @mkconf)
|
|
|
|
@need_ruby_prefix ||= /\A\#\{TMP_RUBY_PREFIX\}/ =~ val
|
|
|
|
c_esc(val)
|
|
|
|
else
|
|
|
|
"nil"
|
|
|
|
end
|
|
|
|
}
|
|
|
|
line.sub!(/require\s*\(?\s*(["'])(.*?)\1\)?/) do
|
|
|
|
orig, path = $&, $2
|
|
|
|
path = File.join(SRCDIR, path)
|
|
|
|
if File.exist?(path)
|
|
|
|
@have_sublib = true
|
|
|
|
"TMP_RUBY_PREFIX.require(#{translate(path, true)[0]})"
|
|
|
|
else
|
|
|
|
orig
|
2009-07-31 10:09:13 +04:00
|
|
|
end
|
2009-07-30 18:12:25 +04:00
|
|
|
end
|
2009-07-31 10:09:13 +04:00
|
|
|
lines << c_esc(line)
|
|
|
|
end
|
2010-03-12 01:15:11 +03:00
|
|
|
result
|
2007-11-15 05:54:55 +03:00
|
|
|
end
|
2007-08-24 19:26:28 +04:00
|
|
|
|
2010-03-12 01:15:11 +03:00
|
|
|
def emit(outfile)
|
|
|
|
init_name = outfile[/\w+(?=_prelude.c\b)/] || 'prelude'
|
|
|
|
erb = ERB.new(<<'EOS', nil, '%')
|
2009-07-30 18:12:25 +04:00
|
|
|
/* -*-c-*-
|
|
|
|
THIS FILE WAS AUTOGENERATED BY tool/compile_prelude.rb. DO NOT EDIT.
|
|
|
|
|
2010-03-12 01:15:11 +03:00
|
|
|
soruces: <%= @preludes.map {|n,*| prelude_base(n)}.join(', ') %>
|
2009-07-30 18:12:25 +04:00
|
|
|
*/
|
2007-08-24 19:26:28 +04:00
|
|
|
#include "ruby/ruby.h"
|
2007-08-25 03:49:19 +04:00
|
|
|
#include "vm_core.h"
|
|
|
|
|
2010-03-12 01:15:11 +03:00
|
|
|
% preludes = @preludes.values.sort
|
|
|
|
% preludes.each {|i, prelude, lines, sub|
|
|
|
|
|
|
|
|
static const char prelude_name<%=i%>[] = <%=c_esc(prelude_name(*prelude))%>;
|
2007-12-21 03:27:40 +03:00
|
|
|
static const char prelude_code<%=i%>[] =
|
2010-03-12 01:15:11 +03:00
|
|
|
% lines.each {|line|
|
2007-12-21 03:27:40 +03:00
|
|
|
<%=line%>
|
|
|
|
% }
|
2007-08-24 19:26:28 +04:00
|
|
|
;
|
2007-12-21 03:27:40 +03:00
|
|
|
% }
|
2007-08-25 05:20:30 +04:00
|
|
|
|
2010-03-12 01:15:11 +03:00
|
|
|
#define PRELUDE_COUNT <%=@have_sublib ? preludes.size : 0%>
|
|
|
|
|
|
|
|
% if @have_sublib or @need_ruby_prefix
|
|
|
|
struct prelude_env {
|
|
|
|
volatile VALUE prefix_path;
|
|
|
|
#if PRELUDE_COUNT > 0
|
|
|
|
char loaded[PRELUDE_COUNT];
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
prelude_prefix_path(VALUE self)
|
|
|
|
{
|
|
|
|
struct prelude_env *ptr = DATA_PTR(self);
|
|
|
|
return ptr->prefix_path;
|
|
|
|
}
|
|
|
|
% end
|
|
|
|
|
|
|
|
% if @have_sublib
|
|
|
|
static VALUE
|
|
|
|
prelude_require(VALUE self, VALUE nth)
|
|
|
|
{
|
|
|
|
struct prelude_env *ptr = DATA_PTR(self);
|
|
|
|
VALUE code, name;
|
|
|
|
int n = FIX2INT(nth);
|
|
|
|
|
|
|
|
if (n > PRELUDE_COUNT) return Qfalse;
|
|
|
|
if (ptr->loaded[n]) return Qfalse;
|
|
|
|
ptr->loaded[n] = 1;
|
|
|
|
switch (n) {
|
|
|
|
% @preludes.each_value do |i, prelude, lines, sub|
|
|
|
|
% if sub
|
|
|
|
case <%=i%>:
|
|
|
|
code = rb_usascii_str_new(prelude_code<%=i%>, sizeof(prelude_code<%=i%>) - 1);
|
|
|
|
name = rb_usascii_str_new(prelude_name<%=i%>, sizeof(prelude_name<%=i%>) - 1);
|
|
|
|
break;
|
|
|
|
% end
|
|
|
|
% end
|
|
|
|
default:
|
|
|
|
return Qfalse;
|
|
|
|
}
|
|
|
|
rb_iseq_eval(rb_iseq_compile(code, name, INT2FIX(1)));
|
|
|
|
return Qtrue;
|
|
|
|
}
|
|
|
|
|
|
|
|
% end
|
2007-08-24 19:26:28 +04:00
|
|
|
void
|
2007-12-25 10:04:30 +03:00
|
|
|
Init_<%=init_name%>(void)
|
2007-08-24 19:26:28 +04:00
|
|
|
{
|
2010-03-12 01:15:11 +03:00
|
|
|
% if @have_sublib or @need_ruby_prefix
|
|
|
|
struct prelude_env memo;
|
|
|
|
ID name = rb_intern("TMP_RUBY_PREFIX");
|
|
|
|
VALUE prelude = Data_Wrap_Struct(rb_cData, 0, 0, &memo);
|
|
|
|
|
|
|
|
memo.prefix_path = rb_const_remove(rb_cObject, name);
|
|
|
|
rb_const_set(rb_cObject, name, prelude);
|
|
|
|
rb_define_singleton_method(prelude, "to_s", prelude_prefix_path, 0);
|
|
|
|
% end
|
|
|
|
% if @have_sublib
|
|
|
|
memset(memo.loaded, 0, sizeof(memo.loaded));
|
|
|
|
rb_define_singleton_method(prelude, "require", prelude_require, 1);
|
|
|
|
% end
|
|
|
|
% preludes.each do |i, prelude, lines, sub|
|
|
|
|
% next if sub
|
|
|
|
rb_iseq_eval(rb_iseq_compile(
|
|
|
|
rb_usascii_str_new(prelude_code<%=i%>, sizeof(prelude_code<%=i%>) - 1),
|
|
|
|
rb_usascii_str_new(prelude_name<%=i%>, sizeof(prelude_name<%=i%>) - 1),
|
|
|
|
INT2FIX(1)));
|
|
|
|
% end
|
|
|
|
% if @have_sublib or @need_ruby_prefix
|
|
|
|
rb_gc_force_recycle(prelude);
|
|
|
|
% end
|
2007-08-25 03:49:19 +04:00
|
|
|
|
|
|
|
#if 0
|
2007-12-21 03:27:40 +03:00
|
|
|
% preludes.length.times {|i|
|
|
|
|
puts(prelude_code<%=i%>);
|
|
|
|
% }
|
2007-08-25 03:49:19 +04:00
|
|
|
#endif
|
2007-08-24 19:26:28 +04:00
|
|
|
}
|
2007-11-15 06:26:20 +03:00
|
|
|
EOS
|
2010-03-12 01:15:11 +03:00
|
|
|
tmp = erb.result(binding)
|
|
|
|
open(outfile, 'w'){|f|
|
|
|
|
f << tmp
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
2007-11-15 06:26:20 +03:00
|
|
|
|
2010-03-12 01:15:11 +03:00
|
|
|
preludes = ARGV.dup
|
|
|
|
outfile = preludes.pop
|
|
|
|
Prelude.new(preludes).emit(outfile)
|
2007-08-24 19:26:28 +04:00
|
|
|
|