Skip to content

Verify module interfaces generated from emit-module jobs #615

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
Apr 26, 2021
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
28 changes: 18 additions & 10 deletions Sources/SwiftDriver/Jobs/Planning.swift
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ extension Driver {

try addPrecompileModuleDependenciesJobs(addJob: addJobBeforeCompiles)
try addPrecompileBridgingHeaderJob(addJob: addJobBeforeCompiles)
try addEmitModuleJob(addJob: addJobBeforeCompiles)
try addEmitModuleJob(addJobBeforeCompiles: addJobBeforeCompiles, addJobAfterCompiles: addJobAfterCompiles)
let linkerInputs = try addJobsFeedingLinker(
addJobBeforeCompiles: addJobBeforeCompiles,
addCompileJobGroup: addCompileJobGroup,
Expand Down Expand Up @@ -183,9 +183,11 @@ extension Driver {
)
}

private mutating func addEmitModuleJob(addJob: (Job) -> Void) throws {
private mutating func addEmitModuleJob(addJobBeforeCompiles: (Job) -> Void, addJobAfterCompiles: (Job) -> Void) throws {
if shouldCreateEmitModuleJob {
addJob( try emitModuleJob() )
let emitModuleJob = try emitModuleJob()
addJobBeforeCompiles(emitModuleJob)
try addVerifyJobs(emitModuleJob: emitModuleJob, addJob: addJobAfterCompiles)
}
}

Expand Down Expand Up @@ -221,9 +223,11 @@ extension Driver {
}
}

try addSingleCompileJobs(addJob: addJobBeforeCompiles,
if let compileJob = try addSingleCompileJobs(addJob: addJobBeforeCompiles,
addJobOutputs: addJobOutputs,
emitModuleTrace: loadedModuleTracePath != nil)
emitModuleTrace: loadedModuleTracePath != nil) {
try addVerifyJobs(emitModuleJob: compileJob, addJob: addJobAfterCompiles)
}

try addJobsForPrimaryInputs(
addCompileJobGroup: addCompileJobGroup,
Expand All @@ -239,7 +243,7 @@ extension Driver {
moduleInputs: moduleInputs,
moduleInputsFromJobOutputs: moduleInputsFromJobOutputs) {
addJobAfterCompiles(mergeJob)
try addVerifyJobs(mergeJob: mergeJob, addJob: addJobAfterCompiles)
try addVerifyJobs(emitModuleJob: mergeJob, addJob: addJobAfterCompiles)
try addWrapJobOrMergeOutputs(
mergeJob: mergeJob,
debugInfo: debugInfo,
Expand All @@ -249,13 +253,15 @@ extension Driver {
return linkerInputs
}

/// When in single compile, add one compile job and possiblity multiple backend jobs.
/// Return the compile job if one was created.
private mutating func addSingleCompileJobs(
addJob: (Job) -> Void,
addJobOutputs: ([TypedVirtualPath]) -> Void,
emitModuleTrace: Bool
) throws {
) throws -> Job? {
Copy link
Contributor

Choose a reason for hiding this comment

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

Just because the name states the plural Jobs, and we may be adding multiple (with backend jobs), it's worth a comment to explain exactly which job this function is going to return.

guard case .singleCompile = compilerMode
else { return }
else { return nil }

if parsedOptions.hasArgument(.embedBitcode),
inputFiles.allSatisfy({ $0.type.isPartOfSwiftCompilation }) {
Expand All @@ -270,6 +276,7 @@ extension Driver {
: nil
}
backendJobs.forEach(addJob)
return compile
} else {
// We can skip the compile jobs if all we want is a module when it's
// built separately.
Expand All @@ -278,6 +285,7 @@ extension Driver {
addJobOutputs: addJobOutputs,
emitModuleTrace: emitModuleTrace)
addJob(compile)
return compile
}
}

Expand Down Expand Up @@ -395,7 +403,7 @@ extension Driver {
return try mergeModuleJob(inputs: moduleInputs, inputsFromOutputs: moduleInputsFromJobOutputs)
}

private mutating func addVerifyJobs(mergeJob: Job, addJob: (Job) -> Void )
private mutating func addVerifyJobs(emitModuleJob: Job, addJob: (Job) -> Void )
throws {
guard
parsedOptions.hasArgument(.enableLibraryEvolution),
Expand All @@ -413,7 +421,7 @@ extension Driver {

let outputType: FileType =
forPrivate ? .privateSwiftInterface : .swiftInterface
let mergeInterfaceOutputs = mergeJob.outputs.filter { $0.type == outputType }
let mergeInterfaceOutputs = emitModuleJob.outputs.filter { $0.type == outputType }
assert(mergeInterfaceOutputs.count == 1,
"Merge module job should only have one swiftinterface output")
let job = try verifyModuleInterfaceJob(interfaceInput: mergeInterfaceOutputs[0])
Expand Down
43 changes: 43 additions & 0 deletions Tests/SwiftDriverTests/SwiftDriverTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3640,13 +3640,56 @@ final class SwiftDriverTests: XCTestCase {
XCTAssertTrue(verifyJob.outputs.isEmpty)
XCTAssertTrue(verifyJob.commandLine.contains(.path(mergeInterfaceOutputs[0].file)))
}

// No Evolution
do {
var driver = try Driver(args: ["swiftc", "foo.swift", "-emit-module", "-module-name",
"foo", "-emit-module-interface", "-verify-emitted-module-interface"])
let plannedJobs = try driver.planBuild()
XCTAssertEqual(plannedJobs.count, 2)
}

// Emit-module separately
do {
var driver = try Driver(args: ["swiftc", "foo.swift", "-emit-module", "-module-name",
"foo", "-emit-module-interface",
"-verify-emitted-module-interface",
"-enable-library-evolution",
"-experimental-emit-module-separately"])
let plannedJobs = try driver.planBuild()
XCTAssertEqual(plannedJobs.count, 2)
let emitJob = plannedJobs[0]
let verifyJob = plannedJobs[1]
XCTAssertEqual(emitJob.kind, .emitModule)
let emitInterfaceOutput = emitJob.outputs.filter { $0.type == .swiftInterface }
XCTAssertTrue(emitInterfaceOutput.count == 1,
"Emit module job should only have one swiftinterface output")
XCTAssertEqual(verifyJob.kind, .verifyModuleInterface)
XCTAssertTrue(verifyJob.inputs.count == 1)
XCTAssertTrue(verifyJob.inputs[0] == emitInterfaceOutput[0])
XCTAssertTrue(verifyJob.commandLine.contains(.path(emitInterfaceOutput[0].file)))
}

// Whole-module
do {
var driver = try Driver(args: ["swiftc", "foo.swift", "-emit-module", "-module-name",
"foo", "-emit-module-interface",
"-verify-emitted-module-interface",
"-enable-library-evolution",
"-whole-module-optimization"])
let plannedJobs = try driver.planBuild()
XCTAssertEqual(plannedJobs.count, 2)
let emitJob = plannedJobs[0]
let verifyJob = plannedJobs[1]
XCTAssertEqual(emitJob.kind, .compile)
let emitInterfaceOutput = emitJob.outputs.filter { $0.type == .swiftInterface }
XCTAssertTrue(emitInterfaceOutput.count == 1,
"Emit module job should only have one swiftinterface output")
XCTAssertEqual(verifyJob.kind, .verifyModuleInterface)
XCTAssertTrue(verifyJob.inputs.count == 1)
XCTAssertTrue(verifyJob.inputs[0] == emitInterfaceOutput[0])
XCTAssertTrue(verifyJob.commandLine.contains(.path(emitInterfaceOutput[0].file)))
}
}

func testPCHGeneration() throws {
Expand Down