2007-11-10 11:33:30 -05:00
|
|
|
# This file is interpreted by $(BASERUBY) and miniruby.
|
2007-11-14 21:54:55 -05:00
|
|
|
# $(BASERUBY) is used for miniprelude.c.
|
|
|
|
# miniruby is used for prelude.c.
|
2007-11-10 11:33:30 -05:00
|
|
|
# Since $(BASERUBY) may be older than Ruby 1.9,
|
|
|
|
# Ruby 1.9 feature should not be used.
|
|
|
|
|
2007-11-10 06:22:28 -05:00
|
|
|
preludes = ARGV.dup
|
|
|
|
outfile = preludes.pop
|
2007-08-24 11:26:28 -04:00
|
|
|
|
2007-11-10 04:22:59 -05: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 11:26:28 -04:00
|
|
|
|
2007-11-14 21:54:55 -05:00
|
|
|
mkconf = nil
|
|
|
|
setup_ruby_prefix = nil
|
2007-11-14 22:32:26 -05:00
|
|
|
teardown_ruby_prefix = nil
|
2007-11-14 21:54:55 -05:00
|
|
|
lines_list = preludes.map {|filename|
|
2007-11-10 04:22:59 -05:00
|
|
|
lines = []
|
2007-11-14 21:54:55 -05:00
|
|
|
need_ruby_prefix = false
|
|
|
|
File.readlines(filename).each {|line|
|
2007-11-10 04:22:59 -05:00
|
|
|
line.gsub!(/RbConfig::CONFIG\["(\w+)"\]/) {
|
2007-11-15 03:08:40 -05:00
|
|
|
key = $1
|
2007-11-14 21:54:55 -05:00
|
|
|
unless mkconf
|
|
|
|
require 'rbconfig'
|
2007-11-14 22:32:26 -05:00
|
|
|
mkconf = RbConfig::MAKEFILE_CONFIG.merge('prefix'=>'#{TMP_RUBY_PREFIX}')
|
2007-11-15 04:07:45 -05:00
|
|
|
setup_ruby_prefix = "TMP_RUBY_PREFIX = $:.reverse.find{|e|e!=\".\"}.sub(%r{(.*)/lib/.*}m, \"\\\\1\")\n"
|
2007-11-14 22:32:26 -05:00
|
|
|
teardown_ruby_prefix = 'Object.class_eval { remove_const "TMP_RUBY_PREFIX" }'
|
2007-11-14 21:54:55 -05:00
|
|
|
end
|
2007-11-15 03:08:40 -05:00
|
|
|
if RbConfig::MAKEFILE_CONFIG.has_key? key
|
|
|
|
val = RbConfig.expand("$(#{key})", mkconf)
|
2007-11-15 03:33:10 -05:00
|
|
|
need_ruby_prefix = true if /\A\#\{TMP_RUBY_PREFIX\}/ =~ val
|
2007-11-14 21:54:55 -05:00
|
|
|
c_esc(val)
|
2007-11-10 04:22:59 -05:00
|
|
|
else
|
2007-11-15 03:08:40 -05:00
|
|
|
"nil"
|
2007-11-10 04:22:59 -05:00
|
|
|
end
|
|
|
|
}
|
|
|
|
lines << c_esc(line)
|
|
|
|
}
|
2007-11-14 21:54:55 -05:00
|
|
|
setup_lines = []
|
|
|
|
if need_ruby_prefix
|
|
|
|
setup_lines << c_esc(setup_ruby_prefix)
|
2007-11-14 22:32:26 -05:00
|
|
|
lines << c_esc(teardown_ruby_prefix)
|
2007-11-14 21:54:55 -05:00
|
|
|
end
|
|
|
|
[setup_lines, lines]
|
2007-08-24 11:26:28 -04:00
|
|
|
}
|
|
|
|
|
2007-11-14 22:26:20 -05:00
|
|
|
def serb(str, var)
|
|
|
|
result = ''
|
|
|
|
str.each_line {|line|
|
|
|
|
if /\A!/ =~ line
|
|
|
|
result << $'
|
|
|
|
else
|
|
|
|
line.split(/(<%.*?%>)/).each {|x|
|
|
|
|
if /\A<%(.*)%>\z/ =~ x
|
|
|
|
result << "#{var} << (#{$1}).to_s\n"
|
|
|
|
else
|
|
|
|
result << "#{var} << #{x.dump}\n"
|
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
}
|
|
|
|
result
|
|
|
|
end
|
2007-08-24 11:26:28 -04:00
|
|
|
|
2007-11-14 22:26:20 -05:00
|
|
|
tmp = ''
|
|
|
|
eval(serb(<<'EOS', 'tmp'))
|
2007-08-24 11:26:28 -04:00
|
|
|
#include "ruby/ruby.h"
|
2007-08-24 19:49:19 -04:00
|
|
|
#include "vm_core.h"
|
|
|
|
|
2007-11-14 22:26:20 -05: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 11:26:28 -04:00
|
|
|
;
|
2007-11-14 22:26:20 -05:00
|
|
|
! }
|
2007-08-24 21:20:30 -04:00
|
|
|
|
2007-08-24 11:26:28 -04:00
|
|
|
void
|
|
|
|
Init_prelude(void)
|
|
|
|
{
|
2007-11-14 22:26:20 -05:00
|
|
|
! lines_list.each_with_index {|(setup_lines, lines), i|
|
2007-08-24 11:26:28 -04:00
|
|
|
rb_iseq_eval(rb_iseq_compile(
|
2007-11-14 22:26:20 -05: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-24 19:49:19 -04:00
|
|
|
|
2007-11-14 22:26:20 -05:00
|
|
|
! }
|
2007-08-24 19:49:19 -04:00
|
|
|
#if 0
|
2007-11-14 22:26:20 -05:00
|
|
|
! preludes.length.times {|i|
|
|
|
|
puts(prelude_code<%i%>);
|
|
|
|
! }
|
2007-08-24 19:49:19 -04:00
|
|
|
#endif
|
2007-08-24 11:26:28 -04:00
|
|
|
}
|
2007-11-14 22:26:20 -05:00
|
|
|
EOS
|
|
|
|
|
|
|
|
open(outfile, 'w'){|f|
|
|
|
|
f << tmp
|
2007-08-24 11:26:28 -04:00
|
|
|
}
|
|
|
|
|