Skip to content

[sourcekit-lsp] Prefer containing toolchain if applicable #139

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 4 commits into from
Aug 10, 2019
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
37 changes: 23 additions & 14 deletions Sources/SKCore/Toolchain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -93,23 +93,14 @@ extension Toolchain {
/// * `path/bin`, `path/lib`
/// * `path/usr/bin`, `path/usr/lib`
///
/// If `path` has an ".xctoolchain" extension, we try to read an Info.plist file to provide the
/// If `path` contains an ".xctoolchain", we try to read an Info.plist file to provide the
/// toolchain identifier, etc. Otherwise this information is derived from the path.
convenience public init?(_ path: AbsolutePath, _ fileSystem: FileSystem = localFileSystem) {
if path.extension == "xctoolchain",
let infoPlist = orLog("", {
try XCToolchainPlist(fromDirectory: path, fileSystem) })
{
self.init(
identifier: infoPlist.identifier,
displayName:
infoPlist.displayName ?? String(path.basename.dropLast(path.suffix?.count ?? 0)),
path: path)
if let (infoPlist, xctoolchainPath) = containingXCToolchain(path, fileSystem) {
let displayName = infoPlist.displayName ?? xctoolchainPath.basenameWithoutExt
self.init(identifier: infoPlist.identifier, displayName: displayName, path: xctoolchainPath)
} else {
self.init(
identifier: path.pathString,
displayName: path.basename,
path: path)
self.init(identifier: path.pathString, displayName: path.basename, path: path)
}

if !searchForTools(path, fileSystem) {
Expand Down Expand Up @@ -175,3 +166,21 @@ extension Toolchain {
return foundAny
}
}

/// Find a containing xctoolchain with plist, if available.
func containingXCToolchain(
_ path: AbsolutePath,
_ fileSystem: FileSystem) -> (XCToolchainPlist, AbsolutePath)?
{
var path = path
while !path.isRoot {
if path.extension == "xctoolchain" {
if let infoPlist = orLog("", { try XCToolchainPlist(fromDirectory: path, fileSystem) }) {
return (infoPlist, path)
}
return nil
}
path = path.parentDirectory
}
return nil
}
21 changes: 11 additions & 10 deletions Sources/SKCore/ToolchainRegistry.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ extension ToolchainRegistry {
///
/// If called with the default values, creates a toolchain registry that searches:
/// * env SOURCEKIT_TOOLCHAIN_PATH <-- will override default toolchain
/// * installPath <-- will override default toolchain
/// * (Darwin) The currently selected Xcode
/// * (Darwin) [~]/Library/Developer/Toolchains
/// * env SOURCEKIT_PATH, PATH
Expand All @@ -77,9 +78,9 @@ extension ToolchainRegistry {
/// let tr = ToolchainRegistry()
/// tr.scanForToolchains()
/// ```
public convenience init(_ fileSystem: FileSystem) {
public convenience init(installPath: AbsolutePath? = nil, _ fileSystem: FileSystem) {
self.init()
scanForToolchains(fileSystem)
scanForToolchains(installPath: installPath, fileSystem)
}
}

Expand Down Expand Up @@ -222,18 +223,12 @@ extension ToolchainRegistry {
///
/// If called with the default values, creates a toolchain registry that searches:
/// * env SOURCEKIT_TOOLCHAIN_PATH <-- will override default toolchain
/// * installPath <-- will override default toolchain
/// * (Darwin) The currently selected Xcode
/// * (Darwin) [~]/Library/Developer/Toolchains
/// * env SOURCEKIT_PATH, PATH
///
/// This is equivalent to
/// ```
/// tr.scanForToolchains(environmentVariables: environmentVariables, setDefault: true)
/// xcodes.forEach { tr.scanForToolchains(xcode: $0) }
/// xctoolchainSearchPaths.forEach { tr.scanForToolchains(xctoolchainSearchPath: $0) }
/// tr.scanForToolchains(pathVariables: pathVariables)
/// ```
public func scanForToolchains(
installPath: AbsolutePath? = nil,
environmentVariables: [String] = ["SOURCEKIT_TOOLCHAIN_PATH"],
xcodes: [AbsolutePath] = [currentXcodeDeveloperPath].compactMap({$0}),
xctoolchainSearchPaths: [AbsolutePath] = [
Expand All @@ -245,6 +240,12 @@ extension ToolchainRegistry {
{
queue.sync {
_scanForToolchains(environmentVariables: environmentVariables, setDefault: true, fileSystem)
if let installPath = installPath,
let toolchain = try? _registerToolchain(installPath, fileSystem),
_default == nil
{
_default = toolchain
}
xcodes.forEach { _scanForToolchains(xcode: $0, fileSystem) }
xctoolchainSearchPaths.forEach { _scanForToolchains(xctoolchainSearchPath: $0, fileSystem) }
_scanForToolchains(pathVariables: pathVariables, fileSystem)
Expand Down
2 changes: 1 addition & 1 deletion Sources/SKSwiftPMWorkspace/SwiftPMWorkspace.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public final class SwiftPMWorkspace {

self.packageRoot = resolveSymlinks(packageRoot)

guard let destinationToolchainBinDir = toolchainRegistry.default?.path?.appending(components: "usr", "bin") else {
guard let destinationToolchainBinDir = toolchainRegistry.default?.swiftc?.parentDirectory else {
throw Error.cannotDetermineHostToolchain
}

Expand Down
3 changes: 3 additions & 0 deletions Sources/sourcekit-lsp/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ do {
exit(1)
}

let installPath = AbsolutePath(Bundle.main.bundlePath)
ToolchainRegistry.shared = ToolchainRegistry(installPath: installPath, localFileSystem)

let server = SourceKitServer(client: clientConnection, buildSetup: buildSetup, onExit: {
clientConnection.close()
})
Expand Down
42 changes: 42 additions & 0 deletions Tests/SKCoreTests/ToolchainRegistryTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,18 @@ final class ToolchainRegistryTests: XCTestCase {
XCTAssertNotNil(tc)
XCTAssertEqual(tc?.identifier, "org.fake.explicit")

let tcBin = Toolchain(path.appending(components: "usr", "bin"), fs)
XCTAssertNotNil(tcBin)
XCTAssertEqual(tc?.identifier, tcBin?.identifier)
XCTAssertEqual(tc?.path, tcBin?.path)
XCTAssertEqual(tc?.displayName, tcBin?.displayName)


let trInstall = ToolchainRegistry()
trInstall.scanForToolchains(installPath: path.appending(components: "usr", "bin"), environmentVariables: [], xcodes: [], xctoolchainSearchPaths: [], pathVariables: [], fs)
XCTAssertEqual(trInstall.default?.identifier, "org.fake.explicit")
XCTAssertEqual(trInstall.default?.path, path)

let overrideReg = ToolchainRegistry(fs)
overrideReg.darwinToolchainOverride = "org.fake.global.B"
XCTAssertEqual(overrideReg.darwinToolchainIdentifier, "org.fake.global.B")
Expand Down Expand Up @@ -412,6 +424,36 @@ final class ToolchainRegistryTests: XCTestCase {
XCTAssert(toolchains[0] === xcodeA)
XCTAssert(toolchains[1] === xcodeB)
}

func testInstallPath() {
let fs = InMemoryFileSystem()
makeToolchain(binPath: AbsolutePath("/t1/bin"), fs, sourcekitd: true)

let trEmpty = ToolchainRegistry(installPath: nil, fs)
XCTAssertNil(trEmpty.default)

let tr1 = ToolchainRegistry(installPath: AbsolutePath("/t1/bin"), fs)
XCTAssertEqual(tr1.default?.path, AbsolutePath("/t1/bin"))
XCTAssertNotNil(tr1.default?.sourcekitd)

let tr2 = ToolchainRegistry(installPath: AbsolutePath("/t2/bin"), fs)
XCTAssertNil(tr2.default)
}

func testInstallPathVsEnv() {
let fs = InMemoryFileSystem()
makeToolchain(binPath: AbsolutePath("/t1/bin"), fs, sourcekitd: true)
makeToolchain(binPath: AbsolutePath("/t2/bin"), fs, sourcekitd: true)

try! ProcessEnv.setVar("TEST_SOURCEKIT_TOOLCHAIN_PATH_1", value: "/t2/bin")

let tr = ToolchainRegistry()
tr.scanForToolchains(installPath: AbsolutePath("/t1/bin"), environmentVariables: ["TEST_SOURCEKIT_TOOLCHAIN_PATH_1"], fs)
XCTAssertEqual(tr.toolchains.count, 2)

// Env variable wins.
XCTAssertEqual(tr.default?.path, AbsolutePath("/t2/bin"))
}
}

#if os(macOS)
Expand Down