Skip to content

Commit 5eb3062

Browse files
Merge pull request #76147 from kateinoigakukun/yt/swift-testing-wasm
Include swift-testing in the Wasm Swift SDK
2 parents e98e431 + 32bfb7e commit 5eb3062

File tree

2 files changed

+71
-15
lines changed

2 files changed

+71
-15
lines changed

utils/swift_build_support/swift_build_support/products/swift_testing.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,9 @@ def build(self, host_target):
102102
if Version(self.args.darwin_deployment_version_osx) < Version('10.15'):
103103
override_deployment_version = '10.15'
104104

105-
self.cmake_options.define('BUILD_SHARED_LIBS', 'YES')
105+
build_shared_libs = not host_target.startswith('wasi')
106+
self.cmake_options.define('BUILD_SHARED_LIBS',
107+
'TRUE' if build_shared_libs else 'FALSE')
106108

107109
# Use empty CMake install prefix, since the `DESTDIR` env var is set by
108110
# `install_with_cmake` later which already has the same prefix.

utils/swift_build_support/swift_build_support/products/wasmswiftsdk.py

Lines changed: 68 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
from . import product
1616
from . import wasisysroot
17+
from .swift_testing import SwiftTestingCMakeShim
1718
from .wasmstdlib import WasmStdlib, WasmThreadsStdlib
1819
from .. import shell
1920

@@ -37,13 +38,56 @@ def should_build(self, host_target):
3738
def should_test(self, host_target):
3839
return False
3940

40-
def _target_package_path(self, target_triple):
41-
return os.path.join(self.build_dir, 'Toolchains', target_triple)
41+
def _target_package_path(self, swift_host_triple):
42+
return os.path.join(self.build_dir, 'Toolchains', swift_host_triple)
43+
44+
def _build_swift_testing(self, swift_host_triple, short_triple, wasi_sysroot):
45+
# TODO: We currently build swift-testing outside of SwiftTesting
46+
# build-script product because we build Wasm stdlib outside of
47+
# the main Swift build unit and we can't use build-script's cross
48+
# compilation infrastructure.
49+
# Once stdlib build is decoupled from compiler's CMake build unit
50+
# and we can use different CMake options for different targets
51+
# for stdlib build, we can fully unify library builds with the
52+
# regular path.
53+
dest_dir = self._target_package_path(swift_host_triple)
54+
55+
swift_testing = SwiftTestingCMakeShim(
56+
args=self.args,
57+
toolchain=self.toolchain,
58+
source_dir=os.path.join(
59+
os.path.dirname(self.source_dir), 'swift-testing'),
60+
build_dir=os.path.join(
61+
os.path.dirname(self.build_dir),
62+
'swift-testing-%s' % short_triple))
63+
64+
swift_testing.cmake_options.define('CMAKE_SYSTEM_NAME:STRING', 'WASI')
65+
swift_testing.cmake_options.define('CMAKE_SYSTEM_PROCESSOR:STRING', 'wasm32')
66+
swift_testing.cmake_options.define(
67+
'CMAKE_CXX_COMPILER_TARGET', swift_host_triple)
68+
swift_testing.cmake_options.define(
69+
'CMAKE_Swift_COMPILER_TARGET', swift_host_triple)
70+
swift_testing.cmake_options.define('CMAKE_SYSROOT', wasi_sysroot)
71+
swift_resource_dir = os.path.join(dest_dir, 'usr', 'lib', 'swift_static')
72+
swift_testing.cmake_options.define(
73+
'CMAKE_Swift_FLAGS',
74+
f'-sdk {wasi_sysroot} -resource-dir {swift_resource_dir}')
75+
clang_resource_dir = os.path.join(dest_dir, 'usr', 'lib', 'clang')
76+
swift_testing.cmake_options.define(
77+
'CMAKE_CXX_FLAGS', f'-resource-dir {clang_resource_dir}')
78+
swift_testing.cmake_options.define('CMAKE_Swift_COMPILER_FORCED', 'TRUE')
79+
swift_testing.cmake_options.define('CMAKE_CXX_COMPILER_FORCED', 'TRUE')
80+
81+
swift_testing.build('wasi-wasm32')
82+
with shell.pushd(swift_testing.build_dir):
83+
shell.call([self.toolchain.cmake, '--install', '.', '--prefix', '/usr'],
84+
env={'DESTDIR': dest_dir})
4285

