Skip to content

Address review comments to #1249 #1310

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Sources/SKCore/BuildSystemManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ extension BuildSystemManager {
}
switch document.fileURL?.pathExtension {
case "c": return .c
case "cpp", "cc", "cxx": return .cpp
case "cpp", "cc", "cxx", "hpp": return .cpp
case "m": return .objective_c
case "mm", "h": return .objective_cpp
case "swift": return .swift
Expand Down
19 changes: 15 additions & 4 deletions Sources/SKSwiftPMWorkspace/SwiftPMBuildSystem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,16 @@ extension SwiftPMBuildSystem: SKCore.BuildSystem {
return nil
}

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

if url.pathExtension == "h", let target = try? target(forHeader: path) {
if let target = try? inferredTarget(for: path) {
return [target]
}

Expand Down Expand Up @@ -614,8 +623,10 @@ extension SwiftPMBuildSystem {
return canonicalPath == path ? nil : impl(canonicalPath)
}

/// This finds the target the header belongs to based on its location in the file system.
private func target(forHeader path: AbsolutePath) throws -> ConfiguredTarget? {
/// This finds the target a file belongs to based on its location in the file system.
///
/// This is primarily intended to find the target a header belongs to.
private func inferredTarget(for path: AbsolutePath) throws -> ConfiguredTarget? {
func impl(_ path: AbsolutePath) throws -> ConfiguredTarget? {
var dir = path.parentDirectory
while !dir.isRoot {
Expand Down