@@ -80,7 +80,7 @@ def _append_platform_cmake_options(self, cmake_options, swift_host_triple, has_p
8080 cmake_options .define ('CMAKE_RANLIB' , os .path .join (
8181 native_toolchain_path , 'bin' , 'llvm-ranlib' ))
8282
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 ):
8484 libxml2 = CMakeProduct (
8585 args = self .args ,
8686 toolchain = self .toolchain ,
@@ -123,6 +123,10 @@ def _build_libxml2(self, swift_host_triple, has_pthread, wasi_sysroot):
123123 libxml2 .cmake_options .define ('LIBXML2_WITH_XPTR' , 'FALSE' )
124124 libxml2 .cmake_options .define ('LIBXML2_WITH_ZLIB' , 'FALSE' )
125125 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 } ' )
126130
127131 cmake_thread_enabled = 'TRUE' if has_pthread else 'FALSE'
128132 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):
135139 shell .call ([self .toolchain .cmake , '--install' , '.' , '--prefix' , '/' , '--component' , 'development' ],
136140 env = {'DESTDIR' : wasi_sysroot })
137141
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 ):
139143 source_root = os .path .dirname (self .source_dir )
140144 host_toolchain = self .native_toolchain_path (self .args .host_target )
141145
@@ -153,9 +157,13 @@ def _build_foundation(self, swift_host_triple, has_pthread, wasi_sysroot):
153157 foundation .cmake_options .define ('_SwiftFoundation_SourceDIR' , os .path .join (source_root , 'swift-foundation' ))
154158 foundation .cmake_options .define ('_SwiftFoundationICU_SourceDIR' , os .path .join (source_root , 'swift-foundation-icu' ))
155159 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 )
159167
160168 foundation .build_with_cmake ([], self .args .build_variant , [],
161169 prefer_native_toolchain = True )
@@ -209,7 +217,7 @@ def _build_xctest(self, swift_host_triple, has_pthread, wasi_sysroot):
209217 shell .call ([self .toolchain .cmake , '--install' , '.' , '--prefix' , '/usr' ],
210218 env = {'DESTDIR' : dest_dir })
211219
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 ,
213221 stdlib_build_path , llvm_runtime_libs_build_path ,
214222 wasi_sysroot ):
215223
@@ -230,8 +238,8 @@ def _build_target_package(self, swift_host_triple, has_pthread,
230238 '--component' , 'clang_rt.builtins-wasm32' ],
231239 env = {'DESTDIR' : clang_dir })
232240
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 )
235243 # Build swift-testing
236244 self ._build_swift_testing (swift_host_triple , has_pthread , wasi_sysroot )
237245 self ._build_xctest (swift_host_triple , has_pthread , wasi_sysroot )
@@ -260,7 +268,7 @@ def build(self, host_target):
260268 wasi_sysroot = wasisysroot .WASILibc .sysroot_install_path (
261269 build_root , clang_multiarch_triple )
262270 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 ,
264272 llvm_runtime_libs_build_path , wasi_sysroot )
265273 if build_sdk :
266274 target_packages .append ((swift_host_triple , wasi_sysroot , package_path ))
0 commit comments