-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Target+PkgConfig.swift
295 lines (260 loc) · 10.3 KB
/
Target+PkgConfig.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/*
This source file is part of the Swift.org open source project
Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
Licensed under Apache License v2.0 with Runtime Library Exception
See http://swift.org/LICENSE.txt for license information
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
*/
import Basics
import PackageModel
import TSCBasic
import enum TSCUtility.Platform
/// Wrapper struct containing result of a pkgConfig query.
public struct PkgConfigResult {
/// The name of the pkgConfig file.
public let pkgConfigName: String
/// The cFlags from pkgConfig.
public let cFlags: [String]
/// The library flags from pkgConfig.
public let libs: [String]
/// Available provider, if any.
public let provider: SystemPackageProviderDescription?
/// Any error encountered during operation.
public let error: Swift.Error?
/// If the pc file was not found.
public var couldNotFindConfigFile: Bool {
switch error {
case PkgConfigError.couldNotFindConfigFile?: return true
default: return false
}
}
/// Create a result.
fileprivate init(
pkgConfigName: String,
cFlags: [String] = [],
libs: [String] = [],
error: Swift.Error? = nil,
provider: SystemPackageProviderDescription? = nil
) {
self.cFlags = cFlags
self.libs = libs
self.error = error
self.provider = provider
self.pkgConfigName = pkgConfigName
}
}
/// Get pkgConfig result for a system library target.
public func pkgConfigArgs(for target: SystemLibraryTarget, brewPrefix: AbsolutePath? = .none, fileSystem: FileSystem, observabilityScope: ObservabilityScope) -> [PkgConfigResult] {
// If there is no pkg config name defined, we're done.
guard let pkgConfigNames = target.pkgConfig else { return [] }
// Compute additional search paths for the provider, if any.
let provider = target.providers?.first { $0.isAvailable }
let additionalSearchPaths = provider?.pkgConfigSearchPath(brewPrefixOverride: brewPrefix) ?? []
var ret: [PkgConfigResult] = []
// Get the pkg config flags.
for pkgConfigName in pkgConfigNames.components(separatedBy: " ") {
let result: PkgConfigResult
do {
let pkgConfig = try PkgConfig(
name: pkgConfigName,
additionalSearchPaths: additionalSearchPaths,
brewPrefix: brewPrefix,
fileSystem: fileSystem,
observabilityScope: observabilityScope
)
// Run the allow list checker.
let filtered = try allowlist(pcFile: pkgConfigName, flags: (pkgConfig.cFlags, pkgConfig.libs))
// Remove any default flags which compiler adds automatically.
let (cFlags, libs) = try removeDefaultFlags(cFlags: filtered.cFlags, libs: filtered.libs)
// Set the error if there are any unallowed flags.
var error: Swift.Error?
if !filtered.unallowed.isEmpty {
error = PkgConfigError.prohibitedFlags(filtered.unallowed.joined(separator: ", "))
}
result = PkgConfigResult(
pkgConfigName: pkgConfigName,
cFlags: cFlags,
libs: libs,
error: error,
provider: provider
)
} catch {
result = PkgConfigResult(pkgConfigName: pkgConfigName, error: error, provider: provider)
}
// If there is no pc file on system and we have an available provider, emit a warning.
if let provider = result.provider, result.couldNotFindConfigFile {
observabilityScope.emit(warning: "you may be able to install \(result.pkgConfigName) using your system-packager:\n\(provider.installText)")
} else if let error = result.error {
observabilityScope.emit(
warning: "\(error)",
metadata: .pkgConfig(pcFile: result.pkgConfigName, targetName: target.name)
)
}
ret.append(result)
}
return ret
}
extension SystemPackageProviderDescription {
public var installText: String {
switch self {
case .brew(let packages):
return " brew install \(packages.joined(separator: " "))\n"
case .apt(let packages):
return " apt-get install \(packages.joined(separator: " "))\n"
case .yum(let packages):
return " yum install \(packages.joined(separator: " "))\n"
}
}
/// Check if the provider is available for the current platform.
var isAvailable: Bool {
guard let platform = Platform.currentPlatform else { return false }
switch self {
case .brew:
if case .darwin = platform {
return true
}
case .apt:
if case .linux(.debian) = platform {
return true
}
if case .android = platform {
return true
}
case .yum:
if case .linux(.fedora) = platform {
return true
}
}
return false
}
func pkgConfigSearchPath(brewPrefixOverride: AbsolutePath?) -> [AbsolutePath] {
switch self {
case .brew(let packages):
let brewPrefix: String
if let brewPrefixOverride = brewPrefixOverride {
brewPrefix = brewPrefixOverride.pathString
} else {
// Homebrew can have multiple versions of the same package. The
// user can choose another version than the latest by running
// ``brew switch NAME VERSION``, so we shouldn't assume to link
// to the latest version. Instead use the version as symlinked
// in /usr/local/opt/(NAME)/lib/pkgconfig.
struct Static {
static let value = { try? Process.checkNonZeroExit(args: "brew", "--prefix").spm_chomp() }()
}
if let value = Static.value {
brewPrefix = value
} else {
return []
}
}
return packages.map({ AbsolutePath(brewPrefix).appending(components: "opt", $0, "lib", "pkgconfig") })
case .apt:
return []
case .yum:
return []
}
}
// FIXME: Get rid of this method once we move on to new Build code.
static func providerForCurrentPlatform(providers: [SystemPackageProviderDescription]) -> SystemPackageProviderDescription? {
return providers.first(where: { $0.isAvailable })
}
}
/// Filters the flags with allowed arguments so unexpected arguments are not passed to
/// compiler/linker. List of allowed flags:
/// cFlags: -I, -F
/// libs: -L, -l, -F, -framework, -w
public func allowlist(
pcFile: String,
flags: (cFlags: [String], libs: [String])
) throws -> (cFlags: [String], libs: [String], unallowed: [String]) {
// Returns a tuple with the array of allowed flag and the array of unallowed flags.
func filter(flags: [String], filters: [String]) throws -> (allowed: [String], unallowed: [String]) {
var allowed = [String]()
var unallowed = [String]()
var it = flags.makeIterator()
while let flag = it.next() {
guard let filter = filters.filter({ flag.hasPrefix($0) }).first else {
unallowed += [flag]
continue
}
// Warning suppression flag has no arguments and is not suffixed.
guard !flag.hasPrefix("-w") || flag == "-w" else {
unallowed += [flag]
continue
}
// If the flag and its value are separated, skip next flag.
if flag == filter && flag != "-w" {
guard let associated = it.next() else {
throw InternalError("Expected associated value")
}
if flag == "-framework" {
allowed += [flag, associated]
continue
}
}
allowed += [flag]
}
return (allowed, unallowed)
}
let filteredCFlags = try filter(flags: flags.cFlags, filters: ["-I", "-F"])
let filteredLibs = try filter(flags: flags.libs, filters: ["-L", "-l", "-F", "-framework", "-w"])
return (filteredCFlags.allowed, filteredLibs.allowed, filteredCFlags.unallowed + filteredLibs.unallowed)
}
/// Remove the default flags which are already added by the compiler.
///
/// This behavior is similar to pkg-config cli tool and helps avoid conflicts between
/// sdk and default search paths in macOS.
public func removeDefaultFlags(cFlags: [String], libs: [String]) throws -> ([String], [String]) {
/// removes a flag from given array of flags.
func remove(flag: (String, String), from flags: [String]) throws -> [String] {
var result = [String]()
var it = flags.makeIterator()
while let curr = it.next() {
switch curr {
case flag.0:
// Check for <flag><space><value> style.
guard let val = it.next() else {
throw InternalError("Expected associated value")
}
// If we found a match, don't add these flags and just skip.
if val == flag.1 { continue }
// Otherwise add both the flags.
result.append(curr)
result.append(val)
case flag.0 + flag.1:
// Check for <flag><value> style.
continue
default:
// Otherwise just append this flag.
result.append(curr)
}
}
return result
}
return (
try remove(flag: ("-I", "/usr/include"), from: cFlags),
try remove(flag: ("-L", "/usr/lib"), from: libs)
)
}
extension ObservabilityMetadata {
public static func pkgConfig(pcFile: String, targetName: String) -> Self {
var metadata = ObservabilityMetadata()
metadata.pcFile = "\(pcFile).pc"
metadata.targetName = targetName
return metadata
}
}
extension ObservabilityMetadata {
public var pcFile: String? {
get {
self[pcFileKey.self]
}
set {
self[pcFileKey.self] = newValue
}
}
enum pcFileKey: Key {
typealias Value = String
}
}