Class: RubyWasm::CrossRubyExtProduct

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(srcdir, toolchain, features:, ext_relative_path: nil) ⇒ CrossRubyExtProduct

Returns a new instance of CrossRubyExtProduct.



8
9
10
11
12
13
14
15
# File 'lib/ruby_wasm/build/product/crossruby.rb', line 8

def initialize(srcdir, toolchain, features:, ext_relative_path: nil)
  @srcdir, @toolchain = srcdir, toolchain
  # ext_relative_path is relative path from build dir
  # e.g. cgi-0.3.6/ext/cgi/escape
  @ext_relative_path = ext_relative_path || File.basename(srcdir)
  @features = features
  @name = @ext_relative_path
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/ruby_wasm/build/product/crossruby.rb', line 6

def name
  @name
end

Instance Method Details

#build(executor, crossruby, extra_mkargs = []) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/ruby_wasm/build/product/crossruby.rb', line 45

def build(executor, crossruby, extra_mkargs = [])
  objdir = product_build_dir crossruby
  executor.mkdir_p objdir
  do_extconf executor, crossruby

  executor.system "make", "-C", objdir, *make_args(crossruby), "clean"
  build_target = crossruby.target.pic? ? "install-so" : "static"
  executor.system "make",
                  "-j#{executor.process_count}",
                  "-C",
                  "#{objdir}",
                  *make_args(crossruby),
                  build_target,
                  *extra_mkargs
  # A ext can provide link args by link.filelist. It contains only built archive file by default.
  unless File.exist?(linklist(crossruby))
    executor.write(
      linklist(crossruby),
      Dir.glob("#{objdir}/*.a").join("\n")
    )
  end
end

#cache_key(digest) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
# File 'lib/ruby_wasm/build/product/crossruby.rb', line 128

def cache_key(digest)
  digest << @name
  # Compute hash value of files under srcdir
  Dir
    .glob("#{@srcdir}/**/*", File::FNM_DOTMATCH)
    .each do |f|
      next if File.directory?(f)
      digest << f
      digest << File.read(f)
    end
end

#do_extconf(executor, crossruby) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/ruby_wasm/build/product/crossruby.rb', line 68

def do_extconf(executor, crossruby)
  unless crossruby.target.pic?
    self.do_legacy_extconf(executor, crossruby)
    return
  end
  objdir = product_build_dir crossruby
  rbconfig_rb = Dir.glob(File.join(crossruby.dest_dir, "usr/local/lib/ruby/*/wasm32-wasi/rbconfig.rb")).first
  raise "rbconfig.rb not found" unless rbconfig_rb
  extconf_args = [
    "-C", objdir,
    "#{@srcdir}/extconf.rb",
    "--target-rbconfig=#{rbconfig_rb}",
  ]
  extconf_args << "--enable-component-model" if @features.support_component_model?
  executor.system crossruby.baseruby_path, *extconf_args
end

#do_install_rb(executor, crossruby) ⇒ Object



123
124
125
126
# File 'lib/ruby_wasm/build/product/crossruby.rb', line 123

def do_install_rb(executor, crossruby)
  objdir = product_build_dir crossruby
  executor.system "make", "-C", objdir, *make_args(crossruby), "install-rb"
end

#do_legacy_extconf(executor, crossruby) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/ruby_wasm/build/product/crossruby.rb', line 85

def do_legacy_extconf(executor, crossruby)
  objdir = product_build_dir crossruby
  source = crossruby.source
  extconf_args = [
    "-C", objdir,
    "--disable=gems",
    # HACK: top_srcdir is required to find ruby headers
    "-e",
    %Q($top_srcdir="#{source.src_dir}"),
    # HACK: extout is required to find config.h
    "-e",
    %Q($extout="#{crossruby.build_dir}/.ext"),
    # HACK: force static ext build by imitating extmk
    "-e",
    "$static = true; trace_var(:$static) {|v| $static = true }",
    # HACK: $0 should be extconf.rb path due to mkmf source file detection
    # and we want to insert some hacks before it. But -e and $0 cannot be
    # used together, so we rewrite $0 in -e.
    "-e",
    %Q($0="#{@srcdir}/extconf.rb"),
    "-e",
    %Q(require_relative "#{@srcdir}/extconf.rb"),
    # HACK: extract "$target" from extconf.rb to get a full target name
    # like "cgi/escape" instead of "escape"
    "-e",
    %Q(require "json"; File.write("#{(crossruby)}", JSON.dump({target: $target}))),
    "-I#{crossruby.build_dir}",
    "--",
  ]
  extconf_args << "--enable-component-model" if @features.support_component_model?
  # Clear RUBYOPT to avoid loading unrelated bundle setup
  executor.system crossruby.baseruby_path,
                  *extconf_args,
                  env: {
                    "RUBYOPT" => ""
                  }
end

#feature_name(crossruby) ⇒ Object



29
30
31
32
# File 'lib/ruby_wasm/build/product/crossruby.rb', line 29

def feature_name(crossruby)
   = JSON.parse(File.read((crossruby)))
  ["target"]
end


21
22
23
# File 'lib/ruby_wasm/build/product/crossruby.rb', line 21

def linklist(crossruby)
  File.join(product_build_dir(crossruby), "link.filelist")
end

#make_args(crossruby) ⇒ Object



34
35
36
37
38
39
40
41
42
43
# File 'lib/ruby_wasm/build/product/crossruby.rb', line 34

def make_args(crossruby)
  make_args = []
  make_args << "CC=#{@toolchain.cc}"
  make_args << "CXX=#{@toolchain.cc}"
  make_args << "LD=#{@toolchain.ld}"
  make_args << "AR=#{@toolchain.ar}"
  make_args << "RANLIB=#{@toolchain.ranlib}"

  make_args
end

#metadata_json(crossruby) ⇒ Object



25
26
27
# File 'lib/ruby_wasm/build/product/crossruby.rb', line 25

def (crossruby)
  File.join(product_build_dir(crossruby), "rbwasm.metadata.json")
end

#product_build_dir(crossruby) ⇒ Object



17
18
19
# File 'lib/ruby_wasm/build/product/crossruby.rb', line 17

def product_build_dir(crossruby)
  File.join(crossruby.ext_build_dir, @ext_relative_path)
end