Skip to content

Commit 9fd10c4

Browse files
[wasm] Enable building swift-testing for wasm32-unknown-wasip1-threads
1 parent 4777f27 commit 9fd10c4

File tree

1 file changed

+57
-44
lines changed

1 file changed

+57
-44
lines changed

utils/swift_build_support/swift_build_support/products/wasmswiftsdk.py

Lines changed: 57 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from . import wasisysroot
1717
from .swift_testing import SwiftTestingCMakeShim
1818
from .wasmstdlib import WasmStdlib, WasmThreadsStdlib
19+
from .cmake_product import CMakeProduct
1920
from .. import shell
2021

2122

@@ -41,49 +42,62 @@ def should_test(self, host_target):
4142
def _target_package_path(self, swift_host_triple):
4243
return os.path.join(self.build_dir, 'Toolchains', swift_host_triple)
4344

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)
45+
def _append_platform_cmake_options(self, cmake_options, swift_host_triple, has_pthread, wasi_sysroot):
46+
cmake_options.define('CMAKE_SYSTEM_NAME:STRING', 'WASI')
47+
cmake_options.define('CMAKE_SYSTEM_PROCESSOR:STRING', 'wasm32')
48+
cmake_options.define('CMAKE_C_COMPILER_TARGET', swift_host_triple)
49+
cmake_options.define('CMAKE_CXX_COMPILER_TARGET', swift_host_triple)
50+
cmake_options.define(
51+
'CMAKE_Swift_COMPILER_TARGET', swift_host_triple)
52+
cmake_options.define('CMAKE_SYSROOT', wasi_sysroot)
5453

