Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 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",
_require_java_runtime = "require_java_runtime",
_to_rlocation_path = "to_rlocation_path",
)

Expand Down Expand Up @@ -177,9 +178,9 @@ def rb_binary_impl(ctx):
tools.append(ctx.file._runfiles_helper)

if ruby_toolchain.version.startswith("jruby"):
java_toolchain = ctx.toolchains["@bazel_tools//tools/jdk:runtime_toolchain_type"]
tools.extend(java_toolchain.java_runtime.files.to_list())
java_bin = java_toolchain.java_runtime.java_executable_runfiles_path[3:]
java_runtime = _require_java_runtime(ruby_toolchain)
tools.extend(java_runtime.files.to_list())
java_bin = java_runtime.java_executable_runfiles_path[3:]

for dep in transitive_deps.to_list():
if BundlerInfo in dep:
Expand Down Expand Up @@ -271,7 +272,6 @@ rb_binary = rule(
),
toolchains = [
"@rules_ruby//ruby:toolchain_type",
"@bazel_tools//tools/jdk:runtime_toolchain_type",
],
doc = """
Runs a Ruby binary.
Expand Down
12 changes: 5 additions & 7 deletions ruby/private/bundle_install.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ load(
_convert_env_to_script = "convert_env_to_script",
_is_windows = "is_windows",
_normalize_path = "normalize_path",
_require_java_runtime = "require_java_runtime",
_resolve_bundle_build_env = "resolve_bundle_build_env",
_to_rlocation_path = "to_rlocation_path",
)
Expand Down Expand Up @@ -74,11 +75,11 @@ def _rb_bundle_install_impl(ctx):
jars_home_strip_suffix = ""

if toolchain.version.startswith("jruby"):
java_toolchain = ctx.exec_groups["ruby"].toolchains["@bazel_tools//tools/jdk:runtime_toolchain_type"]
tools.extend(java_toolchain.java_runtime.files.to_list())
java_runtime = _require_java_runtime(toolchain)
tools.extend(java_runtime.files.to_list())
env.update({
"JARS_SKIP": "true", # Avoid installing extra dependencies during install.
"JAVA_HOME": java_toolchain.java_runtime.java_home,
"JAVA_HOME": java_runtime.java_home,
})
jars_info = _jars_home(ctx, jar_files, ctx.attr.jars_path)
jars_home_strip_suffix = jars_info.strip
Expand Down Expand Up @@ -257,10 +258,7 @@ rb_bundle_install = rule(
},
# JRuby runs Ruby and Java tools in the same action, on the same platform.
exec_groups = {
"ruby": exec_group(toolchains = [
"@rules_ruby//ruby:toolchain_type",
"@bazel_tools//tools/jdk:runtime_toolchain_type",
]),
"ruby": exec_group(toolchains = ["@rules_ruby//ruby:toolchain_type"]),
},
doc = """
Installs Bundler dependencies from cached gems.
Expand Down
1 change: 1 addition & 0 deletions ruby/private/download.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ def _rb_download_impl(repository_ctx):
"{env}": repr(env),
"{bundle_build_env}": repr(bundle_build_env),
"{includes}": repr(includes),
"{java_runtime}": repr("@bazel_tools//tools/jdk:current_java_runtime" if engine == "jruby" else None),
},
)

Expand Down
1 change: 1 addition & 0 deletions ruby/private/download/BUILD.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ rb_toolchain(
"@platforms//os:windows": "dist/bin/{gem_binary_name}.cmd",
"//conditions:default": "dist/bin/{gem_binary_name}",
}),
java_runtime = {java_runtime},
ruby = ":ruby_file",
version = "{version}",
)
Expand Down
17 changes: 9 additions & 8 deletions ruby/private/gem_build.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,17 @@ load(
"get_transitive_deps",
"get_transitive_srcs",
)
load("//ruby/private:utils.bzl", _is_windows = "is_windows")
load(
"//ruby/private:utils.bzl",
_is_windows = "is_windows",
_require_java_runtime = "require_java_runtime",
)

def _rb_gem_build_impl(ctx):
gem_builder = ctx.actions.declare_file("{}_gem_builder.rb".format(ctx.label.name))
transitive_data = get_transitive_data(ctx.files.data, ctx.attr.deps).to_list()
transitive_deps = get_transitive_deps(ctx.attr.deps).to_list()
transitive_srcs = get_transitive_srcs(ctx.files.srcs, ctx.attr.deps).to_list()
java_toolchain = ctx.exec_groups["ruby"].toolchains["@bazel_tools//tools/jdk:runtime_toolchain_type"]
ruby_toolchain = ctx.exec_groups["ruby"].toolchains["@rules_ruby//ruby:toolchain_type"]
if ctx.attr.ruby != None:
ruby_toolchain = ctx.attr.ruby[platform_common.ToolchainInfo]
Expand All @@ -28,8 +31,9 @@ def _rb_gem_build_impl(ctx):
env.update(ruby_toolchain.env)

