|
1 | | -#!/bin/sh |
2 | | - |
3 | | -if [ -n "${RUNFILES_DIR+x}" ]; then |
4 | | - PATH_PREFIX=$RUNFILES_DIR/{workspace_name}/ |
5 | | -elif [ -s `dirname $0`/../../MANIFEST ]; then |
6 | | - PATH_PREFIX=`cd $(dirname $0); pwd`/ |
7 | | -elif [ -d $0.runfiles ]; then |
8 | | - PATH_PREFIX=`cd $0.runfiles; pwd`/{workspace_name}/ |
9 | | -else |
10 | | - echo "WARNING: it does not look to be at the .runfiles directory" >&2 |
11 | | - exit 1 |
12 | | -fi |
13 | | - |
14 | | -$PATH_PREFIX{interpreter} --disable-gems {init_flags} {rubyopt} -I${PATH_PREFIX} {main} "$@" |
| 1 | +#!/usr/bin/env ruby |
| 2 | + |
| 3 | +# Ruby-port of the Bazel's wrapper script for Python |
| 4 | + |
| 5 | +# Copyright 2017 The Bazel Authors. All rights reserved. |
| 6 | +# Copyright 2019 BazelRuby Authors. All rights reserved. |
| 7 | +# |
| 8 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | +# you may not use this file except in compliance with the License. |
| 10 | +# You may obtain a copy of the License at |
| 11 | +# |
| 12 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | +# |
| 14 | +# Unless required by applicable law or agreed to in writing, software |
| 15 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | +# See the License for the specific language governing permissions and |
| 18 | +# limitations under the License. |
| 19 | +# |
| 20 | + |
| 21 | +require 'rbconfig' |
| 22 | + |
| 23 | +def find_runfiles |
| 24 | + stub_filename = File.absolute_path($0) |
| 25 | + runfiles = "#{stub_filename}.runfiles" |
| 26 | + loop do |
| 27 | + case |
| 28 | + when File.directory?(runfiles) |
| 29 | + return runfiles |
| 30 | + when %r!(.*\.runfiles)/.*!o =~ stub_filename |
| 31 | + return $1 |
| 32 | + when File.symlink?(stub_filename) |
| 33 | + target = File.readlink(stub_filename) |
| 34 | + stub_filename = File.absolute_path(target, File.dirname(stub_filename)) |
| 35 | + else |
| 36 | + break |
| 37 | + end |
| 38 | + end |
| 39 | + raise "Cannot find .runfiles directory for #{$0}" |
| 40 | +end |
| 41 | + |
| 42 | +def create_loadpath_entries(custom, runfiles) |
| 43 | + [runfiles] + custom.map {|path| File.join(runfiles, path) } |
| 44 | +end |
| 45 | + |
| 46 | +def get_repository_imports(runfiles) |
| 47 | + Dir.children(runfiles).map {|d| |
| 48 | + File.join(runfiles, d) |
| 49 | + }.select {|d| |
| 50 | + File.directory? d |
| 51 | + } |
| 52 | +end |
| 53 | + |
| 54 | +# Finds the runfiles manifest or the runfiles directory. |
| 55 | +def runfiles_envvar(runfiles) |
| 56 | + # If this binary is the data-dependency of another one, the other sets |
| 57 | + # RUNFILES_MANIFEST_FILE or RUNFILES_DIR for our sake. |
| 58 | + manifest = ENV['RUNFILES_MANIFEST_FILE'] |
| 59 | + if manifest |
| 60 | + return ['RUNFILES_MANIFEST_FILE', manifest] |
| 61 | + end |
| 62 | + |
| 63 | + dir = ENV['RUNFILES_DIR'] |
| 64 | + if dir |
| 65 | + return ['RUNFILES_DIR', dir] |
| 66 | + end |
| 67 | + |
| 68 | + # Look for the runfiles "output" manifest, argv[0] + ".runfiles_manifest" |
| 69 | + manifest = runfiles + '_manifest' |
| 70 | + if File.exists?(manifest) |
| 71 | + return ['RUNFILES_MANIFEST_FILE', manifest] |
| 72 | + end |
| 73 | + |
| 74 | + # Look for the runfiles "input" manifest, argv[0] + ".runfiles/MANIFEST" |
| 75 | + manifest = File.join(runfiles, 'MANIFEST') |
| 76 | + if File.exists?(manifest) |
| 77 | + return ['RUNFILES_DIR', manifest] |
| 78 | + end |
| 79 | + |
| 80 | + # If running in a sandbox and no environment variables are set, then |
| 81 | + # Look for the runfiles next to the binary. |
| 82 | + if runfiles.end_with?('.runfiles') and File.directory?(runfiles) |
| 83 | + return ['RUNFILES_DIR', runfiles] |
| 84 | + end |
| 85 | +end |
| 86 | + |
| 87 | +def find_ruby_binary |
| 88 | + File.join( |
| 89 | + RbConfig::CONFIG['bindir'], |
| 90 | + RbConfig::CONFIG['ruby_install_name'], |
| 91 | + ) |
| 92 | +end |
| 93 | + |
| 94 | +def main(args) |
| 95 | + custom_loadpaths = {loadpaths} |
| 96 | + runfiles = find_runfiles |
| 97 | + |
| 98 | + loadpaths = create_loadpath_entries(custom_loadpaths, runfiles) |
| 99 | + loadpaths += get_repository_imports(runfiles) |
| 100 | + loadpaths += ENV['RUBYLIB'].split(':') if ENV.key?('RUBYLIB') |
| 101 | + ENV['RUBYLIB'] = loadpaths.join(':') |
| 102 | + |
| 103 | + runfiles_envkey, runfiles_envvalue = runfiles_envvar(runfiles) |
| 104 | + ENV[runfiles_envkey] = runfiles_envvalue if runfiles_envkey |
| 105 | + |
| 106 | + ruby_program = find_ruby_binary |
| 107 | + |
| 108 | + main = {main} |
| 109 | + main = File.join(runfiles, main) |
| 110 | + rubyopt = {rubyopt}.map do |opt| |
| 111 | + opt.gsub(/\${(.+?)}/o) do |
| 112 | + case $1 |
| 113 | + when 'RUNFILES_DIR' |
| 114 | + runfiles |
| 115 | + else |
| 116 | + ENV[$1] |
| 117 | + end |
| 118 | + end |
| 119 | + end |
| 120 | + exec(ruby_program, '--disable-gems', *rubyopt, main, *args) |
| 121 | + # TODO(yugui) Support windows |
| 122 | +end |
| 123 | + |
| 124 | +if __FILE__ == $0 |
| 125 | + main(ARGV) |
| 126 | +end |
0 commit comments