Skip to content

Commit 33323a8

Browse files
Merge pull request #72728 from kateinoigakukun/yt/wasm-sdk-generator
build: Build Swift SDK for WebAssembly
2 parents b88566a + 1148e26 commit 33323a8

File tree

5 files changed

+139
-14
lines changed

5 files changed

+139
-14
lines changed

utils/swift_build_support/swift_build_support/build_script_invocation.py

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,21 @@ def compute_product_pipelines(self):
657657
# Begin the post build-script-impl build phase.
658658
builder.begin_pipeline()
659659

660+
builder.add_product(products.WASILibc,
661+
is_enabled=self.args.build_wasmstdlib)
662+
builder.add_product(products.WasmLLVMRuntimeLibs,
663+
is_enabled=self.args.build_wasmstdlib)
664+
builder.add_product(products.WasmThreadsLLVMRuntimeLibs,
665+
is_enabled=self.args.build_wasmstdlib)
666+
builder.add_product(products.WasmKit,
667+
is_enabled=self.args.build_wasmkit)
668+
builder.add_product(products.WasmStdlib,
669+
is_enabled=self.args.build_wasmstdlib)
670+
builder.add_product(products.WasmThreadsStdlib,
671+
is_enabled=self.args.build_wasmstdlib)
672+
builder.add_product(products.WasmSwiftSDK,
673+
is_enabled=self.args.build_wasmstdlib)
674+
660675
builder.add_product(products.SwiftPM,
661676
is_enabled=self.args.build_swiftpm)
662677
builder.add_product(products.SwiftSyntax,
@@ -683,18 +698,6 @@ def compute_product_pipelines(self):
683698
is_enabled=self.args.install_swiftdocc)
684699
builder.add_product(products.MinimalStdlib,
685700
is_enabled=self.args.build_minimalstdlib)
686-
builder.add_product(products.WASILibc,
687-
is_enabled=self.args.build_wasmstdlib)
688-
builder.add_product(products.WasmLLVMRuntimeLibs,
689-
is_enabled=self.args.build_wasmstdlib)
690-
builder.add_product(products.WasmThreadsLLVMRuntimeLibs,
691-
is_enabled=self.args.build_wasmstdlib)
692-
builder.add_product(products.WasmKit,
693-
is_enabled=self.args.build_wasmkit)
694-
builder.add_product(products.WasmStdlib,
695-
is_enabled=self.args.build_wasmstdlib)
696-
builder.add_product(products.WasmThreadsStdlib,
697-
is_enabled=self.args.build_wasmstdlib)
698701

699702
# Keep SwiftDriver at last.
700703
# 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 & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
from .wasisysroot import WASILibc, WasmLLVMRuntimeLibs, WasmThreadsLLVMRuntimeLibs
4141
from .wasmkit import WasmKit
4242
from .wasmstdlib import WasmStdlib, WasmThreadsStdlib
43+
from .wasmswiftsdk import WasmSwiftSDK
4344
from .xctest import XCTest
4445
from .zlib import Zlib
4546

@@ -79,4 +80,5 @@
7980
'WasmStdlib',
8081
'WasmThreadsLLVMRuntimeLibs',
8182
'WasmThreadsStdlib',
83+
'WasmSwiftSDK',
8284
]

utils/swift_build_support/swift_build_support/products/wasmstdlib.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ def build(self, host_target):
4242
self._build(host_target, 'wasm32-wasi')
4343

