14
14
15
15
from . import cmake_product
16
16
from . import llvm
17
+ from . import product
17
18
from . import swift
18
19
from . import wasisysroot
19
20
from . import wasmkit
21
+ from .. import shell
20
22
21
23
22
24
class WasmStdlib (cmake_product .CMakeProduct ):
@@ -42,6 +44,7 @@ def build(self, host_target):
42
44
self ._build (host_target , 'wasm32-wasi' )
43
45
44
46
def _build (self , host_target , target_triple ):
47
+ self .cmake_options .define ('CMAKE_INSTALL_PREFIX:PATH' , 'usr' )
45
48
self .cmake_options .define ('CMAKE_BUILD_TYPE:STRING' , self ._build_variant )
46
49
self .cmake_options .define (
47
50
'SWIFT_STDLIB_BUILD_TYPE:STRING' , self ._build_variant )
@@ -208,3 +211,94 @@ def add_extra_cmake_options(self):
208
211
'-Xcc;-mthread-model;-Xcc;posix;'
209
212
'-Xcc;-pthread;-Xcc;-ftls-model=local-exec' )
210
213
self .cmake_options .define ('SWIFT_ENABLE_WASI_THREADS:BOOL' , 'TRUE' )
214
+
215
+
216
+ class WasmSwiftSDK (product .Product ):
217
+ @classmethod
218
+ def product_source_name (cls ):
219
+ return "swift-sdk-generator"
220
+
221
+ @classmethod
222
+ def is_build_script_impl_product (cls ):
223
+ return False
224
+
225
+ @classmethod
226
+ def is_before_build_script_impl_product (cls ):
227
+ return False
228
+
229
+ def should_build (self , host_target ):
230
+ return self .args .build_wasmstdlib
231
+
232
+ def should_test (self , host_target ):
233
+ return False
234
+
235
+ def _target_package_path (self , target_triple ):
236
+ return os .path .join (self .build_dir , 'Toolchains' , target_triple )
237
+
238
+ def _build_target_package (self , target_triple ,
239
+ stdlib_build_path , llvm_runtime_libs_build_path ):
240
+
241
+ dest_dir = self ._target_package_path (target_triple )
242
+ shell .rmtree (dest_dir )
243
+ shell .makedirs (dest_dir )
244
+
245
+ # Build toolchain package for standalone stdlib
246
+ with shell .pushd (stdlib_build_path ):
247
+ shell .call ([self .toolchain .cmake , '--install' , '.' ],
248
+ env = {'DESTDIR' : dest_dir })
249
+
250
+ # Copy LLVM runtime libraries
251
+ with shell .pushd (llvm_runtime_libs_build_path ):
252
+ clang_dest_dir = os .path .join (dest_dir , 'usr/lib/swift_static/clang' )
253
+ shell .call ([self .toolchain .cmake , '--install' , '.' ,
254
+ '--component' , 'clang_rt.builtins-wasm32' ],
255
+ env = {'DESTDIR' : clang_dest_dir })
256
+ return dest_dir
257
+
258
+ def build (self , host_target ):
259
+ build_root = os .path .dirname (self .build_dir )
260
+ llvm_runtime_libs_build_path = os .path .join (
261
+ build_root , '%s-%s' % ('wasmllvmruntimelibs' , host_target ))
262
+
263
+ target_packages = []
264
+ for target_triple , build_basename in [
265
+ ('wasm32-unknown-wasi' , 'wasmstdlib' ),
266
+ # TODO: Enable threads stdlib once sdk-generator supports multi-target SDK
267
+ # ('wasm32-unknown-wasip1-threads', 'wasmthreadsstdlib'),
268
+ ]:
269
+ stdlib_build_path = os .path .join (
270
+ build_root , '%s-%s' % (build_basename , host_target ))
271
+ package_path = self ._build_target_package (
272
+ target_triple , stdlib_build_path , llvm_runtime_libs_build_path )
273
+ target_packages .append ((target_triple , package_path ))
274
+
275
+ swiftc_path = os .path .abspath (self .toolchain .swiftc )
276
+ toolchain_path = os .path .dirname (os .path .dirname (swiftc_path ))
277
+ swift_run = os .path .join (toolchain_path , 'bin' , 'swift-run' )
278
+
279
+ swift_version = os .environ .get ('TOOLCHAIN_VERSION' ,
280
+ 'swift-DEVELOPMENT-SNAPSHOT' ).lstrip ('swift-' )
281
+ run_args = [
282
+ swift_run ,
283
+ '--package-path' , self .source_dir ,
284
+ '--build-path' , self .build_dir ,
285
+ 'swift-sdk-generator' ,
286
+ 'make-wasm-sdk' ,
287
+ '--wasi-sysroot' , wasisysroot .WASILibc .sysroot_install_path (build_root ),
288
+ '--swift-version' , swift_version ,
289
+ ]
290
+ for target_triple , package_path in target_packages :
291
+ run_args .extend (['--target' , target_triple ])
292
+ run_args .extend (['--target-swift-package-path' , package_path ])
293
+
294
+ shell .call (run_args )
295
+
296
+ def test (self , host_target ):
297
+ pass
298
+
299
+ def should_install (self , host_target ):
300
+ return False
301
+
302
+ @classmethod
303
+ def get_dependencies (cls ):
304
+ return [WasmStdlib , WasmThreadsStdlib ]
0 commit comments