Skip to content

Support module wrapping for debugging on Linux #199

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
Aug 11, 2020
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
1 change: 1 addition & 0 deletions Sources/SwiftDriver/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ add_library(SwiftDriver
Jobs/Job.swift
Jobs/LinkJob.swift
Jobs/MergeModuleJob.swift
Jobs/ModuleWrapJob.swift
Jobs/Planning.swift
Jobs/PrintTargetInfoJob.swift
Jobs/ReplJob.swift
Expand Down
9 changes: 7 additions & 2 deletions Sources/SwiftDriver/Jobs/Job.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public struct Job: Codable, Equatable, Hashable {
case autolinkExtract = "autolink-extract"
case emitModule = "emit-module"
case generatePCH = "generate-pch"
case moduleWrap = "module-wrap"

/// Generate a compiled Clang module.
case generatePCM = "generate-pcm"
Expand Down Expand Up @@ -145,6 +146,9 @@ extension Job : CustomStringConvertible {
case .generatePCH:
return "Compiling bridging header \(displayInputs.first?.file.basename ?? "")"

case .moduleWrap:
return "Wrapping Swift module \(moduleName)"

case .generatePCM:
return "Compiling Clang module \(displayInputs.first?.file.basename ?? "")"

Expand Down Expand Up @@ -184,7 +188,7 @@ extension Job.Kind {
.versionRequest, .scanDependencies:
return true

case .autolinkExtract, .generateDSYM, .help, .link, .verifyDebugInfo:
case .autolinkExtract, .generateDSYM, .help, .link, .verifyDebugInfo, .moduleWrap:
return false
}
}
Expand All @@ -197,7 +201,8 @@ extension Job.Kind {
case .backend, .mergeModule, .emitModule, .generatePCH,
.generatePCM, .interpret, .repl, .printTargetInfo,
.versionRequest, .autolinkExtract, .generateDSYM,
.help, .link, .verifyDebugInfo, .scanDependencies:
.help, .link, .verifyDebugInfo, .scanDependencies,
.moduleWrap:
return false
}
}
Expand Down
39 changes: 39 additions & 0 deletions Sources/SwiftDriver/Jobs/ModuleWrapJob.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//===--------------- ModuleWrapJob.swift - Swift Module Wrapping ----------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

extension Driver {
mutating func moduleWrapJob(moduleInput: TypedVirtualPath) throws -> Job {
var commandLine: [Job.ArgTemplate] = swiftCompilerPrefixArgs.map { Job.ArgTemplate.flag($0) }

commandLine.appendFlags("-modulewrap")

// Add the input.
commandLine.append(.path(moduleInput.file))
assert(compilerOutputType == .object, "-modulewrap mode only produces object files")

commandLine.appendFlags("-target", targetTriple.triple)

let outputPath = try moduleInput.file.replacingExtension(with: .object)
commandLine.appendFlag("-o")
commandLine.appendPath(outputPath)

return Job(
moduleName: moduleOutputInfo.name,
kind: .moduleWrap,
tool: .absolute(try toolchain.getToolPath(.swiftCompiler)),
commandLine: commandLine,
inputs: [moduleInput],
outputs: [.init(file: outputPath, type: .object)],
supportsResponseFiles: true
)
}
}
18 changes: 17 additions & 1 deletion Sources/SwiftDriver/Jobs/Planning.swift
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,10 @@ extension Driver {
jobs.append(contentsOf: backendJobs)

// Plan the merge-module job, if there are module inputs.
var mergeJob: Job?
if moduleOutputInfo.output != nil && !moduleInputs.isEmpty && compilerMode.usesPrimaryFileInputs {
jobs.append(try mergeModuleJob(inputs: moduleInputs))
mergeJob = try mergeModuleJob(inputs: moduleInputs)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You won’t have to force unwrap if you store this into a separate local variable.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wrote it this way to mirror the usage of var link: Job? below on line 219, it might be worth cleaning both instances up as a follow-up though

jobs.append(mergeJob!)
}

// If we need to autolink-extract, do so.
Expand All @@ -199,6 +201,20 @@ extension Driver {
jobs.append(autolinkExtractJob)
}

if let mergeJob = mergeJob, debugInfo.level == .astTypes {
if targetTriple.objectFormat != .macho {
// Module wrapping is required.
let mergeModuleOutputs = mergeJob.outputs.filter { $0.type == .swiftModule }
assert(mergeModuleOutputs.count == 1,
"Merge module job should only have one swiftmodule output")
let wrapJob = try moduleWrapJob(moduleInput: mergeModuleOutputs[0])
linkerInputs.append(contentsOf: wrapJob.outputs)
jobs.append(wrapJob)
} else {
linkerInputs.append(contentsOf: mergeJob.outputs)
}
}

// If we should link, do so.
var link: Job?
if linkerOutputType != nil && !linkerInputs.isEmpty {
Expand Down
56 changes: 53 additions & 3 deletions Tests/SwiftDriverTests/SwiftDriverTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1309,6 +1309,55 @@ final class SwiftDriverTests: XCTestCase {
}
}

func testModuleWrapJob() throws {
do {
var driver = try Driver(args: ["swiftc", "-target", "x86_64-unknown-linux-gnu", "-g", "foo.swift"])
let plannedJobs = try driver.planBuild()
XCTAssertEqual(plannedJobs.count, 4)
// FIXME: There should also be an autolink-extract job. It looks like our
// triple parsing code is not detecting the object file format correctly.
XCTAssertEqual(plannedJobs.map { $0.kind }, [.compile, .mergeModule, .moduleWrap, .link])
XCTAssertEqual(plannedJobs[2].inputs.count, 1)
XCTAssertEqual(plannedJobs[2].inputs.count, 1)
XCTAssertTrue(plannedJobs[2].commandLine.contains(subsequence: ["-target", "x86_64-unknown-linux-gnu"]))
XCTAssertTrue(plannedJobs[1].outputs.contains(plannedJobs[2].inputs.first!))
XCTAssertTrue(plannedJobs[3].inputs.contains(plannedJobs[2].outputs.first!))
}

// dsymutil won't be found on other platforms
#if os(macOS)
do {
var driver = try Driver(args: ["swiftc", "-target", "x86_64-apple-macosx10.15", "-g", "foo.swift"])
let plannedJobs = try driver.planBuild()
XCTAssertEqual(plannedJobs.count, 4)
// No module wrapping with Mach-O.
// FIXME: There should also be an autolink-extract job. It looks like our
// triple parsing code is not detecting the object file format correctly.
XCTAssertEqual(plannedJobs.map { $0.kind }, [.compile, .mergeModule, .link, .generateDSYM])
}
#endif

do {
var driver = try Driver(args: ["swiftc", "-target", "x86_64-unknown-linux-gnu", "foo.swift"])
let plannedJobs = try driver.planBuild()
XCTAssertEqual(plannedJobs.count, 2)
// No merge module/module wrap jobs.
// FIXME: There should also be an autolink-extract job. It looks like our
// triple parsing code is not detecting the object file format correctly.
XCTAssertEqual(plannedJobs.map { $0.kind }, [.compile, .link])
}

do {
var driver = try Driver(args: ["swiftc", "-target", "x86_64-unknown-linux-gnu", "-gdwarf-types", "foo.swift"])
let plannedJobs = try driver.planBuild()
XCTAssertEqual(plannedJobs.count, 3)
// Merge module, but no module wrapping.
// FIXME: There should also be an autolink-extract job. It looks like our
// triple parsing code is not detecting the object file format correctly.
XCTAssertEqual(plannedJobs.map { $0.kind }, [.compile, .mergeModule, .link])
}
}

func testRepl() throws {

func isLLDBREPLFlag(_ arg: Job.ArgTemplate) -> Bool {
Expand Down Expand Up @@ -1562,7 +1611,7 @@ final class SwiftDriverTests: XCTestCase {
func testDSYMGeneration() throws {
let commonArgs = [
"swiftc", "foo.swift", "bar.swift",
"-emit-executable", "-module-name", "Test"
"-emit-executable", "-module-name", "Test",
]

do {
Expand Down Expand Up @@ -1595,7 +1644,8 @@ final class SwiftDriverTests: XCTestCase {
XCTAssertEqual(plannedJobs.count, 5)
XCTAssertEqual(generateDSYMJob.outputs.last?.file, try VirtualPath(path: "Test.dSYM"))
} else {
XCTAssertEqual(plannedJobs.count, 4)
XCTAssertEqual(plannedJobs.count, 5)
XCTAssertFalse(plannedJobs.map { $0.kind }.contains(.generateDSYM))
}

XCTAssertTrue(cmd.contains(.path(try VirtualPath(path: "Test"))))
Expand Down Expand Up @@ -1641,7 +1691,7 @@ final class SwiftDriverTests: XCTestCase {
XCTAssertTrue(cmd.contains(.flag("--quiet")))
XCTAssertTrue(cmd.contains(.path(try VirtualPath(path: "Test.dSYM"))))
} else {
XCTAssertEqual(plannedJobs.count, 4)
XCTAssertEqual(plannedJobs.count, 5)
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions Tests/SwiftDriverTests/XCTestManifests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ extension ExplicitModuleBuildTests {
("testExplicitModuleBuildJobs", testExplicitModuleBuildJobs),
("testExplicitSwiftModuleMap", testExplicitSwiftModuleMap),
("testModuleDependencyBuildCommandGeneration", testModuleDependencyBuildCommandGeneration),
("testModuleDependencyWithExternalCommandGeneration", testModuleDependencyWithExternalCommandGeneration),
]
}

Expand Down Expand Up @@ -107,6 +108,7 @@ extension SwiftDriverTests {
("testModuleNameFallbacks", testModuleNameFallbacks),
("testModuleNaming", testModuleNaming),
("testModuleSettings", testModuleSettings),
("testModuleWrapJob", testModuleWrapJob),
("testMultiThreadedWholeModuleOptimizationCompiles", testMultiThreadedWholeModuleOptimizationCompiles),
("testMultithreading", testMultithreading),
("testMultithreadingDiagnostics", testMultithreadingDiagnostics),
Expand Down