Class: RubyWasm::CrossRubyProduct

Inherits:
AutoconfProduct show all
Defined in:
lib/ruby_wasm/build/product/crossruby.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from AutoconfProduct

#system_triplet_args, #tools_args

Constructor Details

#initialize(params, build_dir, rubies_dir, baseruby, source, toolchain) ⇒ CrossRubyProduct

Returns a new instance of CrossRubyProduct.



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/ruby_wasm/build/product/crossruby.rb', line 161

def initialize(params, build_dir, rubies_dir, baseruby, source, toolchain)
  @params = params
  @rubies_dir = rubies_dir
  @build_dir = build_dir
  @baseruby = baseruby
  @source = source
  @toolchain = toolchain
  @user_exts = []
  @wasmoptflags = []
  @cppflags = []
  @cflags = []
  @ldflags = []
  @debugflags = []
  @xcflags = []
  @xldflags = []
  super(@params.target, @toolchain)
end

Instance Attribute Details

#cflagsObject

Returns the value of attribute cflags.



152
153
154
# File 'lib/ruby_wasm/build/product/crossruby.rb', line 152

def cflags
  @cflags
end

#cppflagsObject

Returns the value of attribute cppflags.



152
153
154
# File 'lib/ruby_wasm/build/product/crossruby.rb', line 152

def cppflags
  @cppflags
end

#debugflagsObject

Returns the value of attribute debugflags.



152
153
154
# File 'lib/ruby_wasm/build/product/crossruby.rb', line 152

def debugflags
  @debugflags
end

#ldflagsObject

Returns the value of attribute ldflags.



152
153
154
# File 'lib/ruby_wasm/build/product/crossruby.rb', line 152

def ldflags
  @ldflags
end

#sourceObject (readonly)

Returns the value of attribute source.



151
152
153
# File 'lib/ruby_wasm/build/product/crossruby.rb', line 151

def source
  @source
end

#targetObject (readonly)

Returns the value of attribute target.



151
152
153
# File 'lib/ruby_wasm/build/product/crossruby.rb', line 151

def target
  @target
end

#toolchainObject (readonly)

Returns the value of attribute toolchain.



151
152
153
# File 'lib/ruby_wasm/build/product/crossruby.rb', line 151

def toolchain
  @toolchain
end

#user_extsObject

Returns the value of attribute user_exts.



152
153
154
# File 'lib/ruby_wasm/build/product/crossruby.rb', line 152

def user_exts
  @user_exts
end

#wasmoptflagsObject

Returns the value of attribute wasmoptflags.



152
153
154
# File 'lib/ruby_wasm/build/product/crossruby.rb', line 152

def wasmoptflags
  @wasmoptflags
end

#xcflagsObject

Returns the value of attribute xcflags.



152
153
154
# File 'lib/ruby_wasm/build/product/crossruby.rb', line 152

def xcflags
  @xcflags
end

#xldflagsObject

Returns the value of attribute xldflags.



152
153
154
# File 'lib/ruby_wasm/build/product/crossruby.rb', line 152

def xldflags
  @xldflags
end

Instance Method Details

#artifactObject



305
306
307
# File 'lib/ruby_wasm/build/product/crossruby.rb', line 305

def artifact
  File.join(@rubies_dir, "#{name}.tar.gz")
end

#baseruby_pathObject



321
322
323
# File 'lib/ruby_wasm/build/product/crossruby.rb', line 321

def baseruby_path
  File.join(@baseruby.install_dir, "bin/ruby")
end

#build(executor, remake: false, reconfigure: false) ⇒ Object



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/ruby_wasm/build/product/crossruby.rb', line 204

