Skip to content

More accurate computation of compile job output paths #67

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
Feb 13, 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
2 changes: 1 addition & 1 deletion Sources/SwiftDriver/Driver/Driver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,7 @@ extension Driver {
return
}

if jobs.contains(where: { $0.requiresInPlaceExecution }) {
if jobs.contains(where: { $0.requiresInPlaceExecution }) || jobs.count == 1 {
assert(jobs.count == 1, "Cannot execute in place for multi-job build plans")
return try executeJobInPlace(jobs[0], resolver: resolver, forceResponseFiles: forceResponseFiles)
}
Expand Down
69 changes: 58 additions & 11 deletions Sources/SwiftDriver/Jobs/CompileJob.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,57 @@ extension Driver {
}
}

/// Add the compiler inputs for a frontend compilation job, and return the corresponding primary set of outputs.
func addCompileInputs(primaryInputs: [TypedVirtualPath], inputs: inout [TypedVirtualPath], commandLine: inout [Job.ArgTemplate]) -> [TypedVirtualPath] {
fileprivate mutating func computePrimaryOutput(for input: TypedVirtualPath, outputType: FileType,
isTopLevel: Bool) -> TypedVirtualPath {
if let path = outputFileMap?.existingOutput(inputFile: input.file, outputType: outputType) {
return TypedVirtualPath(file: path, type: outputType)
}

if isTopLevel {
if let baseOutput = parsedOptions.getLastArgument(.o)?.asSingle,
let baseOutputPath = try? VirtualPath(path: baseOutput){
return TypedVirtualPath(file: baseOutputPath, type: outputType)
} else if compilerOutputType?.isTextual == true {
return TypedVirtualPath(file: .standardOutput, type: outputType)
}
}

let baseName: String
if (!compilerMode.usesPrimaryFileInputs && numThreads == 0) {
baseName = moduleName
} else {
baseName = input.file.basenameWithoutExt
}

if !isTopLevel {
return TypedVirtualPath(file:VirtualPath.temporary(.init(baseName.appendingFileTypeExtension(outputType))),
type: outputType)
}

return TypedVirtualPath(file: .relative(.init(baseName.appendingFileTypeExtension(outputType))), type: outputType)
}

/// Add the compiler inputs for a frontend compilation job, and return the
/// corresponding primary set of outputs.
mutating func addCompileInputs(primaryInputs: [TypedVirtualPath],
inputs: inout [TypedVirtualPath],
commandLine: inout [Job.ArgTemplate]) -> [TypedVirtualPath] {
// Is this compile job top-level
let isTopLevel: Bool

switch compilerOutputType {
case .assembly, .sil, .raw_sil, .llvmIR, .ast:
isTopLevel = true
case .object:
isTopLevel = (linkerOutputType == nil)
case .swift, .sib, .image, .dSYM, .dependencies, .autolink,
.swiftModule, .swiftDocumentation, .swiftInterface,
.swiftSourceInfoFile, .raw_sib, .llvmBitcode, .diagnostics,
.objcHeader, .swiftDeps, .remap, .importedModules, .tbd, .moduleTrace,
.indexData, .optimizationRecord, .pcm, .pch, nil:
isTopLevel = false
}

// Collect the set of input files that are part of the Swift compilation.
let swiftInputFiles: [TypedVirtualPath] = inputFiles.compactMap { inputFile in
if inputFile.type.isPartOfSwiftCompilation {
Expand Down Expand Up @@ -58,21 +107,19 @@ extension Driver {
// add an output for the input.
if isPrimary || numThreads > 0,
let compilerOutputType = compilerOutputType {
let output = (outputFileMap ?? OutputFileMap()).getOutput(
inputFile: input.file,
outputType: compilerOutputType
)
primaryOutputs.append(TypedVirtualPath(file: output, type: compilerOutputType))
primaryOutputs.append(computePrimaryOutput(for: input,
outputType: compilerOutputType,
isTopLevel: isTopLevel))
}
}

// When not using primary file inputs or multithreading, add a single output.
if !usesPrimaryFileInputs && numThreads == 0,
let outputType = compilerOutputType {
let existingOutputPath = outputFileMap?.existingOutputForSingleInput(
outputType: outputType)
let output = existingOutputPath ?? VirtualPath.temporary(.init(moduleName.appendingFileTypeExtension(outputType)))
primaryOutputs.append(TypedVirtualPath(file: output, type: outputType))
primaryOutputs.append(computePrimaryOutput(
for: TypedVirtualPath(file: try! VirtualPath(path: ""),
type: swiftInputFiles[0].type),
outputType: outputType, isTopLevel: isTopLevel))
}

return primaryOutputs
Expand Down
15 changes: 15 additions & 0 deletions Sources/SwiftDriver/Utilities/FileType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -246,3 +246,18 @@ extension FileType {
}
}
}

extension FileType {
var isTextual: Bool {
switch self {
case .swift, .sil, .dependencies, .assembly, .ast, .raw_sil, .llvmIR,
.objcHeader, .autolink, .importedModules, .tbd, .moduleTrace,
.optimizationRecord, .swiftInterface:
return true
case .image, .object, .dSYM, .pch, .sib, .raw_sib, .swiftModule,
.swiftDocumentation, .swiftSourceInfoFile, .llvmBitcode, .diagnostics,
.pcm, .swiftDeps, .remap, .indexData:
return false
}
}
}
28 changes: 28 additions & 0 deletions Tests/SwiftDriverTests/SwiftDriverTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,34 @@ final class SwiftDriverTests: XCTestCase {
}
}

func testBaseOutputPaths() throws {
// Test the combination of -c and -o includes the base output path.
do {
var driver = try Driver(args: ["swiftc", "-c", "foo.swift", "-o", "/some/output/path/bar.o"])
let plannedJobs = try driver.planBuild()
XCTAssertEqual(plannedJobs.count, 1)
XCTAssertEqual(plannedJobs[0].kind, .compile)
XCTAssertTrue(plannedJobs[0].commandLine.contains(.path(try VirtualPath(path: "/some/output/path/bar.o"))))
}

do {
var driver = try Driver(args: ["swiftc", "-emit-sil", "foo.swift", "-o", "/some/output/path/bar.sil"])
let plannedJobs = try driver.planBuild()
XCTAssertEqual(plannedJobs.count, 1)
XCTAssertEqual(plannedJobs[0].kind, .compile)
XCTAssertTrue(plannedJobs[0].commandLine.contains(.path(try VirtualPath(path: "/some/output/path/bar.sil"))))
}

do {
// If no output is specified, verify we print to stdout for textual formats.
var driver = try Driver(args: ["swiftc", "-emit-assembly", "foo.swift"])
let plannedJobs = try driver.planBuild()
XCTAssertEqual(plannedJobs.count, 1)
XCTAssertEqual(plannedJobs[0].kind, .compile)
XCTAssertTrue(plannedJobs[0].commandLine.contains(.path(.standardOutput)))
}
}

func testMultithreading() throws {

XCTAssertEqual(try Driver(args: ["swiftc"]).numThreads, 0)
Expand Down