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 )
@@ -206,3 +209,98 @@ def add_extra_cmake_options(self):
206
209
'-Xcc;-mthread-model;-Xcc;posix;'
207
210
'-Xcc;-pthread;-Xcc;-ftls-model=local-exec' )
208
211
self .cmake_options .define ('SWIFT_ENABLE_WASI_THREADS:BOOL' , 'TRUE' )
212
+
213
+
214
+ class WasmSwiftSDK (product .Product ):
215
+ @classmethod
216
+ def product_source_name (cls ):
217
+ return "swift-sdk-generator"
218
+
219
+ @classmethod
220
+ def is_build_script_impl_product (cls ):
221
+ return False
222
+
223
+ @classmethod
224
+ def is_before_build_script_impl_product (cls ):
225
+ return False
226
+
227
+ def should_build (self , host_target ):
228
+ return self .args .build_wasmstdlib
229
+
230
+ def should_test (self , host_target ):
231
+ return False
232
+
233
+ def _target_package_path (self , target_triple ):
234
+ return os .path .join (self .build_dir , 'Toolchains' , target_triple )
235
+
236
+ def _build_target_package (self , target_triple ,
237
+ stdlib_build_path , llvm_runtime_libs_build_path ):
238
+
239
+ dest_dir = self ._target_package_path (target_triple )
240
+ shell .rmtree (dest_dir )
241
+ shell .makedirs (dest_dir )
242
+
243
+ # Build toolchain package for standalone stdlib
244
+ with shell .pushd (stdlib_build_path ):
245
+ shell .call ([self .toolchain .cmake , '--install' , '.' ],
246
+ env = {'DESTDIR' : dest_dir })
247
+
248
+ # Copy clang builtin libraries
249
+ with shell .pushd (llvm_runtime_libs_build_path ):
250
+ for dirname in ['clang' , 'swift/clang' , 'swift_static/clang' ]:
251
+ clang_dir = os .path .join (dest_dir , f'usr/lib/{ dirname } ' )
252
+ shell .call ([self .toolchain .cmake , '--install' , '.' ,
253
+ '--component' , 'clang_rt.builtins-wasm32' ],
254
+ env = {'DESTDIR' : clang_dir })
255
+
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 , short_triple , build_basename in [
265
+ ('wasm32-unknown-wasi' , 'wasm32-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
+ '--swift-version' , swift_version ,
288
+ ]
289
+ for target_triple , package_path in target_packages :
290
+ run_args .extend (['--target' , target_triple ])
291
+ run_args .extend (['--target-swift-package-path' , package_path ])
292
+ wasi_sysroot = wasisysroot .WASILibc .sysroot_install_path (
293
+ build_root , short_triple )
294
+ run_args .extend (['--wasi-sysroot' , wasi_sysroot ])
295
+
296
+ shell .call (run_args )
297
+
298
+ def test (self , host_target ):
299
+ pass
300
+
301
+ def should_install (self , host_target ):
302
+ return False
303
+
304
+ @classmethod
305
+ def get_dependencies (cls ):
306
+ return [WasmStdlib , WasmThreadsStdlib ]
0 commit comments