1 # This file is interpreted by $(BASERUBY) and miniruby.
2 # $(BASERUBY) is used for miniprelude.c.
3 # miniruby is used for prelude.c.
4 # Since $(BASERUBY) may be older than Ruby 1.9,
5 # Ruby 1.9 feature should not be used.
7 $:.unshift(File.expand_path("../..", __FILE__))
10 outfile = preludes.pop
11 init_name = outfile[/\w+(?=_prelude.c\b)/] || 'prelude'
19 0x00.upto(0x1f) {|ch| C_ESC[[ch].pack("C")] ||= "\\%03o" % ch }
20 0x7f.upto(0xff) {|ch| C_ESC[[ch].pack("C")] = "\\%03o" % ch }
21 C_ESC_PAT = Regexp.union(*C_ESC.keys)
24 '"' + str.gsub(C_ESC_PAT) { C_ESC[$&] } + '"'
28 setup_ruby_prefix = nil
29 teardown_ruby_prefix = nil
30 lines_list = preludes.map {|filename|
32 need_ruby_prefix = false
33 File.readlines(filename).each {|line|
34 line.gsub!(/RbConfig::CONFIG\["(\w+)"\]/) {
38 mkconf = RbConfig::MAKEFILE_CONFIG.merge('prefix'=>'#{TMP_RUBY_PREFIX}')
39 setup_ruby_prefix = "TMP_RUBY_PREFIX = $:.reverse.find{|e|e!=\".\"}.sub(%r{(.*)/lib/.*}m, \"\\\\1\")\n"
40 teardown_ruby_prefix = 'Object.class_eval { remove_const "TMP_RUBY_PREFIX" }'
42 if RbConfig::MAKEFILE_CONFIG.has_key? key
43 val = RbConfig.expand("$(#{key})", mkconf)
44 need_ruby_prefix = true if /\A\#\{TMP_RUBY_PREFIX\}/ =~ val
54 setup_lines << c_esc(setup_ruby_prefix)
55 lines << c_esc(teardown_ruby_prefix)
62 tmp = ERB.new(<<'EOS', nil, '%').result(binding)
63 #include "ruby/ruby.h"
66 % preludes.zip(lines_list).each_with_index {|(prelude, (setup_lines, lines)), i|
67 static const char prelude_name<%=i%>[] = <%=c_esc(File.basename(prelude))%>;
68 static const char prelude_code<%=i%>[] =
69 % (setup_lines+lines).each {|line|
76 Init_<%=init_name%>(void)
78 % lines_list.each_with_index {|(setup_lines, lines), i|
79 rb_iseq_eval(rb_iseq_compile(
80 rb_str_new(prelude_code<%=i%>, sizeof(prelude_code<%=i%>) - 1),
81 rb_str_new(prelude_name<%=i%>, sizeof(prelude_name<%=i%>) - 1),
82 INT2FIX(<%=1-setup_lines.length%>)));
86 % preludes.length.times {|i|
87 puts(prelude_code<%=i%>);
93 open(outfile, 'w'){|f|