Class: RubyWasm::WASISDK

Inherits:
Toolchain show all
Defined in:
lib/ruby_wasm/build/toolchain.rb,
sig/ruby_wasm/build.rbs

Instance Attribute Summary

Attributes inherited from Toolchain

#name

Instance Method Summary collapse

Methods inherited from Toolchain

#ar, #cc, #check_envvar, check_executable, #cxx, find_path, get, #ld, #ranlib

Constructor Details

#initialize(wasi_sdk_path = ENV["WASI_SDK_PATH"], build_dir: nil, version: "23.0", binaryen_version: 108) ⇒ WASISDK

Returns a new instance of WASISDK.

Parameters:

  • wasi_sdk_path (String, nil) (defaults to: ENV["WASI_SDK_PATH"])
  • build_dir: (String, nil) (defaults to: nil)
  • version: (String) (defaults to: "23.0")
  • binaryen_version: (Integer) (defaults to: 108)


61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/ruby_wasm/build/toolchain.rb', line 61

def initialize(
  wasi_sdk_path = ENV["WASI_SDK_PATH"],
  build_dir: nil,
  version: "23.0",
  binaryen_version: 108
)
  @need_fetch_wasi_sdk = wasi_sdk_path.nil?
  if @need_fetch_wasi_sdk
    if build_dir.nil?
      raise "build_dir is required when WASI_SDK_PATH is not set"
    end
    wasi_sdk_path = File.join(build_dir, "toolchain", "wasi-sdk-#{version}")
    if version.nil?
      raise "version is required when WASI_SDK_PATH is not set"
    end
    @version = version
  end

  @binaryen =
    Binaryen.new(build_dir: build_dir, binaryen_version: binaryen_version)

  @tools = {
    cc: "#{wasi_sdk_path}/bin/clang",
    cxx: "#{wasi_sdk_path}/bin/clang++",
    ld: "#{wasi_sdk_path}/bin/clang",
    ar: "#{wasi_sdk_path}/bin/llvm-ar",
    ranlib: "#{wasi_sdk_path}/bin/llvm-ranlib"
  }
  @wasi_sdk_path = wasi_sdk_path
  @name = "wasi-sdk"
end

Instance Method Details

#download_urlString

Returns:

  • (String)


108
109
110
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/ruby_wasm/build/toolchain.rb', line 108

def download_url
  major, _ = @version.split(".").map(&:to_i)
  # @type var assets: Array[[Regexp, Array[String]]]
  assets = [
    [
      /x86_64-linux/,
      [
        "wasi-sdk-#{@version}-x86_64-linux.tar.gz",
        # For wasi-sdk version < 23.0
        "wasi-sdk-#{@version}-linux.tar.gz"
      ]
    ],
    [
      /arm64e?-darwin/,
      [
        "wasi-sdk-#{@version}-arm64-macos.tar.gz",
        # For wasi-sdk version < 23.0
        "wasi-sdk-#{@version}-macos.tar.gz"
      ]
    ],
    [
      /x86_64-darwin/,
      [
        "wasi-sdk-#{@version}-x86_64-macos.tar.gz",
        # For wasi-sdk version < 23.0
        "wasi-sdk-#{@version}-macos.tar.gz"
      ]
    ]
  ]
  asset = assets.find { |os, candidates| os =~ RUBY_PLATFORM }
  if asset.nil?
    raise "unsupported platform for fetching WASI SDK: #{RUBY_PLATFORM}"
  end
  _, candidates = asset
  candidates_urls =
    candidates.map do |candidate|
      "https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-#{major}/#{candidate}"
    end
  require "net/http"
  # Find an asset that exists by checking HEAD response to see if the asset exists
  candidates_urls.each do |url_str|
    # @type var url: URI::HTTPS
    url = URI.parse(url_str)
    ok =
      __skip__ = Net::HTTP.start(
        url.host,
        url.port,
        use_ssl: url.scheme == "https"
      ) do |http|
        response = http.head(url.request_uri)
        next response.code == "302"
      end
    return url_str if ok
  end
  raise "WASI SDK asset not found: #{candidates_urls.join(", ")}"
end

#find_tool(name) ⇒ String

Parameters:

  • name (Symbol)

Returns:

  • (String)


93
94
95
96
97
98
# File 'lib/ruby_wasm/build/toolchain.rb', line 93

def find_tool(name)
  if !File.exist?(@tools[name]) && !ENV["WASI_SDK_PATH"].nil?
    raise "missing tool '#{name}' at #{@tools[name]}"
  end
  @tools[name]
end

#install(executor) ⇒ void

This method returns an undefined value.

Parameters:



189
190
191
192
# File 'lib/ruby_wasm/build/toolchain.rb', line 189

def install(executor)
  install_wasi_sdk(executor)
  @binaryen.install(executor)
end

#install_wasi_sdk(executor) ⇒ void

This method returns an undefined value.

Parameters:



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/ruby_wasm/build/toolchain.rb', line 165

def install_wasi_sdk(executor)
  return unless @need_fetch_wasi_sdk
  wasi_sdk_tarball =
    File.join(File.dirname(@wasi_sdk_path), "wasi-sdk-#{@version}.tar.gz")
  unless File.exist? wasi_sdk_tarball
    FileUtils.mkdir_p File.dirname(wasi_sdk_tarball)
    executor.system "curl",
                    "-fsSL",
                    "-o",
                    wasi_sdk_tarball,
                    self.download_url
  end
  unless File.exist? @wasi_sdk_path
    FileUtils.mkdir_p @wasi_sdk_path
    executor.system "tar",
                    "-C",
                    @wasi_sdk_path,
                    "--strip-component",
                    "1",
                    "-xzf",
                    wasi_sdk_tarball
  end
end

#wasi_sdk_pathString

Returns:

  • (String)


104
105
106
# File 'lib/ruby_wasm/build/toolchain.rb', line 104

def wasi_sdk_path
  @wasi_sdk_path
end

#wasm_optString

Returns:

  • (String)


100
101
102
# File 'lib/ruby_wasm/build/toolchain.rb', line 100

def wasm_opt
  @binaryen.wasm_opt
end