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.



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/ruby_wasm/build/product/crossruby.rb', line 152

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.



143
144
145
# File 'lib/ruby_wasm/build/product/crossruby.rb', line 143

def cflags
  @cflags
end

#cppflagsObject

Returns the value of attribute cppflags.



143
144
145
# File 'lib/ruby_wasm/build/product/crossruby.rb', line 143

def cppflags
  @cppflags
end

#debugflagsObject

Returns the value of attribute debugflags.



143
144
145
# File 'lib/ruby_wasm/build/product/crossruby.rb', line 143

def debugflags
  @debugflags
end

#ldflagsObject

Returns the value of attribute ldflags.



143
144
145
# File 'lib/ruby_wasm/build/product/crossruby.rb', line 143

def ldflags
  @ldflags
end

#sourceObject (readonly)

Returns the value of attribute source.



142
143
144
# File 'lib/ruby_wasm/build/product/crossruby.rb', line 142

def source
  @source
end

#targetObject (readonly)

Returns the value of attribute target.



142
143
144
# File 'lib/ruby_wasm/build/product/crossruby.rb', line 142

def target
  @target
end

#toolchainObject (readonly)

Returns the value of attribute toolchain.



142
143
144
# File 'lib/ruby_wasm/build/product/crossruby.rb', line 142

def toolchain
  @toolchain
end

#user_extsObject

Returns the value of attribute user_exts.



143
144
145
# File 'lib/ruby_wasm/build/product/crossruby.rb', line 143

def user_exts
  @user_exts
end

#wasmoptflagsObject

Returns the value of attribute wasmoptflags.



143
144
145
# File 'lib/ruby_wasm/build/product/crossruby.rb', line 143

def wasmoptflags
  @wasmoptflags
end

#xcflagsObject

Returns the value of attribute xcflags.



143
144
145
# File 'lib/ruby_wasm/build/product/crossruby.rb', line 143

def xcflags
  @xcflags
end

#xldflagsObject

Returns the value of attribute xldflags.



143
144
145
# File 'lib/ruby_wasm/build/product/crossruby.rb', line 143

def xldflags
  @xldflags
end

Instance Method Details

#artifactObject



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

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

#baseruby_pathObject



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

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

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



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
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/ruby_wasm/build/product/crossruby.rb', line 195

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
    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



264
265
266
# File 'lib/ruby_wasm/build/product/crossruby.rb', line 264

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

#build_exts(executor) ⇒ Object



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

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



251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/ruby_wasm/build/product/crossruby.rb', line 251

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



240
241
242
243
244
245
# File 'lib/ruby_wasm/build/product/crossruby.rb', line 240

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



170
171
172
173
174
175
176
177
# File 'lib/ruby_wasm/build/product/crossruby.rb', line 170

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



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
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
# File 'lib/ruby_wasm/build/product/crossruby.rb', line 308

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



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

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

#ext_build_dirObject



268
269
270
# File 'lib/ruby_wasm/build/product/crossruby.rb', line 268

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

#extinit_c_erbObject



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

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

#extinit_objObject



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

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

#nameObject



247
248
249
# File 'lib/ruby_wasm/build/product/crossruby.rb', line 247

def name
  @params.name
end

#need_extinit_obj?Boolean

Returns:

  • (Boolean)


183
184
185
# File 'lib/ruby_wasm/build/product/crossruby.rb', line 183

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

#need_exts_build?Boolean

Returns:

  • (Boolean)


179
180
181
# File 'lib/ruby_wasm/build/product/crossruby.rb', line 179

def need_exts_build?
  @user_exts.any?
end

#with_libyaml(libyaml) ⇒ Object



272
273
274
# File 'lib/ruby_wasm/build/product/crossruby.rb', line 272

def with_libyaml(libyaml)
  @libyaml = libyaml
end

#with_openssl(openssl) ⇒ Object



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

def with_openssl(openssl)
  @openssl = openssl
end

#with_wasi_vfs(wasi_vfs) ⇒ Object



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

def with_wasi_vfs(wasi_vfs)
  @wasi_vfs = wasi_vfs
end

#with_zlib(zlib) ⇒ Object



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

def with_zlib(zlib)
  @zlib = zlib
end