Class: RubyWasm::WASISDK

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

Instance Attribute Summary

Attributes inherited from Toolchain

#name

Instance Method Summary collapse

Methods inherited from Toolchain

#check_envvar, check_executable, find_path, get

Constructor Details

#initialize(wasi_sdk_path = , build_dir: nil, version_major: 21, version_minor: 0, binaryen_version: 108) ⇒ WASISDK

Returns a new instance of WASISDK.



56
57
58
59
60
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
92
93
94
# File 'lib/ruby_wasm/build/toolchain.rb', line 56

def initialize(
  wasi_sdk_path = ENV["WASI_SDK_PATH"],
  build_dir: nil,
  version_major: 21,
  version_minor: 0,
  binaryen_version: 108
)
  @wasm_opt_path = Toolchain.find_path("wasm-opt")
  @need_fetch_wasi_sdk = wasi_sdk_path.nil?
  @need_fetch_binaryen = @wasm_opt_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_major = version_major
    @version_minor = version_minor
  end

  if @need_fetch_binaryen
    if build_dir.nil?
      raise "build_dir is required when wasm-opt not installed in PATH"
    end
    @binaryen_path = File.join(build_dir, "toolchain", "binaryen")
    @binaryen_version = binaryen_version
    @wasm_opt_path = File.join(@binaryen_path, "bin", "wasm-opt")
  end

  @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

#binaryen_download_url(version) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/ruby_wasm/build/toolchain.rb', line 124

def binaryen_download_url(version)
  assets = [
    [
      /x86_64-linux/,
      "binaryen-version_#{@binaryen_version}-x86_64-linux.tar.gz"
    ],
    [
      /x86_64-darwin/,
      "binaryen-version_#{@binaryen_version}-x86_64-macos.tar.gz"
    ],
    [
      /arm64e?-darwin/,
      "binaryen-version_#{@binaryen_version}-arm64-macos.tar.gz"
    ]
  ]
  asset = assets.find { |os, _| os =~ RUBY_PLATFORM }&.at(1)
  if asset.nil?
    raise "unsupported platform for fetching Binaryen: #{RUBY_PLATFORM}"
  end
  "https://github.com/WebAssembly/binaryen/releases/download/version_#{@binaryen_version}/#{asset}"
end

#download_url(version_major, version_minor) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/ruby_wasm/build/toolchain.rb', line 111

def download_url(version_major, version_minor)
  version = "#{version_major}.#{version_minor}"
  assets = [
    [/x86_64-linux/, "wasi-sdk-#{version}-linux.tar.gz"],
    [/(arm64e?|x86_64)-darwin/, "wasi-sdk-#{version}-macos.tar.gz"]
  ]
  asset = assets.find { |os, _| os =~ RUBY_PLATFORM }&.at(1)
  if asset.nil?
    raise "unsupported platform for fetching WASI SDK: #{RUBY_PLATFORM}"
  end
  "https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-#{version_major}/#{asset}"
end

#find_tool(name) ⇒ Object



96
97
98
99
100
101
# File 'lib/ruby_wasm/build/toolchain.rb', line 96

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

#installObject



174
175
176
177
# File 'lib/ruby_wasm/build/toolchain.rb', line 174

def install
  install_wasi_sdk
  install_binaryen
end

#install_binaryenObject



160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/ruby_wasm/build/toolchain.rb', line 160

def install_binaryen
  return unless @need_fetch_binaryen
  binaryen_tarball = File.expand_path("../binaryen.tar.gz", @binaryen_path)
  unless File.exist? binaryen_tarball
    FileUtils.mkdir_p File.dirname(binaryen_tarball)
    system "curl -L -o #{binaryen_tarball} #{self.binaryen_download_url(@binaryen_version)}"
  end

  unless File.exist? @binaryen_path
    FileUtils.mkdir_p @binaryen_path
    system "tar -C #{@binaryen_path} --strip-component 1 -xzf #{binaryen_tarball}"
  end
end

#install_wasi_sdkObject



146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/ruby_wasm/build/toolchain.rb', line 146

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

#wasi_sdk_pathObject



107
108
109
# File 'lib/ruby_wasm/build/toolchain.rb', line 107

def wasi_sdk_path
  @wasi_sdk_path
end

#wasm_optObject



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

def wasm_opt
  @wasm_opt_path
end