@@ -60,10 +60,7 @@ def initialize(
6060 version_minor : 0 ,
6161 binaryen_version : 108
6262 )
63- @wasm_opt_path = Toolchain . find_path ( "wasm-opt" )
6463 @need_fetch_wasi_sdk = wasi_sdk_path . nil?
65- @need_fetch_binaryen = @wasm_opt_path . nil?
66-
6764 if @need_fetch_wasi_sdk
6865 if build_dir . nil?
6966 raise "build_dir is required when WASI_SDK_PATH is not set"
@@ -73,14 +70,7 @@ def initialize(
7370 @version_minor = version_minor
7471 end
7572
76- if @need_fetch_binaryen
77- if build_dir . nil?
78- raise "build_dir is required when wasm-opt not installed in PATH"
79- end
80- @binaryen_path = File . join ( build_dir , "toolchain" , "binaryen" )
81- @binaryen_version = binaryen_version
82- @wasm_opt_path = File . join ( @binaryen_path , "bin" , "wasm-opt" )
83- end
73+ @binaryen = Binaryen . new ( build_dir : build_dir , binaryen_version : binaryen_version )
8474
8575 @tools = {
8676 cc : "#{ wasi_sdk_path } /bin/clang" ,
@@ -101,7 +91,7 @@ def find_tool(name)
10191 end
10292
10393 def wasm_opt
104- @wasm_opt_path
94+ @binaryen . wasm_opt
10595 end
10696
10797 def wasi_sdk_path
@@ -121,7 +111,53 @@ def download_url(version_major, version_minor)
121111 "https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-#{ version_major } /#{ asset } "
122112 end
123113
124- def binaryen_download_url ( version )
114+ def install_wasi_sdk
115+ return unless @need_fetch_wasi_sdk
116+ wasi_sdk_tarball =
117+ File . join ( File . dirname ( @wasi_sdk_path ) , "wasi-sdk.tar.gz" )
118+ unless File . exist? wasi_sdk_tarball
119+ FileUtils . mkdir_p File . dirname ( wasi_sdk_tarball )
120+ system "curl -L -o #{ wasi_sdk_tarball } #{ self . download_url ( @version_major , @version_minor ) } "
121+ end
122+ unless File . exist? @wasi_sdk_path
123+ FileUtils . mkdir_p @wasi_sdk_path
124+ system "tar -C #{ @wasi_sdk_path } --strip-component 1 -xzf #{ wasi_sdk_tarball } "
125+ end
126+ end
127+
128+ def install
129+ install_wasi_sdk
130+ @binaryen . install
131+ end
132+ end
133+
134+ class Binaryen
135+ def initialize ( build_dir : nil , binaryen_version : 108 )
136+ @wasm_opt_path = Toolchain . find_path ( "wasm-opt" )
137+ @need_fetch_binaryen = @wasm_opt_path . nil?
138+ if @need_fetch_binaryen
139+ if build_dir . nil?
140+ raise "build_dir is required when wasm-opt not installed in PATH"
141+ end
142+ @binaryen_path = File . join ( build_dir , "toolchain" , "binaryen" )
143+ @binaryen_version = binaryen_version
144+ @wasm_opt_path = File . join ( @binaryen_path , "bin" , "wasm-opt" )
145+ end
146+ end
147+
148+ def wasm_opt
149+ @wasm_opt_path
150+ end
151+
152+ def binaryen_path
153+ @binaryen_path
154+ end
155+
156+ def binaryen_version
157+ @binaryen_version
158+ end
159+
160+ def download_url ( version )
125161 assets = [
126162 [
127163 /x86_64-linux/ ,
@@ -143,26 +179,12 @@ def binaryen_download_url(version)
143179 "https://github.com/WebAssembly/binaryen/releases/download/version_#{ @binaryen_version } /#{ asset } "
144180 end
145181
146- def install_wasi_sdk
147- return unless @need_fetch_wasi_sdk
148- wasi_sdk_tarball =
149- File . join ( File . dirname ( @wasi_sdk_path ) , "wasi-sdk.tar.gz" )
150- unless File . exist? wasi_sdk_tarball
151- FileUtils . mkdir_p File . dirname ( wasi_sdk_tarball )
152- system "curl -L -o #{ wasi_sdk_tarball } #{ self . download_url ( @version_major , @version_minor ) } "
153- end
154- unless File . exist? @wasi_sdk_path
155- FileUtils . mkdir_p @wasi_sdk_path
156- system "tar -C #{ @wasi_sdk_path } --strip-component 1 -xzf #{ wasi_sdk_tarball } "
157- end
158- end
159-
160- def install_binaryen
182+ def install
161183 return unless @need_fetch_binaryen
162184 binaryen_tarball = File . expand_path ( "../binaryen.tar.gz" , @binaryen_path )
163185 unless File . exist? binaryen_tarball
164186 FileUtils . mkdir_p File . dirname ( binaryen_tarball )
165- system "curl -L -o #{ binaryen_tarball } #{ self . binaryen_download_url ( @binaryen_version ) } "
187+ system "curl -L -o #{ binaryen_tarball } #{ self . download_url ( @binaryen_version ) } "
166188 end
167189
168190 unless File . exist? @binaryen_path
@@ -171,10 +193,6 @@ def install_binaryen
171193 end
172194 end
173195
174- def install
175- install_wasi_sdk
176- install_binaryen
177- end
178196 end
179197
180198 class Emscripten < Toolchain
0 commit comments