Skip to content

Commit 4986256

Browse files
committed
Implement -debug-module-path for implicit modules in swift-driver
There is no fundamental technical reason to restrict this feature to explict modules only, so this patch adds support for passing the correct -debug-module-path also to implicit builds. rdar://168256846
1 parent 8e61f52 commit 4986256

2 files changed

Lines changed: 39 additions & 6 deletions

File tree

Sources/SwiftDriver/Jobs/FrontendJobHelpers.swift

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,12 +109,25 @@ extension Driver {
109109
jobNeedPathRemap = false
110110
}
111111

112-
if isPlanJobForExplicitModule && forObject && isFrontendArgSupported(.debugModulePath),
113-
let explicitModulePlanner {
114-
let mainModule = explicitModulePlanner.dependencyGraph.mainModule
115-
let pathHandle = moduleOutputInfo.output?.outputPath ?? mainModule.modulePath.path
116-
let path = VirtualPath.lookup(pathHandle)
117-
try addPathOption(option: .debugModulePath, path: path, to: &commandLine, remap: jobNeedPathRemap)
112+
// Add the -debug-module-path option to the compile job.
113+
if forObject && isFrontendArgSupported(.debugModulePath) {
114+
let modulePathHandle : VirtualPath.Handle
115+
if isPlanJobForExplicitModule,
116+
let explicitModulePlanner {
117+
let mainModule = explicitModulePlanner.dependencyGraph.mainModule
118+
modulePathHandle = moduleOutputInfo.output?.outputPath ?? mainModule.modulePath.path
119+
} else {
120+
// Recompute the module path based on the module name, because this is effectively passing the output
121+
// of the module merge action which depends on the compile action.
122+
let moduleBase : VirtualPath
123+
if let pathHandle = moduleOutputInfo.output?.outputPath {
124+
moduleBase = VirtualPath.lookup(pathHandle).parentDirectory.appending(component: moduleOutputInfo.name)
125+
} else {
126+
moduleBase = .relative(try RelativePath(validating: moduleOutputInfo.name))
127+
}
128+
modulePathHandle = try moduleBase.replacingExtension(with: .swiftModule).intern()
129+
}
130+
try addPathOption(option: .debugModulePath, path: VirtualPath.lookup(modulePathHandle), to: &commandLine, remap: jobNeedPathRemap)
118131
}
119132

120133
// Check if dependency scanner has put the job into direct clang cc1 mode.

Tests/SwiftDriverTests/SwiftDriverTests.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4118,6 +4118,26 @@ final class SwiftDriverTests: XCTestCase {
41184118
}
41194119
}
41204120

4121+
4122+
func testDebugModulePath() throws {
4123+
do {
4124+
var driver = try Driver(args: ["swiftc", "-target", "arm64-unknown-macos26", "-g", "foo.swift"])
4125+
let plannedJobs = try driver.planBuild()
4126+
let compileJob = try plannedJobs.findJob(.compile)
4127+
// This needs to be "foo.swiftmodule", not the unmerged "foo-1.swiftmodule".
4128+
XCTAssertJobInvocationMatches(compileJob, .flag("-debug-module-path"), .path(try .temporary(RelativePath(validating: "foo")).replacingExtension(with: .swiftModule)))
4129+
}
4130+
// Unfortunately this depends on dsymutil.
4131+
#if os(macOS)
4132+
do {
4133+
var driver = try Driver(args: ["swiftc", "-target", "arm64-unknown-macos26", "-g", "foo.swift"])
4134+
let plannedJobs = try driver.planBuild()
4135+
let compileJob = try plannedJobs.findJob(.compile)
4136+
XCTAssertJobInvocationMatches(compileJob, .flag("-debug-module-path"), .path(try .temporary(RelativePath(validating: "foo")).replacingExtension(with: .swiftModule)))
4137+
}
4138+
#endif
4139+
}
4140+
41214141
func testModuleWrapJob() throws {
41224142
// FIXME: These tests will fail when run on macOS, because
41234143
// swift-autolink-extract is not present

0 commit comments

Comments
 (0)