1#!./miniruby
2
3show = false
4precommand = []
5while arg = ARGV[0]
6  break ARGV.shift if arg == '--'
7  /\A--([-\w]+)(?:=(.*))?\z/ =~ arg or break
8  arg, value = $1, $2
9  re = Regexp.new('\A'+arg.gsub(/\w+\b/, '\&\\w*')+'\z', "i")
10  case
11  when re =~ "srcdir"
12    srcdir = value
13  when re =~ "archdir"
14    archdir = value
15  when re =~ "cpu"
16    precommand << "arch" << "-arch" << value
17  when re =~ "extout"
18    extout = value
19  when re =~ "pure"
20    # obsolete switch do nothing
21  when re =~ "debugger"
22    require 'shellwords'
23    precommand.concat(value ? (Shellwords.shellwords(value) unless value == "no") : %w"gdb --args")
24  when re =~ "precommand"
25    require 'shellwords'
26    precommand.concat(Shellwords.shellwords(value))
27  when re =~ "show"
28    show = true
29  else
30    break
31  end
32  ARGV.shift
33end
34
35unless defined?(File.realpath)
36  def File.realpath(*args)
37    Dir.chdir(expand_path(*args)) do
38      Dir.pwd
39    end
40  end
41end
42
43srcdir ||= File.realpath('..', File.dirname(__FILE__))
44archdir ||= '.'
45
46abs_archdir = File.expand_path(archdir)
47$:.unshift(abs_archdir)
48
49config = File.read(conffile = File.join(abs_archdir, 'rbconfig.rb'))
50config.sub!(/^(\s*)RUBY_VERSION\s*==.*(\sor\s*)$/, '\1true\2')
51config = Module.new {module_eval(config, conffile)}::RbConfig::CONFIG
52
53ruby = File.join(archdir, config["RUBY_INSTALL_NAME"]+config['EXEEXT'])
54unless File.exist?(ruby)
55  abort "#{ruby} is not found.\nTry `make' first, then `make test', please.\n"
56end
57
58libs = [abs_archdir]
59extout ||= config["EXTOUT"]
60if extout
61  abs_extout = File.expand_path(extout, abs_archdir)
62  libs << File.expand_path("common", abs_extout) << File.expand_path(config['arch'], abs_extout)
63end
64libs << File.expand_path("lib", srcdir)
65config["bindir"] = abs_archdir
66
67env = {}
68
69env["RUBY"] = File.expand_path(ruby)
70env["PATH"] = [abs_archdir, ENV["PATH"]].compact.join(File::PATH_SEPARATOR)
71
72if e = ENV["RUBYLIB"]
73  libs |= e.split(File::PATH_SEPARATOR)
74end
75env["RUBYLIB"] = $:.replace(libs).join(File::PATH_SEPARATOR)
76
77libruby_so = File.join(abs_archdir, config['LIBRUBY_SO'])
78if File.file?(libruby_so)
79  if e = config['LIBPATHENV'] and !e.empty?
80    env[e] = [abs_archdir, ENV[e]].compact.join(File::PATH_SEPARATOR)
81  end
82  if /linux/ =~ RUBY_PLATFORM
83    env["LD_PRELOAD"] = [libruby_so, ENV["LD_PRELOAD"]].compact.join(' ')
84  end
85end
86
87ENV.update env
88
89cmd = [ruby]
90cmd.concat(ARGV)
91cmd.unshift(*precommand) unless precommand.empty?
92
93if show
94  require 'shellwords'
95  env.each {|k,v| puts "#{k}=#{v}"}
96  puts Shellwords.join(cmd)
97end
98
99exec(*cmd)
100