@@ -37,6 +37,9 @@ def build(executor, options)
37
37
raise NotImplementedError
38
38
end
39
39
40
+ def build_and_link_exts ( executor , output )
41
+ end
42
+
40
43
# Array of paths to extconf.rb files.
41
44
def specs_with_extensions
42
45
@packager . specs . filter_map do |spec |
@@ -67,7 +70,6 @@ def build(executor, options)
67
70
options [ :remake ] || options [ :clean ] || options [ :reconfigure ]
68
71
if File . exist? ( build . crossruby . artifact ) && !force_rebuild
69
72
# Always build extensions because they are usually not expensive to build
70
- self . build_exts ( executor , build )
71
73
return build . crossruby . artifact
72
74
end
73
75
build . crossruby . clean ( executor ) if options [ :clean ]
@@ -91,6 +93,45 @@ def build(executor, options)
91
93
build . crossruby . artifact
92
94
end
93
95
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
+
94
135
def build_exts ( executor , build )
95
136
exts = specs_with_extensions . flat_map do |spec , exts |
96
137
exts . map do |ext |
0 commit comments