Skip to content

Commit

Permalink
Include the output paths for clang files in the sources property of…
Browse files Browse the repository at this point in the history
… a `BuildDescription`

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.
  • Loading branch information
ahoppen committed Mar 6, 2025
1 parent d82a649 commit f07b88b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 16 deletions.
32 changes: 19 additions & 13 deletions Sources/SourceKitLSPAPI/BuildDescription.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,19 @@ public enum BuildTargetCompiler {
case clang
}

/// Information about a source file that belongs to a target.
public struct SourceItem {
/// The URL of the source file itself.
public let sourceFile: URL

/// If the file has a unique output path (eg. for clang files), the output paths. `nil` for eg. Swift targets,
/// which don't have unique output paths for each file.
public let outputFile: URL?
}

public protocol BuildTarget {
/// Source files in the target
var sources: [URL] { get }
var sources: [SourceItem] { get }

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

var isTestTarget: Bool { get }

var outputPaths: [URL] { get throws }

func compileArguments(for fileURL: URL) throws -> [String]
}

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

public var sources: [URL] {
public var sources: [SourceItem] {
guard let compilePaths = try? description.compilePaths() else {
return []
}
return compilePaths.map(\.source.asURL)
return compilePaths.map {
SourceItem(sourceFile: $0.source.asURL, outputFile: $0.object.asURL)
}
}

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

var outputPaths: [URL] {
get throws {
return try description.compilePaths().map(\.object.asURL)
}
}

public func compileArguments(for fileURL: URL) throws -> [String] {
let filePath = try resolveSymlinks(try Basics.AbsolutePath(validating: fileURL.path))
let commandLine = try description.emitCommandLine(for: filePath)
Expand Down Expand Up @@ -152,8 +156,10 @@ private struct WrappedSwiftTargetBuildDescription: BuildTarget {
return description.destination == .host ? .host : .target
}

var sources: [URL] {
return description.sources.map(\.asURL)
var sources: [SourceItem] {
return description.sources.map {
return SourceItem(sourceFile: $0.asURL, outputFile: nil)
}
}

var headers: [URL] { [] }
Expand Down
8 changes: 5 additions & 3 deletions Sources/SourceKitLSPAPI/PluginTargetBuildDescription.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@ struct PluginTargetBuildDescription: BuildTarget {
self.isPartOfRootPackage = isPartOfRootPackage
}

var sources: [URL] {
return target.sources.paths.map(\.asURL)
var sources: [SourceItem] {
return target.sources.paths.map {
SourceItem(sourceFile: $0.asURL, outputFile: nil)
}
}

var headers: [URL] { [] }
Expand Down Expand Up @@ -74,7 +76,7 @@ struct PluginTargetBuildDescription: BuildTarget {
// FIXME: This is very odd and we should clean this up by merging `ManifestLoader` and `DefaultPluginScriptRunner` again.
var args = ManifestLoader.interpreterFlags(for: self.toolsVersion, toolchain: toolchain)
// 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.
args += try sources.map { try $0.filePath }
args += try sources.map { try $0.sourceFile.filePath }
return args
}
}
Expand Down

0 comments on commit f07b88b

Please sign in to comment.