@@ -80,7 +80,7 @@ def _append_platform_cmake_options(self, cmake_options, swift_host_triple, has_p
80
80
cmake_options .define ('CMAKE_RANLIB' , os .path .join (
81
81
native_toolchain_path , 'bin' , 'llvm-ranlib' ))
82
82
83
- def _build_libxml2 (self , swift_host_triple , has_pthread , wasi_sysroot ):
83
+ def _build_libxml2 (self , swift_host_triple , clang_multiarch_triple , has_pthread , wasi_sysroot ):
84
84
libxml2 = CMakeProduct (
85
85
args = self .args ,
86
86
toolchain = self .toolchain ,
@@ -123,6 +123,10 @@ def _build_libxml2(self, swift_host_triple, has_pthread, wasi_sysroot):
123
123
libxml2 .cmake_options .define ('LIBXML2_WITH_XPTR' , 'FALSE' )
124
124
libxml2 .cmake_options .define ('LIBXML2_WITH_ZLIB' , 'FALSE' )
125
125
libxml2 .cmake_options .define ('BUILD_SHARED_LIBS' , 'FALSE' )
126
+ # Install libxml2.a under <sysroot>/lib/<clang_multiarch_triple>
127
+ # because Clang driver only passes arch-specific library paths as
128
+ # search paths to the linker for WebAssembly targets.
129
+ libxml2 .cmake_options .define ('CMAKE_INSTALL_LIBDIR' , f'lib/{ clang_multiarch_triple } ' )
126
130
127
131
cmake_thread_enabled = 'TRUE' if has_pthread else 'FALSE'
128
132
libxml2 .cmake_options .define ('LIBXML2_WITH_THREAD_ALLOC' , cmake_thread_enabled )
@@ -135,7 +139,7 @@ def _build_libxml2(self, swift_host_triple, has_pthread, wasi_sysroot):
135
139
shell .call ([self .toolchain .cmake , '--install' , '.' , '--prefix' , '/' , '--component' , 'development' ],
136
140
env = {'DESTDIR' : wasi_sysroot })
137
141
138
- def _build_foundation (self , swift_host_triple , has_pthread , wasi_sysroot ):
142
+ def _build_foundation (self , swift_host_triple , clang_multiarch_triple , has_pthread , wasi_sysroot ):
139
143
source_root = os .path .dirname (self .source_dir )
140
144
host_toolchain = self .native_toolchain_path (self .args .host_target )
141
145
@@ -153,9 +157,13 @@ def _build_foundation(self, swift_host_triple, has_pthread, wasi_sysroot):
153
157
foundation .cmake_options .define ('_SwiftFoundation_SourceDIR' , os .path .join (source_root , 'swift-foundation' ))
154
158
foundation .cmake_options .define ('_SwiftFoundationICU_SourceDIR' , os .path .join (source_root , 'swift-foundation-icu' ))
155
159
foundation .cmake_options .define ('SwiftFoundation_MACRO' , os .path .join (host_toolchain , 'lib' , 'swift' , 'host' , 'plugins' ))
156
-
157
- foundation .cmake_options .define ('LIBXML2_INCLUDE_DIR' , os .path .join (wasi_sysroot , 'include' , 'libxml2' ))
158
- foundation .cmake_options .define ('LIBXML2_LIBRARY' , os .path .join (wasi_sysroot , 'lib' ))
160
+ # Teach CMake to use wasi-sysroot for finding packages through `find_package`.
161
+ # With `CMAKE_LIBRARY_ARCHITECTURE`, CMake will search for libraries in
162
+ # `<sysroot>/lib/<clang_multiarch_triple>/cmake/<name>*/<name>-config.cmake`,
163
+ # which is the location where libxml2 installs its CMake config files.
164
+ # See https://cmake.org/cmake/help/latest/command/find_package.html#search-procedure
165
+ foundation .cmake_options .define ('CMAKE_PREFIX_PATH' , wasi_sysroot )
166
+ foundation .cmake_options .define ('CMAKE_LIBRARY_ARCHITECTURE' , clang_multiarch_triple )
159
167
160
168
foundation .build_with_cmake ([], self .args .build_variant , [],
161
169
prefer_native_toolchain = True )
@@ -209,7 +217,7 @@ def _build_xctest(self, swift_host_triple, has_pthread, wasi_sysroot):
209
217
shell .call ([self .toolchain .cmake , '--install' , '.' , '--prefix' , '/usr' ],
210
218
env = {'DESTDIR' : dest_dir })
211
219
212
- def _build_target_package (self , swift_host_triple , has_pthread ,
220
+ def _build_target_package (self , swift_host_triple , clang_multiarch_triple , has_pthread ,
213
221
stdlib_build_path , llvm_runtime_libs_build_path ,
214
222
wasi_sysroot ):
215
223
@@ -230,8 +238,8 @@ def _build_target_package(self, swift_host_triple, has_pthread,
230
238
'--component' , 'clang_rt.builtins-wasm32' ],
231
239
env = {'DESTDIR' : clang_dir })
232
240
233
- self ._build_libxml2 (swift_host_triple , has_pthread , wasi_sysroot )
234
- self ._build_foundation (swift_host_triple , has_pthread , wasi_sysroot )
241
+ self ._build_libxml2 (swift_host_triple , clang_multiarch_triple , has_pthread , wasi_sysroot )
242
+ self ._build_foundation (swift_host_triple , clang_multiarch_triple , has_pthread , wasi_sysroot )
235
243
# Build swift-testing
236
244
self ._build_swift_testing (swift_host_triple , has_pthread , wasi_sysroot )
237
245
self ._build_xctest (swift_host_triple , has_pthread , wasi_sysroot )
@@ -260,7 +268,7 @@ def build(self, host_target):
260
268
wasi_sysroot = wasisysroot .WASILibc .sysroot_install_path (
261
269
build_root , clang_multiarch_triple )
262
270
package_path = self ._build_target_package (
263
- swift_host_triple , has_pthread , stdlib_build_path ,
271
+ swift_host_triple , clang_multiarch_triple , has_pthread , stdlib_build_path ,
264
272
llvm_runtime_libs_build_path , wasi_sysroot )
265
273
if build_sdk :
266
274
target_packages .append ((swift_host_triple , wasi_sysroot , package_path ))
0 commit comments