# This file is interpreted by $(BASERUBY) and miniruby. # $(BASERUBY) is used for miniprelude.c. # miniruby is used for prelude.c. # Since $(BASERUBY) may be older than Ruby 1.9, # Ruby 1.9 feature should not be used. preludes = ARGV.dup outfile = preludes.pop 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 mkconf = nil setup_ruby_prefix = nil lines_list = preludes.map {|filename| lines = [] need_ruby_prefix = false File.readlines(filename).each {|line| line.gsub!(/RbConfig::CONFIG\["(\w+)"\]/) { unless mkconf require 'rbconfig' mkconf = RbConfig::MAKEFILE_CONFIG.merge('prefix'=>'#{ruby_prefix}') exlen = $:.grep(%r{\A/}).last.length - RbConfig::CONFIG["prefix"].length setup_ruby_prefix = "ruby_prefix = $:.grep(%r{\\A/}).last[0..#{-exlen-1}]\n" end if RbConfig::MAKEFILE_CONFIG.has_key? $1 val = RbConfig.expand("$(#$1)", mkconf) need_ruby_prefix = true if /\A\#{ruby_prefix\}/ =~ val c_esc(val) else $& end } lines << c_esc(line) } setup_lines = [] if need_ruby_prefix setup_lines << c_esc(setup_ruby_prefix) end [setup_lines, lines] } open(outfile, 'w'){|f| f.puts <<'EOS__' #include "ruby/ruby.h" #include "vm_core.h" EOS__ preludes.zip(lines_list).each_with_index {|(prelude, (setup_lines, lines)), i| f.puts <