Skip to content

SwiftPM fails to resolve system libraries that are present in the target SDK (--swift-sdk) #7409

Open
@t089

Description

@t089

Is it reproducible with SwiftPM command-line tools: swift build, swift test, swift package etc?

  • Confirmed reproduction steps with SwiftPM CLI.

Description

Currently, when cross-compiling packages with system dependencies, SwiftPM will parse .pc files from the SDK but then take those paths as-is without considering the path of the SDK itself.

For example, glib-2.0 on Ubuntu Jammy defines the following .pc file:

prefix=/usr
libdir=${prefix}/lib/x86_64-linux-gnu
includedir=${prefix}/include

bindir=${prefix}/bin
glib_genmarshal=${bindir}/glib-genmarshal
gobject_query=${bindir}/gobject-query
glib_mkenums=${bindir}/glib-mkenums

Name: GLib
Description: C Utility Library
Version: 2.72.4
Requires.private: libpcre >=  8.31
Libs: -L${libdir} -lglib-2.0
Libs.private: -pthread -lm
Cflags: -I${includedir}/glib-2.0 -I${libdir}/glib-2.0/include

Building a SwiftPM package using a Swift SDK that contains this file will result in flags like -I /usr/lib/x86_64-linux-gnu/glib-2.0. This path is not correct, the expected behaviour is to use a path inside the SDK root:

-I $SDK_ROOT/usr/lib/x86_64-linux-gnu/glib-2.0

One possible solution workaround is to manually set PKG_CONFIG_SYSROOT_DIR env variable to point into the SDK_ROOT. When set, this variable should be prepended to all resolved paths from the pc file.

Proper fix: the SDK sys root should be automatically prepended to the pkg-config search path when using a Swift SDK.

Expected behavior

Building Swift packages that rely on system libraries present in a Swift SDK should be possible. SwiftPM should pass flags that reference the files inside the SDK.

Actual behavior

SwiftPM passes flags that reference files in the host filesystem and not inside the SDK and thus the build fails or is incorrect.

Steps to reproduce

  1. Clone the example repo: t089/swift-glib-test
  2. Generate a Swift SDK based on ubuntu-jammy with libglib2.0-dev installed (see this issue on Swift SDK Generator: Can't get a custom Swift SDK working with glib-2.0 swift-sdk-generator#89)
  3. Try to build the example package using the generated SDK

Swift Package Manager version/commit hash

No response

Swift & OS version (output of swift --version ; uname -a)

swift-driver version: 1.90.11.1 Apple Swift version 5.10 (swiftlang-5.10.0.13 clang-1500.3.9.4)
Target: arm64-apple-macosx14.0
Darwin tobias-m1-pro.local 23.3.0 Darwin Kernel Version 23.3.0: Wed Dec 20 21:30:44 PST 2023; root:xnu-10002.81.5~7/RELEASE_ARM64_T6000 arm64

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions