14
14
15
15
from . import product
16
16
from . import wasisysroot
17
- from .swift_testing import SwiftTestingCMakeShim
18
17
from .wasmstdlib import WasmStdlib , WasmThreadsStdlib
18
+ from .cmake_product import CMakeProduct
19
19
from .. import shell
20
20
21
21
@@ -41,52 +41,71 @@ def should_test(self, host_target):
41
41
def _target_package_path (self , swift_host_triple ):
42
42
return os .path .join (self .build_dir , 'Toolchains' , swift_host_triple )
43
43
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 )
44
+ def _append_platform_cmake_options (self , cmake_options , swift_host_triple , has_pthread , wasi_sysroot , extra_swift_flags ):
45
+ cmake_options .define ('CMAKE_SYSTEM_NAME:STRING' , 'WASI' )
46
+ cmake_options .define ('CMAKE_SYSTEM_PROCESSOR:STRING' , 'wasm32' )
47
+ cmake_options .define ('CMAKE_C_COMPILER_TARGET' , swift_host_triple )
48
+ cmake_options .define ('CMAKE_CXX_COMPILER_TARGET' , swift_host_triple )
49
+ cmake_options .define (
50
+ 'CMAKE_Swift_COMPILER_TARGET' , swift_host_triple )
51
+ cmake_options .define ('CMAKE_SYSROOT' , wasi_sysroot )
54
52
55
- swift_testing = SwiftTestingCMakeShim (
53
+ dest_dir = self ._target_package_path (swift_host_triple )
54
+ swift_resource_dir = os .path .join (dest_dir , 'usr' , 'lib' , 'swift_static' )
55
+ clang_resource_dir = os .path .join (swift_resource_dir , 'clang' )
56
+
57
+ swift_flags = ['-sdk' , wasi_sysroot , '-resource-dir' ,
58
+ 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' ,
66
+ '-Xcc' , '-mthread-model' , '-Xcc' , 'posix' , '-Xcc' , '-pthread' ])
67
+
68
+ cmake_options .define ('CMAKE_Swift_FLAGS' , ' ' .join (swift_flags ))
69
+ cmake_options .define ('CMAKE_C_FLAGS' , ' ' .join (c_flags ))
70
+ cmake_options .define ('CMAKE_CXX_FLAGS' , ' ' .join (cxx_flags ))
71
+ cmake_options .define ('CMAKE_Swift_COMPILER_FORCED' , 'TRUE' )
72
+ cmake_options .define ('CMAKE_CXX_COMPILER_FORCED' , 'TRUE' )
73
+ cmake_options .define ('CMAKE_BUILD_TYPE' , self .args .build_variant )
74
+
75
+ # Explicitly choose ar and ranlib from just-built LLVM tools since tools in the host system
76
+ # unlikely support Wasm object format.
77
+ native_toolchain_path = self .native_toolchain_path (self .args .host_target )
78
+ cmake_options .define ('CMAKE_AR' , os .path .join (
79
+ native_toolchain_path , 'bin' , 'llvm-ar' ))
80
+ cmake_options .define ('CMAKE_RANLIB' , os .path .join (
81
+ native_toolchain_path , 'bin' , 'llvm-ranlib' ))
82
+
83
+ def _build_swift_testing (self , swift_host_triple , has_pthread , wasi_sysroot ):
84
+ swift_testing = CMakeProduct (
56
85
args = self .args ,
57
86
toolchain = self .toolchain ,
58
87
source_dir = os .path .join (
59
88
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' )
89
+ build_dir = os .path .join (self .build_dir , 'swift-testing' , swift_host_triple ))
72
90
# For statically linked objects in an archive, we have to use singlethreaded
73
91
# LLVM codegen unit to prevent runtime metadata sections from being stripped
74
92
# at link-time.
93
+ self ._append_platform_cmake_options (
94
+ swift_testing .cmake_options , swift_host_triple , has_pthread , wasi_sysroot ,
95
+ extra_swift_flags = ['-Xfrontend' , '-enable-single-module-llvm-emission' ])
96
+ swift_testing .cmake_options .define ('BUILD_SHARED_LIBS' , 'FALSE' )
75
97
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' )
98
+ 'CMAKE_Swift_COMPILATION_MODE' , 'wholemodule' )
99
+ swift_testing .cmake_options .define ('SwiftTesting_MACRO' , 'NO' )
83
100
84
- swift_testing .build ('wasi-wasm32' )
101
+ swift_testing .build_with_cmake ([], self .args .build_variant , [],
102
+ prefer_native_toolchain = True )
103
+ dest_dir = self ._target_package_path (swift_host_triple )
85
104
with shell .pushd (swift_testing .build_dir ):
86
105
shell .call ([self .toolchain .cmake , '--install' , '.' , '--prefix' , '/usr' ],
87
106
env = {'DESTDIR' : dest_dir })
88
107
89
- def _build_target_package (self , swift_host_triple , short_triple ,
108
+ def _build_target_package (self , swift_host_triple , has_pthread ,
90
109
stdlib_build_path , llvm_runtime_libs_build_path ,
91
110
wasi_sysroot ):
92
111
@@ -107,25 +126,24 @@ def _build_target_package(self, swift_host_triple, short_triple,
107
126
'--component' , 'clang_rt.builtins-wasm32' ],
108
127
env = {'DESTDIR' : clang_dir })
109
128
# Build swift-testing
110
- self ._build_swift_testing (swift_host_triple , short_triple , wasi_sysroot )
129
+ self ._build_swift_testing (swift_host_triple , has_pthread , wasi_sysroot )
111
130
112
131
return dest_dir
113
132
114
133
def build (self , host_target ):
115
134
build_root = os .path .dirname (self .build_dir )
116
135
117
136
target_packages = []
118
- # NOTE: We have three types of target triples:
137
+ # NOTE: We have two types of target triples:
119
138
# 1. swift_host_triple: The triple used by the Swift compiler's '-target' option
120
139
# 2. clang_multiarch_triple: The triple used by Clang to find library
121
140
# and header paths from the sysroot
122
141
# 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'),
142
+ for swift_host_triple , clang_multiarch_triple , build_basename , build_sdk , has_pthread in [
143
+ ('wasm32-unknown-wasi' , 'wasm32-wasi' , 'wasmstdlib' , True , False ),
144
+ # TODO: Include p1-threads in the Swift SDK once sdk-generator supports multi-target SDK
145
+ ('wasm32-unknown-wasip1-threads' , 'wasm32-wasip1-threads' ,
146
+ 'wasmthreadsstdlib' , False , True ),
129
147
]:
130
148
stdlib_build_path = os .path .join (
131
149
build_root , '%s-%s' % (build_basename , host_target ))
@@ -135,9 +153,10 @@ def build(self, host_target):
135
153
build_root , '%s-%s' % ('wasmllvmruntimelibs' , host_target ),
136
154
clang_multiarch_triple )
137
155
package_path = self ._build_target_package (
138
- swift_host_triple , short_triple , stdlib_build_path ,
156
+ swift_host_triple , has_pthread , stdlib_build_path ,
139
157
llvm_runtime_libs_build_path , wasi_sysroot )
140
- target_packages .append ((swift_host_triple , wasi_sysroot , package_path ))
158
+ if build_sdk :
159
+ target_packages .append ((swift_host_triple , wasi_sysroot , package_path ))
141
160
142
161
swiftc_path = os .path .abspath (self .toolchain .swiftc )
143
162
toolchain_path = os .path .dirname (os .path .dirname (swiftc_path ))
0 commit comments