if ruby_toolchain.version.startswith("jruby"):
env["JAVA_HOME"] = java_toolchain.java_runtime.java_home
tools.extend(java_toolchain.java_runtime.files.to_list())
java_runtime = _require_java_runtime(ruby_toolchain)
env["JAVA_HOME"] = java_runtime.java_home
tools.extend(java_runtime.files.to_list())
if _is_windows(ctx):
env["PATH"] = ruby_toolchain.ruby.dirname

Expand Down Expand Up @@ -117,10 +121,7 @@ rb_gem_build = rule(
},
# JRuby runs Ruby and Java tools in the same action, on the same platform.
exec_groups = {
"ruby": exec_group(toolchains = [
"@rules_ruby//ruby:toolchain_type",
"@bazel_tools//tools/jdk:runtime_toolchain_type",
]),
"ruby": exec_group(toolchains = ["@rules_ruby//ruby:toolchain_type"]),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this might still need both toolchains, otherwise it could fail on RBE, see comment above.

},
doc = """
Builds a Ruby gem.
Expand Down
12 changes: 5 additions & 7 deletions ruby/private/gem_install.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ load(
_convert_env_to_script = "convert_env_to_script",
_is_windows = "is_windows",
_normalize_path = "normalize_path",
_require_java_runtime = "require_java_runtime",
_resolve_bundle_build_env = "resolve_bundle_build_env",
)

Expand All @@ -25,9 +26,9 @@ def _rb_gem_install_impl(ctx):
tools.extend(toolchain.files)

if toolchain.version.startswith("jruby"):
java_toolchain = ctx.exec_groups["ruby"].toolchains["@bazel_tools//tools/jdk:runtime_toolchain_type"]
tools.extend(java_toolchain.java_runtime.files.to_list())
env.update({"JAVA_HOME": java_toolchain.java_runtime.java_home})
java_runtime = _require_java_runtime(toolchain)
tools.extend(java_runtime.files.to_list())
env.update({"JAVA_HOME": java_runtime.java_home})

if _is_windows(ctx):
gem_install = ctx.actions.declare_file("gem_install_{}.cmd".format(ctx.label.name))
Expand Down Expand Up @@ -95,10 +96,7 @@ rb_gem_install = rule(
},
# JRuby runs Ruby and Java tools in the same action, on the same platform.
exec_groups = {
"ruby": exec_group(toolchains = [

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto.

"@rules_ruby//ruby:toolchain_type",
"@bazel_tools//tools/jdk:runtime_toolchain_type",
]),
"ruby": exec_group(toolchains = ["@rules_ruby//ruby:toolchain_type"]),
},
doc = """
Installs a built Ruby gem.
Expand Down
13 changes: 8 additions & 5 deletions ruby/private/gem_push.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

load("//ruby/private:binary.bzl", "generate_rb_binary_script", BINARY_ATTRS = "ATTRS")
load("//ruby/private:library.bzl", LIBRARY_ATTRS = "ATTRS")
load("//ruby/private:utils.bzl", _to_rlocation_path = "to_rlocation_path")
load(
"//ruby/private:utils.bzl",
_require_java_runtime = "require_java_runtime",
_to_rlocation_path = "to_rlocation_path",
)

def _rb_gem_push_impl(ctx):
env = {}
java_toolchain = ctx.toolchains["@bazel_tools//tools/jdk:runtime_toolchain_type"]
ruby_toolchain = ctx.toolchains["@rules_ruby//ruby:toolchain_type"]
if ctx.attr.ruby != None:
ruby_toolchain = ctx.attr.ruby[platform_common.ToolchainInfo]
Expand All @@ -16,8 +19,9 @@ def _rb_gem_push_impl(ctx):
java_bin = ""

if ruby_toolchain.version.startswith("jruby"):
tools.extend(java_toolchain.java_runtime.files.to_list())
java_bin = java_toolchain.java_runtime.java_executable_runfiles_path[3:]
java_runtime = _require_java_runtime(ruby_toolchain)
tools.extend(java_runtime.files.to_list())
java_bin = java_runtime.java_executable_runfiles_path[3:]

