Skip to content

Commit 3131790

Browse files
committed
Run ruby_binary with the interpreter in a SDK again
when the interpreter is not the host interpreter
1 parent 287cdef commit 3131790

File tree

2 files changed

+49
-5
lines changed

2 files changed

+49
-5
lines changed

ruby/private/binary.bzl

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,22 +40,39 @@ def _ruby_binary_impl(ctx):
4040
deps = _transitive_deps(
4141
ctx,
4242
extra_files = interpreter_file_deps + [executable],
43-
extra_deps = interpreter_trans_deps,
43+
extra_deps = interpreter_trans_deps + ctx.attr._misc_deps,
4444
)
4545

4646
rubyopt = reversed(deps.rubyopt.to_list())
47-
47+
intermediate_executable = ctx.actions.declare_file("%s.tpl.intermediate" % ctx.attr.name)
4848
ctx.actions.expand_template(
4949
template = ctx.file._wrapper_template,
50-
output = executable,
50+
output = intermediate_executable,
5151
substitutions = {
52-
"{interpreter}": interpreter.short_path,
5352
"{loadpaths}": repr(deps.incpaths.to_list()),
5453
"{rubyopt}": repr(rubyopt),
5554
"{main}": repr(_to_manifest_path(ctx, main)),
55+
"{interpreter}": _to_manifest_path(ctx, interpreter),
5656
},
57-
is_executable = True,
5857
)
58+
if sdk.is_host:
59+
ctx.actions.run_shell(
60+
inputs = [intermediate_executable],
61+
outputs = [executable],
62+
command = "grep -v '^#shell ' %s > %s" % (
63+
intermediate_executable.path,
64+
executable.path,
65+
),
66+
)
67+
else:
68+
ctx.actions.run_shell(
69+
inputs = [intermediate_executable],
70+
outputs = [executable],
71+
command = "sed 's/^#shell //' %s > %s" % (
72+
intermediate_executable.path,
73+
executable.path,
74+
),
75+
)
5976
return [DefaultInfo(
6077
executable = executable,
6178
default_runfiles = deps.default_files,
@@ -81,6 +98,10 @@ _ATTRS = {
8198
allow_single_file = True,
8299
default = "binary_wrapper.tpl",
83100
),
101+
"_misc_deps": attr.label_list(
102+
allow_files = True,
103+
default = ["@bazel_tools//tools/bash/runfiles"],
104+
),
84105
}
85106

86107
ruby_binary = rule(

ruby/private/binary_wrapper.tpl

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
#shell #!/usr/bin/env bash
2+
#shell # This conditional is evaluated as true in Ruby, false in shell
3+
#shell if [ ]; then
4+
#shell eval <<'END_OF_RUBY'
5+
#shell # -- begin Ruby --
16
#!/usr/bin/env ruby
27

38
# Ruby-port of the Bazel's wrapper script for Python
@@ -124,3 +129,21 @@ end
124129
if __FILE__ == $0
125130
main(ARGV)
126131
end
132+
#shell END_OF_RUBY
133+
#shell __END__
134+
#shell # -- end Ruby --
135+
#shell fi
136+
#shell # -- begin Shell Script --
137+
#shell
138+
#shell # --- begin runfiles.bash initialization v2 ---
139+
#shell # Copy-pasted from the Bazel Bash runfiles library v2.
140+
#shell set -uo pipefail; f=bazel_tools/tools/bash/runfiles/runfiles.bash
141+
#shell source "${RUNFILES_DIR:-/dev/null}/$f" 2>/dev/null || \
142+
#shell source "$(grep -sm1 "^$f " "${RUNFILES_MANIFEST_FILE:-/dev/null}" | cut -f2- -d' ')" 2>/dev/null || \
143+
#shell source "$0.runfiles/$f" 2>/dev/null || \
144+
#shell source "$(grep -sm1 "^$f " "$0.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
145+
#shell source "$(grep -sm1 "^$f " "$0.exe.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
146+
#shell { echo>&2 "ERROR: cannot find $f"; exit 1; }; f=; set -e
147+
#shell # --- end runfiles.bash initialization v2 ---
148+
#shell
149+
#shell exec "$(rlocation {interpreter})" ${BASH_SOURCE:-$0} "$@"

0 commit comments

Comments
 (0)