Skip to content

Commit f3ba910

Browse files
PopFlamingoaciidgh
authored andcommitted
Fix issue where pkgconfig flags were all deleted if any was not whitelisted
Remove unnecessary commented code Update comments to match changes to the filter function
1 parent e766d36 commit f3ba910

File tree

3 files changed

+52
-43
lines changed

3 files changed

+52
-43
lines changed

Sources/PackageLoading/Target+PkgConfig.swift

Lines changed: 45 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,16 @@ public struct PkgConfigResult {
3838
}
3939
}
4040

41-
/// Create a successful result with given cflags and libs.
42-
fileprivate init(pkgConfigName: String, cFlags: [String], libs: [String]) {
43-
self.pkgConfigName = pkgConfigName
41+
/// Create a result.
42+
fileprivate init(
43+
pkgConfigName: String,
44+
cFlags: [String] = [],
45+
libs: [String] = [],
46+
error: Swift.Error? = nil,
47+
provider: SystemPackageProviderDescription? = nil
48+
) {
4449
self.cFlags = cFlags
4550
self.libs = libs
46-
self.error = nil
47-
self.provider = nil
48-
}
49-
50-
/// Create an error result.
51-
fileprivate init(pkgConfigName: String, error: Swift.Error, provider: SystemPackageProviderDescription?) {
52-
self.cFlags = []
53-
self.libs = []
5451
self.error = error
5552
self.provider = provider
5653
self.pkgConfigName = pkgConfigName
@@ -61,21 +58,38 @@ public struct PkgConfigResult {
6158
public func pkgConfigArgs(for target: SystemLibraryTarget, diagnostics: DiagnosticsEngine, fileSystem: FileSystem = localFileSystem) -> PkgConfigResult? {
6259
// If there is no pkg config name defined, we're done.
6360
guard let pkgConfigName = target.pkgConfig else { return nil }
61+
6462
// Compute additional search paths for the provider, if any.
6563
let provider = target.providers?.first { $0.isAvailable }
6664
let additionalSearchPaths = provider?.pkgConfigSearchPath() ?? []
65+
6766
// Get the pkg config flags.
6867
do {
6968
let pkgConfig = try PkgConfig(
7069
name: pkgConfigName,
7170
additionalSearchPaths: additionalSearchPaths,
7271
diagnostics: diagnostics,
7372
fileSystem: fileSystem)
73+
7474
// Run the whitelist checker.
75-
try whitelist(pcFile: pkgConfigName, flags: (pkgConfig.cFlags, pkgConfig.libs))
75+
let filtered = whitelist(pcFile: pkgConfigName, flags: (pkgConfig.cFlags, pkgConfig.libs))
76+
7677
// Remove any default flags which compiler adds automatically.
77-
let (cFlags, libs) = removeDefaultFlags(cFlags: pkgConfig.cFlags, libs: pkgConfig.libs)
78-
return PkgConfigResult(pkgConfigName: pkgConfigName, cFlags: cFlags, libs: libs)
78+
let (cFlags, libs) = removeDefaultFlags(cFlags: filtered.cFlags, libs: filtered.libs)
79+
80+
// Set the error if there are any unallowed flags.
81+
var error: Swift.Error?
82+
if !filtered.unallowed.isEmpty {
83+
error = PkgConfigError.nonWhitelistedFlags(filtered.unallowed.joined(separator: ", "))
84+
}
85+
86+
return PkgConfigResult(
87+
pkgConfigName: pkgConfigName,
88+
cFlags: cFlags,
89+
libs: libs,
90+
error: error,
91+
provider: provider
92+
)
7993
} catch {
8094
return PkgConfigResult(pkgConfigName: pkgConfigName, error: error, provider: provider)
8195
}
@@ -135,20 +149,24 @@ extension SystemPackageProviderDescription {
135149
/// compiler/linker. List of allowed flags:
136150
/// cFlags: -I, -F
137151
/// libs: -L, -l, -F, -framework, -w
138-
func whitelist(pcFile: String, flags: (cFlags: [String], libs: [String])) throws {
139-
// Returns an array of flags which doesn't match any filter.
140-
func filter(flags: [String], filters: [String]) -> [String] {
141-
var filtered = [String]()
152+
func whitelist(
153+
pcFile: String,
154+
flags: (cFlags: [String], libs: [String])
155+
) -> (cFlags: [String], libs: [String], unallowed: [String]) {
156+
// Returns a tuple with the array of allowed flag and the array of unallowed flags.
157+
func filter(flags: [String], filters: [String]) -> (allowed: [String], unallowed: [String]) {
158+
var allowed = [String]()
159+
var unallowed = [String]()
142160
var it = flags.makeIterator()
143161
while let flag = it.next() {
144162
guard let filter = filters.filter({ flag.hasPrefix($0) }).first else {
145-
filtered += [flag]
163+
unallowed += [flag]
146164
continue
147165
}
148166

149167
// Warning suppression flag has no arguments and is not suffixed.
150168
guard !flag.hasPrefix("-w") || flag == "-w" else {
151-
filtered += [flag]
169+
unallowed += [flag]
152170
continue
153171
}
154172

@@ -158,14 +176,15 @@ func whitelist(pcFile: String, flags: (cFlags: [String], libs: [String])) throws
158176
fatalError("Expected associated value")
159177
}
160178
}
179+
allowed += [flag]
161180
}
162-
return filtered
163-
}
164-
let filtered = filter(flags: flags.cFlags, filters: ["-I", "-F"]) +
165-
filter(flags: flags.libs, filters: ["-L", "-l", "-F", "-framework", "-w"])
166-
guard filtered.isEmpty else {
167-
throw PkgConfigError.nonWhitelistedFlags(filtered.joined(separator: ", "))
181+
return (allowed, unallowed)
168182
}
183+
184+
let filteredCFlags = filter(flags: flags.cFlags, filters: ["-I", "-F"])
185+
let filteredLibs = filter(flags: flags.libs, filters: ["-L", "-l", "-F", "-framework", "-w"])
186+
187+
return (filteredCFlags.allowed, filteredLibs.allowed, filteredCFlags.unallowed + filteredLibs.unallowed)
169188
}
170189

171190
/// Remove the default flags which are already added by the compiler.

Tests/PackageLoadingTests/PkgConfigTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ class PkgConfigTests: XCTestCase {
8282
try withCustomEnv(["PKG_CONFIG_PATH": inputsDir.asString]) {
8383
let result = pkgConfigArgs(for: SystemLibraryTarget(pkgConfig: "Bar"), diagnostics: diagnostics)!
8484
XCTAssertEqual(result.pkgConfigName, "Bar")
85-
XCTAssertEqual(result.cFlags, [])
86-
XCTAssertEqual(result.libs, [])
85+
XCTAssertEqual(result.cFlags, ["-I/path/to/inc"])
86+
XCTAssertEqual(result.libs, ["-L/usr/da/lib", "-lSystemModule", "-lok"])
8787
XCTAssertNil(result.provider)
8888
XCTAssertFalse(result.couldNotFindConfigFile)
8989
switch result.error {

Tests/PackageLoadingTests/WhitelistTests.swift

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,31 +15,21 @@ final class PkgConfigWhitelistTests: XCTestCase {
1515
func testSimpleFlags() {
1616
let cFlags = ["-I/usr/local/Cellar/gtk+3/3.18.9/include/gtk-3.0"]
1717
let libs = ["-L/usr/local/Cellar/gtk+3/3.18.9/lib", "-lgtk-3", "-w"]
18-
do {
19-
try whitelist(pcFile: "dummy", flags: (cFlags, libs))
20-
} catch {
21-
XCTFail("Unexpected failure \(error)")
22-
}
18+
XCTAssertTrue(whitelist(pcFile: "dummy", flags: (cFlags, libs)).unallowed.isEmpty)
2319
}
2420

2521
func testFlagsWithInvalidFlags() {
2622
let cFlags = ["-I/usr/local/Cellar/gtk+3/3.18.9/include/gtk-3.0", "-L/hello"]
2723
let libs = ["-L/usr/local/Cellar/gtk+3/3.18.9/lib", "-lgtk-3", "-module-name", "name", "-werror"]
28-
do {
29-
try whitelist(pcFile: "dummy", flags: (cFlags, libs))
30-
} catch {
31-
XCTAssertEqual("\(error)", "non whitelisted flag(s): -L/hello, -module-name, name, -werror")
32-
}
24+
let unallowed = whitelist(pcFile: "dummy", flags: (cFlags, libs)).unallowed
25+
XCTAssertEqual(unallowed, ["-L/hello", "-module-name", "name", "-werror"])
3326
}
3427

3528
func testFlagsWithValueInNextFlag() {
3629
let cFlags = ["-I/usr/local", "-I", "/usr/local/Cellar/gtk+3/3.18.9/include/gtk-3.0", "-L/hello"]
3730
let libs = ["-L", "/usr/lib", "-L/usr/local/Cellar/gtk+3/3.18.9/lib", "-lgtk-3", "-module-name", "-lcool", "ok", "name"]
38-
do {
39-
try whitelist(pcFile: "dummy", flags: (cFlags, libs))
40-
} catch {
41-
XCTAssertEqual("\(error)", "non whitelisted flag(s): -L/hello, -module-name, ok, name")
42-
}
31+
let unallowed = whitelist(pcFile: "dummy", flags: (cFlags, libs)).unallowed
32+
XCTAssertEqual(unallowed, ["-L/hello", "-module-name", "ok", "name"])
4333
}
4434

4535
func testRemoveDefaultFlags() {

0 commit comments

Comments
 (0)