[6.0] pkgconfig: Apply PKG_CONFIG_SYSROOTDIR when generating paths #7472
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Cherry pick of #7461
SwiftPM's pkg-config implementation sets the
pc_sysrootdir
variable, but most.pc
files do not use this variable directly. Instead, they rely onthe pkg-config tool rewriting the generated paths to include the sysroot prefix when necessary. SwiftPM does not do this, so it does not generate the correct compiler flags to use libraries from a sysroot.
This problem was reported in issue
#7409
There are two major pkg-config implementations which handle sysroot differently:
pkg-config
(the original https://pkg-config.freedesktop.org implementation) prepends sysroot after variable expansion, when it creates the compiler flag listspkgconf
(the newer http://pkgconf.org implementation) prepends sysroot to variables when they are defined, so sysroot is included when they are expandedpkg-config
's method skips single character compiler flags, such as-I
and
-L
, and has special cases for longer options. It does not handle spaces between the flags and their values properly, and prepends sysroot multiple times in some cases, such as when the .pc file uses thesysroot_dir
variable directly or has been rewritten to hard-code the sysroot prefix.pkgconf
's method handles spaces correctly, although it also makes extra checks to ensure that sysroot is not applied more than once.In 2024
pkg-config
is the more popular option according to Homebrew installation statistics, but the major Linux distributions have generallyswitched to
pkgconf
.We will use
pkgconf
's method here as it seems more robust thanpkg-config
's, andpkgconf
's greater popularity on Linux means librariesdeveloped there may depend on the specific way it handles
.pc
files.SwiftPM will now apply the sysroot prefix to compiler flags, such as include (
-I
) and library (-L
) search paths.This is a partial fix for
#7409.
The sysroot prefix is only applied when the
PKG_CONFIG_SYSROOT_DIR
environment variable is set. A future commit could apply an appropriate sysroot automatically when the--experimental-swift-sdk
flag is used.Scope: Limited to packages relying on system libraries that utilize pkg-config.
Risk: Low, changes are isolated and the scope is limited to a small fraction of packages.
Testing: Automated with new test cases.
Issues: #7409
Reviewer: @MaxDesiatov.