Skip to content

Commit e20e1c6

Browse files
Merge pull request swiftlang#73077 from kateinoigakukun/yt/split-wasi-sysroot-for-targets
[wasm] Split wasi-sysroot for wasi and wasip1-threads
2 parents bec666c + 1136d50 commit e20e1c6

File tree

2 files changed

+29
-36
lines changed

2 files changed

+29
-36
lines changed

utils/swift_build_support/swift_build_support/products/wasisysroot.py

Lines changed: 19 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ def _build(self, host_target, thread_model='single', target_triple='wasm32-wasi'
5656
clang_tools_path = self.args.native_clang_tools_path or llvm_build_bin_dir
5757
build_jobs = self.args.build_jobs or multiprocessing.cpu_count()
5858

59-
sysroot_build_dir = WASILibc.sysroot_build_path(build_root, host_target)
59+
sysroot_build_dir = WASILibc.sysroot_build_path(
60+
build_root, host_target, target_triple)
6061
# FIXME: Manually create an empty dir that is usually created during
6162
# check-symbols. The directory is required during sysroot installation step.
6263
os.makedirs(os.path.join(sysroot_build_dir, "share"), exist_ok=True)
@@ -73,7 +74,7 @@ def _build(self, host_target, thread_model='single', target_triple='wasm32-wasi'
7374
'-C', self.source_dir,
7475
'OBJDIR=' + os.path.join(self.build_dir, 'obj-' + thread_model),
7576
'SYSROOT=' + sysroot_build_dir,
76-
'INSTALL_DIR=' + WASILibc.sysroot_install_path(build_root),
77+
'INSTALL_DIR=' + WASILibc.sysroot_install_path(build_root, target_triple),
7778
'CC=' + os.path.join(clang_tools_path, 'clang'),
7879
'AR=' + os.path.join(llvm_tools_path, 'llvm-ar'),
7980
'NM=' + os.path.join(llvm_tools_path, 'llvm-nm'),
@@ -86,21 +87,22 @@ def get_dependencies(cls):
8687
return [llvm.LLVM]
8788

8889
@classmethod
89-
def sysroot_build_path(cls, build_root, host_target):
90+
def sysroot_build_path(cls, build_root, host_target, target_triple):
9091
"""
9192
Returns the path to the sysroot build directory, which contains only the
9293
artifacts of wasi-libc (Not including the artifacts of LLVM runtimes).
9394
"""
9495
return os.path.join(build_root,
95-
'%s-%s' % (cls.product_name(), host_target), 'sysroot')
96+
'%s-%s' % (cls.product_name(), host_target),
97+
'sysroot', target_triple)
9698

9799
@classmethod
98-
def sysroot_install_path(cls, build_root):
100+
def sysroot_install_path(cls, build_root, target_triple):
99101
"""
100102
Returns the path to the sysroot install directory, which contains artifacts
101103
of wasi-libc and LLVM runtimes.
102104
"""
103-
return os.path.join(build_root, 'wasi-sysroot')
105+
return os.path.join(build_root, 'wasi-sysroot', target_triple)
104106

105107

106108
class WasmLLVMRuntimeLibs(cmake_product.CMakeProduct):
@@ -130,7 +132,8 @@ def should_install(self, host_target):
130132
def build(self, host_target):
131133
self._build(host_target)
132134

133-
def _build(self, host_target, enable_wasi_threads=False):
135+
def _build(self, host_target, enable_wasi_threads=False,
136+
compiler_rt_os_dir='wasi', target_triple='wasm32-wasi'):
134137
build_root = os.path.dirname(self.build_dir)
135138
llvm_build_bin_dir = os.path.join(
136139
'..', build_root, '%s-%s' % ('llvm', host_target), 'bin')
@@ -139,18 +142,14 @@ def _build(self, host_target, enable_wasi_threads=False):
139142

140143
cmake_has_threads = 'TRUE' if enable_wasi_threads else 'FALSE'
141144

142-
self.cmake_options.define('CMAKE_SYSROOT:PATH',
143-
WASILibc.sysroot_build_path(build_root, host_target))
144-
enable_runtimes = ['libcxx', 'libcxxabi']
145-
if not enable_wasi_threads:
146-
# compiler-rt can be shared between wasi and wasip1-threads
147-
enable_runtimes.append('compiler-rt')
145+
self.cmake_options.define(
146+
'CMAKE_SYSROOT:PATH',
147+
WASILibc.sysroot_build_path(build_root, host_target, target_triple))
148+
enable_runtimes = ['libcxx', 'libcxxabi', 'compiler-rt']
148149
self.cmake_options.define('LLVM_ENABLE_RUNTIMES:STRING',
149150
';'.join(enable_runtimes))
150151

151-
libdir_suffix = '/wasm32-wasi'
152-
if enable_wasi_threads:
153-
libdir_suffix = '/wasm32-wasip1-threads'
152+
libdir_suffix = '/' + target_triple
154153
self.cmake_options.define('LIBCXX_LIBDIR_SUFFIX:STRING', libdir_suffix)
155154
self.cmake_options.define('LIBCXXABI_LIBDIR_SUFFIX:STRING', libdir_suffix)
156155
self.cmake_options.define('CMAKE_STAGING_PREFIX:PATH', '/')
@@ -162,7 +161,7 @@ def _build(self, host_target, enable_wasi_threads=False):
162161
self.cmake_options.define('COMPILER_RT_INCLUDE_TESTS:BOOL', 'FALSE')
163162
self.cmake_options.define('COMPILER_RT_HAS_FPIC_FLAG:BOOL', 'FALSE')
164163
self.cmake_options.define('COMPILER_RT_EXCLUDE_ATOMIC_BUILTIN:BOOL', 'FALSE')
165-
self.cmake_options.define('COMPILER_RT_OS_DIR:STRING', 'wasi')
164+
self.cmake_options.define('COMPILER_RT_OS_DIR:STRING', compiler_rt_os_dir)
166165

167166
self.cmake_options.define('CMAKE_C_COMPILER_WORKS:BOOL', 'TRUE')
168167
self.cmake_options.define('CMAKE_CXX_COMPILER_WORKS:BOOL', 'TRUE')
@@ -190,10 +189,6 @@ def _build(self, host_target, enable_wasi_threads=False):
190189
self.cmake_options.define('CMAKE_C_FLAGS:STRING', ' '.join(c_flags))
191190
self.cmake_options.define('CMAKE_CXX_FLAGS:STRING', ' '.join(cxx_flags))
192191

193-
if enable_wasi_threads:
194-
target_triple = 'wasm32-wasip1-threads'
195-
else:
196-
target_triple = 'wasm32-wasi'
197192
self.cmake_options.define('CMAKE_C_COMPILER_TARGET:STRING', target_triple)
198193
self.cmake_options.define('CMAKE_CXX_COMPILER_TARGET:STRING', target_triple)
199194

@@ -227,7 +222,7 @@ def _build(self, host_target, enable_wasi_threads=False):
227222
self.build_with_cmake([], self.args.build_variant, [],
228223
prefer_native_toolchain=True)
229224
self.install_with_cmake(
230-
["install"], WASILibc.sysroot_install_path(build_root))
225+
["install"], WASILibc.sysroot_install_path(build_root, target_triple))
231226