def build(executor, remake: false, reconfigure: false)
  executor.mkdir_p dest_dir
  executor.mkdir_p build_dir
  @toolchain.install
  [@source, @baseruby, @libyaml, @zlib, @openssl, @wasi_vfs].each do |prod|
    next unless prod
    executor.begin_section prod.class, prod.name, "Building"
    prod.build(executor)
    executor.end_section prod.class, prod.name
  end
  executor.begin_section self.class, name, "Configuring"
  configure(executor, reconfigure: reconfigure)
  executor.end_section self.class, name

  build_exts(executor) if need_exts_build?

  executor.begin_section self.class, name, "Building"

  if need_extinit_obj?
    executor.mkdir_p File.dirname(extinit_obj)
    executor.system "ruby",
                    extinit_c_erb,
                    *@user_exts.map { |ext| ext.feature_name(self) },
                    "--cc",
                    toolchain.cc,
                    "--output",
                    extinit_obj
  end
  install_dir = File.join(build_dir, "install")
  if !File.exist?(install_dir) || remake || reconfigure
    unless target.pic?
      # HACK: force static linking for non-pic target
      executor.rm_f File.join(build_dir, "ruby")
    end
    executor.system "make",
                    "-j#{executor.process_count}",
                    "install",
                    "DESTDIR=#{install_dir}",
                    chdir: build_dir
  end

  executor.rm_rf dest_dir
  executor.cp_r install_dir, dest_dir
  @user_exts.each { |ext| ext.do_install_rb(executor, self) }
  executor.system "tar", "cfz", artifact, "-C", @rubies_dir, name

  executor.end_section self.class, name
end

#build_dirObject



277
278
279
# File 'lib/ruby_wasm/build/product/crossruby.rb', line 277

def build_dir
  File.join(@build_dir, @params.target.to_s, name)
end

#build_exts(executor) ⇒ Object



196
197
198
199
200
201
202
# File 'lib/ruby_wasm/build/product/crossruby.rb', line 196

def build_exts(executor)
  @user_exts.each do |prod|
    executor.begin_section prod.class, prod.name, "Building"
    prod.build(executor, self, [])
    executor.end_section prod.class, prod.name
  end
end

#cache_key(digest) ⇒ Object



264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/ruby_wasm/build/product/crossruby.rb', line 264

def cache_key(digest)
  @params.target.cache_key(digest)
  digest << @params.default_exts
  @wasmoptflags.each { |f| digest << f }
  @cppflags.each { |f| digest << f }
  @cflags.each { |f| digest << f }
  @ldflags.each { |f| digest << f }
  @debugflags.each { |f| digest << f }
  @xcflags.each { |f| digest << f }
  @xldflags.each { |f| digest << f }
  @user_exts.each { |ext| ext.cache_key(digest) }
end

#clean(executor) ⇒ Object



253
254
255
256
257
258
# File 'lib/ruby_wasm/build/product/crossruby.rb', line 253

def clean(executor)
  executor.rm_rf dest_dir
  executor.rm_rf build_dir
  executor.rm_rf ext_build_dir
  executor.rm_f artifact
end

#configure(executor, reconfigure: false) ⇒ Object



179
180
181
182
183
184
185
186
# File 'lib/ruby_wasm/build/product/crossruby.rb', line 179

def configure(executor, reconfigure: false)
  if !File.exist?("#{build_dir}/Makefile") || reconfigure
    args = configure_args(RbConfig::CONFIG["host"], toolchain)
    executor.system source.configure_file, *args, chdir: build_dir
  end
  # NOTE: we need rbconfig.rb at configuration time to build user given extensions with mkmf
  executor.system "make", "rbconfig.rb", chdir: build_dir
end

#configure_args(build_triple, toolchain) ⇒ Object



325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
# File 'lib/ruby_wasm/build/product/crossruby.rb', line 325

