-
Notifications
You must be signed in to change notification settings - Fork 1.4k
pkgconfig: Apply PKG_CONFIG_SYSROOTDIR when generating paths #7461
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -47,13 +47,15 @@ public struct PkgConfig { | |||||
name: String, | ||||||
additionalSearchPaths: [AbsolutePath]? = .none, | ||||||
brewPrefix: AbsolutePath? = .none, | ||||||
sysrootDir: AbsolutePath? = .none, | ||||||
fileSystem: FileSystem, | ||||||
observabilityScope: ObservabilityScope | ||||||
) throws { | ||||||
try self.init( | ||||||
name: name, | ||||||
additionalSearchPaths: additionalSearchPaths ?? [], | ||||||
brewPrefix: brewPrefix, | ||||||
sysrootDir: sysrootDir, | ||||||
loadingContext: LoadingContext(), | ||||||
fileSystem: fileSystem, | ||||||
observabilityScope: observabilityScope | ||||||
|
@@ -64,6 +66,7 @@ public struct PkgConfig { | |||||
name: String, | ||||||
additionalSearchPaths: [AbsolutePath], | ||||||
brewPrefix: AbsolutePath?, | ||||||
sysrootDir: AbsolutePath?, | ||||||
loadingContext: LoadingContext, | ||||||
fileSystem: FileSystem, | ||||||
observabilityScope: ObservabilityScope | ||||||
|
@@ -85,7 +88,7 @@ public struct PkgConfig { | |||||
) | ||||||
} | ||||||
|
||||||
var parser = try PkgConfigParser(pcFile: pcFile, fileSystem: fileSystem) | ||||||
var parser = try PkgConfigParser(pcFile: pcFile, fileSystem: fileSystem, sysrootDir: ProcessEnv.block["PKG_CONFIG_SYSROOT_DIR"]) | ||||||
try parser.parse() | ||||||
|
||||||
func getFlags(from dependencies: [String]) throws -> (cFlags: [String], libs: [String]) { | ||||||
|
@@ -103,6 +106,7 @@ public struct PkgConfig { | |||||
name: dep, | ||||||
additionalSearchPaths: additionalSearchPaths, | ||||||
brewPrefix: brewPrefix, | ||||||
sysrootDir: sysrootDir, | ||||||
loadingContext: loadingContext, | ||||||
fileSystem: fileSystem, | ||||||
observabilityScope: observabilityScope | ||||||
|
@@ -162,13 +166,93 @@ internal struct PkgConfigParser { | |||||
public private(set) var privateDependencies = [String]() | ||||||
public private(set) var cFlags = [String]() | ||||||
public private(set) var libs = [String]() | ||||||
public private(set) var sysrootDir: String? | ||||||
|
||||||
public init(pcFile: AbsolutePath, fileSystem: FileSystem) throws { | ||||||
public init(pcFile: AbsolutePath, fileSystem: FileSystem, sysrootDir: String?) throws { | ||||||
guard fileSystem.isFile(pcFile) else { | ||||||
throw StringError("invalid pcfile \(pcFile)") | ||||||
} | ||||||
self.pcFile = pcFile | ||||||
self.fileSystem = fileSystem | ||||||
self.sysrootDir = sysrootDir | ||||||
} | ||||||
|
||||||
// Compress repeated path separators to one. | ||||||
private func compressPathSeparators(_ value: String) -> String { | ||||||
let components = value.components(separatedBy: "/").filter { !$0.isEmpty }.joined(separator: "/") | ||||||
if value.hasPrefix("/") { | ||||||
return "/" + components | ||||||
} else { | ||||||
return components | ||||||
} | ||||||
} | ||||||
|
||||||
// Trim duplicate sysroot prefixes, matching the approach of pkgconf | ||||||
private func trimDuplicateSysroot(_ value: String) -> String { | ||||||
// If sysroot has been applied more than once, remove the first instance. | ||||||
// pkgconf makes this check after variable expansion to handle rare .pc | ||||||
// files which expand ${pc_sysrootdir} directly: | ||||||
// https://github.com/pkgconf/pkgconf/issues/123 | ||||||
// | ||||||
// For example: | ||||||
// /sysroot/sysroot/remainder -> /sysroot/remainder | ||||||
// | ||||||
// However, pkgconf's algorithm searches for an additional sysrootdir anywhere in | ||||||
// the string after the initial prefix, rather than looking for two sysrootdir prefixes | ||||||
// directly next to each other: | ||||||
// | ||||||
// /sysroot/filler/sysroot/remainder -> /filler/sysroot/remainder | ||||||
// | ||||||
// It might seem more logical not to strip sysroot in this case, as it is not a double | ||||||
// prefix, but for compatibility trimDuplicateSysroot is faithful to pkgconf's approach | ||||||
// in the functions `pkgconf_tuple_parse` and `should_rewrite_sysroot`. | ||||||
|
||||||
// Only trim if sysroot is defined with a meaningful value | ||||||
guard let sysrootDir, sysrootDir != "/" else { | ||||||
return value | ||||||
} | ||||||
|
||||||
// Only trim absolute paths starting with sysroot | ||||||
guard value.hasPrefix("/"), value.hasPrefix(sysrootDir) else { | ||||||
return value | ||||||
} | ||||||
|
||||||
// If sysroot appears multiple times, trim the prefix | ||||||
// N.B. sysroot can appear anywhere in the remainder | ||||||
// of the value, mirroring pkgconf's logic | ||||||
let pathSuffix = value.dropFirst(sysrootDir.count) | ||||||
if pathSuffix.contains(sysrootDir) { | ||||||
return String(pathSuffix) | ||||||
} else { | ||||||
return value | ||||||
} | ||||||
} | ||||||
|
||||||
// Apply sysroot to generated paths, matching the approach of pkgconf | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. doc comment nit
Suggested change
|
||||||
private func applySysroot(_ value: String) -> String { | ||||||
// The two main pkg-config implementations handle sysroot differently: | ||||||
// | ||||||
// `pkg-config` (freedesktop.org) prepends sysroot after variable expansion, when in creates the compiler flag lists | ||||||
// `pkgconf` prepends sysroot to variables when they are defined, so sysroot is included when they are expanded | ||||||
// | ||||||
// pkg-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 the sysroot_dir variable directly or has been rewritten to hard-code the sysroot prefix. | ||||||
// | ||||||
// pkgconf's method handles spaces correctly, although it also requires 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 generally switched to pkgconf. | ||||||
// | ||||||
// We will use pkgconf's method here as it seems more robust than pkg-config's, and pkgconf's greater popularity on Linux | ||||||
// means libraries developed there may depend on the specific way it handles .pc files. | ||||||
|
||||||
if value.hasPrefix("/"), let sysrootDir, !value.hasPrefix(sysrootDir) { | ||||||
return compressPathSeparators(trimDuplicateSysroot(sysrootDir + value)) | ||||||
} else { | ||||||
return compressPathSeparators(trimDuplicateSysroot(value)) | ||||||
} | ||||||
} | ||||||
|
||||||
public mutating func parse() throws { | ||||||
|
@@ -183,7 +267,9 @@ internal struct PkgConfigParser { | |||||
variables["pcfiledir"] = pcFile.parentDirectory.pathString | ||||||
|
||||||
// Add pc_sysrootdir variable. This is the path of the sysroot directory for pc files. | ||||||
variables["pc_sysrootdir"] = ProcessEnv.block["PKG_CONFIG_SYSROOT_DIR"] ?? AbsolutePath.root.pathString | ||||||
// pkgconf does not define pc_sysrootdir if the path of the .pc file is outside sysrootdir. | ||||||
// SwiftPM does not currently make that check. | ||||||
variables["pc_sysrootdir"] = sysrootDir ?? AbsolutePath.root.pathString | ||||||
|
||||||
let fileContents: String = try fileSystem.readFileContents(pcFile) | ||||||
for line in fileContents.components(separatedBy: "\n") { | ||||||
|
@@ -199,7 +285,7 @@ internal struct PkgConfigParser { | |||||
// Found a variable. | ||||||
let (name, maybeValue) = line.spm_split(around: "=") | ||||||
let value = maybeValue?.spm_chuzzle() ?? "" | ||||||
variables[name.spm_chuzzle() ?? ""] = try resolveVariables(value) | ||||||
variables[name.spm_chuzzle() ?? ""] = try applySysroot(resolveVariables(value)) | ||||||
} else { | ||||||
// Unexpected thing in the pc file, abort. | ||||||
throw PkgConfigError.parsingError("Unexpected line: \(line) in \(pcFile)") | ||||||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
prefix=/usr | ||
datarootdir=${prefix}/share | ||
pkgdatadir=${pc_sysrootdir}/${datarootdir}/pkgdata | ||
|
||
Name: double_sysroot | ||
Description: Demonstrate double-prefixing of pc_sysrootdir (https://github.com/pkgconf/pkgconf/issues/123) | ||
Version: 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
Tests/PackageLoadingTests/pkgconfigInputs/not_double_sysroot.pc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
prefix=/usr | ||
datarootdir=${prefix}/share | ||
pkgdatadir=${pc_sysrootdir}/filler/${datarootdir}/pkgdata | ||
|
||
Name: double_sysroot | ||
Description: Demonstrate pc_sysrootdir appearing elsewhere in a path - this is not a double prefix and should not be removed |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
doc comment nit