232227
@classmethod
233228
def get_dependencies(cls):
@@ -236,12 +231,5 @@ def get_dependencies(cls):
236231

237232
class WasmThreadsLLVMRuntimeLibs(WasmLLVMRuntimeLibs):
238233
def build(self, host_target):
239-
self._build(host_target, enable_wasi_threads=True)
240-
241-
build_root = os.path.dirname(self.build_dir)
242-
wasi_sysroot = WASILibc.sysroot_install_path(build_root)
243-
# Copy compiler-rt os dirs to the WASI variant
244-
os_dir = os.path.join(wasi_sysroot, 'lib', 'wasip1')
245-
if os.path.exists(os_dir):
246-
shell.rmtree(os_dir)
247-
shell.copytree(os.path.join(wasi_sysroot, 'lib', 'wasi'), os_dir)
234+
self._build(host_target, enable_wasi_threads=True,
235+
compiler_rt_os_dir='wasip1', target_triple='wasm32-wasip1-threads')

utils/swift_build_support/swift_build_support/products/wasmstdlib.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ def should_test(self, host_target):
3939
return self.args.test_wasmstdlib
4040

4141
def build(self, host_target):
42+
self._build(host_target, 'wasm32-wasi')
43+
44+
def _build(self, host_target, target_triple):
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)
@@ -61,7 +64,7 @@ def build(self, host_target):
6164
self.cmake_options.define(
6265
'SWIFT_BUILD_RUNTIME_WITH_HOST_COMPILER:BOOL', 'FALSE')
6366
self.cmake_options.define('SWIFT_WASI_SYSROOT_PATH:STRING',
64-
self._wasi_sysroot_path)
67+
self._wasi_sysroot_path(target_triple))
6568

6669
# It's ok to use the host LLVM build dir just for CMake functionalities
6770
llvm_cmake_dir = os.path.join(self._host_llvm_build_dir(
@@ -117,7 +120,7 @@ def build(self, host_target):
117120
test_driver_options = [
118121
# compiler-rt is not installed in the final toolchain, so use one
119122
# in build dir
120-
'-Xclang-linker', '-resource-dir=' + self._wasi_sysroot_path,
123+
'-Xclang-linker', '-resource-dir=' + self._wasi_sysroot_path(target_triple),
121124
]
122125
# Leading space is needed to separate from other options
123126
self.cmake_options.define('SWIFT_DRIVER_TEST_OPTIONS:STRING',
@@ -169,10 +172,9 @@ def _host_swift_build_dir(self, host_target):
169172
build_root = os.path.dirname(self.build_dir)
170173
return os.path.join('..', build_root, '%s-%s' % ('swift', host_target))
171174

172-
@property
173-
def _wasi_sysroot_path(self):
175+
def _wasi_sysroot_path(self, target_triple):
174176
build_root = os.path.dirname(self.build_dir)
175-
return wasisysroot.WASILibc.sysroot_install_path(build_root)
177+
return wasisysroot.WASILibc.sysroot_install_path(build_root, target_triple)
176178

177179
def should_install(self, host_target):
178180
return False
@@ -187,6 +189,9 @@ def get_dependencies(cls):
187189

188190

189191
class WasmThreadsStdlib(WasmStdlib):
192+
def build(self, host_target):
193+
self._build(host_target, 'wasm32-wasip1-threads')
194+
190195
def should_test_executable(self):
191196
# TODO(katei): Enable tests once WasmKit supports WASI threads
192197
return False

0 commit comments

Comments
 (0)