14
14
15
15
from . import product
16
16
from . import wasisysroot
17
+ from .swift_testing import SwiftTestingCMakeShim
17
18
from .wasmstdlib import WasmStdlib , WasmThreadsStdlib
18
19
from .. import shell
19
20
@@ -37,13 +38,56 @@ def should_build(self, host_target):
37
38
def should_test (self , host_target ):
38
39
return False
39
40
40
- def _target_package_path (self , target_triple ):
41
- return os .path .join (self .build_dir , 'Toolchains' , target_triple )
41
+ def _target_package_path (self , swift_host_triple ):
42
+ return os .path .join (self .build_dir , 'Toolchains' , swift_host_triple )
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 )
54
+
55
+ swift_testing = SwiftTestingCMakeShim (
56
+ args = self .args ,
57
+ toolchain = self .toolchain ,
58
+ source_dir = os .path .join (
59
+ 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' )
82
+ with shell .pushd (swift_testing .build_dir ):
83
+ shell .call ([self .toolchain .cmake , '--install' , '.' , '--prefix' , '/usr' ],
84
+ env = {'DESTDIR' : dest_dir })
42
85
43
- def _build_target_package (self , target_triple ,
44
- stdlib_build_path , llvm_runtime_libs_build_path ):
86
+ def _build_target_package (self , swift_host_triple , short_triple ,
87
+ stdlib_build_path , llvm_runtime_libs_build_path ,
88
+ wasi_sysroot ):
45
89
46
- dest_dir = self ._target_package_path (target_triple )
90
+ dest_dir = self ._target_package_path (swift_host_triple )
47
91
shell .rmtree (dest_dir )
48
92
shell .makedirs (dest_dir )
49
93
@@ -59,6 +103,8 @@ def _build_target_package(self, target_triple,
59
103
shell .call ([self .toolchain .cmake , '--install' , '.' ,
60
104
'--component' , 'clang_rt.builtins-wasm32' ],
61
105
env = {'DESTDIR' : clang_dir })
106
+ # Build swift-testing
107
+ self ._build_swift_testing (swift_host_triple , short_triple , wasi_sysroot )
62
108
63
109
return dest_dir
64
110
@@ -68,16 +114,26 @@ def build(self, host_target):
68
114
build_root , '%s-%s' % ('wasmllvmruntimelibs' , host_target ))
69
115
70
116
target_packages = []
71
- for target_triple , short_triple , build_basename in [
72
- ('wasm32-unknown-wasi' , 'wasm32-wasi' , 'wasmstdlib' ),
117
+ # NOTE: We have three types of target triples:
118
+ # 1. swift_host_triple: The triple used by the Swift compiler's '-target' option
119
+ # 2. clang_multiarch_triple: The triple used by Clang to find library
120
+ # and header paths from the sysroot
121
+ # https://github.com/llvm/llvm-project/blob/73ef397fcba35b7b4239c00bf3e0b4e689ca0add/clang/lib/Driver/ToolChains/WebAssembly.cpp#L29-L36
122
+ # 3. short_triple: The triple used by build-script to name the build directory
123
+ for swift_host_triple , clang_multiarch_triple , short_triple , build_basename in [
124
+ ('wasm32-unknown-wasi' , 'wasm32-wasi' , 'wasi-wasm32' , 'wasmstdlib' ),
73
125
# TODO: Enable threads stdlib once sdk-generator supports multi-target SDK
74
- # ('wasm32-unknown-wasip1-threads', 'wasmthreadsstdlib'),
126
+ # ('wasm32-unknown-wasip1-threads', 'wasm32-wasip1-threads',
127
+ # 'wasip1-threads-wasm32', 'wasmthreadsstdlib'),
75
128
]:
76
129
stdlib_build_path = os .path .join (
77
130
build_root , '%s-%s' % (build_basename , host_target ))
131
+ wasi_sysroot = wasisysroot .WASILibc .sysroot_install_path (
132
+ build_root , clang_multiarch_triple )
78
133
package_path = self ._build_target_package (
79
- target_triple , stdlib_build_path , llvm_runtime_libs_build_path )
80
- target_packages .append ((target_triple , package_path ))
134
+ swift_host_triple , short_triple , stdlib_build_path ,
135
+ llvm_runtime_libs_build_path , wasi_sysroot )
136
+ target_packages .append ((swift_host_triple , wasi_sysroot , package_path ))
81
137
82
138
swiftc_path = os .path .abspath (self .toolchain .swiftc )
83
139
toolchain_path = os .path .dirname (os .path .dirname (swiftc_path ))
@@ -93,11 +149,9 @@ def build(self, host_target):
93
149
'make-wasm-sdk' ,
94
150
'--swift-version' , swift_version ,
95
151
]
96
- for target_triple , package_path in target_packages :
97
- run_args .extend (['--target' , target_triple ])
152
+ for swift_host_triple , wasi_sysroot , package_path in target_packages :
153
+ run_args .extend (['--target' , swift_host_triple ])
98
154
run_args .extend (['--target-swift-package-path' , package_path ])
99
- wasi_sysroot = wasisysroot .WASILibc .sysroot_install_path (
100
- build_root , short_triple )
101
155
run_args .extend (['--wasi-sysroot' , wasi_sysroot ])
102
156
103
157
env = dict (os .environ )
0 commit comments