def configure_args(build_triple, toolchain)
  target = @params.target.triple
  default_exts = @params.default_exts

  ldflags = @ldflags.dup
  xldflags = @xldflags.dup

  args = self.system_triplet_args + ["--build", build_triple]
  args << "--with-static-linked-ext" unless @params.target.pic?
  args << %Q(--with-ext=#{default_exts})
  args << %Q(--with-libyaml-dir=#{@libyaml.install_root})
  args << %Q(--with-zlib-dir=#{@zlib.install_root})
  args << %Q(--with-openssl-dir=#{@openssl.install_root}) if @openssl
  args << %Q(--with-baseruby=#{baseruby_path})

  case target
  when /^wasm32-unknown-wasi/
    xldflags << @wasi_vfs.lib_wasi_vfs_a if @wasi_vfs
    # TODO: Find a way to force cast or update API
    # @type var wasi_sdk_path: untyped
    wasi_sdk_path = @toolchain
    args << %Q(WASMOPT=#{wasi_sdk_path.wasm_opt})
    args << %Q(WASI_SDK_PATH=#{wasi_sdk_path.wasi_sdk_path})
  when "wasm32-unknown-emscripten"
    ldflags.concat(%w[-s MODULARIZE=1])
    env_emcc_ldflags = ENV["RUBY_WASM_EMCC_LDFLAGS"] || ""
    unless env_emcc_ldflags.empty?
      ldflags << env_emcc_ldflags
    end
  else
    raise "unknown target: #{target}"
  end

  args.concat(self.tools_args)
  (@user_exts || []).each { |lib| xldflags << "@#{lib.linklist(self)}" }
  xldflags << extinit_obj if need_extinit_obj?

  cflags = @cflags.dup
  xcflags = @xcflags.dup
  xcflags << "-DWASM_SETJMP_STACK_BUFFER_SIZE=24576"
  xcflags << "-DWASM_FIBER_STACK_BUFFER_SIZE=24576"
  xcflags << "-DWASM_SCAN_STACK_BUFFER_SIZE=24576"

  args << %Q(LDFLAGS=#{ldflags.join(" ")})
  args << %Q(XLDFLAGS=#{xldflags.join(" ")})
  args << %Q(CFLAGS=#{cflags.join(" ")})
  args << %Q(XCFLAGS=#{xcflags.join(" ")})
  args << %Q(debugflags=#{@debugflags.join(" ")})
  args << %Q(cppflags=#{@cppflags.join(" ")})
  unless wasmoptflags.empty?
    args << %Q(wasmoptflags=#{@wasmoptflags.join(" ")})
  end
  args << "--disable-install-doc"
  unless @params.target.pic?
    # TODO: Remove this hack after dropping Ruby 3.2 support
    args << "ac_cv_func_dlopen=no"
  end
  args
end

#dest_dirObject



301
302
303
# File 'lib/ruby_wasm/build/product/crossruby.rb', line 301

def dest_dir
  File.join(@rubies_dir, name)
end

#ext_build_dirObject



281
282
283
# File 'lib/ruby_wasm/build/product/crossruby.rb', line 281

def ext_build_dir
  File.join(@build_dir, @params.target.to_s, name + "-ext")
end

#extinit_c_erbObject



313
314
315
# File 'lib/ruby_wasm/build/product/crossruby.rb', line 313

def extinit_c_erb
  File.expand_path("../crossruby/extinit.c.erb", __FILE__)
end

#extinit_objObject



309
310
311
# File 'lib/ruby_wasm/build/product/crossruby.rb', line 309

def extinit_obj
  "#{ext_build_dir}/extinit.o"
end

#nameObject



260
261
262
# File 'lib/ruby_wasm/build/product/crossruby.rb', line 260

def name
  @params.name
end

#need_extinit_obj?Boolean

Returns:

  • (Boolean)


192
193
194
# File 'lib/ruby_wasm/build/product/crossruby.rb', line 192

def need_extinit_obj?
  need_exts_build? && !@target.pic?
end

#need_exts_build?Boolean

Returns:

  • (Boolean)


188
189
190
# File 'lib/ruby_wasm/build/product/crossruby.rb', line 188

def need_exts_build?
  @user_exts.any?
end

#rbconfig_rbObject



317
318
319
# File 'lib/ruby_wasm/build/product/crossruby.rb', line 317

def rbconfig_rb
  Dir.glob(File.join(dest_dir, "usr/local/lib/ruby/*/wasm32-wasi/rbconfig.rb")).first
end

#with_libyaml(libyaml) ⇒ Object



285
286
287
# File 'lib/ruby_wasm/build/product/crossruby.rb', line 285

def with_libyaml(libyaml)
  @libyaml = libyaml
end

#with_openssl(openssl) ⇒ Object



297
298
299
# File 'lib/ruby_wasm/build/product/crossruby.rb', line 297

def with_openssl(openssl)
  @openssl = openssl
end

#with_wasi_vfs(wasi_vfs) ⇒ Object



293
294
295
# File 'lib/ruby_wasm/build/product/crossruby.rb', line 293

def with_wasi_vfs(wasi_vfs)
  @wasi_vfs = wasi_vfs
end

#with_zlib(zlib) ⇒ Object



289
290
291
# File 'lib/ruby_wasm/build/product/crossruby.rb', line 289

def with_zlib(zlib)
  @zlib = zlib
end