Skip to content

Commit dd6eb62

Browse files
[wasm] Fix installation paths for libxml2.a
Clang driver only passes arch-specific library paths as search paths to the linker for WebAssembly targets but we were installing libxml2.a under `<sysroot>/lib` without the multiarch triple. It led to the linker not finding libxml2.a when importing FoundationXML
1 parent a541965 commit dd6eb62

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

utils/swift_build_support/swift_build_support/products/wasmswiftsdk.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def _append_platform_cmake_options(self, cmake_options, swift_host_triple, has_p
8181
cmake_options.define('CMAKE_RANLIB', os.path.join(
8282
native_toolchain_path, 'bin', 'llvm-ranlib'))
8383

84-
def _build_libxml2(self, swift_host_triple, has_pthread, wasi_sysroot):
84+
def _build_libxml2(self, swift_host_triple, clang_multiarch_triple, has_pthread, wasi_sysroot):
8585
libxml2 = CMakeProduct(
8686
args=self.args,
8787
toolchain=self.toolchain,
@@ -124,6 +124,10 @@ def _build_libxml2(self, swift_host_triple, has_pthread, wasi_sysroot):
124124
libxml2.cmake_options.define('LIBXML2_WITH_XPTR', 'FALSE')
125125
libxml2.cmake_options.define('LIBXML2_WITH_ZLIB', 'FALSE')
126126
libxml2.cmake_options.define('BUILD_SHARED_LIBS', 'FALSE')
127+
# Install libxml2.a under <sysroot>/lib/<clang_multiarch_triple>
128+
# because Clang driver only passes arch-specific library paths as
129+
# search paths to the linker for WebAssembly targets.
130+
libxml2.cmake_options.define('CMAKE_INSTALL_LIBDIR', f'lib/{clang_multiarch_triple}')
127131

128132
cmake_thread_enabled = 'TRUE' if has_pthread else 'FALSE'
129133
libxml2.cmake_options.define('LIBXML2_WITH_THREAD_ALLOC', cmake_thread_enabled)
@@ -137,7 +141,7 @@ def _build_libxml2(self, swift_host_triple, has_pthread, wasi_sysroot):
137141
shell.call([self.toolchain.cmake, '--install', '.', '--prefix', '/', '--component', 'development'],
138142
env={'DESTDIR': wasi_sysroot})
139143

140-
def _build_foundation(self, swift_host_triple, has_pthread, wasi_sysroot):
144+
def _build_foundation(self, swift_host_triple, clang_multiarch_triple, has_pthread, wasi_sysroot):
141145
source_root = os.path.dirname(self.source_dir)
142146
host_toolchain = self.native_toolchain_path(self.args.host_target)
143147

@@ -155,9 +159,13 @@ def _build_foundation(self, swift_host_triple, has_pthread, wasi_sysroot):
155159
foundation.cmake_options.define('_SwiftFoundation_SourceDIR', os.path.join(source_root, 'swift-foundation'))
156160
foundation.cmake_options.define('_SwiftFoundationICU_SourceDIR', os.path.join(source_root, 'swift-foundation-icu'))
157161
foundation.cmake_options.define('SwiftFoundation_MACRO', os.path.join(host_toolchain, 'lib', 'swift', 'host', 'plugins'))
158-
159-
foundation.cmake_options.define('LIBXML2_INCLUDE_DIR', os.path.join(wasi_sysroot, 'include', 'libxml2'))
160-
foundation.cmake_options.define('LIBXML2_LIBRARY', os.path.join(wasi_sysroot, 'lib'))
162+
# Teach CMake to use wasi-sysroot for finding packages through `find_package`.
163+
# With `CMAKE_LIBRARY_ARCHITECTURE`, CMake will search for libraries in
164+
# `<sysroot>/lib/<clang_multiarch_triple>/cmake/<name>*/<name>-config.cmake`,
165+
# which is the location where libxml2 installs its CMake config files.
166+
# See https://cmake.org/cmake/help/latest/command/find_package.html#search-procedure
167+
foundation.cmake_options.define('CMAKE_PREFIX_PATH', wasi_sysroot)
168+
foundation.cmake_options.define('CMAKE_LIBRARY_ARCHITECTURE', clang_multiarch_triple)
161169

162170
foundation.build_with_cmake([], self.args.build_variant, [],
163171
prefer_native_toolchain=not self.args.build_runtime_with_host_compiler,
@@ -214,7 +222,7 @@ def _build_xctest(self, swift_host_triple, has_pthread, wasi_sysroot):
214222
shell.call([self.toolchain.cmake, '--install', '.', '--prefix', '/usr'],
215223
env={'DESTDIR': dest_dir})
216224

217-
def _build_target_package(self, swift_host_triple, has_pthread,
225+
def _build_target_package(self, swift_host_triple, clang_multiarch_triple, has_pthread,
218226
stdlib_build_path, llvm_runtime_libs_build_path,
219227
wasi_sysroot):
220228

@@ -235,8 +243,8 @@ def _build_target_package(self, swift_host_triple, has_pthread,
235243
'--component', 'clang_rt.builtins-wasm32'],
236244
env={'DESTDIR': clang_dir})
237245

238-
self._build_libxml2(swift_host_triple, has_pthread, wasi_sysroot)
239-
self._build_foundation(swift_host_triple, has_pthread, wasi_sysroot)
246+
self._build_libxml2(swift_host_triple, clang_multiarch_triple, has_pthread, wasi_sysroot)
247+
self._build_foundation(swift_host_triple, clang_multiarch_triple, has_pthread, wasi_sysroot)
240248
# Build swift-testing
241249
self._build_swift_testing(swift_host_triple, has_pthread, wasi_sysroot)
242250
self._build_xctest(swift_host_triple, has_pthread, wasi_sysroot)
@@ -266,7 +274,7 @@ def build(self, host_target):
266274
build_root, '%s-%s' % ('wasmllvmruntimelibs', host_target),
267275
clang_multiarch_triple)
268276
package_path = self._build_target_package(
269-
swift_host_triple, has_pthread, stdlib_build_path,
277+
swift_host_triple, clang_multiarch_triple, has_pthread, stdlib_build_path,
270278
llvm_runtime_libs_build_path, wasi_sysroot)
271279
if build_sdk:
272280
target_packages.append((swift_host_triple, wasi_sysroot, package_path))

0 commit comments

Comments
 (0)