4444
def _build(self, host_target, target_triple):
45+
self.cmake_options.define('CMAKE_INSTALL_PREFIX:PATH', '/usr')
4546
self.cmake_options.define('CMAKE_BUILD_TYPE:STRING', self._build_variant)
4647
self.cmake_options.define(
4748
'SWIFT_STDLIB_BUILD_TYPE:STRING', self._build_variant)
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
# swift_build_support/products/wasmswiftsdk.py ------------------*- python -*-
2+
#
3+
# This source file is part of the Swift.org open source project
4+
#
5+
# Copyright (c) 2024 Apple Inc. and the Swift project authors
6+
# Licensed under Apache License v2.0 with Runtime Library Exception
7+
#
8+
# See https://swift.org/LICENSE.txt for license information
9+
# See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
#
11+
# ----------------------------------------------------------------------------
12+
13+
import os
14+
15+
from . import product
16+
from . import wasisysroot
17+
from .wasmstdlib import WasmStdlib, WasmThreadsStdlib
18+
from .. import shell
19+
20+
21+
class WasmSwiftSDK(product.Product):
22+
@classmethod
23+
def product_source_name(cls):
24+
return "swift-sdk-generator"
25+
26+
@classmethod
27+
def is_build_script_impl_product(cls):
28+
return False
29+
30+
@classmethod
31+
def is_before_build_script_impl_product(cls):
32+
return False
33+
34+
def should_build(self, host_target):
35+
return self.args.build_wasmstdlib
36+
37+
def should_test(self, host_target):
38+
return False
39+
40+
def _target_package_path(self, target_triple):
41+
return os.path.join(self.build_dir, 'Toolchains', target_triple)
42+
43+
def _build_target_package(self, target_triple,
44+
stdlib_build_path, llvm_runtime_libs_build_path):
45+
46+
dest_dir = self._target_package_path(target_triple)
47+
shell.rmtree(dest_dir)
48+
shell.makedirs(dest_dir)
49+
50+
# Build toolchain package for standalone stdlib
51+
with shell.pushd(stdlib_build_path):
52+
shell.call([self.toolchain.cmake, '--install', '.'],
53+
env={'DESTDIR': dest_dir})
54+
55+
# Copy clang builtin libraries
56+
with shell.pushd(llvm_runtime_libs_build_path):
57+
for dirname in ['clang', 'swift/clang', 'swift_static/clang']:
58+
clang_dir = os.path.join(dest_dir, f'usr/lib/{dirname}')
59+
shell.call([self.toolchain.cmake, '--install', '.',
60+
'--component', 'clang_rt.builtins-wasm32'],
61+
env={'DESTDIR': clang_dir})
62+
63+
return dest_dir
64+
65+
def build(self, host_target):
66+
build_root = os.path.dirname(self.build_dir)
67+
llvm_runtime_libs_build_path = os.path.join(
68+
build_root, '%s-%s' % ('wasmllvmruntimelibs', host_target))
69+
70+
target_packages = []
71+
for target_triple, short_triple, build_basename in [
72+
('wasm32-unknown-wasi', 'wasm32-wasi', 'wasmstdlib'),
73+
# TODO: Enable threads stdlib once sdk-generator supports multi-target SDK
74+
# ('wasm32-unknown-wasip1-threads', 'wasmthreadsstdlib'),
75+
]:
76+
stdlib_build_path = os.path.join(
77+
build_root, '%s-%s' % (build_basename, host_target))
78+
package_path = self._build_target_package(
79+
target_triple, stdlib_build_path, llvm_runtime_libs_build_path)
80+
target_packages.append((target_triple, package_path))
81+
82+
swiftc_path = os.path.abspath(self.toolchain.swiftc)
83+
toolchain_path = os.path.dirname(os.path.dirname(swiftc_path))
84+
swift_run = os.path.join(toolchain_path, 'bin', 'swift-run')
85+
86+
swift_version = os.environ.get('TOOLCHAIN_VERSION',
87+
'swift-DEVELOPMENT-SNAPSHOT').lstrip('swift-')
88+
run_args = [
89+
swift_run,
90+
'--package-path', self.source_dir,
91+
'--build-path', self.build_dir,
92+
'swift-sdk-generator',
93+
'make-wasm-sdk',
94+
'--swift-version', swift_version,
95+
]
96+
for target_triple, package_path in target_packages:
97+
run_args.extend(['--target', target_triple])
98+
run_args.extend(['--target-swift-package-path', package_path])
99+
wasi_sysroot = wasisysroot.WASILibc.sysroot_install_path(
100+
build_root, short_triple)
101+
run_args.extend(['--wasi-sysroot', wasi_sysroot])
102+
103+
env = dict(os.environ)
104+
env['SWIFTCI_USE_LOCAL_DEPS'] = '1'
105+
106+
shell.call(run_args, env=env)
107+
108+
def test(self, host_target):
109+
pass
110+
111+
def should_install(self, host_target):
112+
return False
113+
114+
@classmethod
115+
def get_dependencies(cls):
116+
return [WasmStdlib, WasmThreadsStdlib]

utils/update_checkout/update-checkout-config.json

Lines changed: 5 additions & 2 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": {
@@ -123,7 +125,7 @@
123125
"swift-crypto": "3.0.0",
124126
"swift-certificates": "1.0.1",
125127
"swift-asn1": "1.0.0",
126-
"swift-async-algorithms": "1.0.0",
128+
"swift-async-algorithms": "main",
127129
"swift-driver": "main",
128130
"swift-log": "1.5.4",
129131
"swift-numerics": "1.0.2",
@@ -147,8 +149,9 @@
147149
"swift-docc-render-artifact": "main",
148150
"swift-docc-symbolkit": "main",
149151
"swift-markdown": "main",
150-
"swift-nio": "2.31.2",
152+
"swift-nio": "2.65.0",
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)