Skip to content

Commit

Permalink
Use bundle install --target-rbconfig to install gems for Wasm target
Browse files Browse the repository at this point in the history
  • Loading branch information
kateinoigakukun committed Jul 4, 2024
1 parent dcdae31 commit 99535c5
Show file tree
Hide file tree
Showing 18 changed files with 129 additions and 33 deletions.
6 changes: 3 additions & 3 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,20 @@ NPM_PACKAGES = [
{
name: "ruby-head-wasm-wasi",
ruby_version: "head",
gemfile: "packages/npm-packages/ruby-wasm-wasi/Gemfile",
gemfile: "packages/npm-packages/ruby-head-wasm-wasi/Gemfile",
target: "wasm32-unknown-wasip1",
enable_component_model: true,
},
{
name: "ruby-3.3-wasm-wasi",
ruby_version: "3.3",
gemfile: "packages/npm-packages/ruby-wasm-wasi/Gemfile",
gemfile: "packages/npm-packages/ruby-3.3-wasm-wasi/Gemfile",
target: "wasm32-unknown-wasip1"
},
{
name: "ruby-3.2-wasm-wasi",
ruby_version: "3.2",
gemfile: "packages/npm-packages/ruby-wasm-wasi/Gemfile",
gemfile: "packages/npm-packages/ruby-3.2-wasm-wasi/Gemfile",
target: "wasm32-unknown-wasip1"
},
{ name: "ruby-wasm-wasi", target: "wasm32-unknown-wasip1" }
Expand Down
4 changes: 3 additions & 1 deletion bin/setup
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ set -vx
root="$(cd "$(dirname "$0")/.." && pwd)"

env BUNDLE_GEMFILE="$root/Gemfile" bundle install
env BUNDLE_GEMFILE="$root/packages/npm-packages/ruby-wasm-wasi/Gemfile" bundle install
for gemfile in $root/packages/npm-packages/*/Gemfile; do
env BUNDLE_GEMFILE="$gemfile" bundle install
done

# Build vendored jco if Rust toolchain is available and submodule is checked out
if command -v rustc && [ -f vendor/jco/package.json ]; then
Expand Down
3 changes: 3 additions & 0 deletions ext/ruby_wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,9 @@ impl WasiVirt {
// Disable sockets for now since `sockets/ip-name-lookup` is not
// supported by @bytecodealliance/preview2-shim yet
virt.sockets(false);
// Disable http for now since `http` is not supported by
// wasmtime yet
virt.http(false);
Ok(())
})
}
Expand Down
6 changes: 5 additions & 1 deletion lib/ruby_wasm/build/product/crossruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def do_extconf(executor, crossruby)
return
end
objdir = product_build_dir crossruby
rbconfig_rb = Dir.glob(File.join(crossruby.dest_dir, "usr/local/lib/ruby/*/wasm32-wasi/rbconfig.rb")).first
rbconfig_rb = crossruby.rbconfig_rb
raise "rbconfig.rb not found" unless rbconfig_rb
extconf_args = [
"-C", objdir,
Expand Down Expand Up @@ -301,6 +301,10 @@ def extinit_c_erb
File.expand_path("../crossruby/extinit.c.erb", __FILE__)
end

def rbconfig_rb
Dir.glob(File.join(dest_dir, "usr/local/lib/ruby/*/wasm32-wasi/rbconfig.rb")).first
end

def baseruby_path
File.join(@baseruby.install_dir, "bin/ruby")
end
Expand Down
2 changes: 1 addition & 1 deletion lib/ruby_wasm/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ def derive_packager(options)
Bundler.ui.level = old_level
end
end
RubyWasm.logger.info "Using Gemfile: #{definition.gemfiles}" if definition
RubyWasm.logger.info "Using Gemfile: #{definition.gemfiles.map(&:to_s).join(", ")}" if definition
RubyWasm::Packager.new(
root, build_config(options), definition,
features: RubyWasm::FeatureSet.derive_from_env
Expand Down
2 changes: 1 addition & 1 deletion lib/ruby_wasm/packager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def package(executor, dest_dir, options)

ruby_core.build_gem_exts(executor, fs.bundle_dir)

fs.package_gems
fs.package_gems unless features.support_component_model?
fs.remove_non_runtime_files(executor)
if options[:stdlib]
options[:without_stdlib_components].each do |component|
Expand Down
58 changes: 34 additions & 24 deletions lib/ruby_wasm/packager/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -187,31 +187,41 @@ def _build_gem_exts(executor, build, gem_home)
baseruby.build(executor)
end

exts = specs_with_extensions.flat_map do |spec, exts|
exts.map do |ext|
ext_feature = File.dirname(ext) # e.g. "ext/cgi/escape"
ext_srcdir = File.join(spec.full_gem_path, ext_feature)
ext_relative_path = File.join(spec.full_name, ext_feature)
prod = RubyWasm::CrossRubyExtProduct.new(
ext_srcdir,
build.toolchain,
features: @packager.features,
ext_relative_path: ext_relative_path
)
[prod, spec]
end
end
crossruby = build.crossruby
rbconfig_rb = crossruby.rbconfig_rb

exts.each do |prod, spec|
libdir = File.join(gem_home, "gems", spec.full_name, spec.raw_require_paths.first)
extra_mkargs = [
"sitearchdir=#{libdir}",
"sitelibdir=#{libdir}",
]
executor.begin_section prod.class, prod.name, "Building"
prod.build(executor, build.crossruby, extra_mkargs)
executor.end_section prod.class, prod.name
end
options = @packager.full_build_options
target_triplet = options[:target]

local_path = File.join("bundle", target_triplet)
env = {
"BUNDLE_APP_CONFIG" => File.join(".bundle", target_triplet),
"BUNDLE_PATH" => local_path,
"BUNDLE_WITHOUT" => "build",
# FIXME: BUNDLE_PATH is set as a installation destination here, but
# it is also used as a source of gems to be loaded by RubyGems itself.
# RubyGems loads "psych" gem and if Gemfile includes "psych" gem,
# RubyGems tries to load "psych" gem from BUNDLE_PATH at the second
# time of "bundle install" command. But the extension of "psych" gem
# under BUNDLE_PATH is built for Wasm target, not for host platform,
# so it fails to load the extension.
#
# Thus we preload psych from the default LOAD_PATH here to avoid
# loading Wasm version of psych.so via `Kernel#require` patched by
# RubyGems.
"RUBYOPT" => "-rpsych",
}

args = [
File.join(baseruby.install_dir, "bin", "bundle"),
"install",
"--standalone",
"--target-rbconfig",
rbconfig_rb,
]

executor.system(*args, env: env)
executor.cp_r(local_path, gem_home)
end

def cache_key(digest)
Expand Down
1 change: 0 additions & 1 deletion lib/ruby_wasm/packager/file_system.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ def remove_stdlib_component(executor, component)
when "enc"
# Remove all encodings except for encdb.so and transdb.so
enc_dir = File.join(@ruby_root, "lib", "ruby", ruby_version, "wasm32-wasi", "enc")
puts File.join(enc_dir, "**/*.so")
Dir.glob(File.join(enc_dir, "**/*.so")).each do |entry|
next if entry.end_with?("encdb.so", "transdb.so")
RubyWasm.logger.debug "Removing stdlib encoding: #{entry}"
Expand Down
1 change: 1 addition & 0 deletions packages/npm-packages/ruby-3.2-wasm-wasi/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
*.tgz
/bundle
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ DEPENDENCIES
test-unit

BUNDLED WITH
2.5.3
2.5.9
1 change: 1 addition & 0 deletions packages/npm-packages/ruby-3.3-wasm-wasi/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
*.tgz
/bundle
8 changes: 8 additions & 0 deletions packages/npm-packages/ruby-3.3-wasm-wasi/Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

source "https://rubygems.org"

gem "js", path: "../../gems/js"
gem "ruby_wasm", path: "../../../"
gem "power_assert"
gem "test-unit"
29 changes: 29 additions & 0 deletions packages/npm-packages/ruby-3.3-wasm-wasi/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
PATH
remote: ../../..
specs:
ruby_wasm (2.6.2)

PATH
remote: ../../gems/js
specs:
js (2.6.2)

GEM
remote: https://rubygems.org/
specs:
power_assert (2.0.3)
test-unit (3.6.2)
power_assert

PLATFORMS
ruby
x86_64-linux

DEPENDENCIES
js!
power_assert
ruby_wasm!
test-unit

BUNDLED WITH
2.5.9
1 change: 1 addition & 0 deletions packages/npm-packages/ruby-head-wasm-wasi/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
*.tgz
/tmp
/bundle
8 changes: 8 additions & 0 deletions packages/npm-packages/ruby-head-wasm-wasi/Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

source "https://rubygems.org"

gem "js", path: "../../gems/js"
gem "ruby_wasm", path: "../../../"
gem "power_assert"
gem "test-unit"
29 changes: 29 additions & 0 deletions packages/npm-packages/ruby-head-wasm-wasi/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
PATH
remote: ../../..
specs:
ruby_wasm (2.6.2)

PATH
remote: ../../gems/js
specs:
js (2.6.2)

GEM
remote: https://rubygems.org/
specs:
power_assert (2.0.3)
test-unit (3.6.2)
power_assert

PLATFORMS
ruby
wasm32-wasi

DEPENDENCIES
js!
power_assert
ruby_wasm!
test-unit

BUNDLED WITH
2.6.0.dev
1 change: 1 addition & 0 deletions sig/ruby_wasm/build.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ module RubyWasm
def extinit_c_erb: -> String
def baseruby_path: -> String
def configure_args: (String build_triple, Toolchain toolchain) -> Array[String]
def rbconfig_rb: -> String?
end

class WitBindgen
Expand Down

0 comments on commit 99535c5

Please sign in to comment.