Skip to content

Commit bfb8857

Browse files
authored
Include the output paths for clang files in the sources property of a BuildDescription (#8332)
We need the mapping from source file to output file in SourceKit-LSP, so the old approach of returning all output paths in a separate property does not work. Return them in a single property instead. This will be used in SourceKit-LSP by swiftlang/sourcekit-lsp#2038.
1 parent e837e11 commit bfb8857

File tree

3 files changed

+27
-19
lines changed

3 files changed

+27
-19
lines changed

Sources/SourceKitLSPAPI/BuildDescription.swift

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,19 @@ public enum BuildTargetCompiler {
3232
case clang
3333
}
3434

35+
/// Information about a source file that belongs to a target.
36+
public struct SourceItem {
37+
/// The URL of the source file itself.
38+
public let sourceFile: URL
39+
40+
/// If the file has a unique output path (eg. for clang files), the output paths. `nil` for eg. Swift targets,
41+
/// which don't have unique output paths for each file.
42+
public let outputFile: URL?
43+
}
44+
3545
public protocol BuildTarget {
3646
/// Source files in the target
37-
var sources: [URL] { get }
47+
var sources: [SourceItem] { get }
3848

3949
/// Header files in the target
4050
var headers: [URL] { get }
@@ -61,8 +71,6 @@ public protocol BuildTarget {
6171

6272
var isTestTarget: Bool { get }
6373

64-
var outputPaths: [URL] { get throws }
65-
6674
func compileArguments(for fileURL: URL) throws -> [String]
6775
}
6876

@@ -77,11 +85,13 @@ private struct WrappedClangTargetBuildDescription: BuildTarget {
7785
self.isTestTarget = description.isTestTarget
7886
}
7987

80-
public var sources: [URL] {
88+
public var sources: [SourceItem] {
8189
guard let compilePaths = try? description.compilePaths() else {
8290
return []
8391
}
84-
return compilePaths.map(\.source.asURL)
92+
return compilePaths.map {
93+
SourceItem(sourceFile: $0.source.asURL, outputFile: $0.object.asURL)
94+
}
8595
}
8696

8797
public var headers: [URL] {
@@ -117,12 +127,6 @@ private struct WrappedClangTargetBuildDescription: BuildTarget {
117127
return description.destination == .host ? .host : .target
118128
}
119129

120-
var outputPaths: [URL] {
121-
get throws {
122-
return try description.compilePaths().map(\.object.asURL)
123-
}
124-
}
125-
126130
public func compileArguments(for fileURL: URL) throws -> [String] {
127131
let filePath = try resolveSymlinks(try Basics.AbsolutePath(validating: fileURL.path))
128132
let commandLine = try description.emitCommandLine(for: filePath)
@@ -152,8 +156,10 @@ private struct WrappedSwiftTargetBuildDescription: BuildTarget {
152156
return description.destination == .host ? .host : .target
153157
}
154158

155-
var sources: [URL] {
156-
return description.sources.map(\.asURL)
159+
var sources: [SourceItem] {
160+
return description.sources.map {
161+
return SourceItem(sourceFile: $0.asURL, outputFile: nil)
162+
}
157163
}
158164

159165
var headers: [URL] { [] }

Sources/SourceKitLSPAPI/PluginTargetBuildDescription.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,10 @@ struct PluginTargetBuildDescription: BuildTarget {
3232
self.isPartOfRootPackage = isPartOfRootPackage
3333
}
3434

35-
var sources: [URL] {
36-
return target.sources.paths.map(\.asURL)
35+
var sources: [SourceItem] {
36+
return target.sources.paths.map {
37+
SourceItem(sourceFile: $0.asURL, outputFile: nil)
38+
}
3739
}
3840

3941
var headers: [URL] { [] }
@@ -74,7 +76,7 @@ struct PluginTargetBuildDescription: BuildTarget {
7476
// FIXME: This is very odd and we should clean this up by merging `ManifestLoader` and `DefaultPluginScriptRunner` again.
7577
var args = ManifestLoader.interpreterFlags(for: self.toolsVersion, toolchain: toolchain)
7678
// Note: we ignore the `fileURL` here as the expectation is that we get a commandline for the entire target in case of Swift. Plugins are always assumed to only consist of Swift files.
77-
args += try sources.map { try $0.filePath }
79+
args += try sources.map { try $0.sourceFile.filePath }
7880
return args
7981
}
8082
}

Tests/SourceKitLSPAPITests/SourceKitLSPAPITests.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -364,8 +364,8 @@ final class SourceKitLSPAPITests: XCTestCase {
364364

365365
let target = try XCTUnwrap(description.getBuildTarget(for: XCTUnwrap(graph.module(for: "lib")), destination: .target))
366366
XCTAssertEqual(target.compiler, .clang)
367-
XCTAssertEqual(try target.outputPaths.count, 1)
368-
XCTAssertEqual(try target.outputPaths.last?.lastPathComponent, "lib.cpp.o")
367+
XCTAssertEqual(target.sources.count, 1)
368+
XCTAssertEqual(target.sources.last?.outputFile?.lastPathComponent, "lib.cpp.o")
369369
}
370370
}
371371

@@ -387,7 +387,7 @@ extension SourceKitLSPAPI.BuildDescription {
387387
XCTAssertEqual(buildTarget.ignored, ignoredFiles, "build target \(targetName) contains unexpected ignored files")
388388
XCTAssertEqual(buildTarget.others, otherFiles, "build target \(targetName) contains unexpected other files")
389389

390-
guard let source = buildTarget.sources.first else {
390+
guard let source = buildTarget.sources.first?.sourceFile else {
391391
XCTFail("build target \(targetName) contains no source files")
392392
return false
393393
}

0 commit comments

Comments
 (0)