Skip to content

Commit 5a7ab33

Browse files
authored
Merge pull request #1310 from ahoppen/review-comments-1249
Address review comments to #1249
2 parents bcea952 + 372673f commit 5a7ab33

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

Sources/SKCore/BuildSystemManager.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ extension BuildSystemManager {
118118
}
119119
switch document.fileURL?.pathExtension {
120120
case "c": return .c
121-
case "cpp", "cc", "cxx": return .cpp
121+
case "cpp", "cc", "cxx", "hpp": return .cpp
122122
case "m": return .objective_c
123123
case "mm", "h": return .objective_cpp
124124
case "swift": return .swift

Sources/SKSwiftPMWorkspace/SwiftPMBuildSystem.swift

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,16 @@ extension SwiftPMBuildSystem: SKCore.BuildSystem {
371371
return nil
372372
}
373373

374-
if url.pathExtension == "h", let substituteFile = buildTarget.sources.first {
374+
if !buildTarget.sources.contains(url),
375+
let substituteFile = buildTarget.sources.sorted(by: { $0.path < $1.path }).first
376+
{
377+
// If `url` is not part of the target's source, it's most likely a header file. Fake compiler arguments for it
378+
// from a substitute file within the target.
379+
// Even if the file is not a header, this should give reasonable results: Say, there was a new `.cpp` file in a
380+
// target and for some reason the `SwiftPMBuildSystem` doesn’t know about it. Then we would infer the target based
381+
// on the file's location on disk and generate compiler arguments for it by picking a source file in that target,
382+
// getting its compiler arguments and then patching up the compiler arguments by replacing the substitute file
383+
// with the `.cpp` file.
375384
return FileBuildSettings(
376385
compilerArguments: try buildTarget.compileArguments(for: substituteFile),
377386
workingDirectory: workspacePath.pathString
@@ -406,7 +415,7 @@ extension SwiftPMBuildSystem: SKCore.BuildSystem {
406415
return [ConfiguredTarget(targetID: "", runDestinationID: "dummy")]
407416
}
408417

409-
if url.pathExtension == "h", let target = try? target(forHeader: path) {
418+
if let target = try? inferredTarget(for: path) {
410419
return [target]
411420
}
412421

@@ -614,8 +623,10 @@ extension SwiftPMBuildSystem {
614623
return canonicalPath == path ? nil : impl(canonicalPath)
615624
}
616625

617-
/// This finds the target the header belongs to based on its location in the file system.
618-
private func target(forHeader path: AbsolutePath) throws -> ConfiguredTarget? {
626+
/// This finds the target a file belongs to based on its location in the file system.
627+
///
628+
/// This is primarily intended to find the target a header belongs to.
629+
private func inferredTarget(for path: AbsolutePath) throws -> ConfiguredTarget? {
619630
func impl(_ path: AbsolutePath) throws -> ConfiguredTarget? {
620631
var dir = path.parentDirectory
621632
while !dir.isRoot {

0 commit comments

Comments
 (0)