Skip to content

Commit 7600334

Browse files
committed
[PkgConfig] Add checks for circular dependencies
1 parent 32a081a commit 7600334

File tree

1 file changed

+25
-3
lines changed

1 file changed

+25
-3
lines changed

Sources/TSCUtility/PkgConfig.swift

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,15 @@ public struct PCFileFinder {
104104
}
105105
}
106106

107+
/// Informations to track circular dependencies and other PkgConfig issues
108+
public class LoadingContext {
109+
public init() {
110+
pkgConfigStack = [String]()
111+
}
112+
113+
public var pkgConfigStack: [String]
114+
}
115+
107116
/// Information on an individual `pkg-config` supported package.
108117
public struct PkgConfig {
109118
/// The name of the package.
@@ -138,8 +147,10 @@ public struct PkgConfig {
138147
additionalSearchPaths: [AbsolutePath] = [],
139148
diagnostics: DiagnosticsEngine,
140149
fileSystem: FileSystem = localFileSystem,
141-
brewPrefix: AbsolutePath?
150+
brewPrefix: AbsolutePath?,
151+
loadingContext: LoadingContext = LoadingContext()
142152
) throws {
153+
loadingContext.pkgConfigStack.append(name)
143154

144155
if let path = try? AbsolutePath(validating: name) {
145156
guard fileSystem.isFile(path) else { throw PkgConfigError.couldNotFindConfigFile(name: name) }
@@ -163,13 +174,21 @@ public struct PkgConfig {
163174
var libs = [String]()
164175

165176
for dep in dependencies {
177+
let firstOccurrence = loadingContext.pkgConfigStack.firstIndex(of: dep)
178+
179+
if let index = firstOccurrence {
180+
diagnostics.emit(warning: "circular dependency detected while parsing \(loadingContext.pkgConfigStack[0]): \(loadingContext.pkgConfigStack[index..<loadingContext.pkgConfigStack.count].joined(separator: " -> ")) -> \(dep)")
181+
continue
182+
}
183+
166184
// FIXME: This is wasteful, we should be caching the PkgConfig result.
167185
let pkg = try PkgConfig(
168-
name: dep,
186+
name: dep,
169187
additionalSearchPaths: additionalSearchPaths,
170188
diagnostics: diagnostics,
171189
fileSystem: fileSystem,
172-
brewPrefix: brewPrefix
190+
brewPrefix: brewPrefix,
191+
loadingContext: loadingContext
173192
)
174193

175194
cFlags += pkg.cFlags
@@ -184,6 +203,8 @@ public struct PkgConfig {
184203

185204
self.cFlags = parser.cFlags + dependencyFlags.cFlags + privateDependencyFlags.cFlags
186205
self.libs = parser.libs + dependencyFlags.libs
206+
207+
loadingContext.pkgConfigStack.removeLast();
187208
}
188209

189210
private static var envSearchPaths: [AbsolutePath] {
@@ -408,3 +429,4 @@ public struct PkgConfigParser {
408429
return splits
409430
}
410431
}
432+

0 commit comments

Comments
 (0)