Skip to content

Commit b702fbf

Browse files
[wasm] Enable building swift-testing for wasm32-unknown-wasip1-threads
1 parent 5e37849 commit b702fbf

File tree

1 file changed

+58
-44
lines changed

1 file changed

+58
-44
lines changed

utils/swift_build_support/swift_build_support/products/wasmswiftsdk.py

Lines changed: 58 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,52 +42,66 @@ 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, extra_swift_flags):
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] + extra_swift_flags
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')
86+
build_dir=os.path.join(self.build_dir, 'swift-testing', swift_host_triple))
7287
# For statically linked objects in an archive, we have to use singlethreaded
7388
# LLVM codegen unit to prevent runtime metadata sections from being stripped
7489
# at link-time.
75-
swift_testing.cmake_options.define(
76-
'CMAKE_Swift_FLAGS',
77-
f'-sdk {wasi_sysroot} -resource-dir {swift_resource_dir} -Xfrontend -enable-single-module-llvm-emission')
78-
clang_resource_dir = os.path.join(dest_dir, 'usr', 'lib', 'clang')
79-
swift_testing.cmake_options.define(
80-
'CMAKE_CXX_FLAGS', f'-resource-dir {clang_resource_dir}')
81-
swift_testing.cmake_options.define('CMAKE_Swift_COMPILER_FORCED', 'TRUE')
82-
swift_testing.cmake_options.define('CMAKE_CXX_COMPILER_FORCED', 'TRUE')
83-
84-
swift_testing.build('wasi-wasm32')
90+
self._append_platform_cmake_options(
91+
swift_testing.cmake_options, swift_host_triple, has_pthread, wasi_sysroot,
92+
extra_swift_flags=['-Xfrontend', '-enable-single-module-llvm-emission'])
93+
swift_testing.cmake_options.define('BUILD_SHARED_LIBS', 'FALSE')
94+
swift_testing.cmake_options.define('CMAKE_Swift_COMPILATION_MODE', 'wholemodule')
95+
swift_testing.cmake_options.define('SwiftTesting_MACRO', 'NO')
96+
97+
swift_testing.build_with_cmake([], self.args.build_variant, [],
98+
prefer_native_toolchain=True)
99+
dest_dir = self._target_package_path(swift_host_triple)
85100
with shell.pushd(swift_testing.build_dir):
86101
shell.call([self.toolchain.cmake, '--install', '.', '--prefix', '/usr'],
87102
env={'DESTDIR': dest_dir})
88103

89-
def _build_target_package(self, swift_host_triple, short_triple,
104+
def _build_target_package(self, swift_host_triple, has_pthread,
90105
stdlib_build_path, llvm_runtime_libs_build_path,
91106
wasi_sysroot):
92107

@@ -107,25 +122,23 @@ def _build_target_package(self, swift_host_triple, short_triple,
107122
'--component', 'clang_rt.builtins-wasm32'],
108123
env={'DESTDIR': clang_dir})
109124
# Build swift-testing
110-
self._build_swift_testing(swift_host_triple, short_triple, wasi_sysroot)
125+
self._build_swift_testing(swift_host_triple, has_pthread, wasi_sysroot)
111126

112127
return dest_dir
113128

114129
def build(self, host_target):
115130
build_root = os.path.dirname(self.build_dir)
116131

117132
target_packages = []
118-
# NOTE: We have three types of target triples:
133+
# NOTE: We have two types of target triples:
119134
# 1. swift_host_triple: The triple used by the Swift compiler's '-target' option
120135
# 2. clang_multiarch_triple: The triple used by Clang to find library
121136
# and header paths from the sysroot
122137
# https://github.com/llvm/llvm-project/blob/73ef397fcba35b7b4239c00bf3e0b4e689ca0add/clang/lib/Driver/ToolChains/WebAssembly.cpp#L29-L36
123-
# 3. short_triple: The triple used by build-script to name the build directory
124-
for swift_host_triple, clang_multiarch_triple, short_triple, build_basename in [
125-
('wasm32-unknown-wasi', 'wasm32-wasi', 'wasi-wasm32', 'wasmstdlib'),
126-
# TODO: Enable threads stdlib once sdk-generator supports multi-target SDK
127-
# ('wasm32-unknown-wasip1-threads', 'wasm32-wasip1-threads',
128-
# 'wasip1-threads-wasm32', 'wasmthreadsstdlib'),
138+
for swift_host_triple, clang_multiarch_triple, build_basename, build_sdk, has_pthread in [
139+
('wasm32-unknown-wasi', 'wasm32-wasi', 'wasmstdlib', True, False),
140+
# TODO: Include p1-threads in the Swift SDK once sdk-generator supports multi-target SDK
141+
('wasm32-unknown-wasip1-threads', 'wasm32-wasip1-threads', 'wasmthreadsstdlib', False, True),
129142
]:
130143
stdlib_build_path = os.path.join(
131144
build_root, '%s-%s' % (build_basename, host_target))
@@ -135,9 +148,10 @@ def build(self, host_target):
135148
build_root, '%s-%s' % ('wasmllvmruntimelibs', host_target),
136149
clang_multiarch_triple)
137150
package_path = self._build_target_package(
138-
swift_host_triple, short_triple, stdlib_build_path,
151+
swift_host_triple, has_pthread, stdlib_build_path,
139152
llvm_runtime_libs_build_path, wasi_sysroot)
140-
target_packages.append((swift_host_triple, wasi_sysroot, package_path))
153+
if build_sdk:
154+
target_packages.append((swift_host_triple, wasi_sysroot, package_path))
141155

142156
swiftc_path = os.path.abspath(self.toolchain.swiftc)
143157
toolchain_path = os.path.dirname(os.path.dirname(swiftc_path))

0 commit comments

Comments
 (0)