@@ -22,6 +22,7 @@ import PackageModel
22
22
import SKCore
23
23
import SKSupport
24
24
import SourceControl
25
+ import SwiftPM
25
26
import Workspace
26
27
27
28
import struct Basics. AbsolutePath
@@ -43,6 +44,20 @@ public enum ReloadPackageStatus {
43
44
case end
44
45
}
45
46
47
+ /// A build target in SwiftPM
48
+ public typealias SwiftBuildTarget = SwiftPM . BuildTarget
49
+
50
+ /// A build target in `BuildServerProtocol`
51
+ public typealias BuildServerTarget = BuildServerProtocol . BuildTarget
52
+
53
+ extension SwiftBuildTarget {
54
+ func fixedCompileArguments( ) throws -> [ String ] {
55
+ var args = try self . compileArguments ( )
56
+ args += self . sources. map { $0. path }
57
+ return args
58
+ }
59
+ }
60
+
46
61
/// Swift Package Manager build system and workspace support.
47
62
///
48
63
/// This class implements the `BuildSystem` interface to provide the build settings for a Swift
@@ -75,8 +90,8 @@ public actor SwiftPMWorkspace {
75
90
public let buildParameters : BuildParameters
76
91
let fileSystem : FileSystem
77
92
78
- var fileToTarget : [ AbsolutePath : TargetBuildDescription ] = [ : ]
79
- var sourceDirToTarget : [ AbsolutePath : TargetBuildDescription ] = [ : ]
93
+ var fileToTarget : [ AbsolutePath : SwiftBuildTarget ] = [ : ]
94
+ var sourceDirToTarget : [ AbsolutePath : SwiftBuildTarget ] = [ : ]
80
95
81
96
/// The URIs for which the delegate has registered for change notifications,
82
97
/// mapped to the language the delegate specified when registering for change notifications.
@@ -213,13 +228,13 @@ extension SwiftPMWorkspace {
213
228
/// with only some properties modified.
214
229
self . packageGraph = packageGraph
215
230
216
- self . fileToTarget = [ AbsolutePath: TargetBuildDescription ] (
231
+ self . fileToTarget = [ AbsolutePath: SwiftBuildTarget ] (
217
232
packageGraph. allTargets. flatMap { target in
218
233
return target. sources. paths. compactMap {
219
- guard let td = plan. targetMap [ target] else {
234
+ guard let buildTarget = BuildDescription ( buildPlan : plan) . getBuildTarget ( for : target) else {
220
235
return nil
221
236
}
222
- return ( key: $0, value: td )
237
+ return ( key: $0, value: buildTarget )
223
238
}
224
239
} ,
225
240
uniquingKeysWith: { td, _ in
@@ -228,12 +243,12 @@ extension SwiftPMWorkspace {
228
243
}
229
244
)
230
245
231
- self . sourceDirToTarget = [ AbsolutePath: TargetBuildDescription ] (
232
- packageGraph. allTargets. compactMap { target in
233
- guard let td = plan. targetMap [ target] else {
246
+ self . sourceDirToTarget = [ AbsolutePath: SwiftBuildTarget ] (
247
+ packageGraph. allTargets. compactMap { ( target) -> ( AbsolutePath , SwiftBuildTarget ) ? in
248
+ guard let buildTarget = BuildDescription ( buildPlan : plan) . getBuildTarget ( for : target) else {
234
249
return nil
235
250
}
236
- return ( key: target. sources. root, value: td )
251
+ return ( key: target. sources. root, value: buildTarget )
237
252
} ,
238
253
uniquingKeysWith: { td, _ in
239
254
// FIXME: is there a preferred target?
@@ -284,8 +299,11 @@ extension SwiftPMWorkspace: SKCore.BuildSystem {
284
299
return nil
285
300
}
286
301
287
- if let td = try targetDescription ( for: path) {
288
- return try settings ( for: path, language, td)
302
+ if let buildTarget = try buildTarget ( for: path) {
303
+ return FileBuildSettings (
304
+ compilerArguments: try buildTarget. fixedCompileArguments ( ) ,
305
+ workingDirectory: workspacePath. pathString
306
+ )
289
307
}
290
308
291
309
if path. basename == " Package.swift " {
@@ -310,7 +328,7 @@ extension SwiftPMWorkspace: SKCore.BuildSystem {
310
328
}
311
329
312
330
/// Returns the resolved target description for the given file, if one is known.
313
- private func targetDescription ( for file: AbsolutePath ) throws -> TargetBuildDescription ? {
331
+ private func buildTarget ( for file: AbsolutePath ) throws -> SwiftBuildTarget ? {
314
332
if let td = fileToTarget [ file] {
315
333
return td
316
334
}
@@ -359,7 +377,7 @@ extension SwiftPMWorkspace: SKCore.BuildSystem {
359
377
guard let fileUrl = uri. fileURL else {
360
378
return . unhandled
361
379
}
362
- if ( try ? targetDescription ( for: AbsolutePath ( validating: fileUrl. path) ) ) != nil {
380
+ if ( try ? buildTarget ( for: AbsolutePath ( validating: fileUrl. path) ) ) != nil {
363
381
return . handled
364
382
} else {
365
383
return . unhandled
@@ -371,24 +389,6 @@ extension SwiftPMWorkspace {
371
389
372
390
// MARK: Implementation details
373
391
374
- /// Retrieve settings for the given file, which is part of a known target build description.
375
- public func settings(
376
- for path: AbsolutePath ,
377
- _ language: Language ,
378
- _ td: TargetBuildDescription
379
- ) throws -> FileBuildSettings ? {
380
- switch ( td, language) {
381
- case ( . swift( let td) , . swift) :
382
- return try settings ( forSwiftFile: path, td)
383
- case ( . clang, . swift) :
384
- return nil
385
- case ( . clang( let td) , _) :
386
- return try settings ( forClangFile: path, language, td)
387
- default :
388
- return nil
389
- }
390
- }
391
-
392
392
/// Retrieve settings for a package manifest (Package.swift).
393
393
private func settings( forPackageManifest path: AbsolutePath ) throws -> FileBuildSettings ? {
394
394
func impl( _ path: AbsolutePath ) -> FileBuildSettings ? {
@@ -412,8 +412,11 @@ extension SwiftPMWorkspace {
412
412
func impl( _ path: AbsolutePath ) throws -> FileBuildSettings ? {
413
413
var dir = path. parentDirectory
414
414
while !dir. isRoot {
415
- if let td = sourceDirToTarget [ dir] {
416
- return try settings ( for: path, language, td)
415
+ if let buildTarget = sourceDirToTarget [ dir] {
416
+ return FileBuildSettings (
417
+ compilerArguments: try buildTarget. fixedCompileArguments ( ) ,
418
+ workingDirectory: workspacePath. pathString
419
+ )
417
420
}
418
421
dir = dir. parentDirectory
419
422
}
@@ -428,36 +431,6 @@ extension SwiftPMWorkspace {
428
431
return try canonicalPath == path ? nil : impl ( canonicalPath)
429
432
}
430
433
431
- /// Retrieve settings for the given swift file, which is part of a known target build description.
432
- public func settings(
433
- forSwiftFile path: AbsolutePath ,
434
- _ td: SwiftTargetBuildDescription
435
- ) throws -> FileBuildSettings {
436
- // FIXME: this is re-implementing llbuild's constructCommandLineArgs.
437
- var args : [ String ] = [
438
- " -module-name " ,
439
- td. target. c99name,
440
- " -incremental " ,
441
- " -emit-dependencies " ,
442
- " -emit-module " ,
443
- " -emit-module-path " ,
444
- buildPath. appending ( component: " \( td. target. c99name) .swiftmodule " ) . pathString,
445
- // -output-file-map <path>
446
- ]
447
- if td. target. type == . library || td. target. type == . test {
448
- args += [ " -parse-as-library " ]
449
- }
450
- args += [ " -c " ]
451
- args += td. sources. map { $0. pathString }
452
- args += [ " -I " , buildPath. pathString]
453
- args += try td. compileArguments ( )
454
-
455
- return FileBuildSettings (
456
- compilerArguments: args,
457
- workingDirectory: workspacePath. pathString
458
- )
459
- }
460
-
461
434
/// Retrieve settings for the given C-family language file, which is part of a known target build
462
435
/// description.
463
436
///
0 commit comments