@@ -25,14 +25,20 @@ def self.get(target, build_dir = nil)
25
25
end
26
26
end
27
27
28
- def self . check_executable ( command )
28
+ def self . find_path ( command )
29
29
( ENV [ "PATH" ] || "" )
30
30
. split ( File ::PATH_SEPARATOR )
31
31
. each do |path_dir |
32
32
bin_path = File . join ( path_dir , command )
33
33
return bin_path if File . executable? ( bin_path )
34
34
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
36
42
end
37
43
38
44
%i[ cc ranlib ld ar ] . each do |name |
@@ -48,19 +54,31 @@ def initialize(
48
54
wasi_sdk_path = ENV [ "WASI_SDK_PATH" ] ,
49
55
build_dir : nil ,
50
56
version_major : 14 ,
51
- version_minor : 0
57
+ version_minor : 0 ,
58
+ binaryen_version : 108
52
59
)
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
54
65
if build_dir . nil?
55
66
raise "build_dir is required when WASI_SDK_PATH is not set"
56
67
end
57
68
wasi_sdk_path = File . join ( build_dir , "toolchain" , "wasi-sdk" )
58
- @need_fetch = true
59
69
@version_major = version_major
60
70
@version_minor = version_minor
61
- else
62
- @need_fetch = false
63
71
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
+
64
82
@tools = {
65
83
cc : "#{ wasi_sdk_path } /bin/clang" ,
66
84
ld : "#{ wasi_sdk_path } /bin/clang" ,
@@ -80,7 +98,7 @@ def find_tool(name)
80
98
end
81
99
82
100
def define_task
83
- @task ||= fetch_task ( @wasi_sdk_path )
101
+ @task ||= fetch_task
84
102
end
85
103
86
104
def install_task
@@ -100,19 +118,57 @@ def download_url(version_major, version_minor)
100
118
"https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-#{ version_major } /#{ asset } "
101
119
end
102
120
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 } "
107
139
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 ) } "
112
149
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 ) } "
115
161
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
116
172
end
117
173
end
118
174
@@ -123,8 +179,11 @@ def initialize
123
179
end
124
180
125
181
def define_task
182
+ @task ||= task "emscripten:install"
126
183
end
184
+
127
185
def install_task
186
+ @task
128
187
end
129
188
130
189
def find_tool ( name )
0 commit comments