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.
|
|
|
|
|
2007-11-10 14:22:28 +03:00
|
|
|
preludes = ARGV.dup
|
|
|
|
outfile = preludes.pop
|
2007-08-24 19:26:28 +04:00
|
|
|
|
2007-11-10 12:22:59 +03:00
|
|
|
C_ESC = {
|
|
|
|
"\\" => "\\\\",
|
|
|
|
'"' => '\"',
|
|
|
|
"\n" => '\n',
|
|
|
|
}
|
|
|
|
|
|
|
|
0x00.upto(0x1f) {|ch| C_ESC[[ch].pack("C")] ||= "\\x%02x" % ch }
|
|
|
|
0x7f.upto(0xff) {|ch| C_ESC[[ch].pack("C")] = "\\x%02x" % ch }
|
|
|
|
C_ESC_PAT = Regexp.union(*C_ESC.keys)
|
|
|
|
|
|
|
|
def c_esc(str)
|
|
|
|
'"' + str.gsub(C_ESC_PAT) { C_ESC[$&] } + '"'
|
|
|
|
end
|
2007-08-24 19:26:28 +04:00
|
|
|
|
2007-11-15 05:54:55 +03:00
|
|
|
mkconf = nil
|
|
|
|
setup_ruby_prefix = nil
|
2007-11-15 06:32:26 +03:00
|
|
|
teardown_ruby_prefix = nil
|
2007-11-15 05:54:55 +03:00
|
|
|
lines_list = preludes.map {|filename|
|
2007-11-10 12:22:59 +03:00
|
|
|
lines = []
|
2007-11-15 05:54:55 +03:00
|
|
|
need_ruby_prefix = false
|
|
|
|
File.readlines(filename).each {|line|
|
2007-11-10 12:22:59 +03:00
|
|
|
line.gsub!(/RbConfig::CONFIG\["(\w+)"\]/) {
|
2007-11-15 11:08:40 +03:00
|
|
|
key = $1
|
2007-11-15 05:54:55 +03:00
|
|
|
unless mkconf
|
|
|
|
require 'rbconfig'
|
2007-11-15 06:32:26 +03:00
|
|
|
mkconf = RbConfig::MAKEFILE_CONFIG.merge('prefix'=>'#{TMP_RUBY_PREFIX}')
|
2007-11-15 12:07:45 +03:00
|
|
|
setup_ruby_prefix = "TMP_RUBY_PREFIX = $:.reverse.find{|e|e!=\".\"}.sub(%r{(.*)/lib/.*}m, \"\\\\1\")\n"
|
2007-11-15 06:32:26 +03:00
|
|
|
teardown_ruby_prefix = 'Object.class_eval { remove_const "TMP_RUBY_PREFIX" }'
|
2007-11-15 05:54:55 +03:00
|
|
|
end
|
2007-11-15 11:08:40 +03:00
|
|
|
if RbConfig::MAKEFILE_CONFIG.has_key? key
|
|
|
|
val = RbConfig.expand("$(#{key})", mkconf)
|
2007-11-15 11:33:10 +03:00
|
|
|
need_ruby_prefix = true if /\A\#\{TMP_RUBY_PREFIX\}/ =~ val
|
2007-11-15 05:54:55 +03:00
|
|
|
c_esc(val)
|
2007-11-10 12:22:59 +03:00
|
|
|
else
|
2007-11-15 11:08:40 +03:00
|
|
|
"nil"
|
2007-11-10 12:22:59 +03:00
|
|
|
end
|
|
|
|
}
|
|
|
|
lines << c_esc(line)
|
|
|
|
}
|
2007-11-15 05:54:55 +03:00
|
|
|
setup_lines = []
|
|
|
|
if need_ruby_prefix
|
|
|
|
setup_lines << c_esc(setup_ruby_prefix)
|
2007-11-15 06:32:26 +03:00
|
|
|
lines << c_esc(teardown_ruby_prefix)
|
2007-11-15 05:54:55 +03:00
|
|
|
end
|
|
|
|
[setup_lines, lines]
|
2007-08-24 19:26:28 +04:00
|
|
|
}
|
|
|
|
|
2007-12-17 11:47:28 +03:00
|
|
|
require 'tool/serb'
|
2007-08-24 19:26:28 +04:00
|
|
|
|
2007-11-15 06:26:20 +03:00
|
|
|
tmp = ''
|
|
|
|
eval(serb(<<'EOS', 'tmp'))
|
2007-08-24 19:26:28 +04:00
|
|
|
#include "ruby/ruby.h"
|
2007-08-25 03:49:19 +04:00
|
|
|
#include "vm_core.h"
|
|
|
|
|
2007-11-15 06:26:20 +03:00
|
|
|
! preludes.zip(lines_list).each_with_index {|(prelude, (setup_lines, lines)), i|
|
|
|
|
static const char prelude_name<%i%>[] = <%c_esc(File.basename(prelude))%>;
|
|
|
|
static const char prelude_code<%i%>[] =
|
|
|
|
! (setup_lines+lines).each {|line|
|
|
|
|
<%line%>
|
|
|
|
! }
|
2007-08-24 19:26:28 +04:00
|
|
|
;
|
2007-11-15 06:26:20 +03:00
|
|
|
! }
|
2007-08-25 05:20:30 +04:00
|
|
|
|
2007-08-24 19:26:28 +04:00
|
|
|
void
|
|
|
|
Init_prelude(void)
|
|
|
|
{
|
2007-11-15 06:26:20 +03:00
|
|
|
! lines_list.each_with_index {|(setup_lines, lines), i|
|
2007-08-24 19:26:28 +04:00
|
|
|
rb_iseq_eval(rb_iseq_compile(
|
2007-11-15 06:26:20 +03:00
|
|
|
rb_str_new(prelude_code<%i%>, sizeof(prelude_code<%i%>) - 1),
|
|
|
|
rb_str_new(prelude_name<%i%>, sizeof(prelude_name<%i%>) - 1),
|
|
|
|
INT2FIX(<%1-setup_lines.length%>)));
|
2007-08-25 03:49:19 +04:00
|
|
|
|
2007-11-15 06:26:20 +03:00
|
|
|
! }
|
2007-08-25 03:49:19 +04:00
|
|
|
#if 0
|
2007-11-15 06:26:20 +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
|
|
|
|
|
|
|
|
open(outfile, 'w'){|f|
|
|
|
|
f << tmp
|
2007-08-24 19:26:28 +04:00
|
|
|
}
|
|
|
|
|