Skip to content

Commit 72a766b

Browse files
Use bundle install --target-rbconfig to install gems for Wasm target
1 parent dcdae31 commit 72a766b

File tree

17 files changed

+125
-32
lines changed

17 files changed

+125
-32
lines changed

Rakefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,20 @@ NPM_PACKAGES = [
2828
{
2929
name: "ruby-head-wasm-wasi",
3030
ruby_version: "head",
31-
gemfile: "packages/npm-packages/ruby-wasm-wasi/Gemfile",
31+
gemfile: "packages/npm-packages/ruby-head-wasm-wasi/Gemfile",
3232
target: "wasm32-unknown-wasip1",
3333
enable_component_model: true,
3434
},
3535
{
3636
name: "ruby-3.3-wasm-wasi",
3737
ruby_version: "3.3",
38-
gemfile: "packages/npm-packages/ruby-wasm-wasi/Gemfile",
38+
gemfile: "packages/npm-packages/ruby-3.3-wasm-wasi/Gemfile",
3939
target: "wasm32-unknown-wasip1"
4040
},
4141
{
4242
name: "ruby-3.2-wasm-wasi",
4343
ruby_version: "3.2",
44-
gemfile: "packages/npm-packages/ruby-wasm-wasi/Gemfile",
44+
gemfile: "packages/npm-packages/ruby-3.2-wasm-wasi/Gemfile",
4545
target: "wasm32-unknown-wasip1"
4646
},
4747
{ name: "ruby-wasm-wasi", target: "wasm32-unknown-wasip1" }

bin/setup

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ set -vx
66
root="$(cd "$(dirname "$0")/.." && pwd)"
77

88
env BUNDLE_GEMFILE="$root/Gemfile" bundle install
9-
env BUNDLE_GEMFILE="$root/packages/npm-packages/ruby-wasm-wasi/Gemfile" bundle install
109

1110
# Build vendored jco if Rust toolchain is available and submodule is checked out
1211
if command -v rustc && [ -f vendor/jco/package.json ]; then

ext/ruby_wasm/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,9 @@ impl WasiVirt {
252252
// Disable sockets for now since `sockets/ip-name-lookup` is not
253253
// supported by @bytecodealliance/preview2-shim yet
254254
virt.sockets(false);
255+
// Disable http for now since `http` is not supported by
256+
// wasmtime yet
257+
virt.http(false);
255258
Ok(())
256259
})
257260
}

