16
16
from . import wasisysroot
17
17
from .swift_testing import SwiftTestingCMakeShim
18
18
from .wasmstdlib import WasmStdlib , WasmThreadsStdlib
19
+ from .cmake_product import CMakeProduct
19
20
from .. import shell
20
21
21
22
@@ -41,49 +42,62 @@ def should_test(self, host_target):
41
42
def _target_package_path (self , swift_host_triple ):
42
43
return os .path .join (self .build_dir , 'Toolchains' , swift_host_triple )
43
44
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 )
54
53
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 (
56
82
args = self .args ,
57
83
toolchain = self .toolchain ,
58
84
source_dir = os .path .join (
59
85
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 )
82
96
with shell .pushd (swift_testing .build_dir ):
83
97
shell .call ([self .toolchain .cmake , '--install' , '.' , '--prefix' , '/usr' ],
84
98
env = {'DESTDIR' : dest_dir })
85
99
86
- def _build_target_package (self , swift_host_triple , short_triple ,
100
+ def _build_target_package (self , swift_host_triple , has_pthread ,
87
101
stdlib_build_path , llvm_runtime_libs_build_path ,
88
102
wasi_sysroot ):
89
103
@@ -104,25 +118,23 @@ def _build_target_package(self, swift_host_triple, short_triple,
104
118
'--component' , 'clang_rt.builtins-wasm32' ],
105
119
env = {'DESTDIR' : clang_dir })
106
120
# 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 )
108
122
109
123
return dest_dir
110
124
111
125
def build (self , host_target ):
112
126
build_root = os .path .dirname (self .build_dir )
113
127
114
128
target_packages = []
115
- # NOTE: We have three types of target triples:
129
+ # NOTE: We have two types of target triples:
116
130
# 1. swift_host_triple: The triple used by the Swift compiler's '-target' option
117
131
# 2. clang_multiarch_triple: The triple used by Clang to find library
118
132
# and header paths from the sysroot
119
133
# 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 ),
126
138
]:
127
139
stdlib_build_path = os .path .join (
128
140
build_root , '%s-%s' % (build_basename , host_target ))
@@ -132,9 +144,10 @@ def build(self, host_target):
132
144
build_root , '%s-%s' % ('wasmllvmruntimelibs' , host_target ),
133
145
clang_multiarch_triple )
134
146
package_path = self ._build_target_package (
135
- swift_host_triple , short_triple , stdlib_build_path ,
147
+ swift_host_triple , has_pthread , stdlib_build_path ,
136
148
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 ))
138
151
139
152
swiftc_path = os .path .abspath (self .toolchain .swiftc )
140
153
toolchain_path = os .path .dirname (os .path .dirname (swiftc_path ))
0 commit comments