env.update(ruby_toolchain.env)
env.update(ctx.attr.env)
Expand Down Expand Up @@ -69,7 +73,6 @@ Gem file to push to RubyGems. You would usually use an output of `rb_gem_build()
),
toolchains = [
"@rules_ruby//ruby:toolchain_type",
"@bazel_tools//tools/jdk:runtime_toolchain_type",

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto.

],
doc = """
Pushes a built Ruby gem.
Expand Down
1 change: 0 additions & 1 deletion ruby/private/test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ rb_test = rule(
),
toolchains = [
"@rules_ruby//ruby:toolchain_type",
"@bazel_tools//tools/jdk:runtime_toolchain_type",

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto.

],
doc = """
Runs a Ruby test.
Expand Down
14 changes: 14 additions & 0 deletions ruby/private/utils.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -232,3 +232,17 @@ def to_rlocation_path(ctx, source):
return workspace_name + "/" + source.short_path

return source.short_path

def require_java_runtime(toolchain):
"""Returns the JavaRuntimeInfo of a JRuby toolchain or fails.

Args:
toolchain: the Ruby `ToolchainInfo`

Returns:
the `JavaRuntimeInfo`
"""
runtime = toolchain.java_runtime
if runtime == None:
fail("The JRuby toolchain {} has no `java_runtime`; set `java_runtime` on its rb_toolchain().".format(toolchain.version))
return runtime
3 changes: 3 additions & 0 deletions ruby/tests/BUILD
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
load(":download_tests.bzl", "download_test_suite")
load(":jars_downloader_tests.bzl", "jars_downloader_test_suite")
load(":java_runtime_tests.bzl", "java_runtime_test_suite")
load(":repository_proxy_tests.bzl", "repository_proxy_test_suite")
load(":toolchain_selection_tests.bzl", "toolchain_selection_test_suite")
load(":utils_tests.bzl", "utils_test_suite")
Expand All @@ -13,3 +14,5 @@ utils_test_suite()
toolchain_selection_test_suite()

download_test_suite()

java_runtime_test_suite(name = "java_runtime_tests")
38 changes: 38 additions & 0 deletions ruby/tests/java_runtime_tests.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
"""Analysis tests for the Java runtime of Ruby toolchains."""

load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts")
load("//ruby:toolchain.bzl", "rb_toolchain")
load("//ruby/private:utils.bzl", "require_java_runtime")

def _subject_impl(ctx):
require_java_runtime(ctx.attr.toolchain[platform_common.ToolchainInfo])
return []

_subject = rule(
implementation = _subject_impl,
attrs = {"toolchain": attr.label(providers = [platform_common.ToolchainInfo])},
)

def _missing_java_runtime_test_impl(ctx):
env = analysistest.begin(ctx)
asserts.expect_failure(env, "has no `java_runtime`; set `java_runtime` on its rb_toolchain()")
return analysistest.end(env)

_missing_java_runtime_test = analysistest.make(_missing_java_runtime_test_impl, expect_failure = True)

def java_runtime_test_suite(name):
rb_toolchain(
name = name + "_toolchain",
ruby = "java_runtime_tests.bzl",
version = "jruby-10.1.2.0",
tags = ["manual"],
)
_subject(
name = name + "_subject",
toolchain = ":" + name + "_toolchain",
tags = ["manual"],
)
_missing_java_runtime_test(
name = name + "_missing_test",
target_under_test = ":" + name + "_subject",
)
10 changes: 10 additions & 0 deletions ruby/toolchain.bzl
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
"Define a Bazel toolchain for a Ruby interpreter"

load("@rules_java//java:defs.bzl", "java_common")

def _runtime(target):
return target[java_common.JavaRuntimeInfo] if target else None

def _rb_toolchain_impl(ctx):
return platform_common.ToolchainInfo(
ruby = ctx.executable.ruby,
Expand All @@ -9,6 +14,7 @@ def _rb_toolchain_impl(ctx):
env = ctx.attr.env,
bundle_build_env = ctx.attr.bundle_build_env,
files = ctx.files.files,
java_runtime = _runtime(ctx.attr.java_runtime),
)

rb_toolchain = rule(
Expand Down Expand Up @@ -51,5 +57,9 @@ runs, so no absolute path is stored in the toolchain.
allow_files = True,
doc = "All files necessary for toolchain",
),
"java_runtime": attr.label(
doc = "JDK for JRuby, e.g. `@bazel_tools//tools/jdk:current_java_runtime`. Required for JRuby only.",
providers = [java_common.JavaRuntimeInfo],
),
},
)
Loading