Skip to content

Commit 377ee26

Browse files
Use officialized --target-rbconfig and prototype wasm-tools integration
1 parent dd40eff commit 377ee26

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

lib/ruby_wasm/build/product/crossruby.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def do_extconf(executor, crossruby)
7575
]
7676
rbconfig_rb = Dir.glob(File.join(crossruby.dest_dir, "usr/local/lib/ruby/*/wasm32-wasi/rbconfig.rb")).first
7777
raise "rbconfig.rb not found" unless rbconfig_rb
78-
extconf_env["MKMF_TARGET_RBCONFIG"] = rbconfig_rb
78+
extconf_args << "--target-rbconfig=#{rbconfig_rb}"
7979
else
8080
extconf_args.concat [
8181
"--disable=gems",

lib/ruby_wasm/packager/core.rb

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ def build(executor, options)
3737
raise NotImplementedError
3838
end
3939

40+
def build_and_link_exts(executor, output)
41+
end
42+
4043
# Array of paths to extconf.rb files.
4144
def specs_with_extensions
4245
@packager.specs.filter_map do |spec|
@@ -67,7 +70,6 @@ def build(executor, options)
6770
options[:remake] || options[:clean] || options[:reconfigure]
6871
if File.exist?(build.crossruby.artifact) && !force_rebuild
6972
# Always build extensions because they are usually not expensive to build
70-
self.build_exts(executor, build)
7173
return build.crossruby.artifact
7274
end
7375
build.crossruby.clean(executor) if options[:clean]
@@ -91,6 +93,45 @@ def build(executor, options)
9193
build.crossruby.artifact
9294
end
9395

96+
def build_and_link_exts(executor, output)
97+
build = derive_build
98+
self.build_exts(executor, build)
99+
self.link_exts(executor, build, output)
100+
end
101+
102+
def link_exts(executor, build, output)
103+
wasm_tools = ENV["WASM_TOOLS"] || "wasm-tools"
104+
ruby_root = build.crossruby.dest_dir
105+
106+
link_args = %W[component link]
107+
link_args << File.join(ruby_root, "usr", "local", "bin", "ruby")
108+
109+
# TODO: Should be computed from dyinfo of ruby binary
110+
wasi_libc_shared_libs = [
111+
"libc.so",
112+
"libdl.so",
113+
"libwasi-emulated-getpid.so",
114+
"libwasi-emulated-mman.so",
115+
"libwasi-emulated-process-clocks.so",
116+
"libwasi-emulated-signal.so",
117+
]
118+
119+
wasi_libc_shared_libs.each do |lib|
120+
wasi_sdk_path = build.toolchain.wasi_sdk_path
121+
link_args << File.join(wasi_sdk_path, "share/wasi-sysroot/lib/wasm32-wasi", lib)
122+
end
123+
wasi_adapter = ENV["WASI_COMPONENT_ADAPTER"] or raise "WASI_COMPONENT_ADAPTER is not set"
124+
link_args.concat ["--adapt", wasi_adapter]
125+
Dir.glob(File.join(ruby_root, "usr", "local", "lib", "ruby", "**", "*.so")).each do |so|
126+
link_args << "--dl-openable"
127+
link_args << "#{so.delete_prefix(ruby_root)}=#{so}"
128+
end
129+
link_args << "-o"
130+
link_args << File.join(ruby_root, "usr", "local", "bin", "ruby.component.wasm")
131+
132+
executor.system(wasm_tools, *link_args)
133+
end
134+
94135
def build_exts(executor, build)
95136
exts = specs_with_extensions.flat_map do |spec, exts|
96137
exts.map do |ext|

0 commit comments

Comments
 (0)