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

fix: expose headers/jars on system ruby #213

Merged
merged 4 commits into from
Feb 21, 2025
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 1 addition & 1 deletion .bcr/presubmit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ bcr_test_module:
platform: ${{ platform }}
bazel: ${{ bazel }}
test_flags:
- "--test_tag_filters -bcr-presubmit-failure"
- "--test_tag_filters=-bcr-presubmit-failure"
test_targets:
- "//..."
11 changes: 9 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ jobs:
- if: matrix.ruby == 'system'
uses: ruby/setup-ruby@v1
with:
ruby-version: "3.1.6"
ruby-version: 3.1.6
- run: bazel build ...
- run: bazel run lib/gem:add-numbers 2
- run: bazel run lib/gem:print-version
Expand Down Expand Up @@ -114,6 +114,9 @@ jobs:
ruby:
- 3.3.7
- jruby-9.4.12.0
use-system-ruby:
- true
- false
defaults:
run:
working-directory: examples/native_ext
Expand All @@ -123,7 +126,11 @@ jobs:
with:
bazelrc: common --announce_rc --color=yes
repository-cache: examples/native_ext/MODULE.bazel
- run: echo ${{ matrix.ruby }} > .ruby-version
- if: matrix.use-system-ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby }}
- run: echo ${{ matrix.use-system-ruby && 'system' || matrix.ruby }} > .ruby-version
- run: bazel build ...
- if: failure() && runner.debug == '1'
uses: mxschmitt/action-tmate@v3
Expand Down
51 changes: 30 additions & 21 deletions ruby/private/download.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -85,36 +85,34 @@ def _rb_download_impl(repository_ctx):
else:
fail("missing value for one of mandatory attributes 'version' or 'version_file'")

engine = "ruby"
env = {}
ruby_binary_name = "ruby"
gem_binary_name = "gem"
if version.startswith("jruby"):
_install_jruby(repository_ctx, version)
elif version == "system":
_symlink_system_ruby(repository_ctx)
elif repository_ctx.os.name.startswith("windows"):
_install_via_rubyinstaller(repository_ctx, version)
else:
_install_via_ruby_build(repository_ctx, version)

if version.startswith("jruby"):
engine = "jruby"
ruby_binary_name = "jruby"
gem_binary_name = "jgem"
else:
ruby_binary_name = "ruby"
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"):
_install_via_ruby_build(repository_ctx, version)

engine = "truffleruby"

# TruffleRuby needs explicit locale
# https://www.graalvm.org/dev/reference-manual/ruby/UTF8Locale/
env.update({"LANG": "en_US.UTF-8"})
elif version == "system":
engine = _symlink_system_ruby(repository_ctx)
elif repository_ctx.os.name.startswith("windows"):
_install_via_rubyinstaller(repository_ctx, version)
else:
_install_via_ruby_build(repository_ctx, version)

includes = []
if repository_ctx.path("dist/include").exists:
Expand Down Expand Up @@ -262,11 +260,22 @@ def _symlink_system_ruby(repository_ctx):
result = repository_ctx.execute(["ruby", "-e", "puts RbConfig.ruby"])
if result.return_code != 0:
fail("Failed to determine the system Ruby path:\n%s\n%s" % (result.stdout, result.stderr))
ruby_path = result.stdout.strip()
ruby_dir = repository_ctx.path(ruby_path).dirname
repository_ctx.symlink(ruby_dir, "dist/bin")
if repository_ctx.os.name.startswith("windows"):
repository_ctx.symlink(ruby_dir.dirname.get_child("lib"), "dist/lib")

_symlink_system_ruby_dir("bindir", repository_ctx)
_symlink_system_ruby_dir("libdir", repository_ctx)
_symlink_system_ruby_dir("rubyhdrdir", repository_ctx)

engine = repository_ctx.execute(["ruby", "-e", "puts RbConfig::CONFIG['RUBY_BASE_NAME']"]).stdout.strip()
return engine

def _symlink_system_ruby_dir(dirname, repository_ctx):
prefix = repository_ctx.execute(["ruby", "-e", "puts RbConfig::CONFIG['prefix']"]).stdout.strip()
path = repository_ctx.execute(["ruby", "-e", "puts RbConfig::CONFIG['{dirname}']".format(dirname = dirname)]).stdout.strip()
src = repository_ctx.path(path)
dirname = path.removeprefix(prefix).removeprefix("/")
dst = repository_ctx.path("dist/{dirname}".format(dirname = dirname))
if not dst.exists:
repository_ctx.symlink(src, dst)

rb_download = repository_rule(
implementation = _rb_download_impl,
Expand All @@ -275,7 +284,7 @@ rb_download = repository_rule(
doc = "Ruby version to install.",
),
"version_file": attr.label(
allow_single_file = [".ruby-version"],
allow_single_file = [".ruby-version", ".tool-versions"],
doc = "File to read Ruby version from.",
),
"msys2_packages": attr.string_list(
Expand Down