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, user_exts: []) ⇒ CrossRubyProduct

Returns a new instance of CrossRubyProduct.



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/ruby_wasm/build/product/crossruby.rb', line 111

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

Instance Attribute Details

#cflagsObject

Returns the value of attribute cflags.



102
103
104
# File 'lib/ruby_wasm/build/product/crossruby.rb', line 102

def cflags
  @cflags
end

#cppflagsObject

Returns the value of attribute cppflags.



102
103
104
# File 'lib/ruby_wasm/build/product/crossruby.rb', line 102

def cppflags
  @cppflags
end

#debugflagsObject

Returns the value of attribute debugflags.



102
103
104
# File 'lib/ruby_wasm/build/product/crossruby.rb', line 102

def debugflags
  @debugflags
end

#ldflagsObject

Returns the value of attribute ldflags.



102
103
104
# File 'lib/ruby_wasm/build/product/crossruby.rb', line 102

def ldflags
  @ldflags
end

#sourceObject (readonly)

Returns the value of attribute source.



101
102
103
# File 'lib/ruby_wasm/build/product/crossruby.rb', line 101

def source
  @source
end

#toolchainObject (readonly)

Returns the value of attribute toolchain.



101
102
103
# File 'lib/ruby_wasm/build/product/crossruby.rb', line 101

def toolchain
  @toolchain
end

#user_extsObject

Returns the value of attribute user_exts.



102
103
104
# File 'lib/ruby_wasm/build/product/crossruby.rb', line 102

def user_exts
  @user_exts
end

#wasmoptflagsObject

Returns the value of attribute wasmoptflags.



102
103
104
# File 'lib/ruby_wasm/build/product/crossruby.rb', line 102

def wasmoptflags
  @wasmoptflags
end

#xcflagsObject

Returns the value of attribute xcflags.



102
103
104
# File 'lib/ruby_wasm/build/product/crossruby.rb', line 102

def xcflags
  @xcflags
end

#xldflagsObject

Returns the value of attribute xldflags.



102
103
104
# File 'lib/ruby_wasm/build/product/crossruby.rb', line 102

def xldflags
  @xldflags
end

Instance Method Details

#artifactObject



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

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

#baseruby_pathObject



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

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

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



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
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/ruby_wasm/build/product/crossruby.rb', line 154

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

  executor.begin_section self.class, name, "Building"
  executor.mkdir_p File.dirname(extinit_obj)
  executor.system "ruby",
                  extinit_c_erb,
                  *@user_exts.map(&:name),
                  "--cc",
                  toolchain.cc,
                  "--output",
                  extinit_obj
  install_dir = File.join(build_dir, "install")
  if !File.exist?(install_dir) || remake || reconfigure
    executor.system "make",
                    "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", name

  executor.end_section self.class, name
end

#build_dirObject



218
219
220
# File 'lib/ruby_wasm/build/product/crossruby.rb', line 218

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

#build_exts(executor) ⇒ Object



146
147
148
149
150
151
152
# File 'lib/ruby_wasm/build/product/crossruby.rb', line 146

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



205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/ruby_wasm/build/product/crossruby.rb', line 205

def cache_key(digest)
  digest << @params.target
  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



194
195
196
197
198
199
# File 'lib/ruby_wasm/build/product/crossruby.rb', line 194

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



137
138
139
140
141
142
143
144
# File 'lib/ruby_wasm/build/product/crossruby.rb', line 137

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



263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
# File 'lib/ruby_wasm/build/product/crossruby.rb', line 263

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

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

  args = self.system_triplet_args + ["--build", build_triple]
  args << "--with-static-linked-ext"
  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])
  else
    raise "unknown target: #{target}"
  end

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

  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(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"
  args
end

#dest_dirObject



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

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

#ext_build_dirObject



222
223
224
# File 'lib/ruby_wasm/build/product/crossruby.rb', line 222

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

#extinit_c_erbObject



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

def extinit_c_erb
  lib_root = File.expand_path("../../../../..", __FILE__)
  File.join(lib_root, "ext", "extinit.c.erb")
end

#extinit_objObject



250
251
252
# File 'lib/ruby_wasm/build/product/crossruby.rb', line 250

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

#nameObject



201
202
203
# File 'lib/ruby_wasm/build/product/crossruby.rb', line 201

def name
  @params.name
end

#with_libyaml(libyaml) ⇒ Object



226
227
228
# File 'lib/ruby_wasm/build/product/crossruby.rb', line 226

def with_libyaml(libyaml)
  @libyaml = libyaml
end

#with_openssl(openssl) ⇒ Object



238
239
240
# File 'lib/ruby_wasm/build/product/crossruby.rb', line 238

def with_openssl(openssl)
  @openssl = openssl
end

#with_wasi_vfs(wasi_vfs) ⇒ Object



234
235
236
# File 'lib/ruby_wasm/build/product/crossruby.rb', line 234

def with_wasi_vfs(wasi_vfs)
  @wasi_vfs = wasi_vfs
end

#with_zlib(zlib) ⇒ Object



230
231
232
# File 'lib/ruby_wasm/build/product/crossruby.rb', line 230

def with_zlib(zlib)
  @zlib = zlib
end