Skip to content

Commit 545fee7

Browse files
committed
fix: expose headers/jars on system ruby
Fixes #208
1 parent 0c294be commit 545fee7

File tree

2 files changed

+38
-22
lines changed

2 files changed

+38
-22
lines changed

.github/workflows/ci.yml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ jobs:
6868
- if: matrix.ruby == 'system'
6969
uses: ruby/setup-ruby@v1
7070
with:
71-
ruby-version: "3.1.6"
71+
ruby-version: 3.1.6
7272
- run: bazel build ...
7373
- run: bazel run lib/gem:add-numbers 2
7474
- run: bazel run lib/gem:print-version
@@ -114,6 +114,9 @@ jobs:
114114
ruby:
115115
- 3.3.7
116116
- jruby-9.4.12.0
117+
use-system-ruby:
118+
- true
119+
- false
117120
defaults:
118121
run:
119122
working-directory: examples/native_ext
@@ -123,7 +126,11 @@ jobs:
123126
with:
124127
bazelrc: common --announce_rc --color=yes
125128
repository-cache: examples/native_ext/MODULE.bazel
126-
- run: echo ${{ matrix.ruby }} > .ruby-version
129+
- if: matrix.use-system-ruby
130+
uses: ruby/setup-ruby@v1
131+
with:
132+
ruby-version: ${{ matrix.ruby }}
133+
- run: echo ${{ matrix.use-system-ruby && 'system' || matrix.ruby }} > .ruby-version
127134
- run: bazel build ...
128135
- if: failure() && runner.debug == '1'
129136
uses: mxschmitt/action-tmate@v3

ruby/private/download.bzl

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -85,36 +85,34 @@ def _rb_download_impl(repository_ctx):
8585
else:
8686
fail("missing value for one of mandatory attributes 'version' or 'version_file'")
8787

88+
engine = "ruby"
89+
env = {}
90+
ruby_binary_name = "ruby"
91+
gem_binary_name = "gem"
8892
if version.startswith("jruby"):
8993
_install_jruby(repository_ctx, version)
90-
elif version == "system":
91-
_symlink_system_ruby(repository_ctx)
92-
elif repository_ctx.os.name.startswith("windows"):
93-
_install_via_rubyinstaller(repository_ctx, version)
94-
else:
95-
_install_via_ruby_build(repository_ctx, version)
9694

97-
if version.startswith("jruby"):
95+
engine = "jruby"
9896
ruby_binary_name = "jruby"
9997
gem_binary_name = "jgem"
100-
else:
101-
ruby_binary_name = "ruby"
102-
gem_binary_name = "gem"
103-
104-
env = {}
105-
engine = "ruby"
106-
if version.startswith("jruby"):
107-
engine = "jruby"
10898

10999
# JRuby might fail with "Errno::EACCES: Permission denied - NUL" on Windows:
110100
# https://github.com/jruby/jruby/issues/7182#issuecomment-1112953015
111101
env.update({"JAVA_OPTS": "-Djdk.io.File.enableADS=true"})
112102
elif version.startswith("truffleruby"):
103+
_install_via_ruby_build(repository_ctx, version)
104+
113105
engine = "truffleruby"
114106

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

119117
includes = []
120118
if repository_ctx.path("dist/include").exists:
@@ -262,11 +260,22 @@ def _symlink_system_ruby(repository_ctx):
262260
result = repository_ctx.execute(["ruby", "-e", "puts RbConfig.ruby"])
263261
if result.return_code != 0:
264262
fail("Failed to determine the system Ruby path:\n%s\n%s" % (result.stdout, result.stderr))
265-
ruby_path = result.stdout.strip()
266-
ruby_dir = repository_ctx.path(ruby_path).dirname
267-
repository_ctx.symlink(ruby_dir, "dist/bin")
268-
if repository_ctx.os.name.startswith("windows"):
269-
repository_ctx.symlink(ruby_dir.dirname.get_child("lib"), "dist/lib")
263+
264+
_symlink_system_ruby_dir("bindir", repository_ctx)
265+
_symlink_system_ruby_dir("libdir", repository_ctx)
266+
_symlink_system_ruby_dir("rubyhdrdir", repository_ctx)
267+
268+
engine = repository_ctx.execute(["ruby", "-e", "puts RbConfig::CONFIG['RUBY_BASE_NAME']"]).stdout.strip()
269+
return engine
270+
271+
def _symlink_system_ruby_dir(dirname, repository_ctx):
272+
prefix = repository_ctx.execute(["ruby", "-e", "puts RbConfig::CONFIG['prefix']"]).stdout.strip()
273+
path = repository_ctx.execute(["ruby", "-e", "puts RbConfig::CONFIG['{dirname}']".format(dirname = dirname)]).stdout.strip()
274+
src = repository_ctx.path(path)
275+
dirname = path.removeprefix(prefix).removeprefix("/")
276+
dst = repository_ctx.path("dist/{dirname}".format(dirname = dirname))
277+
if not dst.exists:
278+
repository_ctx.symlink(src, dst)
270279

271280
rb_download = repository_rule(
272281
implementation = _rb_download_impl,

0 commit comments

Comments
 (0)