Skip to content

Commit 7f57dee

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 cb12b0c commit 7f57dee

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
@@ -684,6 +684,8 @@ def compute_product_pipelines(self):
684684
is_enabled=self.args.build_wasmstdlib)
685685
builder.add_product(products.WasmThreadsStdlib,
686686
is_enabled=self.args.build_wasmstdlib)
687+
builder.add_product(products.WasmSwiftSDK,
688+
is_enabled=self.args.build_wasmstdlib)
687689

688690
# Keep SwiftDriver at last.
689691
# 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):
@@ -39,6 +41,7 @@ def should_test(self, host_target):
3941
return self.args.test_wasmstdlib
4042

4143
def build(self, host_target):
44+
self.cmake_options.define('CMAKE_INSTALL_PREFIX:PATH', 'usr')
4245
self.cmake_options.define('CMAKE_BUILD_TYPE:STRING', self._build_variant)
4346
self.cmake_options.define(
4447
'SWIFT_STDLIB_BUILD_TYPE:STRING', self._build_variant)
@@ -194,3 +197,94 @@ def add_extra_cmake_options(self):
194197
'-Xcc;-mthread-model;-Xcc;posix;'
195198
'-Xcc;-pthread;-Xcc;-ftls-model=local-exec')
196199
self.cmake_options.define('SWIFT_ENABLE_WASI_THREADS:BOOL', 'TRUE')
200+
201+
202+
class WasmSwiftSDK(product.Product):
203+
@classmethod
204+
def product_source_name(cls):
205+
return "swift-sdk-generator"
206+
207+
@classmethod
208+
def is_build_script_impl_product(cls):
209+
return False
210+
211+
@classmethod
212+
def is_before_build_script_impl_product(cls):
213+
return False
214+
215+
def should_build(self, host_target):
216+
return self.args.build_wasmstdlib
217+
218+
def should_test(self, host_target):
219+
return False
220+
221+
def _target_package_path(self, target_triple):
222+
return os.path.join(self.build_dir, 'Toolchains', target_triple)
223+
224+
def _build_target_package(self, target_triple,
225+
stdlib_build_path, llvm_runtime_libs_build_path):
226+
227+
dest_dir = self._target_package_path(target_triple)
228+
shell.rmtree(dest_dir)
229+
shell.makedirs(dest_dir)
230+
231+
# Build toolchain package for standalone stdlib
232+
with shell.pushd(stdlib_build_path):
233+
shell.call([self.toolchain.cmake, '--install', '.'],
234+
env={'DESTDIR': dest_dir})
235+
236+
# Copy LLVM runtime libraries
237+
with shell.pushd(llvm_runtime_libs_build_path):
238+
clang_dest_dir = os.path.join(dest_dir, 'usr/lib/swift_static/clang')
239+
shell.call([self.toolchain.cmake, '--install', '.',
240+
'--component', 'clang_rt.builtins-wasm32'],
241+
env={'DESTDIR': clang_dest_dir})
242+
return dest_dir
243+
244+
def build(self, host_target):
245+
build_root = os.path.dirname(self.build_dir)
246+
llvm_runtime_libs_build_path = os.path.join(
247+
build_root, '%s-%s' % ('wasmllvmruntimelibs', host_target))
248+
249+
target_packages = []
250+
for target_triple, build_basename in [
251+
('wasm32-unknown-wasi', 'wasmstdlib'),
252+
# TODO: Enable threads stdlib once sdk-generator supports multi-target SDK
253+
# ('wasm32-unknown-wasip1-threads', 'wasmthreadsstdlib'),
254+
]:
255+
stdlib_build_path = os.path.join(
256+
build_root, '%s-%s' % (build_basename, host_target))
257+
package_path = self._build_target_package(
258+
target_triple, stdlib_build_path, llvm_runtime_libs_build_path)
259+
target_packages.append((target_triple, package_path))
260+
261+
swiftc_path = os.path.abspath(self.toolchain.swiftc)
262+
toolchain_path = os.path.dirname(os.path.dirname(swiftc_path))
263+
swift_run = os.path.join(toolchain_path, 'bin', 'swift-run')
264+
265+
swift_version = os.environ.get('TOOLCHAIN_VERSION',
266+
'swift-DEVELOPMENT-SNAPSHOT').lstrip('swift-')
267+
run_args = [
268+
swift_run,
269+
'--package-path', self.source_dir,
270+
'--build-path', self.build_dir,
271+
'swift-sdk-generator',
272+
'make-wasm-sdk',
273+
'--wasi-sysroot', wasisysroot.WASILibc.sysroot_install_path(build_root),
274+
'--swift-version', swift_version,
275+
]
276+
for target_triple, package_path in target_packages:
277+
run_args.extend(['--target', target_triple])
278+
run_args.extend(['--target-swift-package-path', package_path])
279+
280+
shell.call(run_args)
281+
282+
def test(self, host_target):
283+
pass
284+
285+
def should_install(self, host_target):
286+
return False
287+
288+
@classmethod
289+
def get_dependencies(cls):
290+
return [WasmStdlib, WasmThreadsStdlib]

utils/update_checkout/update-checkout-config.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@
7979
"remote": { "id": "apple/swift-nio-ssl" } },
8080
"swift-experimental-string-processing": {
8181
"remote": { "id": "apple/swift-experimental-string-processing" } },
82+
"swift-sdk-generator": {
83+
"remote": { "id": "apple/swift-sdk-generator" } },
8284
"swift-llvm-bindings": {
8385
"remote": { "id": "apple/swift-llvm-bindings" } },
8486
"llvm-project": {
@@ -144,6 +146,7 @@
144146
"swift-nio": "2.31.2",
145147
"swift-nio-ssl": "2.15.0",
146148
"swift-experimental-string-processing": "swift/main",
149+
"swift-sdk-generator": "main",
147150
"wasi-libc": "wasi-sdk-20",
148151
"wasmkit": "0.0.3",
149152
"curl": "curl-8_4_0",

0 commit comments

Comments
 (0)