55-
swift_testing = SwiftTestingCMakeShim(
54+
dest_dir = self._target_package_path(swift_host_triple)
55+
swift_resource_dir = os.path.join(dest_dir, 'usr', 'lib', 'swift_static')
56+
clang_resource_dir = os.path.join(swift_resource_dir, 'clang')
57+
58+
swift_flags = ['-sdk', wasi_sysroot, '-resource-dir', swift_resource_dir]
59+
c_flags = ['-resource-dir', clang_resource_dir]
60+
cxx_flags = c_flags + ['-fno-exceptions']
61+
if has_pthread:
62+
clang_flags = ['-mthread-model', 'posix', '-pthread']
63+
c_flags.extend(clang_flags)
64+
cxx_flags.extend(clang_flags)
65+
swift_flags.extend(['-Xcc', '-matomics', '-Xcc', '-mbulk-memory', '-Xcc', '-mthread-model', '-Xcc', 'posix', '-Xcc', '-pthread'])
66+
67+
cmake_options.define('CMAKE_Swift_FLAGS', ' '.join(swift_flags))
68+
cmake_options.define('CMAKE_C_FLAGS', ' '.join(c_flags))
69+
cmake_options.define('CMAKE_CXX_FLAGS', ' '.join(cxx_flags))
70+
cmake_options.define('CMAKE_Swift_COMPILER_FORCED', 'TRUE')
71+
cmake_options.define('CMAKE_CXX_COMPILER_FORCED', 'TRUE')
72+
cmake_options.define('CMAKE_BUILD_TYPE', self.args.build_variant)
73+
74+
# Explicitly choose ar and ranlib from just-built LLVM tools since tools in the host system
75+
# unlikely support Wasm object format.
76+
native_toolchain_path = self.native_toolchain_path(self.args.host_target)
77+
cmake_options.define('CMAKE_AR', os.path.join(native_toolchain_path, 'bin', 'llvm-ar'))
78+
cmake_options.define('CMAKE_RANLIB', os.path.join(native_toolchain_path, 'bin', 'llvm-ranlib'))
79+
80+
def _build_swift_testing(self, swift_host_triple, has_pthread, wasi_sysroot):
81+
swift_testing = CMakeProduct(
5682
args=self.args,
5783
toolchain=self.toolchain,
5884
source_dir=os.path.join(
5985
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')
86+
build_dir=os.path.join(self.build_dir, 'swift-testing', swift_host_triple))
87+
self._append_platform_cmake_options(
88+
swift_testing.cmake_options, swift_host_triple, has_pthread, wasi_sysroot)
89+
swift_testing.cmake_options.define('BUILD_SHARED_LIBS', 'FALSE')
90+
swift_testing.cmake_options.define('CMAKE_Swift_COMPILATION_MODE', 'wholemodule')
91+
swift_testing.cmake_options.define('SwiftTesting_MACRO', 'NO')
92+
93+
swift_testing.build_with_cmake([], self.args.build_variant, [],
94+
prefer_native_toolchain=True)
95+
dest_dir = self._target_package_path(swift_host_triple)
8296
with shell.pushd(swift_testing.build_dir):
8397
shell.call([self.toolchain.cmake, '--install', '.', '--prefix', '/usr'],
8498
env={'DESTDIR': dest_dir})
8599

86-
def _build_target_package(self, swift_host_triple, short_triple,
100+
def _build_target_package(self, swift_host_triple, has_pthread,
87101
stdlib_build_path, llvm_runtime_libs_build_path,
88102
wasi_sysroot):
89103

@@ -104,25 +118,23 @@ def _build_target_package(self, swift_host_triple, short_triple,
104118
'--component', 'clang_rt.builtins-wasm32'],
105119
env={'DESTDIR': clang_dir})
106120
# Build swift-testing
107-
self._build_swift_testing(swift_host_triple, short_triple, wasi_sysroot)
121+
self._build_swift_testing(swift_host_triple, has_pthread, wasi_sysroot)
108122

109123
return dest_dir
110124

111125
def build(self, host_target):
112126
build_root = os.path.dirname(self.build_dir)
113127

114128
target_packages = []
115-
# NOTE: We have three types of target triples:
129+
# NOTE: We have two types of target triples:
116130
# 1. swift_host_triple: The triple used by the Swift compiler's '-target' option
117131
# 2. clang_multiarch_triple: The triple used by Clang to find library
118132
# and header paths from the sysroot
119133
# https://github.com/llvm/llvm-project/blob/73ef397fcba35b7b4239c00bf3e0b4e689ca0add/clang/lib/Driver/ToolChains/WebAssembly.cpp#L29-L36
120-
# 3. short_triple: The triple used by build-script to name the build directory
121-
for swift_host_triple, clang_multiarch_triple, short_triple, build_basename in [
122-
('wasm32-unknown-wasi', 'wasm32-wasi', 'wasi-wasm32', 'wasmstdlib'),
123-
# TODO: Enable threads stdlib once sdk-generator supports multi-target SDK
124-
# ('wasm32-unknown-wasip1-threads', 'wasm32-wasip1-threads',
125-
# 'wasip1-threads-wasm32', 'wasmthreadsstdlib'),
134+
for swift_host_triple, clang_multiarch_triple, build_basename, build_sdk, has_pthread in [
135+
('wasm32-unknown-wasi', 'wasm32-wasi', 'wasmstdlib', True, False),
136+
# TODO: Include p1-threads in the Swift SDK once sdk-generator supports multi-target SDK
137+
('wasm32-unknown-wasip1-threads', 'wasm32-wasip1-threads', 'wasmthreadsstdlib', False, True),
126138
]:
127139
stdlib_build_path = os.path.join(
128140
build_root, '%s-%s' % (build_basename, host_target))
@@ -132,9 +144,10 @@ def build(self, host_target):
132144
build_root, '%s-%s' % ('wasmllvmruntimelibs', host_target),
133145
clang_multiarch_triple)
134146
package_path = self._build_target_package(
135-
swift_host_triple, short_triple, stdlib_build_path,
147+
swift_host_triple, has_pthread, stdlib_build_path,
136148
llvm_runtime_libs_build_path, wasi_sysroot)
137-
target_packages.append((swift_host_triple, wasi_sysroot, package_path))
149+
if build_sdk:
150+
target_packages.append((swift_host_triple, wasi_sysroot, package_path))
138151

139152
swiftc_path = os.path.abspath(self.toolchain.swiftc)
140153
toolchain_path = os.path.dirname(os.path.dirname(swiftc_path))

0 commit comments

Comments
 (0)