Class: RubyWasm::Packager::Core::DynamicLinking
- Inherits:
-
BuildStrategy
- Object
- BuildStrategy
- RubyWasm::Packager::Core::DynamicLinking
- Defined in:
- lib/ruby_wasm/packager/core.rb
Instance Method Summary collapse
- #_build_gem_exts(executor, build, gem_home) ⇒ Object
- #_link_gem_exts(executor, build, ruby_root, gem_home, module_bytes) ⇒ Object
- #artifact ⇒ Object
- #build(executor, options) ⇒ Object
- #build_gem_exts(executor, gem_home) ⇒ Object
- #cache_key(digest) ⇒ Object
- #derive_build ⇒ Object
- #link_gem_exts(executor, ruby_root, gem_home, module_bytes) ⇒ Object
- #name ⇒ Object
- #target ⇒ Object
Methods inherited from BuildStrategy
#initialize, #specs_with_extensions, #with_unbundled_env
Constructor Details
This class inherits a constructor from RubyWasm::Packager::Core::BuildStrategy
Instance Method Details
#_build_gem_exts(executor, build, gem_home) ⇒ Object
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/ruby_wasm/packager/core.rb', line 181 def _build_gem_exts(executor, build, gem_home) build.toolchain.install baseruby = build.baseruby unless Dir.exist?(baseruby.install_dir) baseruby.build(executor) end crossruby = build.crossruby rbconfig_rb = crossruby.rbconfig_rb = @packager. target_triplet = [:target] local_path = File.join("bundle", target_triplet) env = { "BUNDLE_APP_CONFIG" => File.join(".bundle", target_triplet), "BUNDLE_PATH" => local_path, "BUNDLE_WITHOUT" => "build", # Do not auto-switch bundler version by Gemfile.lock "BUNDLE_VERSION" => "system", # 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 |
#_link_gem_exts(executor, build, ruby_root, gem_home, module_bytes) ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/ruby_wasm/packager/core.rb', line 113 def _link_gem_exts(executor, build, ruby_root, gem_home, module_bytes) libraries = [] # TODO: Should be computed from dyinfo of ruby binary wasi_libc_shared_libs = [ "libc.so", "libc++.so", "libc++abi.so", "libwasi-emulated-getpid.so", "libwasi-emulated-mman.so", "libwasi-emulated-process-clocks.so", "libwasi-emulated-signal.so", ] wasi_libc_shared_libs.each do |lib| # @type var toolchain: RubyWasm::WASISDK toolchain = build.toolchain wasi_sdk_path = toolchain.wasi_sdk_path libraries << File.join(wasi_sdk_path, "share/wasi-sysroot/lib/wasm32-wasi", lib) end dl_openable_libs = [] dl_openable_libs << [File.dirname(ruby_root), Dir.glob(File.join(ruby_root, "lib", "ruby", "**", "*.so"))] dl_openable_libs << [gem_home, Dir.glob(File.join(gem_home, "**", "*.so"))] has_js_so = dl_openable_libs.any? do |root, libs| libs.any? { |lib| lib.end_with?("/js.so") } end wasi_adapter = RubyWasm::Packager::ComponentAdapter.wasi_snapshot_preview1(has_js_so ? "reactor" : "command") adapters = [wasi_adapter] linker = RubyWasmExt::ComponentLink.new linker.use_built_in_libdl(true) linker.stub_missing_functions(false) linker.validate(ENV["RUBYWASM_SKIP_LINKER_VALIDATION"] != "1") linker.library("ruby", module_bytes, false) RubyWasm.logger.info "Linking Ruby with extensions" libraries.each do |lib| # Non-DL openable libraries should be referenced as base name lib_name = File.basename(lib) module_bytes = File.binread(lib) RubyWasm.logger.debug "Linking #{lib_name} (#{module_bytes.size} bytes)" linker.library(lib_name, module_bytes, false) end dl_openable_libs.each do |root, libs| libs.each do |lib| # DL openable lib_name should be a relative path from ruby_root lib_name = "/" + Pathname.new(lib).relative_path_from(Pathname.new(File.dirname(root))).to_s module_bytes = File.binread(lib) RubyWasm.logger.debug "Linking #{lib_name} (#{module_bytes.size} bytes)" linker.library(lib_name, module_bytes, true) end end adapters.each do |adapter| adapter_name = File.basename(adapter) # e.g. wasi_snapshot_preview1.command.wasm -> wasi_snapshot_preview1 adapter_name = adapter_name.split(".")[0] module_bytes = File.binread(adapter) RubyWasm.logger.debug "Linking adapter #{adapter_name}=#{adapter} (#{module_bytes.size} bytes)" linker.adapter(adapter_name, module_bytes) end return linker.encode() end |
#artifact ⇒ Object
231 232 233 |
# File 'lib/ruby_wasm/packager/core.rb', line 231 def artifact derive_build.crossruby.artifact end |
#build(executor, options) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/ruby_wasm/packager/core.rb', line 80 def build(executor, ) build = derive_build force_rebuild = [:remake] || [:clean] || [:reconfigure] if File.exist?(build.crossruby.artifact) && !force_rebuild # Always build extensions because they are usually not expensive to build return build.crossruby.artifact end build.crossruby.clean(executor) if [:clean] self.with_unbundled_env do build.crossruby.build( executor, remake: [:remake], reconfigure: [:reconfigure] ) end build.crossruby.artifact end |
#build_gem_exts(executor, gem_home) ⇒ Object
101 102 103 104 105 106 |
# File 'lib/ruby_wasm/packager/core.rb', line 101 def build_gem_exts(executor, gem_home) build = derive_build self.with_unbundled_env do self._build_gem_exts(executor, build, gem_home) end end |
#cache_key(digest) ⇒ Object
227 228 229 |
# File 'lib/ruby_wasm/packager/core.rb', line 227 def cache_key(digest) derive_build.cache_key(digest) end |
#derive_build ⇒ Object
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 |
# File 'lib/ruby_wasm/packager/core.rb', line 239 def derive_build return @build if @build __skip__ = build ||= RubyWasm::Build.new( name, **@packager., target: target, # NOTE: We don't need linking libwasi_vfs because we use wasi-virt instead. wasi_vfs: nil ) build.crossruby.cflags = %w[-fPIC -fvisibility=default] if @packager.[:target] != "wasm32-unknown-emscripten" build.crossruby.debugflags = %w[-g] build.crossruby.wasmoptflags = %w[-O3 -g --pass-arg=asyncify-relocatable] build.crossruby.ldflags = %w[ -Xlinker --stack-first -Xlinker -z -Xlinker stack-size=16777216 ] build.crossruby.xldflags = %w[ -Xlinker -shared -Xlinker --export-dynamic -Xlinker --export-all -Xlinker --experimental-pic -Xlinker -export-if-defined=__main_argc_argv ] end @build = build build end |
#link_gem_exts(executor, ruby_root, gem_home, module_bytes) ⇒ Object
108 109 110 111 |
# File 'lib/ruby_wasm/packager/core.rb', line 108 def link_gem_exts(executor, ruby_root, gem_home, module_bytes) build = derive_build self._link_gem_exts(executor, build, ruby_root, gem_home, module_bytes) end |
#name ⇒ Object
272 273 274 275 276 277 278 |
# File 'lib/ruby_wasm/packager/core.rb', line 272 def name require "digest" = @packager. src_channel = [:src][:name] target_triplet = [:target] "ruby-#{src_channel}-#{target_triplet}-pic#{[:suffix]}" end |