Skip to content

Commit 1cfad93

Browse files
Add --without-stdlib option to exclude stdlib components
1 parent 4c3ce92 commit 1cfad93

File tree

4 files changed

+39
-11
lines changed

4 files changed

+39
-11
lines changed

lib/ruby_wasm/cli.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ def build(args)
5353
target_triplet: "wasm32-unknown-wasip1",
5454
profile: "full",
5555
stdlib: true,
56+
without_stdlib_components: [],
5657
dest_dir: nil,
5758
disable_gems: false,
5859
gemfile: nil,
@@ -105,6 +106,10 @@ def build(args)
105106
options[:stdlib] = stdlib
106107
end
107108

109+
opts.on("--without-stdlib COMPONENT", "Exclude stdlib component") do |component|
110+
options[:without_stdlib_components] << component
111+
end
112+
108113
opts.on("--disable-gems", "Disable gems") do
109114
options[:disable_gems] = true
110115
end

lib/ruby_wasm/packager.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,13 @@ def package(executor, dest_dir, options)
3636

3737
fs.package_gems
3838
fs.remove_non_runtime_files(executor)
39-
fs.remove_stdlib(executor) unless options[:stdlib]
39+
if options[:stdlib]
40+
options[:without_stdlib_components].each do |component|
41+
fs.remove_stdlib_component(executor, component)
42+
end
43+
else
44+
fs.remove_stdlib(executor)
45+
end
4046

4147
if full_build_options[:target] == "wasm32-unknown-wasip1" && !features.support_dynamic_linking?
4248
# wasi-vfs supports only WASI target
@@ -46,7 +52,7 @@ def package(executor, dest_dir, options)
4652

4753
wasm_bytes = wasi_vfs.pack(wasm_bytes)
4854
end
49-
wasm_bytes = ruby_core.link_gem_exts(executor, fs.bundle_dir, wasm_bytes)
55+
wasm_bytes = ruby_core.link_gem_exts(executor, fs.ruby_root, fs.bundle_dir, wasm_bytes)
5056

5157
wasm_bytes = RubyWasmExt.preinitialize(wasm_bytes) if options[:optimize]
5258
wasm_bytes

lib/ruby_wasm/packager/core.rb

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def build_gem_exts(executor, gem_home)
4141
raise NotImplementedError
4242
end
4343

44-
def link_gem_exts(executor, gem_home, module_bytes)
44+
def link_gem_exts(executor, ruby_root, gem_home, module_bytes)
4545
raise NotImplementedError
4646
end
4747

@@ -110,15 +110,13 @@ def build_gem_exts(executor, gem_home)
110110
self._build_gem_exts(executor, build, gem_home)
111111
end
112112

113-
def link_gem_exts(executor, gem_home, module_bytes)
113+
def link_gem_exts(executor, ruby_root, gem_home, module_bytes)
114114
build = derive_build
115-
self._link_gem_exts(executor, build, gem_home)
115+
self._link_gem_exts(executor, build, ruby_root, gem_home, module_bytes)
116116
end
117117

118-
def _link_gem_exts(executor, build, gem_home)
119-
ruby_root = build.crossruby.dest_dir
120-
121-
libraries = [File.join(ruby_root, "usr", "local", "bin", "ruby")]
118+
def _link_gem_exts(executor, build, ruby_root, gem_home, module_bytes)
119+
libraries = []
122120

123121
# TODO: Should be computed from dyinfo of ruby binary
124122
wasi_libc_shared_libs = [
@@ -138,14 +136,16 @@ def _link_gem_exts(executor, build, gem_home)
138136
wasi_adapter = RubyWasm::Packager::ComponentAdapter.wasi_snapshot_preview1(wasi_exec_model)
139137
adapters = [wasi_adapter]
140138
dl_openable_libs = []
141-
dl_openable_libs << [File.join(ruby_root, "usr"), Dir.glob(File.join(ruby_root, "usr", "local", "lib", "ruby", "**", "*.so"))]
139+
dl_openable_libs << [File.dirname(ruby_root), Dir.glob(File.join(ruby_root, "lib", "ruby", "**", "*.so"))]
142140
dl_openable_libs << [gem_home, Dir.glob(File.join(gem_home, "**", "*.so"))]
143141

144142
linker = RubyWasmExt::ComponentLink.new
145143
linker.use_built_in_libdl(true)
146144
linker.stub_missing_functions(false)
147145
linker.validate(ENV["RUBYWASM_SKIP_LINKER_VALIDATION"] != "1")
148146

147+
linker.library("ruby", module_bytes, false)
148+
149149
libraries.each do |lib|
150150
# Non-DL openable libraries should be referenced as base name
151151
lib_name = File.basename(lib)
@@ -333,7 +333,7 @@ def build_gem_exts(executor, gem_home)
333333
# No-op because we already built extensions as part of the Ruby build
334334
end
335335

336-
def link_gem_exts(executor, gem_home, module_bytes)
336+
def link_gem_exts(executor, ruby_root, gem_home, module_bytes)
337337
return module_bytes unless @packager.features.support_component_model?
338338

339339
linker = RubyWasmExt::ComponentEncode.new

lib/ruby_wasm/packager/file_system.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,23 @@ def remove_stdlib(executor)
3838
File.write(rbconfig, rbconfig_contents)
3939
end
4040

41+
def remove_stdlib_component(executor, component)
42+
RubyWasm.logger.info "Removing stdlib component: #{component}"
43+
case component
44+
when "enc"
45+
# Remove all encodings except for encdb.so and transdb.so
46+
enc_dir = File.join(@ruby_root, "lib", "ruby", ruby_version, "wasm32-wasi", "enc")
47+
puts File.join(enc_dir, "**/*.so")
48+
Dir.glob(File.join(enc_dir, "**/*.so")).each do |entry|
49+
next if entry.end_with?("encdb.so", "transdb.so")
50+
RubyWasm.logger.debug "Removing stdlib encoding: #{entry}"
51+
executor.rm_rf entry
52+
end
53+
else
54+
raise "Unknown stdlib component: #{component}"
55+
end
56+
end
57+
4158
def package_gems
4259
@packager.specs.each do |spec|
4360
RubyWasm.logger.info "Packaging gem: #{spec.full_name}"

0 commit comments

Comments
 (0)