diff --git a/utils/swift_build_support/swift_build_support/products/wasisysroot.py b/utils/swift_build_support/swift_build_support/products/wasisysroot.py index b77d2ec14468f..e780809ff38ac 100644 --- a/utils/swift_build_support/swift_build_support/products/wasisysroot.py +++ b/utils/swift_build_support/swift_build_support/products/wasisysroot.py @@ -56,7 +56,8 @@ def _build(self, host_target, thread_model='single', target_triple='wasm32-wasi' clang_tools_path = self.args.native_clang_tools_path or llvm_build_bin_dir build_jobs = self.args.build_jobs or multiprocessing.cpu_count() - sysroot_build_dir = WASILibc.sysroot_build_path(build_root, host_target) + sysroot_build_dir = WASILibc.sysroot_build_path( + build_root, host_target, target_triple) # FIXME: Manually create an empty dir that is usually created during # check-symbols. The directory is required during sysroot installation step. 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' '-C', self.source_dir, 'OBJDIR=' + os.path.join(self.build_dir, 'obj-' + thread_model), 'SYSROOT=' + sysroot_build_dir, - 'INSTALL_DIR=' + WASILibc.sysroot_install_path(build_root), + 'INSTALL_DIR=' + WASILibc.sysroot_install_path(build_root, target_triple), 'CC=' + os.path.join(clang_tools_path, 'clang'), 'AR=' + os.path.join(llvm_tools_path, 'llvm-ar'), 'NM=' + os.path.join(llvm_tools_path, 'llvm-nm'), @@ -86,21 +87,22 @@ def get_dependencies(cls): return [llvm.LLVM] @classmethod - def sysroot_build_path(cls, build_root, host_target): + def sysroot_build_path(cls, build_root, host_target, target_triple): """ Returns the path to the sysroot build directory, which contains only the artifacts of wasi-libc (Not including the artifacts of LLVM runtimes). """ return os.path.join(build_root, - '%s-%s' % (cls.product_name(), host_target), 'sysroot') + '%s-%s' % (cls.product_name(), host_target), + 'sysroot', target_triple) @classmethod - def sysroot_install_path(cls, build_root): + def sysroot_install_path(cls, build_root, target_triple): """ Returns the path to the sysroot install directory, which contains artifacts of wasi-libc and LLVM runtimes. """ - return os.path.join(build_root, 'wasi-sysroot') + return os.path.join(build_root, 'wasi-sysroot', target_triple) class WasmLLVMRuntimeLibs(cmake_product.CMakeProduct): @@ -130,7 +132,8 @@ def should_install(self, host_target): def build(self, host_target): self._build(host_target) - def _build(self, host_target, enable_wasi_threads=False): + def _build(self, host_target, enable_wasi_threads=False, + compiler_rt_os_dir='wasi', target_triple='wasm32-wasi'): build_root = os.path.dirname(self.build_dir) llvm_build_bin_dir = os.path.join( '..', build_root, '%s-%s' % ('llvm', host_target), 'bin') @@ -139,18 +142,14 @@ def _build(self, host_target, enable_wasi_threads=False): cmake_has_threads = 'TRUE' if enable_wasi_threads else 'FALSE' - self.cmake_options.define('CMAKE_SYSROOT:PATH', - WASILibc.sysroot_build_path(build_root, host_target)) - enable_runtimes = ['libcxx', 'libcxxabi'] - if not enable_wasi_threads: - # compiler-rt can be shared between wasi and wasip1-threads - enable_runtimes.append('compiler-rt') + self.cmake_options.define( + 'CMAKE_SYSROOT:PATH', + WASILibc.sysroot_build_path(build_root, host_target, target_triple)) + enable_runtimes = ['libcxx', 'libcxxabi', 'compiler-rt'] self.cmake_options.define('LLVM_ENABLE_RUNTIMES:STRING', ';'.join(enable_runtimes)) - libdir_suffix = '/wasm32-wasi' - if enable_wasi_threads: - libdir_suffix = '/wasm32-wasip1-threads' + libdir_suffix = '/' + target_triple self.cmake_options.define('LIBCXX_LIBDIR_SUFFIX:STRING', libdir_suffix) self.cmake_options.define('LIBCXXABI_LIBDIR_SUFFIX:STRING', libdir_suffix) self.cmake_options.define('CMAKE_STAGING_PREFIX:PATH', '/') @@ -162,7 +161,7 @@ def _build(self, host_target, enable_wasi_threads=False): self.cmake_options.define('COMPILER_RT_INCLUDE_TESTS:BOOL', 'FALSE') self.cmake_options.define('COMPILER_RT_HAS_FPIC_FLAG:BOOL', 'FALSE') self.cmake_options.define('COMPILER_RT_EXCLUDE_ATOMIC_BUILTIN:BOOL', 'FALSE') - self.cmake_options.define('COMPILER_RT_OS_DIR:STRING', 'wasi') + self.cmake_options.define('COMPILER_RT_OS_DIR:STRING', compiler_rt_os_dir) self.cmake_options.define('CMAKE_C_COMPILER_WORKS:BOOL', 'TRUE') self.cmake_options.define('CMAKE_CXX_COMPILER_WORKS:BOOL', 'TRUE') @@ -190,10 +189,6 @@ def _build(self, host_target, enable_wasi_threads=False): self.cmake_options.define('CMAKE_C_FLAGS:STRING', ' '.join(c_flags)) self.cmake_options.define('CMAKE_CXX_FLAGS:STRING', ' '.join(cxx_flags)) - if enable_wasi_threads: - target_triple = 'wasm32-wasip1-threads' - else: - target_triple = 'wasm32-wasi' self.cmake_options.define('CMAKE_C_COMPILER_TARGET:STRING', target_triple) self.cmake_options.define('CMAKE_CXX_COMPILER_TARGET:STRING', target_triple) @@ -227,7 +222,7 @@ def _build(self, host_target, enable_wasi_threads=False): self.build_with_cmake([], self.args.build_variant, [], prefer_native_toolchain=True) self.install_with_cmake( - ["install"], WASILibc.sysroot_install_path(build_root)) + ["install"], WASILibc.sysroot_install_path(build_root, target_triple)) @classmethod def get_dependencies(cls): @@ -236,12 +231,5 @@ def get_dependencies(cls): class WasmThreadsLLVMRuntimeLibs(WasmLLVMRuntimeLibs): def build(self, host_target): - self._build(host_target, enable_wasi_threads=True) - - build_root = os.path.dirname(self.build_dir) - wasi_sysroot = WASILibc.sysroot_install_path(build_root) - # Copy compiler-rt os dirs to the WASI variant - os_dir = os.path.join(wasi_sysroot, 'lib', 'wasip1') - if os.path.exists(os_dir): - shell.rmtree(os_dir) - shell.copytree(os.path.join(wasi_sysroot, 'lib', 'wasi'), os_dir) + self._build(host_target, enable_wasi_threads=True, + compiler_rt_os_dir='wasip1', target_triple='wasm32-wasip1-threads') diff --git a/utils/swift_build_support/swift_build_support/products/wasmstdlib.py b/utils/swift_build_support/swift_build_support/products/wasmstdlib.py index 08807e47b0e35..820a165f0337a 100644 --- a/utils/swift_build_support/swift_build_support/products/wasmstdlib.py +++ b/utils/swift_build_support/swift_build_support/products/wasmstdlib.py @@ -39,6 +39,9 @@ def should_test(self, host_target): return self.args.test_wasmstdlib def build(self, host_target): + self._build(host_target, 'wasm32-wasi') + + def _build(self, host_target, target_triple): self.cmake_options.define('CMAKE_BUILD_TYPE:STRING', self._build_variant) self.cmake_options.define( 'SWIFT_STDLIB_BUILD_TYPE:STRING', self._build_variant) @@ -61,7 +64,7 @@ def build(self, host_target): self.cmake_options.define( 'SWIFT_BUILD_RUNTIME_WITH_HOST_COMPILER:BOOL', 'FALSE') self.cmake_options.define('SWIFT_WASI_SYSROOT_PATH:STRING', - self._wasi_sysroot_path) + self._wasi_sysroot_path(target_triple)) # It's ok to use the host LLVM build dir just for CMake functionalities llvm_cmake_dir = os.path.join(self._host_llvm_build_dir( @@ -117,7 +120,7 @@ def build(self, host_target): test_driver_options = [ # compiler-rt is not installed in the final toolchain, so use one # in build dir - '-Xclang-linker', '-resource-dir=' + self._wasi_sysroot_path, + '-Xclang-linker', '-resource-dir=' + self._wasi_sysroot_path(target_triple), ] # Leading space is needed to separate from other options self.cmake_options.define('SWIFT_DRIVER_TEST_OPTIONS:STRING', @@ -169,10 +172,9 @@ def _host_swift_build_dir(self, host_target): build_root = os.path.dirname(self.build_dir) return os.path.join('..', build_root, '%s-%s' % ('swift', host_target)) - @property - def _wasi_sysroot_path(self): + def _wasi_sysroot_path(self, target_triple): build_root = os.path.dirname(self.build_dir) - return wasisysroot.WASILibc.sysroot_install_path(build_root) + return wasisysroot.WASILibc.sysroot_install_path(build_root, target_triple) def should_install(self, host_target): return False @@ -187,6 +189,9 @@ def get_dependencies(cls): class WasmThreadsStdlib(WasmStdlib): + def build(self, host_target): + self._build(host_target, 'wasm32-wasip1-threads') + def should_test_executable(self): # TODO(katei): Enable tests once WasmKit supports WASI threads return False