Class: RubyWasm::Packager::Core::StaticLinking

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

#artifactObject



299
300
301
# File 'lib/ruby_wasm/packager/core.rb', line 299

def artifact
  derive_build.crossruby.artifact
end

#build(executor, options) ⇒ Object



272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# File 'lib/ruby_wasm/packager/core.rb', line 272

def build(executor, options)
  build = derive_build
  force_rebuild =
    options[:remake] || options[:clean] || options[:reconfigure]
  if File.exist?(build.crossruby.artifact) && !force_rebuild
    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



336
337
338
# File 'lib/ruby_wasm/packager/core.rb', line 336

def build_gem_exts(executor, gem_home)
  # No-op because we already built extensions as part of the Ruby build
end

#cache_key(digest) ⇒ Object



292
293
294
295
296
297
# File 'lib/ruby_wasm/packager/core.rb', line 292

def cache_key(digest)
  derive_build.cache_key(digest)
  if enabled = @packager.features.support_component_model?
    digest << enabled.to_s
  end
end

#derive_buildObject



307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
# File 'lib/ruby_wasm/packager/core.rb', line 307

def derive_build
  return @build if @build
  __skip__ = build ||= RubyWasm::Build.new(
    name, **@packager.full_build_options, target: target,
  )
  build.crossruby.user_exts = user_exts(build)
  # Emscripten uses --global-base=1024 by default, but it conflicts with
  # --stack-first and -z stack-size since global-base 1024 is smaller than
  # the large stack size.
  # Also -g produces some warnings on Emscripten and it confuses the configure
  # script of Ruby.
  if @packager.full_build_options[:target] != "wasm32-unknown-emscripten"
    build.crossruby.debugflags = %w[-g]
    # We assume that imported functions provided through WASI will not change
    # asyncify state, so we ignore them.
    build.crossruby.wasmoptflags = %w[-O3 -g --pass-arg=asyncify-ignore-imports]
    build.crossruby.ldflags = %w[
      -Xlinker
      --stack-first
      -Xlinker
      -z
      -Xlinker
      stack-size=16777216
    ]
  end
  @build = build
  build
end


340
341
342
343
344
345
346
347
348
349
350
351
352
# File 'lib/ruby_wasm/packager/core.rb', line 340

def link_gem_exts(executor, ruby_root, gem_home, module_bytes)
  return module_bytes unless @packager.features.support_component_model?

  linker = RubyWasmExt::ComponentEncode.new
  linker.validate(ENV["RUBYWASM_SKIP_LINKER_VALIDATION"] != "1")
  linker.module(module_bytes)
  linker.adapter(
    "wasi_snapshot_preview1",
    File.binread(RubyWasm::Packager::ComponentAdapter.wasi_snapshot_preview1(wasi_exec_model))
  )

  linker.encode()
end

#nameObject



371
372
373
374
375
376
377
378
379
380
381
382
383
384
# File 'lib/ruby_wasm/packager/core.rb', line 371

def name
  require "digest"
  options = @packager.full_build_options
  src_channel = options[:src][:name]
  target_triplet = options[:target]
  base = "ruby-#{src_channel}-#{target_triplet}#{options[:suffix]}"
  exts = specs_with_extensions.sort
  hash = ::Digest::MD5.new
  specs_with_extensions.each { |spec, _| hash << spec.full_name }
  if enabled = @packager.features.support_component_model?
    hash << enabled.to_s
  end
  exts.empty? ? base : "#{base}-#{hash.hexdigest}"
end

#targetObject



303
304
305
# File 'lib/ruby_wasm/packager/core.rb', line 303

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

#user_exts(build) ⇒ Object



354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
# File 'lib/ruby_wasm/packager/core.rb', line 354

def user_exts(build)
  @user_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)
        RubyWasm::CrossRubyExtProduct.new(
          ext_srcdir,
          build.toolchain,
          features: @packager.features,
          ext_relative_path: ext_relative_path
        )
      end
    end
end