Skip to content

Update to new path APIs #1207

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
Oct 25, 2022
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 Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

53 changes: 30 additions & 23 deletions Sources/SwiftDriver/Driver/Driver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -395,8 +395,10 @@ public struct Driver {
guard let moduleOutput = moduleOutputInfo.output else {
return nil
}
return TypedVirtualPath(file: VirtualPath.lookup(moduleOutput.outputPath)
.replacingExtension(with: .jsonABIBaseline).intern(), type: .jsonABIBaseline)
guard let path = try? VirtualPath.lookup(moduleOutput.outputPath).replacingExtension(with: .jsonABIBaseline) else {
return nil
}
return TypedVirtualPath(file: path.intern(), type: .jsonABIBaseline)
}()

public static func isOptionFound(_ opt: String, allOpts: Set<String>) -> Bool {
Expand Down Expand Up @@ -513,7 +515,7 @@ public struct Driver {
// Compute the working directory.
workingDirectory = try parsedOptions.getLastArgument(.workingDirectory).map { workingDirectoryArg in
let cwd = fileSystem.currentWorkingDirectory
return try cwd.map{ AbsolutePath(workingDirectoryArg.asSingle, relativeTo: $0) } ?? AbsolutePath(validating: workingDirectoryArg.asSingle)
return try cwd.map{ try AbsolutePath(validating: workingDirectoryArg.asSingle, relativeTo: $0) } ?? AbsolutePath(validating: workingDirectoryArg.asSingle)
}

// Apply the working directory to the parsed options.
Expand Down Expand Up @@ -823,7 +825,7 @@ public struct Driver {
// with private Clang modules.
if let swiftInterfacePath = self.swiftInterfacePath,
givenPrivateInterfacePath == nil {
self.swiftPrivateInterfacePath = VirtualPath.lookup(swiftInterfacePath)
self.swiftPrivateInterfacePath = try VirtualPath.lookup(swiftInterfacePath)
.replacingExtension(with: .privateSwiftInterface).intern()
} else {
self.swiftPrivateInterfacePath = givenPrivateInterfacePath
Expand Down Expand Up @@ -1167,7 +1169,10 @@ extension Driver {
) -> AbsolutePath? {
let responseFile: AbsolutePath
if let basePath = basePath {
responseFile = AbsolutePath(path, relativeTo: basePath)
guard let absolutePath = try? AbsolutePath(validating: path, relativeTo: basePath) else {
return nil
}
responseFile = absolutePath
} else {
guard let absolutePath = try? AbsolutePath(validating: path) else {
return nil
Expand Down Expand Up @@ -1741,7 +1746,7 @@ extension Driver {
/// Apply the given working directory to all paths in the parsed options.
private static func applyWorkingDirectory(_ workingDirectory: AbsolutePath,
to parsedOptions: inout ParsedOptions) throws {
parsedOptions.forEachModifying { parsedOption in
try parsedOptions.forEachModifying { parsedOption in
// Only translate options whose arguments are paths.
if !parsedOption.option.attributes.contains(.argumentIsPath) { return }

Expand All @@ -1754,12 +1759,12 @@ extension Driver {
if arg == "-" {
translatedArgument = parsedOption.argument
} else {
translatedArgument = .single(AbsolutePath(arg, relativeTo: workingDirectory).pathString)
translatedArgument = .single(try AbsolutePath(validating: arg, relativeTo: workingDirectory).pathString)
}

case .multiple(let args):
translatedArgument = .multiple(args.map { arg in
AbsolutePath(arg, relativeTo: workingDirectory).pathString
translatedArgument = .multiple(try args.map { arg in
try AbsolutePath(validating: arg, relativeTo: workingDirectory).pathString
})
}

Expand Down Expand Up @@ -2400,7 +2405,7 @@ extension Driver {

// Use working directory if specified
if let moduleRelative = moduleOutputPath.relativePath {
moduleOutputPath = Driver.useWorkingDirectory(moduleRelative, workingDirectory)
moduleOutputPath = try Driver.useWorkingDirectory(moduleRelative, workingDirectory)
}

switch moduleOutputKind! {
Expand Down Expand Up @@ -2500,8 +2505,8 @@ extension Driver {
// FIXME: TSC should provide a better utility for this.
if let absPath = try? AbsolutePath(validating: sdkPath) {
path = absPath
} else if let cwd = fileSystem.currentWorkingDirectory {
path = AbsolutePath(sdkPath, relativeTo: cwd)
} else if let cwd = fileSystem.currentWorkingDirectory, let absPath = try? AbsolutePath(validating: sdkPath, relativeTo: cwd) {
path = absPath
} else {
diagnosticsEngine.emit(.warning_no_such_sdk(sdkPath))
return nil
Expand Down Expand Up @@ -2585,7 +2590,7 @@ extension Driver {
return nil
}

if let outputPath = outputFileMap?.existingOutput(inputFile: input, outputType: .pch) {
if let outputPath = try outputFileMap?.existingOutput(inputFile: input, outputType: .pch) {
return outputPath
}

Expand Down Expand Up @@ -2689,9 +2694,11 @@ extension Driver {
if let profileArgs = parsedOptions.getLastArgument(.profileUse)?.asMultiple,
let workingDirectory = workingDirectory ?? fileSystem.currentWorkingDirectory {
for profilingData in profileArgs {
if !fileSystem.exists(AbsolutePath(profilingData,
relativeTo: workingDirectory)) {
diagnosticEngine.emit(Error.missingProfilingData(profilingData))
if let path = try? AbsolutePath(validating: profilingData,
relativeTo: workingDirectory) {
if !fileSystem.exists(path) {
diagnosticEngine.emit(Error.missingProfilingData(profilingData))
}
}
}
}
Expand Down Expand Up @@ -3065,7 +3072,7 @@ extension Driver {
// If this is a single-file compile and there is an entry in the
// output file map, use that.
if compilerMode.isSingleCompilation,
let singleOutputPath = outputFileMap?.existingOutputForSingleInput(
let singleOutputPath = try outputFileMap?.existingOutputForSingleInput(
outputType: type) {
return singleOutputPath
}
Expand All @@ -3074,23 +3081,23 @@ extension Driver {
// primary output type is a .swiftmodule and we are using the emit-module-separately
// flow, then also consider single output paths specified in the output file-map.
if compilerOutputType == .swiftModule && emitModuleSeparately,
let singleOutputPath = outputFileMap?.existingOutputForSingleInput(
let singleOutputPath = try outputFileMap?.existingOutputForSingleInput(
outputType: type) {
return singleOutputPath
}

// Emit-module serialized diagnostics are always specified as a single-output
// file
if type == .emitModuleDiagnostics,
let singleOutputPath = outputFileMap?.existingOutputForSingleInput(
let singleOutputPath = try outputFileMap?.existingOutputForSingleInput(
outputType: type) {
return singleOutputPath
}

// Emit-module discovered dependencies are always specified as a single-output
// file
if type == .emitModuleDependencies,
let path = outputFileMap?.existingOutputForSingleInput(outputType: type) {
let path = try outputFileMap?.existingOutputForSingleInput(outputType: type) {
return path
}

Expand All @@ -3113,7 +3120,7 @@ extension Driver {
// synthesize a path from the master swift dependency path. This is
// important as we may otherwise emit this file at the location where the
// driver was invoked, which is normally the root of the package.
if let path = outputFileMap?.existingOutputForSingleInput(outputType: .swiftDeps) {
if let path = try outputFileMap?.existingOutputForSingleInput(outputType: .swiftDeps) {
return VirtualPath.lookup(path)
.parentDirectory
.appending(component: "\(moduleName).\(type.rawValue)")
Expand Down Expand Up @@ -3230,7 +3237,7 @@ extension Driver {
// If this is a single-file compile and there is an entry in the
// output file map, use that.
if compilerMode.isSingleCompilation,
let singleOutputPath = outputFileMap?.existingOutputForSingleInput(
let singleOutputPath = try outputFileMap?.existingOutputForSingleInput(
outputType: type) {
return singleOutputPath
}
Expand All @@ -3249,7 +3256,7 @@ extension Driver {
parentPath = VirtualPath.lookup(moduleOutputPath).parentDirectory
}

return parentPath
return try parentPath
.appending(component: VirtualPath.lookup(moduleOutputPath).basename)
.replacingExtension(with: type)
.intern()
Expand Down
14 changes: 7 additions & 7 deletions Sources/SwiftDriver/Driver/OutputFileMap.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ public struct OutputFileMap: Hashable, Codable {

/// For the given input file, retrieve or create an output file for the given
/// file type.
public func getOutput(inputFile: VirtualPath.Handle, outputType: FileType) -> VirtualPath.Handle {
public func getOutput(inputFile: VirtualPath.Handle, outputType: FileType) throws -> VirtualPath.Handle {
// If we already have an output file, retrieve it.
if let output = existingOutput(inputFile: inputFile, outputType: outputType) {
if let output = try existingOutput(inputFile: inputFile, outputType: outputType) {
return output
}

Expand All @@ -50,7 +50,7 @@ public struct OutputFileMap: Hashable, Codable {
return VirtualPath.createUniqueTemporaryFile(RelativePath(inputFile.basenameWithoutExt.appendingFileTypeExtension(outputType))).intern()
}

public func existingOutput(inputFile: VirtualPath.Handle, outputType: FileType) -> VirtualPath.Handle? {
public func existingOutput(inputFile: VirtualPath.Handle, outputType: FileType) throws -> VirtualPath.Handle? {
if let path = entries[inputFile]?[outputType] {
return path
}
Expand All @@ -60,14 +60,14 @@ public struct OutputFileMap: Hashable, Codable {
guard let path = entries[inputFile]?[.swiftModule] else {
return nil
}
return VirtualPath.lookup(path).replacingExtension(with: outputType).intern()
return try VirtualPath.lookup(path).replacingExtension(with: outputType).intern()

case .jsonAPIBaseline, .jsonABIBaseline:
// Infer paths for these entities using .swiftsourceinfo path.
guard let path = entries[inputFile]?[.swiftSourceInfoFile] else {
return nil
}
return VirtualPath.lookup(path).replacingExtension(with: outputType).intern()
return try VirtualPath.lookup(path).replacingExtension(with: outputType).intern()

case .object:
// We may generate .o files from bitcode .bc files, but the output file map
Expand All @@ -86,8 +86,8 @@ public struct OutputFileMap: Hashable, Codable {
}
}

public func existingOutputForSingleInput(outputType: FileType) -> VirtualPath.Handle? {
existingOutput(inputFile: Self.singleInputKey, outputType: outputType)
public func existingOutputForSingleInput(outputType: FileType) throws -> VirtualPath.Handle? {
try existingOutput(inputFile: Self.singleInputKey, outputType: outputType)
}

public func resolveRelativePaths(relativeTo absPath: AbsolutePath) -> OutputFileMap {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ import class Dispatch.DispatchQueue
recordedInputModificationDates: [TypedVirtualPath: TimePoint]
) {
// Cannot write a buildRecord without a path.
guard let buildRecordPath = Self.computeBuildRecordPath(
guard let buildRecordPath = try? Self.computeBuildRecordPath(
outputFileMap: outputFileMap,
incremental: incremental,
compilerOutputType: compilerOutputType,
Expand Down Expand Up @@ -127,14 +127,14 @@ import class Dispatch.DispatchQueue
compilerOutputType: FileType?,
workingDirectory: AbsolutePath?,
diagnosticEngine: DiagnosticsEngine
) -> VirtualPath? {
) throws -> VirtualPath? {
// FIXME: This should work without an output file map. We should have
// another way to specify a build record and where to put intermediates.
guard let ofm = outputFileMap else {
return nil
}
guard let partialBuildRecordPath =
ofm.existingOutputForSingleInput(outputType: .swiftDeps)
try ofm.existingOutputForSingleInput(outputType: .swiftDeps)
else {
if incremental {
diagnosticEngine.emit(.warning_incremental_requires_build_record_entry)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,10 @@ extension IncrementalCompilationState {
report(message, pathIfGiven?.file)
return
}
let output = outputFileMap.getOutput(inputFile: path.fileHandle, outputType: .object)
guard let output = try? outputFileMap.getOutput(inputFile: path.fileHandle, outputType: .object) else {
report(message, pathIfGiven?.file)
return
}
let compiling = " {compile: \(VirtualPath.lookup(output).basename) <= \(input.basename)}"
diagnosticEngine.emit(.remark_incremental_compilation(because: "\(message) \(compiling)"))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ extension ModuleDependencyGraph {
// to preserve legacy behavior cancel whole thing
info.diagnosticEngine.emit(
.remark_incremental_compilation_has_been_disabled(
because: "malformed dependencies file '\(dependencySource.fileToRead(info: info)?.file.name ?? "none?!")'"))
because: "malformed dependencies file '\((try? dependencySource.fileToRead(info: info))?.file.name ?? "none?!")'"))
return nil
}
let invalidatedNodes = Integrator.integrate(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ extension DependencySource {
info: IncrementalCompilationState.IncrementalDependencyAndInputSetup,
internedStringTable: InternedStringTable
) -> SourceFileDependencyGraph? {
guard let fileToRead = fileToRead(info: info) else {return nil}
guard let fileToRead = try? fileToRead(info: info) else {return nil}
do {
info.reporter?.report("Reading dependencies from \(description)")
return try SourceFileDependencyGraph.read(from: fileToRead,
Expand All @@ -89,10 +89,10 @@ extension DependencySource {
/// - Returns: The corresponding swiftdeps file for a swift file, or the swiftmodule file for an incremental imports source.
public func fileToRead(
info: IncrementalCompilationState.IncrementalDependencyAndInputSetup
) -> TypedVirtualPath? {
) throws -> TypedVirtualPath? {
typedFile.type != .swift
? typedFile
: info.outputFileMap.getSwiftDeps(for: typedFile, diagnosticEngine: info.diagnosticEngine)
: try info.outputFileMap.getSwiftDeps(for: typedFile, diagnosticEngine: info.diagnosticEngine)
}
}

Expand All @@ -101,9 +101,9 @@ extension OutputFileMap {
fileprivate func getSwiftDeps(
for sourceFile: TypedVirtualPath,
diagnosticEngine: DiagnosticsEngine
) -> TypedVirtualPath? {
) throws -> TypedVirtualPath? {
assert(sourceFile.type == FileType.swift)
guard let swiftDepsHandle = existingOutput(inputFile: sourceFile.fileHandle,
guard let swiftDepsHandle = try existingOutput(inputFile: sourceFile.fileHandle,
outputType: .swiftDeps)
else {
// The legacy driver fails silently here.
Expand Down
6 changes: 3 additions & 3 deletions Sources/SwiftDriver/Jobs/BackendJob.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ extension Driver {
if let compilerOutputType = compilerOutputType {
// If there is no baseInput (singleCompileMode), primary output computation
// is not input-specific, therefore it does not matter which input is passed.
let output = computePrimaryOutput(for: baseInput ?? input,
outputType: compilerOutputType,
isTopLevel: isTopLevelOutput(type: compilerOutputType))
let output = try computePrimaryOutput(for: baseInput ?? input,
outputType: compilerOutputType,
isTopLevel: isTopLevelOutput(type: compilerOutputType))
commandLine.appendFlag(.o)
commandLine.appendPath(output.file)
outputs.append(output)
Expand Down
Loading