Skip to content

Commit d40491a

Browse files
install binaryen wasm-opt if not found
1 parent 5ac225d commit d40491a

File tree

1 file changed

+77
-18
lines changed

1 file changed

+77
-18
lines changed

lib/ruby_wasm/build_system/toolchain.rb

Lines changed: 77 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,20 @@ def self.get(target, build_dir = nil)
2525
end
2626
end
2727

28-
def self.check_executable(command)
28+
def self.find_path(command)
2929
(ENV["PATH"] || "")
3030
.split(File::PATH_SEPARATOR)
3131
.each do |path_dir|
3232
bin_path = File.join(path_dir, command)
3333
return bin_path if File.executable?(bin_path)
3434
end
35-
raise "missing executable: #{command}"
35+
nil
36+
end
37+
38+
def self.check_executable(command)
39+
tool = find_path(command)
40+
raise "missing executable: #{command}" unless tool
41+
tool
3642
end
3743

3844
%i[cc ranlib ld ar].each do |name|
@@ -48,19 +54,31 @@ def initialize(
4854
wasi_sdk_path = ENV["WASI_SDK_PATH"],
4955
build_dir: nil,
5056
version_major: 14,
51-
version_minor: 0
57+
version_minor: 0,
58+
binaryen_version: 108
5259
)
53-
if wasi_sdk_path.nil?
60+
@wasm_opt_path = Toolchain.find_path("wasm-opt")
61+
@need_fetch_wasi_sdk = wasi_sdk_path.nil?
62+
@need_fetch_binaryen = @wasm_opt_path.nil?
63+
64+
if @need_fetch_wasi_sdk
5465
if build_dir.nil?
5566
raise "build_dir is required when WASI_SDK_PATH is not set"
5667
end
5768
wasi_sdk_path = File.join(build_dir, "toolchain", "wasi-sdk")
58-
@need_fetch = true
5969
@version_major = version_major
6070
@version_minor = version_minor
61-
else
62-
@need_fetch = false
6371
end
72+
73+
if @need_fetch_binaryen
74+
if build_dir.nil?
75+
raise "build_dir is required when wasm-opt not installed in PATH"
76+
end
77+
@wasm_opt_path =
78+
File.join(build_dir, "toolchain", "binaryen", "bin", "wasm-opt")
79+
@binaryen_version = binaryen_version
80+
end
81+
6482
@tools = {
6583
cc: "#{wasi_sdk_path}/bin/clang",
6684
ld: "#{wasi_sdk_path}/bin/clang",
@@ -80,7 +98,7 @@ def find_tool(name)
8098
end
8199

82100
def define_task
83-
@task ||= fetch_task(@wasi_sdk_path)
101+
@task ||= fetch_task
84102
end
85103

86104
def install_task
@@ -100,19 +118,57 @@ def download_url(version_major, version_minor)
100118
"https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-#{version_major}/#{asset}"
101119
end
102120

103-
def fetch_task(output_dir)
104-
unless @need_fetch
105-
return task "wasi-sdk:fetch" => [] do
106-
end
121+
def binaryen_download_url(version)
122+
assets = [
123+
[
124+
/x86_64-linux/,
125+
"binaryen-version_#{@binaryen_version}-x86_64-linux.tar.gz"
126+
],
127+
[
128+
/x86_64-darwin/,
129+
"binaryen-version_#{@binaryen_version}-x86_64-macos.tar.gz"
130+
],
131+
[
132+
/arm64-darwin/,
133+
"binaryen-version_#{@binaryen_version}-arm64-macos.tar.gz"
134+
]
135+
]
136+
asset = assets.find { |os, _| os =~ RUBY_PLATFORM }&.at(1)
137+
if asset.nil?
138+
raise "unsupported platform for fetching Binaryen: #{RUBY_PLATFORM}"
107139
end
108-
tarball = File.join(File.dirname(output_dir), "wasi-sdk.tar.gz")
109-
file tarball do
110-
mkdir_p output_dir
111-
sh "curl -L -o #{tarball} #{self.download_url(@version_major, @version_minor)}"
140+
"https://github.com/WebAssembly/binaryen/releases/download/version_#{@binaryen_version}/#{asset}"
141+
end
142+
143+
def fetch_task
144+
wasi_sdk_tarball =
145+
File.join(File.dirname(@wasi_sdk_path), "wasi-sdk.tar.gz")
146+
file wasi_sdk_tarball do
147+
mkdir_p @wasi_sdk_path
148+
sh "curl -L -o #{wasi_sdk_tarball} #{self.download_url(@version_major, @version_minor)}"
112149
end
113-
task "wasi-sdk:fetch" => tarball do
114-
sh "tar -C #{output_dir} --strip-component 1 -xzf #{tarball}"
150+
wasi_sdk =
151+
file_create @wasi_sdk_path => wasi_sdk_tarball do
152+
sh "tar -C #{@wasi_sdk_path} --strip-component 1 -xzf #{wasi_sdk_tarball}"
153+
end
154+
155+
binaryen_path = File.expand_path("../..", @wasm_opt_path)
156+
binaryen_tarball =
157+
File.expand_path("../../../binaryen.tar.gz", @wasm_opt_path)
158+
file binaryen_tarball do
159+
mkdir_p File.dirname(binaryen_tarball)
160+
sh "curl -L -o #{binaryen_tarball} #{self.binaryen_download_url(@binaryen_version)}"
115161
end
162+
binaryen =
163+
file_create binaryen_path => binaryen_tarball do
164+
mkdir_p binaryen_path
165+
sh "tar -C #{binaryen_path} --strip-component 1 -xzf #{binaryen_tarball}"
166+
end
167+
168+
required = []
169+
required << wasi_sdk if @need_fetch_wasi_sdk
170+
required << binaryen if @need_fetch_binaryen
171+
multitask "wasi-sdk:install" => required
116172
end
117173
end
118174

@@ -123,8 +179,11 @@ def initialize
123179
end
124180

125181
def define_task
182+
@task ||= task "emscripten:install"
126183
end
184+
127185
def install_task
186+
@task
128187
end
129188

130189
def find_tool(name)

0 commit comments

Comments
 (0)