Skip to content

Commit 3cbc37e

Browse files
build: Build Swift SDK for WebAssembly
This change adds a new product, WasmSwiftSDK, to build the Swift SDK for WebAssembly. The product is built using the swift-sdk-generator package and takes just built WebAssembly stdlib, clang runtime libraries, and wasi-sysroot as input, and produces a Swift SDK artifactbundle under swift-sdk-generator/Bundles.
1 parent 55a2a41 commit 3cbc37e

File tree

4 files changed

+101
-1
lines changed

4 files changed

+101
-1
lines changed

utils/swift_build_support/swift_build_support/build_script_invocation.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,8 @@ def compute_product_pipelines(self):
695695
is_enabled=self.args.build_wasmstdlib)
696696
builder.add_product(products.WasmThreadsStdlib,
697697
is_enabled=self.args.build_wasmstdlib)
698+
builder.add_product(products.WasmSwiftSDK,
699+
is_enabled=self.args.build_wasmstdlib)
698700

699701
# Keep SwiftDriver at last.
700702
# swift-driver's integration with the build scripts is not fully

utils/swift_build_support/swift_build_support/products/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
from .tsan_libdispatch import TSanLibDispatch
4040
from .wasisysroot import WASILibc, WasmLLVMRuntimeLibs, WasmThreadsLLVMRuntimeLibs
4141
from .wasmkit import WasmKit
42-
from .wasmstdlib import WasmStdlib, WasmThreadsStdlib
42+
from .wasmstdlib import WasmStdlib, WasmSwiftSDK, WasmThreadsStdlib
4343
from .xctest import XCTest
4444
from .zlib import Zlib
4545

@@ -79,4 +79,5 @@
7979
'WasmStdlib',
8080
'WasmThreadsLLVMRuntimeLibs',
8181
'WasmThreadsStdlib',
82+
'WasmSwiftSDK',
8283
]

utils/swift_build_support/swift_build_support/products/wasmstdlib.py

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@
1414

1515
from . import cmake_product
1616
from . import llvm
17+
from . import product
1718
from . import swift
1819
from . import wasisysroot
1920
from . import wasmkit
21+
from .. import shell
2022

2123

2224
class WasmStdlib(cmake_product.CMakeProduct):
@@ -42,6 +44,7 @@ def build(self, host_target):
4244
self._build(host_target, 'wasm32-wasi')
4345

4446
def _build(self, host_target, target_triple):
47+
self.cmake_options.define('CMAKE_INSTALL_PREFIX:PATH', 'usr')
4548
self.cmake_options.define('CMAKE_BUILD_TYPE:STRING', self._build_variant)
4649
self.cmake_options.define(
4750
'SWIFT_STDLIB_BUILD_TYPE:STRING', self._build_variant)
@@ -208,3 +211,94 @@ def add_extra_cmake_options(self):
208211
'-Xcc;-mthread-model;-Xcc;posix;'
209212
'-Xcc;-pthread;-Xcc;-ftls-model=local-exec')
210213
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]

utils/update_checkout/update-checkout-config.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@
8383
"remote": { "id": "apple/swift-nio-ssl" } },
8484
"swift-experimental-string-processing": {
8585
"remote": { "id": "apple/swift-experimental-string-processing" } },
86+
"swift-sdk-generator": {
87+
"remote": { "id": "apple/swift-sdk-generator" } },
8688
"swift-llvm-bindings": {
8789
"remote": { "id": "apple/swift-llvm-bindings" } },
8890
"llvm-project": {
@@ -149,6 +151,7 @@
149151
"swift-markdown": "main",
150152
"swift-nio": "2.31.2",
151153
"swift-experimental-string-processing": "swift/main",
154+
"swift-sdk-generator": "main",
152155
"wasi-libc": "wasi-sdk-20",
153156
"wasmkit": "0.0.3",
154157
"curl": "curl-8_5_0",

0 commit comments

Comments
 (0)