Skip to content

Commit 9da1bc9

Browse files
kateinoigakukunAnka
authored andcommitted
[wasm] Configure LLVM for WebAssembly target independently from the native LLVM build
We have been using the host LLVM build directory to configure the Wasm Swift stdlib. This has been working fine so far as the stdlib build is configured with `CMAKE_SYSTEM_NAME` having host system name. However, we fixed the stdlib build to use `WASI` as the system name properly in the previous commit. This exposed the issue that the host LLVM build directory is not suitable for configuring the stdlib build.
1 parent 92f225d commit 9da1bc9

File tree

1 file changed

+54
-6
lines changed
  • utils/swift_build_support/swift_build_support/products

1 file changed

+54
-6
lines changed

utils/swift_build_support/swift_build_support/products/wasmstdlib.py

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
from . import swift
1818
from . import wasisysroot
1919
from . import wasmkit
20+
from .. import cmake
21+
from .. import shell
2022

2123

2224
class WasmStdlib(cmake_product.CMakeProduct):
@@ -39,13 +41,52 @@ def should_test(self, host_target):
3941
return self.args.test_wasmstdlib
4042

4143
def build(self, host_target):
42-
self._build(host_target, 'wasm32-wasi')
44+
self._build(host_target, 'wasm32-wasi', 'wasi-wasm32')
4345

44-
def _build(self, host_target, target_triple):
46+
def _build(self, host_target, target_triple, short_triple):
47+
llvm_build_dir = self._configure_llvm(target_triple, short_triple)
48+
llvm_cmake_dir = os.path.join(llvm_build_dir, 'lib', 'cmake', 'llvm')
49+
self._build_stdlib(host_target, target_triple, llvm_cmake_dir)
50+
51+
def _configure_llvm(self, target_triple, short_triple):
52+
# Configure LLVM for WebAssembly target independently
53+
# from the native LLVM build to turn off zlib and libxml2
54+
build_root = os.path.dirname(self.build_dir)
55+
build_dir = os.path.join(
56+
build_root, 'llvm-%s' % short_triple)
57+
llvm_source_dir = os.path.join(
58+
os.path.dirname(self.source_dir), 'llvm-project', 'llvm')
59+
cmake_options = cmake.CMakeOptions()
60+
cmake_options.define('CMAKE_BUILD_TYPE:STRING', self._build_variant)
61+
# compiler-rt for WebAssembly target is not installed in the host toolchain
62+
# so skip compiler health checks here.
63+
cmake_options.define('CMAKE_C_COMPILER_WORKS:BOOL', 'TRUE')
64+
cmake_options.define('CMAKE_CXX_COMPILER_WORKS:BOOL', 'TRUE')
65+
cmake_options.define('LLVM_COMPILER_CHECKED:BOOL', 'TRUE')
66+
cmake_options.define('LLVM_ENABLE_ZLIB:BOOL', 'FALSE')
67+
cmake_options.define('LLVM_ENABLE_LIBXML2:BOOL', 'FALSE')
68+
cmake_options.define('LLVM_ENABLE_LIBEDIT:BOOL', 'FALSE')
69+
cmake_options.define('LLVM_ENABLE_TERMINFO:BOOL', 'FALSE')
70+
71+
llvm_cmake = cmake.CMake(
72+
self.args, self.toolchain, prefer_native_toolchain=True)
73+
# Only configure LLVM, not build it because we just need
74+
# LLVM CMake functionalities
75+
shell.call(["env", self.toolchain.cmake, "-B", build_dir]
76+
+ list(llvm_cmake.common_options(self))
77+
+ list(cmake_options)
78+
+ [llvm_source_dir])
79+
return build_dir
80+
81+
def _build_stdlib(self, host_target, target_triple, llvm_cmake_dir):
4582
self.cmake_options.define('CMAKE_INSTALL_PREFIX:PATH', '/usr')
4683
self.cmake_options.define('CMAKE_BUILD_TYPE:STRING', self._build_variant)
84+
# Teach about the WebAssembly target. UNIX is explicitly set to TRUE
85+
# as CMake still doesn't recognize WASI as a UNIX platform and the
86+
# variable is used in LLVM CMake configuration.
4787
self.cmake_options.define('CMAKE_SYSTEM_NAME:STRING', 'WASI')
4888
self.cmake_options.define('CMAKE_SYSTEM_PROCESSOR:STRING', 'wasm32')
89+
self.cmake_options.define('UNIX:BOOL', 'TRUE')
4990
self.cmake_options.define(
5091
'SWIFT_STDLIB_BUILD_TYPE:STRING', self._build_variant)
5192

@@ -71,9 +112,13 @@ def _build(self, host_target, target_triple):
71112
self.cmake_options.define('SWIFT_WASI_SYSROOT_PATH:STRING',
72113
self._wasi_sysroot_path(target_triple))
73114

74-
# It's ok to use the host LLVM build dir just for CMake functionalities
75-
llvm_cmake_dir = os.path.join(self._host_llvm_build_dir(
76-
host_target), 'lib', 'cmake', 'llvm')
115+
# compiler-rt for WebAssembly target is not installed in the host toolchain
116+
# so skip compiler health checks here.
117+
self.cmake_options.define('CMAKE_C_COMPILER_WORKS:BOOL', 'TRUE')
118+
self.cmake_options.define('CMAKE_CXX_COMPILER_WORKS:BOOL', 'TRUE')
119+
self.cmake_options.define('CMAKE_Swift_COMPILER_WORKS:BOOL', 'TRUE')
120+
self.cmake_options.define('LLVM_COMPILER_CHECKED:BOOL', 'TRUE')
121+
77122
self.cmake_options.define('LLVM_DIR:PATH', llvm_cmake_dir)
78123

79124
# Standalone stdlib configuration
@@ -90,6 +135,9 @@ def _build(self, host_target, target_triple):
90135
self.cmake_options.define('SWIFT_BUILD_STATIC_STDLIB:BOOL', 'TRUE')
91136
self.cmake_options.define('SWIFT_BUILD_DYNAMIC_STDLIB:BOOL', 'FALSE')
92137
self.cmake_options.define('SWIFT_BUILD_STATIC_SDK_OVERLAY:BOOL', 'TRUE')
138+
# TODO: Turn off library evolution once we establish a good way to teach
139+
# libraries including swift-testing whether to use the stable ABI.
140+
self.cmake_options.define('SWIFT_STDLIB_STABLE_ABI:BOOL', 'TRUE')
93141
self.cmake_options.define('SWIFT_STDLIB_TRACING:BOOL', 'FALSE')
94142
self.cmake_options.define('SWIFT_STDLIB_HAS_ASLR:BOOL', 'FALSE')
95143
self.cmake_options.define('SWIFT_STDLIB_CONCURRENCY_TRACING:BOOL', 'FALSE')
@@ -200,7 +248,7 @@ def get_dependencies(cls):
200248

201249
class WasmThreadsStdlib(WasmStdlib):
202250
def build(self, host_target):
203-
self._build(host_target, 'wasm32-wasip1-threads')
251+
self._build(host_target, 'wasm32-wasip1-threads', 'wasip1-threads-wasm32')
204252

205253
def should_test_executable(self):
206254
# TODO(katei): Enable tests once WasmKit supports WASI threads

0 commit comments

Comments
 (0)