Skip to content

Support VirtualPaths in ToolChain #77

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

Closed
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 @@ -251,7 +251,7 @@ public struct Driver {
self.swiftCompilerPrefixArgs = []
} else {
let frontendPath = frontendCommandLine.removeFirst()
self.toolchain.overrideToolPath(.swiftCompiler, path: try AbsolutePath(validating: frontendPath))
self.toolchain.overrideToolPath(.swiftCompiler, path: try VirtualPath(path: frontendPath))
self.swiftCompilerPrefixArgs = frontendCommandLine
}
} else {
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftDriver/Jobs/AutolinkExtractJob.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ extension Driver {

return Job(
kind: .autolinkExtract,
tool: .absolute(try toolchain.getToolPath(.swiftAutolinkExtract)),
tool: try toolchain.getToolPath(.swiftAutolinkExtract),
commandLine: commandLine,
inputs: inputs,
outputs: [.init(file: output, type: .autolink)],
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftDriver/Jobs/CompileJob.swift
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ extension Driver {

return Job(
kind: .compile,
tool: .absolute(try toolchain.getToolPath(.swiftCompiler)),
tool: try toolchain.getToolPath(.swiftCompiler),
commandLine: commandLine,
displayInputs: primaryInputs,
inputs: inputs,
Expand Down
6 changes: 3 additions & 3 deletions Sources/SwiftDriver/Jobs/DarwinToolchain+LinkerSupport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import TSCBasic
import SwiftOptions

extension DarwinToolchain {
private func findARCLiteLibPath() throws -> AbsolutePath? {
private func findARCLiteLibPath() throws -> VirtualPath? {
let path = try getToolPath(.swiftCompiler)
.parentDirectory // 'swift'
.parentDirectory // 'bin'
Expand Down Expand Up @@ -198,7 +198,7 @@ extension DarwinToolchain {
sanitizers: Set<Sanitizer>,
targetTriple: Triple,
targetVariantTriple: Triple?
) throws -> AbsolutePath {
) throws -> VirtualPath {

// FIXME: If we used Clang as a linker instead of going straight to ld,
// we wouldn't have to replicate a bunch of Clang's logic here.
Expand All @@ -217,7 +217,7 @@ extension DarwinToolchain {
parsedOptions: &parsedOptions)
.appending(component: "libclang_rt.\(darwinPlatformSuffix).a")
if localFileSystem.exists(compilerRTPath) {
commandLine.append(.path(.absolute(compilerRTPath)))
commandLine.append(.path(compilerRTPath))
}

// Set up for linking.
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftDriver/Jobs/EmitModuleJob.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ extension Driver {

return Job(
kind: .emitModule,
tool: .absolute(try toolchain.getToolPath(.swiftCompiler)),
tool: try toolchain.getToolPath(.swiftCompiler),
commandLine: commandLine,
inputs: inputs,
outputs: outputs
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftDriver/Jobs/GenerateDSYMJob.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ extension Driver {

return Job(
kind: .generateDSYM,
tool: .absolute(try toolchain.getToolPath(.dsymutil)),
tool: try toolchain.getToolPath(.dsymutil),
commandLine: commandLine,
displayInputs: [],
inputs: inputs,
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftDriver/Jobs/GeneratePCHJob.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ extension Driver {

return Job(
kind: .generatePCH,
tool: .absolute(try toolchain.getToolPath(.swiftCompiler)),
tool: try toolchain.getToolPath(.swiftCompiler),
commandLine: commandLine,
displayInputs: [],
inputs: inputs,
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftDriver/Jobs/GeneratePCMJob.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ extension Driver {

return Job(
kind: .generatePCM,
tool: .absolute(try toolchain.getToolPath(.swiftCompiler)),
tool: try toolchain.getToolPath(.swiftCompiler),
commandLine: commandLine,
displayInputs: [],
inputs: inputs,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ extension GenericUnixToolchain {
sanitizers: Set<Sanitizer>,
targetTriple: Triple,
targetVariantTriple: Triple?
) throws -> AbsolutePath {
) throws -> VirtualPath {
switch linkerOutputType {
case .dynamicLibrary:
// Same options as an executable, just with '-shared'
Expand Down Expand Up @@ -107,7 +107,7 @@ extension GenericUnixToolchain {

// If there is a clang in the toolchain folder, use that instead.
if let tool = lookupExecutablePath(filename: "clang", searchPaths: [toolsDir]) {
clangPath = tool
clangPath = .absolute(tool)
}

// Look for binutils in the toolchain folder.
Expand Down Expand Up @@ -191,7 +191,7 @@ extension GenericUnixToolchain {
// Link the standard library. In two paths, we do this using a .lnk file
// if we're going that route, we'll set `linkFilePath` to the path to that
// file.
var linkFilePath: AbsolutePath? = try computeResourceDirPath(
var linkFilePath: VirtualPath? = try computeResourceDirPath(
for: targetTriple,
parsedOptions: &parsedOptions,
isShared: false
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftDriver/Jobs/InterpretJob.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ extension Driver {

return Job(
kind: .interpret,
tool: .absolute(try toolchain.getToolPath(.swiftCompiler)),
tool: try toolchain.getToolPath(.swiftCompiler),
commandLine: commandLine,
inputs:inputs,
outputs: [],
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftDriver/Jobs/LinkJob.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ extension Driver {
// TODO: some, but not all, linkers support response files.
return Job(
kind: .link,
tool: .absolute(toolPath),
tool: toolPath,
commandLine: commandLine,
inputs: inputs,
outputs: [.init(file: outputFile, type: .object)]
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftDriver/Jobs/MergeModuleJob.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ extension Driver {

return Job(
kind: .mergeModule,
tool: .absolute(try toolchain.getToolPath(.swiftCompiler)),
tool: try toolchain.getToolPath(.swiftCompiler),
commandLine: commandLine,
inputs: inputs,
outputs: outputs,
Expand Down
6 changes: 3 additions & 3 deletions Sources/SwiftDriver/Jobs/Planning.swift
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ extension Driver {
try commandLine.appendLast(.sdk, from: &parsedOptions)
try commandLine.appendLast(.resourceDir, from: &parsedOptions)
return Job(kind: .printTargetInfo,
tool: .absolute(try toolchain.getToolPath(.swiftCompiler)),
tool: try toolchain.getToolPath(.swiftCompiler),
commandLine: commandLine,
inputs: [],
outputs: [],
Expand All @@ -185,7 +185,7 @@ extension Driver {

if parsedOptions.hasArgument(.version) || parsedOptions.hasArgument(.version_) {
return Job(kind: .versionRequest,
tool: .absolute(try toolchain.getToolPath(.swiftCompiler)),
tool: try toolchain.getToolPath(.swiftCompiler),
commandLine: [.flag("--version")],
inputs: [],
outputs: [],
Expand All @@ -198,7 +198,7 @@ extension Driver {
commandLine.append(.flag("-show-hidden"))
}
return Job(kind: .help,
tool: .absolute(try toolchain.getToolPath(.swiftHelp)),
tool: try toolchain.getToolPath(.swiftHelp),
commandLine: commandLine,
inputs: [],
outputs: [],
Expand Down
4 changes: 2 additions & 2 deletions Sources/SwiftDriver/Jobs/ReplJob.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ extension Driver {
let lldbArg = "--repl=\(commandLine.joinedArguments)"
return Job(
kind: .repl,
tool: .absolute(try toolchain.getToolPath(.lldb)),
tool: try toolchain.getToolPath(.lldb),
commandLine: [Job.ArgTemplate.flag(lldbArg)],
inputs: [],
outputs: [],
Expand All @@ -41,7 +41,7 @@ extension Driver {
commandLine.appendFlags("-module-name", moduleName)
return Job(
kind: .repl,
tool: .absolute(try toolchain.getToolPath(.swiftCompiler)),
tool: try toolchain.getToolPath(.swiftCompiler),
commandLine: commandLine,
inputs: [],
outputs: [],
Expand Down
23 changes: 11 additions & 12 deletions Sources/SwiftDriver/Jobs/Toolchain+LinkerSupport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,12 @@ extension Toolchain {
for triple: Triple,
parsedOptions: inout ParsedOptions,
isShared: Bool
) throws -> AbsolutePath {
// FIXME: This almost certainly won't be an absolute path in practice...
let resourceDirBase: AbsolutePath
) throws -> VirtualPath {
let resourceDirBase: VirtualPath
if let resourceDir = parsedOptions.getLastArgument(.resourceDir) {
resourceDirBase = try AbsolutePath(validating: resourceDir.asSingle)
resourceDirBase = try VirtualPath(path: resourceDir.asSingle)
} else if let sdk = parsedOptions.getLastArgument(.sdk),
let sdkPath = try? AbsolutePath(validating: sdk.asSingle) {
let sdkPath = try? VirtualPath(path: sdk.asSingle) {
resourceDirBase = sdkPath
.appending(components: "usr", "lib",
isShared ? "swift" : "swift_static")
Expand All @@ -38,15 +37,15 @@ extension Toolchain {
return resourceDirBase.appending(components: triple.platformName() ?? "")
}

func computeSecondaryResourceDirPath(for triple: Triple, primaryPath: AbsolutePath) -> AbsolutePath? {
func computeSecondaryResourceDirPath(for triple: Triple, primaryPath: VirtualPath) -> VirtualPath? {
guard triple.isMacCatalyst else { return nil }
return primaryPath.parentDirectory.appending(component: "macosx")
}

func clangLibraryPath(
for triple: Triple,
parsedOptions: inout ParsedOptions
) throws -> AbsolutePath {
) throws -> VirtualPath {
return try computeResourceDirPath(for: triple,
parsedOptions: &parsedOptions,
isShared: true)
Expand All @@ -60,7 +59,7 @@ extension Toolchain {
parsedOptions: inout ParsedOptions,
sdkPath: String?,
isShared: Bool
) throws -> [AbsolutePath] {
) throws -> [VirtualPath] {
let resourceDirPath = try computeResourceDirPath(
for: triple,
parsedOptions: &parsedOptions,
Expand All @@ -73,13 +72,13 @@ extension Toolchain {
}

if let path = sdkPath {
let sdkPath = AbsolutePath(path)
let sdkPath = try VirtualPath(path: path)
// If we added the secondary resource dir, we also need the iOSSupport directory.
if secondaryResourceDir != nil {
result.append(sdkPath.appending(components: "System", "iOSSupport", "usr", "lib", "swift"))
}

result.append(sdkPath.appending(RelativePath("usr/lib/swift")))
result.append(sdkPath.appending(components: "usr", "lib", "swift"))
}

return result
Expand Down Expand Up @@ -263,12 +262,12 @@ extension DarwinToolchain {
}
}

func paths(runtimeLibraryPaths: [AbsolutePath]) -> [AbsolutePath] {
func paths(runtimeLibraryPaths: [VirtualPath]) -> [VirtualPath] {
switch self {
case .toolchain:
return runtimeLibraryPaths
case .os:
return [AbsolutePath("/usr/lib/swift")]
return [.absolute(AbsolutePath("/usr/lib/swift"))]
case .none:
return []
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftDriver/Jobs/VerifyDebugInfoJob.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ extension Driver {

return Job(
kind: .verifyDebugInfo,
tool: .absolute(try toolchain.getToolPath(.dwarfdump)),
tool: try toolchain.getToolPath(.dwarfdump),
commandLine: commandLine,
displayInputs: [],
inputs: inputs,
Expand Down
22 changes: 11 additions & 11 deletions Sources/SwiftDriver/Toolchains/DarwinToolchain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ public final class DarwinToolchain: Toolchain {
public let env: [String: String]

/// Doubles as path cache and point for overriding normal lookup
private var toolPaths = [Tool: AbsolutePath]()
private var toolPaths = [Tool: VirtualPath]()

public init(env: [String: String]) {
self.env = env
}

/// Retrieve the absolute path for a given tool.
public func getToolPath(_ tool: Tool) throws -> AbsolutePath {
public func getToolPath(_ tool: Tool) throws -> VirtualPath {
// Check the cache
if let toolPath = toolPaths[tool] {
return toolPath
Expand All @@ -36,7 +36,7 @@ public final class DarwinToolchain: Toolchain {
return path
}

private func lookupToolPath(_ tool: Tool) throws -> AbsolutePath {
private func lookupToolPath(_ tool: Tool) throws -> VirtualPath {
switch tool {
case .swiftCompiler:
return try lookup(executable: "swift")
Expand All @@ -55,7 +55,7 @@ public final class DarwinToolchain: Toolchain {
arguments: ["xcrun", "-toolchain", "default", "-f", "clang"],
environment: env
).spm_chomp()
return AbsolutePath(result)
return .absolute(AbsolutePath(result))
case .swiftAutolinkExtract:
return try lookup(executable: "swift-autolink-extract")
case .lldb:
Expand All @@ -67,7 +67,7 @@ public final class DarwinToolchain: Toolchain {
}
}

public func overrideToolPath(_ tool: Tool, path: AbsolutePath) {
public func overrideToolPath(_ tool: Tool, path: VirtualPath) {
toolPaths[tool] = path
}

Expand All @@ -85,10 +85,10 @@ public final class DarwinToolchain: Toolchain {
sdk.appending(RelativePath("usr/lib/swift"))
}

public var resourcesDirectory: Result<AbsolutePath, Swift.Error> {
public var resourcesDirectory: Result<VirtualPath, Swift.Error> {
// FIXME: This will need to take -resource-dir and target triple into account.
return Result {
try getToolPath(.swiftCompiler).appending(RelativePath("../../lib/swift/macosx"))
try getToolPath(.swiftCompiler).appending(components: "..", "..", "lib", "swift", "macosx")
}
}

Expand All @@ -100,16 +100,16 @@ public final class DarwinToolchain: Toolchain {
}
}

public var compatibility50: Result<AbsolutePath, Error> {
public var compatibility50: Result<VirtualPath, Error> {
resourcesDirectory.map{ $0.appending(component: "libswiftCompatibility50.a") }
}

public var compatibilityDynamicReplacements: Result<AbsolutePath, Error> {
public var compatibilityDynamicReplacements: Result<VirtualPath, Error> {
resourcesDirectory.map{ $0.appending(component: "libswiftCompatibilityDynamicReplacements.a") }
}

public var clangRT: Result<AbsolutePath, Error> {
resourcesDirectory.map{ $0.appending(RelativePath("../clang/lib/darwin/libclang_rt.osx.a")) }
public var clangRT: Result<VirtualPath, Error> {
resourcesDirectory.map{ $0.appending(components: "..", "clang", "lib", "darwin", "libclang_rt.osx.a") }
}

public func defaultSDKPath() throws -> AbsolutePath? {
Expand Down
8 changes: 4 additions & 4 deletions Sources/SwiftDriver/Toolchains/GenericUnixToolchain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public final class GenericUnixToolchain: Toolchain {
public let env: [String: String]

/// Doubles as path cache and point for overriding normal lookup
private var toolPaths = [Tool: AbsolutePath]()
private var toolPaths = [Tool: VirtualPath]()

public init(env: [String: String]) {
self.env = env
Expand All @@ -31,7 +31,7 @@ public final class GenericUnixToolchain: Toolchain {
}

/// Retrieve the absolute path for a given tool.
public func getToolPath(_ tool: Tool) throws -> AbsolutePath {
public func getToolPath(_ tool: Tool) throws -> VirtualPath {
// Check the cache
if let toolPath = toolPaths[tool] {
return toolPath
Expand All @@ -42,7 +42,7 @@ public final class GenericUnixToolchain: Toolchain {
return path
}

private func lookupToolPath(_ tool: Tool) throws -> AbsolutePath {
private func lookupToolPath(_ tool: Tool) throws -> VirtualPath {
switch tool {
case .swiftCompiler:
return try lookup(executable: "swift")
Expand All @@ -66,7 +66,7 @@ public final class GenericUnixToolchain: Toolchain {
}
}

public func overrideToolPath(_ tool: Tool, path: AbsolutePath) {
public func overrideToolPath(_ tool: Tool, path: VirtualPath) {
toolPaths[tool] = path
}

Expand Down
Loading