|
23 | 23 | # limitations under the License.
|
24 | 24 | #
|
25 | 25 |
|
| 26 | +require 'rbconfig' |
| 27 | +require 'shellwords' |
| 28 | + |
| 29 | +def find_runfiles |
| 30 | + stub_filename = File.absolute_path($0) |
| 31 | + runfiles = "#{stub_filename}.runfiles" |
| 32 | + loop do |
| 33 | + case |
| 34 | + when File.directory?(runfiles) |
| 35 | + return runfiles |
| 36 | + when %r!(.*\.runfiles)/.*!o =~ stub_filename |
| 37 | + return $1 |
| 38 | + when File.symlink?(stub_filename) |
| 39 | + target = File.readlink(stub_filename) |
| 40 | + stub_filename = File.absolute_path(target, File.dirname(stub_filename)) |
| 41 | + else |
| 42 | + break |
| 43 | + end |
| 44 | + end |
| 45 | + raise "Cannot find .runfiles directory for #{$0}" |
| 46 | +end |
| 47 | + |
| 48 | +def create_loadpath_entries(custom, runfiles) |
| 49 | + [runfiles] + custom.map {|path| File.join(runfiles, path) } |
| 50 | +end |
| 51 | + |
| 52 | +def get_repository_imports(runfiles) |
| 53 | + Dir.children(runfiles).map {|d| |
| 54 | + File.join(runfiles, d) |
| 55 | + }.select {|d| |
| 56 | + File.directory? d |
| 57 | + } |
| 58 | +end |
| 59 | + |
| 60 | +# Finds the runfiles manifest or the runfiles directory. |
| 61 | +def runfiles_envvar(runfiles) |
| 62 | + # If this binary is the data-dependency of another one, the other sets |
| 63 | + # RUNFILES_MANIFEST_FILE or RUNFILES_DIR for our sake. |
| 64 | + manifest = ENV['RUNFILES_MANIFEST_FILE'] |
| 65 | + if manifest |
| 66 | + return ['RUNFILES_MANIFEST_FILE', manifest] |
| 67 | + end |
| 68 | + |
| 69 | + dir = ENV['RUNFILES_DIR'] |
| 70 | + if dir |
| 71 | + return ['RUNFILES_DIR', dir] |
| 72 | + end |
| 73 | + |
| 74 | + # Look for the runfiles "output" manifest, argv[0] + ".runfiles_manifest" |
| 75 | + manifest = runfiles + '_manifest' |
| 76 | + if File.exists?(manifest) |
| 77 | + return ['RUNFILES_MANIFEST_FILE', manifest] |
| 78 | + end |
| 79 | + |
| 80 | + # Look for the runfiles "input" manifest, argv[0] + ".runfiles/MANIFEST" |
| 81 | + manifest = File.join(runfiles, 'MANIFEST') |
| 82 | + if File.exists?(manifest) |
| 83 | + return ['RUNFILES_DIR', manifest] |
| 84 | + end |
| 85 | + |
| 86 | + # If running in a sandbox and no environment variables are set, then |
| 87 | + # Look for the runfiles next to the binary. |
| 88 | + if runfiles.end_with?('.runfiles') and File.directory?(runfiles) |
| 89 | + return ['RUNFILES_DIR', runfiles] |
| 90 | + end |
| 91 | +end |
| 92 | + |
| 93 | +def find_ruby_program |
| 94 | + File.join( |
| 95 | + RbConfig::CONFIG['bindir'], |
| 96 | + RbConfig::CONFIG['ruby_install_name'], |
| 97 | + ) |
| 98 | +end |
| 99 | + |
| 100 | +def expand_vars(args) |
| 101 | + args.map do |arg| |
| 102 | + arg.gsub(/\${(.+?)}/o) do |
| 103 | + case $1 |
| 104 | + when 'RUNFILES_DIR' |
| 105 | + runfiles |
| 106 | + else |
| 107 | + ENV[$1] |
| 108 | + end |
| 109 | + end |
| 110 | + end |
| 111 | +end |
| 112 | + |
26 | 113 | def main(args)
|
27 | 114 | custom_loadpaths = {loadpaths}
|
28 |
| - rubyopt = {rubyopt} |
| 115 | + runfiles = find_runfiles |
| 116 | + |
| 117 | + loadpaths = create_loadpath_entries(custom_loadpaths, runfiles) |
| 118 | + loadpaths += get_repository_imports(runfiles) |
| 119 | + loadpaths += ENV['RUBYLIB'].split(':') if ENV.key?('RUBYLIB') |
| 120 | + ENV['RUBYLIB'] = loadpaths.join(':') |
| 121 | + |
| 122 | + runfiles_envkey, runfiles_envvalue = runfiles_envvar(runfiles) |
| 123 | + ENV[runfiles_envkey] = runfiles_envvalue if runfiles_envkey |
| 124 | + |
| 125 | + program_name = {program} |
| 126 | + if program_name |
| 127 | + program = File.join(runfiles, program_name) |
| 128 | + else |
| 129 | + program = find_ruby_program |
| 130 | + end |
| 131 | + program_opts = expand_vars({program_opts}) |
| 132 | + |
29 | 133 | main = {main}
|
| 134 | + main = File.join(runfiles, main) |
| 135 | + rubyopt = expand_vars({rubyopt}) |
| 136 | + ENV['RUBYOPT'] = Shellwords.join(expand_vars({rubyopt})) |
30 | 137 |
|
31 |
| - Runfiles.new(custom_loadpaths, rubyopt).exec(main, *args) |
| 138 | + exec(program, *program_opts, main, *args) |
32 | 139 | # TODO(yugui) Support windows
|
33 | 140 | end
|
34 | 141 |
|
|
52 | 159 | #shell { echo>&2 "ERROR: cannot find $f"; exit 1; }; f=; set -e
|
53 | 160 | #shell # --- end runfiles.bash initialization v2 ---
|
54 | 161 | #shell
|
55 |
| -#shell exec "$(rlocation {interpreter})" -r"$(rlocation @bazelruby_ruby_rules//ruby/private/tools:runfiles.rb)" ${BASH_SOURCE:-$0} "$@" |
| 162 | +#shell exec "$(rlocation {interpreter})" ${BASH_SOURCE:-$0} "$@" |
0 commit comments