Skip to content

[Job] Add some convenient APIs for SwiftPM #110

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 3 commits into from
May 23, 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
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ extension Driver {
swiftModuleDetails.commandLine?.forEach { commandLine.appendFlag($0) }

return Job(
moduleName: moduleName,
kind: .emitModule,
tool: .absolute(try toolchain.getToolPath(.swiftCompiler)),
commandLine: commandLine,
Expand Down Expand Up @@ -99,6 +100,7 @@ extension Driver {
clangModuleDetails.commandLine?.forEach { commandLine.appendFlags("-Xcc", $0) }

return Job(
moduleName: moduleName,
kind: .generatePCM,
tool: .absolute(try toolchain.getToolPath(.swiftCompiler)),
commandLine: commandLine,
Expand Down
1 change: 1 addition & 0 deletions Sources/SwiftDriver/Jobs/AutolinkExtractJob.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ extension Driver {
commandLine.appendPath(output)

return Job(
moduleName: moduleName,
kind: .autolinkExtract,
tool: .absolute(try toolchain.getToolPath(.swiftAutolinkExtract)),
commandLine: commandLine,
Expand Down
1 change: 1 addition & 0 deletions Sources/SwiftDriver/Jobs/BackendJob.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ extension Driver {
allOutputs += outputs

return Job(
moduleName: moduleName,
kind: .backend,
tool: .absolute(try toolchain.getToolPath(.swiftCompiler)),
commandLine: commandLine,
Expand Down
1 change: 1 addition & 0 deletions Sources/SwiftDriver/Jobs/CompileJob.swift
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ extension Driver {
}

return Job(
moduleName: moduleName,
kind: .compile,
tool: .absolute(try toolchain.getToolPath(.swiftCompiler)),
commandLine: commandLine,
Expand Down
1 change: 1 addition & 0 deletions Sources/SwiftDriver/Jobs/EmitModuleJob.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ extension Driver {
commandLine.appendPath(moduleOutputPath)

return Job(
moduleName: moduleName,
kind: .emitModule,
tool: .absolute(try toolchain.getToolPath(.swiftCompiler)),
commandLine: commandLine,
Expand Down
1 change: 1 addition & 0 deletions Sources/SwiftDriver/Jobs/GenerateDSYMJob.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ extension Driver {
commandLine.appendPath(outputPath)

return Job(
moduleName: moduleName,
kind: .generateDSYM,
tool: .absolute(try toolchain.getToolPath(.dsymutil)),
commandLine: commandLine,
Expand Down
1 change: 1 addition & 0 deletions Sources/SwiftDriver/Jobs/GeneratePCHJob.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ extension Driver {
outputs.append(output)

return Job(
moduleName: moduleName,
kind: .generatePCH,
tool: .absolute(try toolchain.getToolPath(.swiftCompiler)),
commandLine: commandLine,
Expand Down
1 change: 1 addition & 0 deletions Sources/SwiftDriver/Jobs/GeneratePCMJob.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ extension Driver {
try commandLine.appendLast(.indexStorePath, from: &parsedOptions)

return Job(
moduleName: moduleName,
kind: .generatePCM,
tool: .absolute(try toolchain.getToolPath(.swiftCompiler)),
commandLine: commandLine,
Expand Down
1 change: 1 addition & 0 deletions Sources/SwiftDriver/Jobs/InterpretJob.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ extension Driver {
targetTriple: self.targetTriple)

return Job(
moduleName: moduleName,
kind: .interpret,
tool: .absolute(try toolchain.getToolPath(.swiftCompiler)),
commandLine: commandLine,
Expand Down
72 changes: 72 additions & 0 deletions Sources/SwiftDriver/Jobs/Job.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ public struct Job: Codable, Equatable, Hashable {
case path(VirtualPath)
}

/// The SWift module this job involves.
public var moduleName: String

/// The tool to invoke.
public var tool: VirtualPath

Expand Down Expand Up @@ -70,6 +73,7 @@ public struct Job: Codable, Equatable, Hashable {
public var kind: Kind

public init(
moduleName: String,
kind: Kind,
tool: VirtualPath,
commandLine: [ArgTemplate],
Expand All @@ -80,6 +84,7 @@ public struct Job: Codable, Equatable, Hashable {
requiresInPlaceExecution: Bool = false,
supportsResponseFiles: Bool = false
) {
self.moduleName = moduleName
self.kind = kind
self.tool = tool
self.commandLine = commandLine
Expand Down Expand Up @@ -115,6 +120,73 @@ extension Job {
}
}

extension Job : CustomStringConvertible {
public var description: String {
let moduleName = "hello"
switch kind {
case .compile:
return "Compiling \(moduleName) \(displayInputs.first?.file.name ?? "")"

case .mergeModule:
return "Merging module \(moduleName)"

case .link:
return "Linking \(moduleName)"

case .generateDSYM:
return "Generating dSYM for module \(moduleName)"

case .autolinkExtract:
return "Extracting autolink information for module \(moduleName)"

case .emitModule:
return "Emitting module for \(moduleName)"

case .generatePCH:
return "Compiling bridging header \(displayInputs.first!.file.name)"

case .generatePCM:
return "Compiling Clang module \(displayInputs.first!.file.name)"

case .interpret:
return "Interpreting \(displayInputs.first!.file.name)"

case .repl:
return "Executing Swift REPL"

case .verifyDebugInfo:
return "Verifying debug information for module \(moduleName)"

case .printTargetInfo:
return "Gathering target information for module \(moduleName)"

case .versionRequest:
return "Getting Swift version information"

case .help:
return "Swift help"

case .backend:
return "Embedding bitcode for \(moduleName) \(displayInputs.first?.file.name ?? "")"
}
}
}

extension Job.Kind {
/// Whether this job kind uses the Swift frontend.
public var isSwiftFrontend: Bool {
switch self {
case .backend, .compile, .mergeModule, .emitModule, .generatePCH,
.generatePCM, .interpret, .repl, .printTargetInfo,
.versionRequest:
return true

case .autolinkExtract, .generateDSYM, .help, .link, .verifyDebugInfo:
return false
}

}
}
// MARK: - Job.ArgTemplate + Codable

extension Job.ArgTemplate: Codable {
Expand Down
1 change: 1 addition & 0 deletions Sources/SwiftDriver/Jobs/LinkJob.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ extension Driver {

// TODO: some, but not all, linkers support response files.
return Job(
moduleName: moduleName,
kind: .link,
tool: .absolute(toolPath),
commandLine: commandLine,
Expand Down
1 change: 1 addition & 0 deletions Sources/SwiftDriver/Jobs/MergeModuleJob.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ extension Driver {
commandLine.appendPath(moduleOutput!.outputPath)

return Job(
moduleName: moduleName,
kind: .mergeModule,
tool: .absolute(try toolchain.getToolPath(.swiftCompiler)),
commandLine: commandLine,
Expand Down
42 changes: 24 additions & 18 deletions Sources/SwiftDriver/Jobs/Planning.swift
Original file line number Diff line number Diff line change
Expand Up @@ -226,34 +226,40 @@ extension Driver {
try commandLine.appendLast(.targetVariant, from: &parsedOptions)
try commandLine.appendLast(.sdk, from: &parsedOptions)
try commandLine.appendLast(.resourceDir, from: &parsedOptions)
return Job(kind: .printTargetInfo,
tool: .absolute(try toolchain.getToolPath(.swiftCompiler)),
commandLine: commandLine,
inputs: [],
outputs: [],
requiresInPlaceExecution: true)
return Job(
moduleName: moduleName,
kind: .printTargetInfo,
tool: .absolute(try toolchain.getToolPath(.swiftCompiler)),
commandLine: commandLine,
inputs: [],
outputs: [],
requiresInPlaceExecution: true)
}

if parsedOptions.hasArgument(.version) || parsedOptions.hasArgument(.version_) {
return Job(kind: .versionRequest,
tool: .absolute(try toolchain.getToolPath(.swiftCompiler)),
commandLine: [.flag("--version")],
inputs: [],
outputs: [],
requiresInPlaceExecution: true)
return Job(
moduleName: moduleName,
kind: .versionRequest,
tool: .absolute(try toolchain.getToolPath(.swiftCompiler)),
commandLine: [.flag("--version")],
inputs: [],
outputs: [],
requiresInPlaceExecution: true)
}

if parsedOptions.contains(.help) || parsedOptions.contains(.helpHidden) {
var commandLine: [Job.ArgTemplate] = [.flag("-tool=\(driverKind.rawValue)")]
if parsedOptions.contains(.helpHidden) {
commandLine.append(.flag("-show-hidden"))
}
return Job(kind: .help,
tool: .absolute(try toolchain.getToolPath(.swiftHelp)),
commandLine: commandLine,
inputs: [],
outputs: [],
requiresInPlaceExecution: true)
return Job(
moduleName: moduleName,
kind: .help,
tool: .absolute(try toolchain.getToolPath(.swiftHelp)),
commandLine: commandLine,
inputs: [],
outputs: [],
requiresInPlaceExecution: true)
}

return nil
Expand Down
1 change: 1 addition & 0 deletions Sources/SwiftDriver/Jobs/ReplJob.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ extension Driver {
// Squash important frontend options into a single argument for LLDB.
let lldbArg = "--repl=\(commandLine.joinedArguments)"
return Job(
moduleName: moduleName,
kind: .repl,
tool: .absolute(try toolchain.getToolPath(.lldb)),
commandLine: [Job.ArgTemplate.flag(lldbArg)],
Expand Down
1 change: 1 addition & 0 deletions Sources/SwiftDriver/Jobs/VerifyDebugInfoJob.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ extension Driver {
commandLine.appendPath(input.file)

return Job(
moduleName: moduleName,
kind: .verifyDebugInfo,
tool: .absolute(try toolchain.getToolPath(.dwarfdump)),
commandLine: commandLine,
Expand Down
4 changes: 4 additions & 0 deletions Tests/SwiftDriverTests/JobExecutorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ final class JobExecutorTests: XCTestCase {
]

let compileFoo = Job(
moduleName: "main",
kind: .compile,
tool: .absolute(try toolchain.getToolPath(.swiftCompiler)),
commandLine: [
Expand All @@ -104,6 +105,7 @@ final class JobExecutorTests: XCTestCase {
)

let compileMain = Job(
moduleName: "main",
kind: .compile,
tool: .absolute(try toolchain.getToolPath(.swiftCompiler)),
commandLine: [
Expand All @@ -127,6 +129,7 @@ final class JobExecutorTests: XCTestCase {
)

let link = Job(
moduleName: "main",
kind: .link,
tool: .absolute(try toolchain.getToolPath(.dynamicLinker)),
commandLine: [
Expand Down Expand Up @@ -167,6 +170,7 @@ final class JobExecutorTests: XCTestCase {

func testStubProcessProtocol() throws {
let job = Job(
moduleName: "main",
kind: .compile,
tool: .absolute(AbsolutePath("/usr/bin/swift")),
commandLine: [.flag("something")],
Expand Down