43-
def _build_target_package(self, target_triple,
44-
stdlib_build_path, llvm_runtime_libs_build_path):
86+
def _build_target_package(self, swift_host_triple, short_triple,
87+
stdlib_build_path, llvm_runtime_libs_build_path,
88+
wasi_sysroot):
4589

46-
dest_dir = self._target_package_path(target_triple)
90+
dest_dir = self._target_package_path(swift_host_triple)
4791
shell.rmtree(dest_dir)
4892
shell.makedirs(dest_dir)
4993

@@ -59,6 +103,8 @@ def _build_target_package(self, target_triple,
59103
shell.call([self.toolchain.cmake, '--install', '.',
60104
'--component', 'clang_rt.builtins-wasm32'],
61105
env={'DESTDIR': clang_dir})
106+
# Build swift-testing
107+
self._build_swift_testing(swift_host_triple, short_triple, wasi_sysroot)
62108

63109
return dest_dir
64110

@@ -68,16 +114,26 @@ def build(self, host_target):
68114
build_root, '%s-%s' % ('wasmllvmruntimelibs', host_target))
69115

70116
target_packages = []
71-
for target_triple, short_triple, build_basename in [
72-
('wasm32-unknown-wasi', 'wasm32-wasi', 'wasmstdlib'),
117+
# NOTE: We have three types of target triples:
118+
# 1. swift_host_triple: The triple used by the Swift compiler's '-target' option
119+
# 2. clang_multiarch_triple: The triple used by Clang to find library
120+
# and header paths from the sysroot
121+
# https://github.com/llvm/llvm-project/blob/73ef397fcba35b7b4239c00bf3e0b4e689ca0add/clang/lib/Driver/ToolChains/WebAssembly.cpp#L29-L36
122+
# 3. short_triple: The triple used by build-script to name the build directory
123+
for swift_host_triple, clang_multiarch_triple, short_triple, build_basename in [
124+
('wasm32-unknown-wasi', 'wasm32-wasi', 'wasi-wasm32', 'wasmstdlib'),
73125
# TODO: Enable threads stdlib once sdk-generator supports multi-target SDK
74-
# ('wasm32-unknown-wasip1-threads', 'wasmthreadsstdlib'),
126+
# ('wasm32-unknown-wasip1-threads', 'wasm32-wasip1-threads',
127+
# 'wasip1-threads-wasm32', 'wasmthreadsstdlib'),
75128
]:
76129
stdlib_build_path = os.path.join(
77130
build_root, '%s-%s' % (build_basename, host_target))
131+
wasi_sysroot = wasisysroot.WASILibc.sysroot_install_path(
132+
build_root, clang_multiarch_triple)
78133
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))
134+
swift_host_triple, short_triple, stdlib_build_path,
135+
llvm_runtime_libs_build_path, wasi_sysroot)
136+
target_packages.append((swift_host_triple, wasi_sysroot, package_path))
81137

82138
swiftc_path = os.path.abspath(self.toolchain.swiftc)
83139
toolchain_path = os.path.dirname(os.path.dirname(swiftc_path))
@@ -93,11 +149,9 @@ def build(self, host_target):
93149
'make-wasm-sdk',
94150
'--swift-version', swift_version,
95151
]
96-
for target_triple, package_path in target_packages:
97-
run_args.extend(['--target', target_triple])
152+
for swift_host_triple, wasi_sysroot, package_path in target_packages:
153+
run_args.extend(['--target', swift_host_triple])
98154
run_args.extend(['--target-swift-package-path', package_path])
99-
wasi_sysroot = wasisysroot.WASILibc.sysroot_install_path(
100-
build_root, short_triple)
101155
run_args.extend(['--wasi-sysroot', wasi_sysroot])
102156

103157
env = dict(os.environ)

0 commit comments

Comments
 (0)