@@ -12,7 +12,7 @@ def build(executor, options)
12
12
13
13
extend Forwardable
14
14
15
- def_delegators :build_strategy , :cache_key , :artifact , :build_and_link_exts
15
+ def_delegators :build_strategy , :cache_key , :artifact , :build_gem_exts , :link_gem_exts
16
16
17
17
private
18
18
@@ -37,7 +37,11 @@ def build(executor, options)
37
37
raise NotImplementedError
38
38
end
39
39
40
- def build_and_link_exts ( executor , module_bytes )
40
+ def build_gem_exts ( executor , gem_home )
41
+ raise NotImplementedError
42
+ end
43
+
44
+ def link_gem_exts ( executor , gem_home , module_bytes )
41
45
raise NotImplementedError
42
46
end
43
47
@@ -93,13 +97,17 @@ def build(executor, options)
93
97
build . crossruby . artifact
94
98
end
95
99
96
- def build_and_link_exts ( executor , module_bytes )
100
+ def build_gem_exts ( executor , gem_home )
97
101
build = derive_build
98
- self . build_exts ( executor , build )
99
- self . link_exts ( executor , build )
102
+ self . _build_gem_exts ( executor , build , gem_home )
100
103
end
101
104
102
- def link_exts ( executor , build )
105
+ def link_gem_exts ( executor , gem_home , module_bytes )
106
+ build = derive_build
107
+ self . _link_gem_exts ( executor , build , gem_home )
108
+ end
109
+
110
+ def _link_gem_exts ( executor , build , gem_home )
103
111
ruby_root = build . crossruby . dest_dir
104
112
105
113
libraries = [ File . join ( ruby_root , "usr" , "local" , "bin" , "ruby" ) ]
@@ -121,7 +129,10 @@ def link_exts(executor, build)
121
129
end
122
130
wasi_adapter = RubyWasm ::Packager ::ComponentAdapter . wasi_snapshot_preview1 ( "command" )
123
131
adapters = [ wasi_adapter ]
124
- dl_openable_libs = Dir . glob ( File . join ( ruby_root , "usr" , "local" , "lib" , "ruby" , "**" , "*.so" ) )
132
+ dl_openable_libs = [ ]
133
+ dl_openable_libs << [ File . join ( ruby_root , "usr" ) , Dir . glob ( File . join ( ruby_root , "usr" , "local" , "lib" , "ruby" , "**" , "*.so" ) ) ]
134
+ dl_openable_libs << [ gem_home , Dir . glob ( File . join ( gem_home , "gems" , "**" , "*.so" ) ) ]
135
+
125
136
linker = RubyWasmExt ::ComponentLink . new
126
137
linker . use_built_in_libdl ( true )
127
138
linker . stub_missing_functions ( false )
@@ -135,12 +146,14 @@ def link_exts(executor, build)
135
146
linker . library ( lib_name , module_bytes , false )
136
147
end
137
148
138
- dl_openable_libs . each do |lib |
139
- # DL openable lib_name should be a relative path from ruby_root
140
- lib_name = "/" + Pathname . new ( lib ) . relative_path_from ( Pathname . new ( ruby_root ) ) . to_s
141
- module_bytes = File . binread ( lib )
142
- RubyWasm . logger . info "Linking #{ lib_name } (#{ module_bytes . size } bytes)"
143
- linker . library ( lib_name , module_bytes , true )
149
+ dl_openable_libs . each do |root , libs |
150
+ libs . each do |lib |
151
+ # DL openable lib_name should be a relative path from ruby_root
152
+ lib_name = "/" + Pathname . new ( lib ) . relative_path_from ( Pathname . new ( File . dirname ( root ) ) ) . to_s
153
+ module_bytes = File . binread ( lib )
154
+ RubyWasm . logger . info "Linking #{ lib_name } (#{ module_bytes . size } bytes)"
155
+ linker . library ( lib_name , module_bytes , true )
156
+ end
144
157
end
145
158
146
159
adapters . each do |adapter |
@@ -153,24 +166,30 @@ def link_exts(executor, build)
153
166
return linker . encode ( )
154
167
end
155
168
156
- def build_exts ( executor , build )
169
+ def _build_gem_exts ( executor , build , gem_home )
157
170
exts = specs_with_extensions . flat_map do |spec , exts |
158
171
exts . map do |ext |
159
172
ext_feature = File . dirname ( ext ) # e.g. "ext/cgi/escape"
160
173
ext_srcdir = File . join ( spec . full_gem_path , ext_feature )
161
174
ext_relative_path = File . join ( spec . full_name , ext_feature )
162
- RubyWasm ::CrossRubyExtProduct . new (
175
+ prod = RubyWasm ::CrossRubyExtProduct . new (
163
176
ext_srcdir ,
164
177
build . toolchain ,
165
178
features : @packager . features ,
166
179
ext_relative_path : ext_relative_path
167
180
)
181
+ [ prod , spec ]
168
182
end
169
183
end
170
184
171
- exts . each do |prod |
185
+ exts . each do |prod , spec |
186
+ libdir = File . join ( gem_home , "gems" , spec . full_name , spec . raw_require_paths . first )
187
+ extra_mkargs = [
188
+ "sitearchdir=#{ libdir } " ,
189
+ "sitelibdir=#{ libdir } " ,
190
+ ]
172
191
executor . begin_section prod . class , prod . name , "Building"
173
- prod . build ( executor , build . crossruby )
192
+ prod . build ( executor , build . crossruby , extra_mkargs )
174
193
executor . end_section prod . class , prod . name
175
194
end
176
195
end
@@ -301,7 +320,11 @@ def derive_build
301
320
build
302
321
end
303
322
304
- def build_and_link_exts ( executor , module_bytes )
323
+ def build_gem_exts ( executor , gem_home )
324
+ # No-op because we already built extensions as part of the Ruby build
325
+ end
326
+
327
+ def link_gem_exts ( executor , gem_home , module_bytes )
305
328
return module_bytes unless @packager . features . support_component_model?
306
329
307
330
linker = RubyWasmExt ::ComponentEncode . new
0 commit comments