ruby/tool/runruby.rb

177 строки
4.6 KiB
Ruby
Исходник Обычный вид История

#!./miniruby
# Used by "make runruby", configure, and by hand to run a locally-built Ruby
# with correct environment variables and arguments.
show = false
precommand = []
srcdir = File.realpath('..', File.dirname(__FILE__))
case
when ENV['RUNRUBY_USE_GDB'] == 'true'
debugger = :gdb
when ENV['RUNRUBY_USE_LLDB'] == 'true'
debugger = :lldb
end
while arg = ARGV[0]
break ARGV.shift if arg == '--'
case arg
when '-C', /\A-C(.+)/m
ARGV.shift
Dir.chdir($1 || ARGV.shift)
next
end
/\A--([-\w]+)(?:=(.*))?\z/ =~ arg or break
arg, value = $1, $2
re = Regexp.new('\A'+arg.gsub(/\w+\b/, '\&\\w*')+'\z', "i")
case
when re =~ "srcdir"
srcdir = value
when re =~ "archdir"
archdir = value
when re =~ "cpu"
precommand << "arch" << "-arch" << value
when re =~ "extout"
extout = value
when re =~ "pure"
# obsolete switch do nothing
when re =~ "debugger"
require 'shellwords'
case value
when nil
debugger = :gdb
when "lldb"
debugger = :lldb
when "no"
else
debugger = Shellwords.shellwords(value)
end and precommand |= [:debugger]
when re =~ "precommand"
require 'shellwords'
precommand.concat(Shellwords.shellwords(value))
when re =~ "show"
show = true
when re =~ "chdir"
Dir.chdir(value)
else
break
end
ARGV.shift
end
unless defined?(File.realpath)
def File.realpath(*args)
path = expand_path(*args)
if File.stat(path).directory?
Dir.chdir(path) {Dir.pwd}
else
dir, base = File.split(path)
File.join(Dir.chdir(dir) {Dir.pwd}, base)
end
end
end
begin
conffile = File.realpath('rbconfig.rb', archdir)
rescue Errno::ENOENT => e
# retry if !archdir and ARGV[0] and File.directory?(archdir = ARGV.shift)
abort "#$0: rbconfig.rb not found, use --archdir option"
end
abs_archdir = File.dirname(conffile)
archdir ||= abs_archdir
$:.unshift(abs_archdir)
config = File.read(conffile)
config.sub!(/^(\s*)RUBY_VERSION\b.*(\sor\s*)\n.*\n/, '')
config = Module.new {module_eval(config, conffile)}::RbConfig::CONFIG
install_name = config["RUBY_INSTALL_NAME"]+config['EXEEXT']
ruby = File.join(archdir, install_name)
unless File.exist?(ruby)
abort "#{ruby} is not found.\nTry `make' first, then `make test', please.\n"
end
libs = [abs_archdir]
extout ||= config["EXTOUT"]
if extout
abs_extout = File.expand_path(extout, abs_archdir)
libs << File.expand_path("common", abs_extout) << File.expand_path(config['arch'], abs_extout)
end
libs << File.expand_path("lib", srcdir)
config["bindir"] = abs_archdir
env = {
# Test with the smallest possible machine stack sizes.
# These values are clamped to machine-dependent minimum values in vm_core.h
'RUBY_THREAD_MACHINE_STACK_SIZE' => '1',
'RUBY_FIBER_MACHINE_STACK_SIZE' => '1',
}
runner = File.join(abs_archdir, "exe/#{install_name}")
runner = nil unless File.exist?(runner)
abs_ruby = runner || File.expand_path(ruby)
env["RUBY"] = abs_ruby
env["GEM_PATH"] = env["GEM_HOME"] = File.expand_path(".bundle", srcdir)
Fix some bundler specs (#2380) * These seem to consistenly pass already * Show actual command when running `make test-bundler` Current the setup command that installs the necessary gems for testing bundler was printed, but not the actual command that runs the tests. That was a bit confusing. * Borrow trick from setproctitle specs * A title that long doesn't get set sometimes No idea why, but the test doesn't need that the title is that long. * Fix most gem helper spec ruby-core failures * Fix the rest of the gem helper failures * Fix version spec by improving the assertion * Remove unnecessary `BUNDLE_RUBY` environment var We can use `RUBY` when necessary, and `BUNDLE_RUBY` is not a good name because bundler considers `BUNDLE_*` variables as settings. * Rename `BUNDLE_GEM` to `GEM_COMMAND` This is more descriptive I think, and also friendlier for bundler because `BUNDLE_` env variables are interpreted by bundler as settings, and this is not a bundler setting. This fixes one bundler spec failure in config specs against ruby-core. * Fix quality spec when run in core Use the proper path helper. * Fix dummy lib builder to never load default gems If a dummy library is named as a default gem, when requiring the library from its executable, the default gem would be loaded when running from core, because in core all default gems share path with bundler, and thus they are always in the $LOAD_PATH. We fix the issue by loading lib relatively inside dummy lib executables. * More exact assertions Sometimes I have the problem that I do some "print debugging" inside specs, and suddently the spec passes. This happens when the assertion is too relaxed, and the things I print make it match, specially when they are simple strings like "1.0" than can be easily be part of gem paths that I print for debugging. I fix this by making a more exact assertion. * Detect the correct shebang when ENV["RUBY"] is set * Relax assertion So that the spec passes even if another paths containing "ext" are in the load path. This works to fix a ruby-core issue, but it's a better assertion in general. We just want to know that the extension path was added. * Use folder structure independent path helper It should fix this spec for ruby-core. * Fix the last failing spec on ruby-core * Skip `bundle open <default_gem>` spec when no default gems
2019-08-20 03:46:31 +03:00
env["GEM_COMMAND"] = "#{abs_ruby} -rrubygems #{srcdir}/bin/gem --backtrace"
env["PATH"] = [File.dirname(abs_ruby), abs_archdir, ENV["PATH"]].compact.join(File::PATH_SEPARATOR)
if e = ENV["RUBYLIB"]
libs |= e.split(File::PATH_SEPARATOR)
end
env["RUBYLIB"] = $:.replace(libs).join(File::PATH_SEPARATOR)
libruby_so = File.join(abs_archdir, config['LIBRUBY_SO'])
if File.file?(libruby_so)
if e = config['LIBPATHENV'] and !e.empty?
env[e] = [abs_archdir, ENV[e]].compact.join(File::PATH_SEPARATOR)
end
unless runner
if e = config['PRELOADENV']
e = nil if e.empty?
e ||= "LD_PRELOAD" if /linux/ =~ RUBY_PLATFORM
end
if e
env[e] = [libruby_so, ENV[e]].compact.join(File::PATH_SEPARATOR)
end
end
end
ENV.update env
if debugger
case debugger
when :gdb, nil
debugger = %W'gdb -x #{srcdir}/.gdbinit'
if File.exist?(gdb = 'run.gdb') or
File.exist?(gdb = File.join(abs_archdir, 'run.gdb'))
debugger.push('-x', gdb)
end
debugger << '--args'
when :lldb
debugger = ['lldb', '-O', "command script import #{srcdir}/misc/lldb_cruby.py"]
if File.exist?(lldb = 'run.lldb') or
File.exist?(lldb = File.join(abs_archdir, 'run.lldb'))
debugger.push('-s', lldb)
end
debugger << '--'
end
if idx = precommand.index(:debugger)
precommand[idx, 1] = debugger
else
precommand.concat(debugger)
end
end
cmd = [runner || ruby]
cmd.concat(ARGV)
cmd.unshift(*precommand) unless precommand.empty?
if show
require 'shellwords'
env.each {|k,v| puts "#{k}=#{v}"}
puts Shellwords.join(cmd)
end
exec(*cmd, close_others: false)