Skip to content

Commit

Permalink
feat: expose Ruby engine configuration (#185)
Browse files Browse the repository at this point in the history
  • Loading branch information
p0deje authored Dec 24, 2024
1 parent 9886833 commit f6cff64
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 1 deletion.
16 changes: 16 additions & 0 deletions docs/repository_rules.md

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

6 changes: 5 additions & 1 deletion examples/gem/spec/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ rb_test(
"LOCATION_SRC": "$(location :env_spec)",
"LOCATION_DEP": "$(location //lib/gem:add)",
"LOCATION_DATA": "$(location //spec/support:file)",
},
} | select({
"@ruby//engine:jruby": {"RUBY_ENGINE": "jruby"},
"@ruby//engine:truffleruby": {"RUBY_ENGINE": "truffleruby"},
"//conditions:default": {"RUBY_ENGINE": "ruby"},
}),
env_inherit = [
"USER", # POSIX
"USERNAME", # Windows
Expand Down
4 changes: 4 additions & 0 deletions examples/gem/spec/env_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,8 @@
expect(File).to exist(ENV.fetch('LOCATION_DATA'))
expect(File).to exist(ENV.fetch('LOCATION_DEP'))
end

specify do
expect(ENV.fetch('RUBY_ENGINE')).to eq(RUBY_ENGINE)
end
end
18 changes: 18 additions & 0 deletions ruby/private/download.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,16 @@ def _rb_download_impl(repository_ctx):
gem_binary_name = "gem"

env = {}
engine = "ruby"
if version.startswith("jruby"):
engine = "jruby"

# JRuby might fail with "Errno::EACCES: Permission denied - NUL" on Windows:
# https://github.com/jruby/jruby/issues/7182#issuecomment-1112953015
env.update({"JAVA_OPTS": "-Djdk.io.File.enableADS=true"})
elif version.startswith("truffleruby"):
engine = "truffleruby"

# TruffleRuby needs explicit locale
# https://www.graalvm.org/dev/reference-manual/ruby/UTF8Locale/
env.update({"LANG": "en_US.UTF-8"})
Expand All @@ -120,6 +125,15 @@ def _rb_download_impl(repository_ctx):
},
)

repository_ctx.template(
"engine/BUILD",
repository_ctx.attr._build_engine_tpl,
executable = False,
substitutions = {
"{ruby_engine}": engine,
},
)

def _read_version_from_file(repository_ctx):
version = repository_ctx.read(repository_ctx.attr.version_file).strip("\r\n")
if repository_ctx.attr.version_file.name == ".tool-versions":
Expand Down Expand Up @@ -271,5 +285,9 @@ which isn't available in this ruby-build yet.
allow_single_file = True,
default = "@rules_ruby//:ruby/private/download/BUILD.tpl",
),
"_build_engine_tpl": attr.label(
allow_single_file = True,
default = "@rules_ruby//:ruby/private/download/BUILD.engine.tpl",
),
},
)
36 changes: 36 additions & 0 deletions ruby/private/download/BUILD.engine.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
load("@bazel_skylib//rules:common_settings.bzl", "string_flag")

package(default_visibility = ["//visibility:public"])

string_flag(
name = "engine",
values = [
"jruby",
"truffleruby",
"ruby",
],
build_setting_default = "{ruby_engine}",
)

config_setting(
name = "jruby",
flag_values = {
":engine": "jruby",
},
)

config_setting(
name = "truffleruby",
flag_values = {
":engine": "truffleruby",
},
)

config_setting(
name = "ruby",
flag_values = {
":engine": "ruby",
},
)

# vim: ft=bzl
16 changes: 16 additions & 0 deletions ruby/private/toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,22 @@ def rb_register_toolchains(
$ bazel run @ruby//:gem -- install rails
```
You can also use Ruby engine targets to `select()` depending on installed Ruby interpreter:
`BUILD`:
```bazel
rb_library(
name = "my_lib",
srcs = ["my_lib.rb"],
deps = select({
"@ruby//engine:jruby": [":my_jruby_lib"],
"@ruby//engine:truffleruby": ["//:my_truffleruby_lib"],
"@ruby//engine:ruby": ["//:my__lib"],
"//conditions:default": [],
}),
)
```
Args:
name: base name of resulting repositories, by default "ruby"
version: a semver version of MRI, or a string like [interpreter type]-[version], or "system"
Expand Down

0 comments on commit f6cff64

Please sign in to comment.