Skip to content

Commit

Permalink
feat: support RBE
Browse files Browse the repository at this point in the history
This is known to work for JRuby only at the moment.
  • Loading branch information
p0deje committed Jan 16, 2024
1 parent f68794b commit b1015ea
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 18 deletions.
14 changes: 8 additions & 6 deletions ruby/private/binary.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ load(
_convert_env_to_script = "convert_env_to_script",
_is_windows = "is_windows",
_normalize_path = "normalize_path",
_to_rlocation_path = "to_rlocation_path",
)

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

environment = {}
environment.update(env)

if _is_windows(ctx):
rlocation_function = BATCH_RLOCATION_FUNCTION
script = ctx.actions.declare_file("{}.rb.cmd".format(ctx.label.name))
template = ctx.file._binary_cmd_tpl
environment.update({"PATH": _normalize_path(ctx, toolchain.bindir) + ";%PATH%"})
else:
rlocation_function = BASH_RLOCATION_FUNCTION
script = ctx.actions.declare_file("{}.rb.sh".format(ctx.label.name))
template = ctx.file._binary_sh_tpl
environment.update({"PATH": "%s:$PATH" % toolchain.bindir})

if bundler:
bundler_command = "bundle exec"
Expand All @@ -94,6 +94,7 @@ def generate_rb_binary_script(ctx, binary, bundler = False, args = [], env = {},
"{binary}": _normalize_path(ctx, binary_path),
"{env}": _convert_env_to_script(ctx, environment),
"{bundler_command}": bundler_command,
"{ruby}": _to_rlocation_path(toolchain.ruby),
"{ruby_binary_name}": toolchain.ruby.basename,
"{java_bin}": java_bin,
"{rlocation_function}": rlocation_function,
Expand All @@ -114,7 +115,8 @@ def rb_binary_impl(ctx):
transitive_srcs = get_transitive_srcs(ctx.files.srcs, ctx.attr.deps).to_list()

ruby_toolchain = ctx.toolchains["@rules_ruby//ruby:toolchain_type"]
tools = [ruby_toolchain.ruby, ruby_toolchain.bundle, ruby_toolchain.gem, ctx.file._runfiles_library]
tools = [ctx.file._runfiles_library]
tools.extend(ruby_toolchain.files)

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

# See https://bundler.io/v2.5/man/bundle-config.1.html for confiugration keys.
env.update({
"BUNDLE_GEMFILE": info.gemfile.short_path.removeprefix("../"),
"BUNDLE_PATH": info.path.short_path.removeprefix("../"),
"BUNDLE_GEMFILE": _to_rlocation_path(info.gemfile),
"BUNDLE_PATH": _to_rlocation_path(info.path),
})

bundle_env = get_bundle_env(ctx.attr.env, ctx.attr.deps)
Expand All @@ -160,7 +162,7 @@ def rb_binary_impl(ctx):
runfiles = runfiles,
),
RubyFilesInfo(
transitive_data = depset(transitive_data),
transitive_data = depset(transitive_data + tools),
transitive_deps = depset(transitive_deps),
transitive_srcs = depset(transitive_srcs),
bundle_env = bundle_env,
Expand Down
5 changes: 5 additions & 0 deletions ruby/private/binary/binary.cmd.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ setlocal enableextensions enabledelayedexpansion
set RUNFILES_MANIFEST_ONLY=1
{rlocation_function}

:: Find location of Ruby in runfiles.
call :rlocation {ruby} ruby
for %%a in ("!ruby!\..") do set PATH=%%~fa;%PATH%

:: Find location of JAVA_HOME in runfiles.
if "{java_bin}" neq "" (
call :rlocation {java_bin} java_bin
Expand All @@ -13,6 +17,7 @@ if "{java_bin}" neq "" (
:: Set environment variables.
{env}

:: Find location of Bundle path in runfiles.
if "{bundler_command}" neq "" (
call :rlocation "!BUNDLE_GEMFILE!" BUNDLE_GEMFILE
call :rlocation "!BUNDLE_PATH!" BUNDLE_PATH
Expand Down
4 changes: 4 additions & 0 deletions ruby/private/binary/binary.sh.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ realpath() (

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

# Find location of Ruby in runfiles.
export PATH=$(dirname $(rlocation {ruby})):$PATH

# Find location of JAVA_HOME in runfiles.
if [ -n "{java_bin}" ]; then
export JAVA_HOME=$(dirname $(dirname $(rlocation "{java_bin}")))
Expand All @@ -26,6 +29,7 @@ fi
# Set environment variables.
{env}

# Find location of Bundle path in runfiles.
if [ -n "{bundler_command}" ]; then
export BUNDLE_GEMFILE=$(rlocation $BUNDLE_GEMFILE)
export BUNDLE_PATH=$(rlocation $BUNDLE_PATH)
Expand Down
7 changes: 4 additions & 3 deletions ruby/private/bundle_install.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ load(
def _rb_bundle_install_impl(ctx):
toolchain = ctx.toolchains["@rules_ruby//ruby:toolchain_type"]

tools = [toolchain.ruby, toolchain.bundle]
tools = []
tools.extend(toolchain.files)
bundler_exe = toolchain.bundle.path

for gem in ctx.attr.gems:
Expand All @@ -35,11 +36,11 @@ def _rb_bundle_install_impl(ctx):
if _is_windows(ctx):
script = ctx.actions.declare_file("bundle_install_{}.cmd".format(ctx.label.name))
template = ctx.file._bundle_install_cmd_tpl
env.update({"PATH": _normalize_path(ctx, toolchain.bindir) + ";%PATH%"})
env.update({"PATH": _normalize_path(ctx, toolchain.ruby.dirname) + ";%PATH%"})
else:
script = ctx.actions.declare_file("bundle_install_{}.sh".format(ctx.label.name))
template = ctx.file._bundle_install_sh_tpl
env.update({"PATH": "%s:$PATH" % toolchain.bindir})
env.update({"PATH": "%s:$PATH" % toolchain.ruby.dirname})

# Calculate relative location between BUNDLE_GEMFILE and BUNDLE_PATH.
relative_dir = "../../"
Expand Down
1 change: 0 additions & 1 deletion ruby/private/download.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ def _rb_download_impl(repository_ctx):
repository_ctx.attr._build_tpl,
executable = False,
substitutions = {
"{bindir}": repr(repository_ctx.path("dist/bin")),
"{version}": version,
"{ruby_binary_name}": ruby_binary_name,
"{gem_binary_name}": gem_binary_name,
Expand Down
2 changes: 1 addition & 1 deletion ruby/private/download/BUILD.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ filegroup(

rb_toolchain(
name = "toolchain",
bindir = "{bindir}",
bundle = ":bundle",
env = {env},
gem = ":gem",
ruby = ":ruby",
version = "{version}",
files = glob(["dist/**/*"]),
)

# vim: ft=bzl
9 changes: 6 additions & 3 deletions ruby/private/gem_install.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ def _rb_gem_install_impl(ctx):

env = {}
env.update(toolchain.env)
tools = [toolchain.gem]

tools = []
tools.extend(toolchain.files)

if toolchain.version.startswith("jruby"):
java_toolchain = ctx.toolchains["@bazel_tools//tools/jdk:runtime_toolchain_type"]
tools.extend(java_toolchain.java_runtime.files.to_list())
Expand All @@ -24,11 +27,11 @@ def _rb_gem_install_impl(ctx):
if _is_windows(ctx):
gem_install = ctx.actions.declare_file("gem_install_{}.cmd".format(ctx.label.name))
template = ctx.file._gem_install_cmd_tpl
env.update({"PATH": _normalize_path(ctx, toolchain.bindir) + ";%PATH%"})
env.update({"PATH": _normalize_path(ctx, toolchain.ruby.dirname) + ";%PATH%"})
else:
gem_install = ctx.actions.declare_file("gem_install_{}.sh".format(ctx.label.name))
template = ctx.file._gem_install_sh_tpl
env.update({"PATH": "%s:$PATH" % toolchain.bindir})
env.update({"PATH": "%s:$PATH" % toolchain.ruby.dirname})

ctx.actions.expand_template(
template = template,
Expand Down
4 changes: 4 additions & 0 deletions ruby/private/utils.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,7 @@ def normalize_bzlmod_repository_name(name):
repository name
"""
return name.rpartition("~")[-1]

def to_rlocation_path(source):
"""Returns source path that can be used with runfiles library."""
return source.short_path.removeprefix("../")
9 changes: 5 additions & 4 deletions ruby/toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ def _rb_toolchain_impl(ctx):
ruby = ctx.executable.ruby,
bundle = ctx.executable.bundle,
gem = ctx.executable.gem,
bindir = ctx.attr.bindir,
version = ctx.attr.version,
env = ctx.attr.env,
files = ctx.files.files,
)

rb_toolchain = rule(
Expand All @@ -31,14 +31,15 @@ rb_toolchain = rule(
executable = True,
cfg = "exec",
),
"bindir": attr.string(
doc = "Path to Ruby bin/ directory",
),
"version": attr.string(
doc = "Ruby version",
),
"env": attr.string_dict(
doc = "Environment variables required by an interpreter",
),
"files": attr.label_list(
allow_files = True,
doc = "All files necessary for toolchain",
),
},
)

0 comments on commit b1015ea

Please sign in to comment.