Skip to content

Commit 71a8048

Browse files
committed
feat: support RBE
This is known to work for JRuby only at the moment.
1 parent 22051a1 commit 71a8048

File tree

10 files changed

+39
-18
lines changed

10 files changed

+39
-18
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ On Windows, [RubyInstaller][6] is used to install MRI.
122122

123123
On all operating systems, JRuby is downloaded manually.
124124
It uses Bazel runtime Java toolchain as JDK.
125+
JRuby is currently the only toolchain that supports [Remote Build Execution][15].
125126

126127
### TruffleRuby
127128

@@ -161,3 +162,4 @@ However, some are known not to work or work only partially (e.g. mRuby has no bu
161162
[12]: https://github.com/rubocop/rubocop/pull/12062
162163
[13]: https://github.com/bazel-contrib/rules_ruby/releases/tag/v0.3.0
163164
[14]: examples/
165+
[15]: https://bazel.build/remote/rbe

ruby/private/binary.bzl

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ load(
1818
_convert_env_to_script = "convert_env_to_script",
1919
_is_windows = "is_windows",
2020
_normalize_path = "normalize_path",
21+
_to_rlocation_path = "to_rlocation_path",
2122
)
2223

2324
ATTRS = {
@@ -66,16 +67,15 @@ def generate_rb_binary_script(ctx, binary, bundler = False, args = [], env = {},
6667

6768
environment = {}
6869
environment.update(env)
70+
6971
if _is_windows(ctx):
7072
rlocation_function = BATCH_RLOCATION_FUNCTION
7173
script = ctx.actions.declare_file("{}.rb.cmd".format(ctx.label.name))
7274
template = ctx.file._binary_cmd_tpl
73-
environment.update({"PATH": _normalize_path(ctx, toolchain.bindir) + ";%PATH%"})
7475
else:
7576
rlocation_function = BASH_RLOCATION_FUNCTION
7677
script = ctx.actions.declare_file("{}.rb.sh".format(ctx.label.name))
7778
template = ctx.file._binary_sh_tpl
78-
environment.update({"PATH": "%s:$PATH" % toolchain.bindir})
7979

8080
if bundler:
8181
bundler_command = "bundle exec"
@@ -94,6 +94,7 @@ def generate_rb_binary_script(ctx, binary, bundler = False, args = [], env = {},
9494
"{binary}": _normalize_path(ctx, binary_path),
9595
"{env}": _convert_env_to_script(ctx, environment),
9696
"{bundler_command}": bundler_command,
97+
"{ruby}": _to_rlocation_path(toolchain.ruby),
9798
"{ruby_binary_name}": toolchain.ruby.basename,
9899
"{java_bin}": java_bin,
99100
"{rlocation_function}": rlocation_function,
@@ -114,7 +115,8 @@ def rb_binary_impl(ctx):
114115
transitive_srcs = get_transitive_srcs(ctx.files.srcs, ctx.attr.deps).to_list()
115116

116117
ruby_toolchain = ctx.toolchains["@rules_ruby//ruby:toolchain_type"]
117-
tools = [ruby_toolchain.ruby, ruby_toolchain.bundle, ruby_toolchain.gem, ctx.file._runfiles_library]
118+
tools = [ctx.file._runfiles_library]
119+
tools.extend(ruby_toolchain.files)
118120

119121
if ruby_toolchain.version.startswith("jruby"):
120122
java_toolchain = ctx.toolchains["@bazel_tools//tools/jdk:runtime_toolchain_type"]
@@ -133,8 +135,8 @@ def rb_binary_impl(ctx):
133135

134136
# See https://bundler.io/v2.5/man/bundle-config.1.html for confiugration keys.
135137
env.update({
136-
"BUNDLE_GEMFILE": info.gemfile.short_path.removeprefix("../"),
137-
"BUNDLE_PATH": info.path.short_path.removeprefix("../"),
138+
"BUNDLE_GEMFILE": _to_rlocation_path(info.gemfile),
139+
"BUNDLE_PATH": _to_rlocation_path(info.path),
138140
})
139141

140142
bundle_env = get_bundle_env(ctx.attr.env, ctx.attr.deps)
@@ -160,7 +162,7 @@ def rb_binary_impl(ctx):
160162
runfiles = runfiles,
161163
),
162164
RubyFilesInfo(
163-
transitive_data = depset(transitive_data),
165+
transitive_data = depset(transitive_data + tools),
164166
transitive_deps = depset(transitive_deps),
165167
transitive_srcs = depset(transitive_srcs),
166168
bundle_env = bundle_env,

ruby/private/binary/binary.cmd.tpl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ setlocal enableextensions enabledelayedexpansion
44
set RUNFILES_MANIFEST_ONLY=1
55
{rlocation_function}
66

7+
:: Find location of Ruby in runfiles.
8+
call :rlocation {ruby} ruby
9+
for %%a in ("!ruby!\..") do set PATH=%%~fa;%PATH%
10+
711
:: Find location of JAVA_HOME in runfiles.
812
if "{java_bin}" neq "" (
913
call :rlocation {java_bin} java_bin
@@ -13,6 +17,7 @@ if "{java_bin}" neq "" (
1317
:: Set environment variables.
1418
{env}
1519

20+
:: Find location of Bundle path in runfiles.
1621
if "{bundler_command}" neq "" (
1722
call :rlocation "!BUNDLE_GEMFILE!" BUNDLE_GEMFILE
1823
call :rlocation "!BUNDLE_PATH!" BUNDLE_PATH

ruby/private/binary/binary.sh.tpl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ realpath() (
1818

1919
export RUNFILES_DIR="$(realpath "${RUNFILES_DIR:-$0.runfiles}")"
2020

21+
# Find location of Ruby in runfiles.
22+
export PATH=$(dirname $(rlocation {ruby})):$PATH
23+
2124
# Find location of JAVA_HOME in runfiles.
2225
if [ -n "{java_bin}" ]; then
2326
export JAVA_HOME=$(dirname $(dirname $(rlocation "{java_bin}")))
@@ -26,6 +29,7 @@ fi
2629
# Set environment variables.
2730
{env}
2831

32+
# Find location of Bundle path in runfiles.
2933
if [ -n "{bundler_command}" ]; then
3034
export BUNDLE_GEMFILE=$(rlocation $BUNDLE_GEMFILE)
3135
export BUNDLE_PATH=$(rlocation $BUNDLE_PATH)

ruby/private/bundle_install.bzl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ load(
1111
def _rb_bundle_install_impl(ctx):
1212
toolchain = ctx.toolchains["@rules_ruby//ruby:toolchain_type"]
1313

14-
tools = [toolchain.ruby, toolchain.bundle]
14+
tools = []
15+
tools.extend(toolchain.files)
1516
bundler_exe = toolchain.bundle.path
1617

1718
for gem in ctx.attr.gems:
@@ -35,11 +36,11 @@ def _rb_bundle_install_impl(ctx):
3536
if _is_windows(ctx):
3637
script = ctx.actions.declare_file("bundle_install_{}.cmd".format(ctx.label.name))
3738
template = ctx.file._bundle_install_cmd_tpl
38-
env.update({"PATH": _normalize_path(ctx, toolchain.bindir) + ";%PATH%"})
39+
env.update({"PATH": _normalize_path(ctx, toolchain.ruby.dirname) + ";%PATH%"})
3940
else:
4041
script = ctx.actions.declare_file("bundle_install_{}.sh".format(ctx.label.name))
4142
template = ctx.file._bundle_install_sh_tpl
42-
env.update({"PATH": "%s:$PATH" % toolchain.bindir})
43+
env.update({"PATH": "%s:$PATH" % toolchain.ruby.dirname})
4344

4445
# Calculate relative location between BUNDLE_GEMFILE and BUNDLE_PATH.
4546
relative_dir = "../../"

ruby/private/download.bzl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ def _rb_download_impl(repository_ctx):
4545
repository_ctx.attr._build_tpl,
4646
executable = False,
4747
substitutions = {
48-
"{bindir}": repr(repository_ctx.path("dist/bin")),
4948
"{version}": version,
5049
"{ruby_binary_name}": ruby_binary_name,
5150
"{gem_binary_name}": gem_binary_name,

ruby/private/download/BUILD.tpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ filegroup(
2828

2929
rb_toolchain(
3030
name = "toolchain",
31-
bindir = "{bindir}",
3231
bundle = ":bundle",
3332
env = {env},
3433
gem = ":gem",
3534
ruby = ":ruby",
3635
version = "{version}",
36+
files = glob(["dist/**/*"]),
3737
)
3838

3939
# vim: ft=bzl

ruby/private/gem_install.bzl

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ def _rb_gem_install_impl(ctx):
1515

1616
env = {}
1717
env.update(toolchain.env)
18-
tools = [toolchain.gem]
18+
19+
tools = []
20+
tools.extend(toolchain.files)
21+
1922
if toolchain.version.startswith("jruby"):
2023
java_toolchain = ctx.toolchains["@bazel_tools//tools/jdk:runtime_toolchain_type"]
2124
tools.extend(java_toolchain.java_runtime.files.to_list())
@@ -24,11 +27,11 @@ def _rb_gem_install_impl(ctx):
2427
if _is_windows(ctx):
2528
gem_install = ctx.actions.declare_file("gem_install_{}.cmd".format(ctx.label.name))
2629
template = ctx.file._gem_install_cmd_tpl
27-
env.update({"PATH": _normalize_path(ctx, toolchain.bindir) + ";%PATH%"})
30+
env.update({"PATH": _normalize_path(ctx, toolchain.ruby.dirname) + ";%PATH%"})
2831
else:
2932
gem_install = ctx.actions.declare_file("gem_install_{}.sh".format(ctx.label.name))
3033
template = ctx.file._gem_install_sh_tpl
31-
env.update({"PATH": "%s:$PATH" % toolchain.bindir})
34+
env.update({"PATH": "%s:$PATH" % toolchain.ruby.dirname})
3235

3336
ctx.actions.expand_template(
3437
template = template,

ruby/private/utils.bzl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,3 +139,7 @@ def normalize_bzlmod_repository_name(name):
139139
repository name
140140
"""
141141
return name.rpartition("~")[-1]
142+
143+
def to_rlocation_path(source):
144+
"""Returns source path that can be used with runfiles library."""
145+
return source.short_path.removeprefix("../")

ruby/toolchain.bzl

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ def _rb_toolchain_impl(ctx):
55
ruby = ctx.executable.ruby,
66
bundle = ctx.executable.bundle,
77
gem = ctx.executable.gem,
8-
bindir = ctx.attr.bindir,
98
version = ctx.attr.version,
109
env = ctx.attr.env,
10+
files = ctx.files.files,
1111
)
1212

1313
rb_toolchain = rule(
@@ -31,14 +31,15 @@ rb_toolchain = rule(
3131
executable = True,
3232
cfg = "exec",
3333
),
34-
"bindir": attr.string(
35-
doc = "Path to Ruby bin/ directory",
36-
),
3734
"version": attr.string(
3835
doc = "Ruby version",
3936
),
4037
"env": attr.string_dict(
4138
doc = "Environment variables required by an interpreter",
4239
),
40+
"files": attr.label_list(
41+
allow_files = True,
42+
doc = "All files necessary for toolchain",
43+
),
4344
},
4445
)

0 commit comments

Comments
 (0)