Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support per-target Ruby toolchain #140

Merged
merged 1 commit into from
Jul 16, 2024
Merged
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
3 changes: 2 additions & 1 deletion docs/repository_rules.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 10 additions & 5 deletions docs/rules.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions ruby/private/binary.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ Supports `$(location)` expansion for targets from `srcs`, `data` and `deps`.
"env_inherit": attr.string_list(
doc = "List of environment variable names to be inherited by the test runner.",
),
"ruby": attr.label(
doc = "Override Ruby toolchain to use when running the script.",
providers = [platform_common.ToolchainInfo],
),
"_binary_cmd_tpl": attr.label(
allow_single_file = True,
default = "@rules_ruby//ruby/private/binary:binary.cmd.tpl",
Expand All @@ -63,6 +67,8 @@ Supports `$(location)` expansion for targets from `srcs`, `data` and `deps`.
# buildifier: disable=function-docstring
def generate_rb_binary_script(ctx, binary, bundler = False, args = [], env = {}, java_bin = ""):
toolchain = ctx.toolchains["@rules_ruby//ruby:toolchain_type"]
if ctx.attr.ruby != None:
toolchain = ctx.attr.ruby[platform_common.ToolchainInfo]

binary_path = ""
locate_binary_in_runfiles = ""
Expand Down
10 changes: 10 additions & 0 deletions ruby/private/bundle_fetch.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ _GEM_INSTALL_BUILD_FRAGMENT = """
rb_gem_install(
name = "{name}",
gem = "{cache_path}/{gem}",
ruby = {ruby},
)
"""

Expand All @@ -31,6 +32,7 @@ rb_binary(
name = "{name}",
main = "{path}/{name}",
deps = ["//:{repository_name}"],
ruby = {ruby},
)
"""

Expand Down Expand Up @@ -136,6 +138,7 @@ def _rb_bundle_fetch_impl(repository_ctx):
gem_fragments = []
gem_install_fragments = []
gem_checksums = {}
ruby_toolchain_attr = "None" if repository_ctx.attr.ruby == None else '"{}"'.format(repository_ctx.attr.ruby)
repository_name = _normalize_bzlmod_repository_name(repository_ctx.name)

# Fetch gems and expose them as `rb_gem()` targets.
Expand Down Expand Up @@ -164,6 +167,7 @@ def _rb_bundle_fetch_impl(repository_ctx):
name = gemfile_lock.bundler.full_name,
gem = gemfile_lock.bundler.filename,
cache_path = cache_path,
ruby = ruby_toolchain_attr,
),
)

Expand All @@ -178,6 +182,7 @@ def _rb_bundle_fetch_impl(repository_ctx):
name = executable,
repository_name = repository_name,
path = BINSTUBS_LOCATION.partition("/")[-1],
ruby = ruby_toolchain_attr,
),
)
repository_ctx.template(
Expand All @@ -203,6 +208,7 @@ def _rb_bundle_fetch_impl(repository_ctx):
"{gem_fragments}": "".join(gem_fragments),
"{gem_install_fragments}": "".join(gem_install_fragments),
"{env}": repr(repository_ctx.attr.env),
"{ruby}": ruby_toolchain_attr,
},
)

Expand Down Expand Up @@ -250,6 +256,10 @@ rb_bundle_fetch = repository_rule(
default = {},
doc = "SHA-256 checksums for remote gems. Keys are gem names (e.g. foobar-1.2.3), values are SHA-256 checksums.",
),
"ruby": attr.label(
doc = "Override Ruby toolchain to use for installation.",
providers = [platform_common.ToolchainInfo],
),
"_build_tpl": attr.label(
allow_single_file = True,
default = "@rules_ruby//:ruby/private/bundle_fetch/BUILD.tpl",
Expand Down
1 change: 1 addition & 0 deletions ruby/private/bundle_fetch/BUILD.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ rb_bundle_install(
gemfile = "{gemfile_path}",
gemfile_lock = "{gemfile_lock_path}",
gems = {gems},
ruby = {ruby},
)

{gem_fragments}
Expand Down
6 changes: 6 additions & 0 deletions ruby/private/bundle_install.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ load(

def _rb_bundle_install_impl(ctx):
toolchain = ctx.toolchains["@rules_ruby//ruby:toolchain_type"]
if ctx.attr.ruby != None:
toolchain = ctx.attr.ruby[platform_common.ToolchainInfo]

tools = []
tools.extend(toolchain.files)
Expand Down Expand Up @@ -137,6 +139,10 @@ rb_bundle_install = rule(
"env": attr.string_dict(
doc = "Environment variables to use during installation.",
),
"ruby": attr.label(
doc = "Override Ruby toolchain to use when installing the gem.",
providers = [platform_common.ToolchainInfo],
),
"_runfiles_library": attr.label(
allow_single_file = True,
default = "@bazel_tools//tools/bash/runfiles",
Expand Down
6 changes: 6 additions & 0 deletions ruby/private/gem_install.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ def _rb_gem_install_impl(ctx):
gem = ctx.file.gem
install_dir = ctx.actions.declare_directory(gem.basename[:-4])
toolchain = ctx.toolchains["@rules_ruby//ruby:toolchain_type"]
if ctx.attr.ruby != None:
toolchain = ctx.attr.ruby[platform_common.ToolchainInfo]

env = {}
env.update(toolchain.env)
Expand Down Expand Up @@ -71,6 +73,10 @@ rb_gem_install = rule(
mandatory = True,
doc = "Gem file to install.",
),
"ruby": attr.label(
doc = "Override Ruby toolchain to use when installing the gem.",
providers = [platform_common.ToolchainInfo],
),
"_gem_install_cmd_tpl": attr.label(
allow_single_file = True,
default = "@rules_ruby//ruby/private/gem_install:gem_install.cmd.tpl",
Expand Down
1 change: 1 addition & 0 deletions ruby/private/gem_push.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ Gem file to push to RubyGems. You would usually use an output of `rb_gem_build()
),
env = BINARY_ATTRS["env"],
env_inherit = BINARY_ATTRS["env_inherit"],
ruby = BINARY_ATTRS["ruby"],
_binary_cmd_tpl = BINARY_ATTRS["_binary_cmd_tpl"],
_binary_sh_tpl = BINARY_ATTRS["_binary_sh_tpl"],
_windows_constraint = BINARY_ATTRS["_windows_constraint"],
Expand Down