Skip to content

PrebuiltModuleGen: generate per-interface log files #792

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 12, 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
56 changes: 45 additions & 11 deletions Sources/SwiftDriver/Jobs/PrebuiltModulesJob.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,33 +24,40 @@ public class PrebuitModuleGenerationDelegate: JobExecutionDelegate {
let diagnosticsEngine: DiagnosticsEngine
let verbose: Bool
var failingCriticalOutputs: Set<VirtualPath>
public init(_ jobs: [Job], _ diagnosticsEngine: DiagnosticsEngine, _ verbose: Bool) {
let logPath: AbsolutePath?
public init(_ jobs: [Job], _ diagnosticsEngine: DiagnosticsEngine, _ verbose: Bool, logPath: AbsolutePath?) {
self.diagnosticsEngine = diagnosticsEngine
self.verbose = verbose
self.failingCriticalOutputs = Set<VirtualPath>(jobs.compactMap(PrebuitModuleGenerationDelegate.getCriticalOutput))
self.logPath = logPath
}

/// Dangling jobs are macabi-only modules. We should run those jobs if foundation
/// is built successfully for macabi.
public var shouldRunDanglingJobs: Bool {
return !failingCriticalOutputs.contains(where: isIosMac)
}
func printJobInfo(_ job: Job, _ start: Bool) {
guard verbose else {
return
}

func getInputInterfacePath(_ job: Job) -> AbsolutePath {
for arg in job.commandLine {
if case .path(let p) = arg {
if p.extension == "swiftinterface" {
Driver.stdErrQueue.sync {
stderrStream <<< (start ? "started: " : "finished: ")
stderrStream <<< p.absolutePath!.pathString <<< "\n"
stderrStream.flush()
}
return
return p.absolutePath!
}
}
}
fatalError()
}

func printJobInfo(_ job: Job, _ start: Bool) {
guard verbose else {
return
}
Driver.stdErrQueue.sync {
stderrStream <<< (start ? "started: " : "finished: ")
stderrStream <<< getInputInterfacePath(job).pathString <<< "\n"
stderrStream.flush()
}
}

static func getCriticalOutput(_ job: Job) -> VirtualPath? {
Expand All @@ -66,6 +73,24 @@ public class PrebuitModuleGenerationDelegate: JobExecutionDelegate {
return !failingCriticalOutputs.isEmpty
}

fileprivate func logOutput(_ job: Job, _ result: ProcessResult, _ stdout: Bool) throws {
guard let logPath = logPath else {
return
}
let content = stdout ? try result.utf8Output() : try result.utf8stderrOutput()
guard !content.isEmpty else {
return
}
if !localFileSystem.exists(logPath) {
try localFileSystem.createDirectory(logPath, recursive: true)
}
let interfaceBase = getInputInterfacePath(job).basenameWithoutExt
let fileName = "\(job.moduleName)-\(interfaceBase)-\(stdout ? "out" : "err").txt"
try localFileSystem.writeFileContents(logPath.appending(component: fileName)) {
$0 <<< content
}
}

public func jobFinished(job: Job, result: ProcessResult, pid: Int) {
switch result.exitStatus {
case .terminated(code: let code):
Expand All @@ -86,6 +111,15 @@ public class PrebuitModuleGenerationDelegate: JobExecutionDelegate {
diagnosticsEngine.emit(.remark("\(job.moduleName) interrupted"))
#endif
}
do {
try logOutput(job, result, true)
try logOutput(job, result, false)
} catch {
Driver.stdErrQueue.sync {
stderrStream <<< "Failed to generate log file"
stderrStream.flush()
}
}
}

public func jobSkipped(job: Job) {
Expand Down
2 changes: 1 addition & 1 deletion Sources/swift-build-sdk-interfaces/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ do {
if skipExecution {
exit(0)
}
let delegate = PrebuitModuleGenerationDelegate(jobs, diagnosticsEngine, verbose)
let delegate = PrebuitModuleGenerationDelegate(jobs, diagnosticsEngine, verbose, logPath: try getArgumentAsPath("-log-path"))
do {
try executor.execute(workload: DriverExecutorWorkload.init(jobs, nil, continueBuildingAfterErrors: true),
delegate: delegate, numParallelJobs: 128)
Expand Down