lib/ruby_wasm/build/product/crossruby.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def do_extconf(executor, crossruby)
7171
return
7272
end
7373
objdir = product_build_dir crossruby
74-
rbconfig_rb = Dir.glob(File.join(crossruby.dest_dir, "usr/local/lib/ruby/*/wasm32-wasi/rbconfig.rb")).first
74+
rbconfig_rb = crossruby.rbconfig_rb
7575
raise "rbconfig.rb not found" unless rbconfig_rb
7676
extconf_args = [
7777
"-C", objdir,
@@ -301,6 +301,10 @@ def extinit_c_erb
301301
File.expand_path("../crossruby/extinit.c.erb", __FILE__)
302302
end
303303

304+
def rbconfig_rb
305+
Dir.glob(File.join(dest_dir, "usr/local/lib/ruby/*/wasm32-wasi/rbconfig.rb")).first
306+
end
307+
304308
def baseruby_path
305309
File.join(@baseruby.install_dir, "bin/ruby")
306310
end

lib/ruby_wasm/packager.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def package(executor, dest_dir, options)
3434

3535
ruby_core.build_gem_exts(executor, fs.bundle_dir)
3636

37-
fs.package_gems
37+
fs.package_gems unless features.support_component_model?
3838
fs.remove_non_runtime_files(executor)
3939
if options[:stdlib]
4040
options[:without_stdlib_components].each do |component|

lib/ruby_wasm/packager/core.rb

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -187,31 +187,41 @@ def _build_gem_exts(executor, build, gem_home)
187187
baseruby.build(executor)
188188
end
189189

190-
exts = specs_with_extensions.flat_map do |spec, exts|
191-
exts.map do |ext|
192-
ext_feature = File.dirname(ext) # e.g. "ext/cgi/escape"
193-
ext_srcdir = File.join(spec.full_gem_path, ext_feature)
194-
ext_relative_path = File.join(spec.full_name, ext_feature)
195-
prod = RubyWasm::CrossRubyExtProduct.new(
196-
ext_srcdir,
197-
build.toolchain,
198-
features: @packager.features,
199-
ext_relative_path: ext_relative_path
200-
)
201-
[prod, spec]
202-
end
203-
end
190+
crossruby = build.crossruby
191+
rbconfig_rb = crossruby.rbconfig_rb
204192

205-
exts.each do |prod, spec|
206-
libdir = File.join(gem_home, "gems", spec.full_name, spec.raw_require_paths.first)
207-
extra_mkargs = [
208-
"sitearchdir=#{libdir}",
209-
"sitelibdir=#{libdir}",
210-
]
211-
executor.begin_section prod.class, prod.name, "Building"
212-
prod.build(executor, build.crossruby, extra_mkargs)
213-
executor.end_section prod.class, prod.name
214-
end
193+
options = @packager.full_build_options
194+
target_triplet = options[:target]
195+
196+
local_path = File.join("bundle", target_triplet)
197+
env = {
198+
"BUNDLE_APP_CONFIG" => File.join(".bundle", target_triplet),
199+
"BUNDLE_PATH" => local_path,
200+
"BUNDLE_WITHOUT" => "build",
201+
# FIXME: BUNDLE_PATH is set as a installation destination here, but
202+
# it is also used as a source of gems to be loaded by RubyGems itself.
203+
# RubyGems loads "psych" gem and if Gemfile includes "psych" gem,
204+
# RubyGems tries to load "psych" gem from BUNDLE_PATH at the second
205+
# time of "bundle install" command. But the extension of "psych" gem
206+
# under BUNDLE_PATH is built for Wasm target, not for host platform,
207+
# so it fails to load the extension.
208+
#
209+
# Thus we preload psych from the default LOAD_PATH here to avoid
210+
# loading Wasm version of psych.so via `Kernel#require` patched by
211+
# RubyGems.
212+
"RUBYOPT" => "-rpsych",
213+
}
214+
215+
args = [
216+
File.join(baseruby.install_dir, "bin", "bundle"),
217+
"install",
218+
"--standalone",
219+
"--target-rbconfig",
220+
rbconfig_rb,
221+
]
222+
223+
executor.system(*args, env: env)
224+
executor.cp_r(local_path, gem_home)
215225
end
216226

217227
def cache_key(digest)

lib/ruby_wasm/packager/file_system.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ def remove_stdlib_component(executor, component)
4444
when "enc"
4545
# Remove all encodings except for encdb.so and transdb.so
4646
enc_dir = File.join(@ruby_root, "lib", "ruby", ruby_version, "wasm32-wasi", "enc")
47-
puts File.join(enc_dir, "**/*.so")
4847
Dir.glob(File.join(enc_dir, "**/*.so")).each do |entry|
4948
next if entry.end_with?("encdb.so", "transdb.so")
5049
RubyWasm.logger.debug "Removing stdlib encoding: #{entry}"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
*.tgz
2+
/bundle

packages/npm-packages/ruby-wasm-wasi/Gemfile.lock renamed to packages/npm-packages/ruby-3.2-wasm-wasi/Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,4 @@ DEPENDENCIES
2626
test-unit
2727

2828
BUNDLED WITH
29-
2.5.3
29+
2.5.9
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
*.tgz
2+
/bundle
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# frozen_string_literal: true
2+
3+
source "https://rubygems.org"
4+
5+
gem "js", path: "../../gems/js"
6+
gem "ruby_wasm", path: "../../../"
7+
gem "power_assert"
8+
gem "test-unit"
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
PATH
2+
remote: ../../..
3+
specs:
4+
ruby_wasm (2.6.2)
5+
6+
PATH
7+
remote: ../../gems/js
8+
specs:
9+
js (2.6.2)
10+
11+
GEM
12+
remote: https://rubygems.org/
13+
specs:
14+
power_assert (2.0.3)
15+
test-unit (3.6.2)
16+
power_assert
17+
18+
PLATFORMS
19+
ruby
20+
x86_64-linux
21+
22+
DEPENDENCIES
23+
js!
24+
power_assert
25+
ruby_wasm!
26+
test-unit
27+
28+
BUNDLED WITH
29+
2.5.9
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
*.tgz
22
/tmp
3+
/bundle
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# frozen_string_literal: true
2+
3+
source "https://rubygems.org"
4+
5+
gem "js", path: "../../gems/js"
6+
gem "ruby_wasm", path: "../../../"
7+
gem "power_assert"
8+
gem "test-unit"
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
PATH
2+
remote: ../../..
3+
specs:
4+
ruby_wasm (2.6.2)
5+
6+
PATH
7+
remote: ../../gems/js
8+
specs:
9+
js (2.6.2)
10+
11+
GEM
12+
remote: https://rubygems.org/
13+
specs:
14+
power_assert (2.0.3)
15+
test-unit (3.6.2)
16+
power_assert
17+
18+
PLATFORMS
19+
ruby
20+
wasm32-wasi
21+
22+
DEPENDENCIES
23+
js!
24+
power_assert
25+
ruby_wasm!
26+
test-unit
27+
28+
BUNDLED WITH
29+
2.6.0.dev

sig/ruby_wasm/build.rbs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ module RubyWasm
219219
def extinit_c_erb: -> String
220220
def baseruby_path: -> String
221221
def configure_args: (String build_triple, Toolchain toolchain) -> Array[String]
222+
def rbconfig_rb: -> String?
222223
end
223224

224225
class WitBindgen

0 commit comments

Comments
 (0)