Skip to content

Infer object format when it can't be determined from a triple's successfully parsed environment #201

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 2 commits 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)
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
2 changes: 2 additions & 0 deletions Sources/SwiftDriver/Utilities/Triple.swift
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@ public struct Triple {
if let parsedEnv = parsedEnv {
self.environment = parsedEnv.value.environment
self.objectFormat = parsedEnv.value.objectFormat
?? ObjectFormat.infer(arch: parsedArch?.value.arch,
os: parsedOS?.value)
}
else {
self.environment = Environment.infer(archName: parsedArch?.substring)
Expand Down
5 changes: 5 additions & 0 deletions Tests/SwiftDriverTests/ExplicitModuleBuildTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,11 @@ final class ExplicitModuleBuildTests: XCTestCase {
case .relative(RelativePath("main")):
XCTAssertTrue(driver.isExplicitMainModuleJob(job: job))
XCTAssertEqual(job.kind, .link)

case .temporary(RelativePath("main.autolink")):
XCTAssertTrue(driver.isExplicitMainModuleJob(job: job))
XCTAssertEqual(job.kind, .autolinkExtract)

default:
XCTFail("Unexpected module dependency build job output: \(job.outputs[0].file)")
}
Expand Down
Loading