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,52 +42,66 @@ 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 , extra_swift_flags ):
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 ] + 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' , '-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' )
86
+ build_dir = os .path .join (self .build_dir , 'swift-testing' , swift_host_triple ))
72
87
# For statically linked objects in an archive, we have to use singlethreaded
73
88
# LLVM codegen unit to prevent runtime metadata sections from being stripped
74
89
# at link-time.
75
- 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' )
83
-
84
- swift_testing . build ( 'wasi-wasm32' )
90
+ self . _append_platform_cmake_options (
91
+ swift_testing . cmake_options , swift_host_triple , has_pthread , wasi_sysroot ,
92
+ extra_swift_flags = [ '- Xfrontend' , ' -enable-single-module-llvm-emission'] )
93
+ swift_testing . cmake_options . define ( 'BUILD_SHARED_LIBS ' , 'FALSE ' )
94
+ swift_testing .cmake_options .define ('CMAKE_Swift_COMPILATION_MODE' , 'wholemodule' )
95
+ swift_testing . cmake_options . define ( 'SwiftTesting_MACRO ' , 'NO ' )
96
+
97
+ swift_testing .build_with_cmake ([], self . args . build_variant , [],
98
+ prefer_native_toolchain = True )
99
+ dest_dir = self . _target_package_path ( swift_host_triple )
85
100
with shell .pushd (swift_testing .build_dir ):
86
101
shell .call ([self .toolchain .cmake , '--install' , '.' , '--prefix' , '/usr' ],
87
102
env = {'DESTDIR' : dest_dir })
88
103
89
- def _build_target_package (self , swift_host_triple , short_triple ,
104
+ def _build_target_package (self , swift_host_triple , has_pthread ,
90
105
stdlib_build_path , llvm_runtime_libs_build_path ,
91
106
wasi_sysroot ):
92
107
@@ -107,25 +122,23 @@ def _build_target_package(self, swift_host_triple, short_triple,
107
122
'--component' , 'clang_rt.builtins-wasm32' ],
108
123
env = {'DESTDIR' : clang_dir })
109
124
# Build swift-testing
110
- self ._build_swift_testing (swift_host_triple , short_triple , wasi_sysroot )
125
+ self ._build_swift_testing (swift_host_triple , has_pthread , wasi_sysroot )
111
126
112
127
return dest_dir
113
128
114
129
def build (self , host_target ):
115
130
build_root = os .path .dirname (self .build_dir )
116
131
117
132
target_packages = []
118
- # NOTE: We have three types of target triples:
133
+ # NOTE: We have two types of target triples:
119
134
# 1. swift_host_triple: The triple used by the Swift compiler's '-target' option
120
135
# 2. clang_multiarch_triple: The triple used by Clang to find library
121
136
# and header paths from the sysroot
122
137
# 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'),
138
+ for swift_host_triple , clang_multiarch_triple , build_basename , build_sdk , has_pthread in [
139
+ ('wasm32-unknown-wasi' , 'wasm32-wasi' , 'wasmstdlib' , True , False ),
140
+ # TODO: Include p1-threads in the Swift SDK once sdk-generator supports multi-target SDK
141
+ ('wasm32-unknown-wasip1-threads' , 'wasm32-wasip1-threads' , 'wasmthreadsstdlib' , False , True ),
129
142
]:
130
143
stdlib_build_path = os .path .join (
131
144
build_root , '%s-%s' % (build_basename , host_target ))
@@ -135,9 +148,10 @@ def build(self, host_target):
135
148
build_root , '%s-%s' % ('wasmllvmruntimelibs' , host_target ),
136
149
clang_multiarch_triple )
137
150
package_path = self ._build_target_package (
138
- swift_host_triple , short_triple , stdlib_build_path ,
151
+ swift_host_triple , has_pthread , stdlib_build_path ,
139
152
llvm_runtime_libs_build_path , wasi_sysroot )
140
- target_packages .append ((swift_host_triple , wasi_sysroot , package_path ))
153
+ if build_sdk :
154
+ target_packages .append ((swift_host_triple , wasi_sysroot , package_path ))
141
155
142
156
swiftc_path = os .path .abspath (self .toolchain .swiftc )
143
157
toolchain_path = os .path .dirname (os .path .dirname (swiftc_path ))
0 commit comments