Class: RubyWasm::Packager::Core::DynamicLinking

Inherits:
BuildStrategy show all
Defined in:
lib/ruby_wasm/packager/core.rb

Instance Method Summary collapse

Methods inherited from BuildStrategy

#initialize, #specs_with_extensions, #wasi_exec_model, #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



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
# File 'lib/ruby_wasm/packager/core.rb', line 183

def _build_gem_exts(executor, build, gem_home)
  build.toolchain.install
  baseruby = build.baseruby
  unless Dir.exist?(baseruby.install_dir)
    baseruby.build(executor)
  end

  exts = specs_with_extensions.flat_map do |spec, exts|
    exts.map do |ext|
      ext_feature = File.dirname(ext) # e.g. "ext/cgi/escape"
      ext_srcdir = File.join(spec.full_gem_path, ext_feature)
      ext_relative_path = File.join(spec.full_name, ext_feature)
      prod = RubyWasm::CrossRubyExtProduct.new(
        ext_srcdir,
        build.toolchain,
        features: @packager.features,
        ext_relative_path: ext_relative_path
      )
      [prod, spec]
    end
  end

  exts.each do |prod, spec|
    libdir = File.join(gem_home, "gems", spec.full_name, spec.raw_require_paths.first)
    extra_mkargs = [
      "sitearchdir=#{libdir}",
      "sitelibdir=#{libdir}",
    ]
    executor.begin_section prod.class, prod.name, "Building"
    prod.build(executor, build.crossruby, extra_mkargs)
    executor.end_section prod.class, prod.name
  end
end


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
180
181
# File 'lib/ruby_wasm/packager/core.rb', line 121

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
  wasi_adapter = RubyWasm::Packager::ComponentAdapter.wasi_snapshot_preview1(wasi_exec_model)
  adapters = [wasi_adapter]
  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"))]

  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)

  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.info "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.info "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.info "Linking adapter #{adapter_name}=#{adapter} (#{module_bytes.size} bytes)"
    linker.adapter(adapter_name, module_bytes)
  end
  return linker.encode()
end

#artifactObject



221
222
223
# File 'lib/ruby_wasm/packager/core.rb', line 221

def artifact
  derive_build.crossruby.artifact
end

#build(executor, options) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/ruby_wasm/packager/core.rb', line 88

def build(executor, options)
  build = derive_build
  force_rebuild =
    options[:remake] || options[:clean] || options[: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 options[:clean]

  self.with_unbundled_env do
    build.crossruby.build(
      executor,
      remake: options[:remake],
      reconfigure: options[:reconfigure]
    )
  end

  build.crossruby.artifact
end

#build_gem_exts(executor, gem_home) ⇒ Object



109
110
111
112
113
114
# File 'lib/ruby_wasm/packager/core.rb', line 109

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



217
218
219
# File 'lib/ruby_wasm/packager/core.rb', line 217

def cache_key(digest)
  derive_build.cache_key(digest)
end

#derive_buildObject



229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/ruby_wasm/packager/core.rb', line 229

def derive_build
  return @build if @build
  __skip__ =
    build ||= RubyWasm::Build.new(
      name, **@packager.full_build_options,
      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.full_build_options[: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


116
117
118
119
# File 'lib/ruby_wasm/packager/core.rb', line 116

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

#nameObject



262
263
264
265
266
267
268
# File 'lib/ruby_wasm/packager/core.rb', line 262

def name
  require "digest"
  options = @packager.full_build_options
  src_channel = options[:src][:name]
  target_triplet = options[:target]
  "ruby-#{src_channel}-#{target_triplet}-pic#{options[:suffix]}"
end

#targetObject



225
226
227
# File 'lib/ruby_wasm/packager/core.rb', line 225

def target
  RubyWasm::Target.new(@packager.full_build_options[:target], pic: true)
end