Skip to content

Separate toolchain discovery from build settings #143

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
Aug 15, 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
3 changes: 3 additions & 0 deletions Sources/SKCore/BuildSystem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,8 @@ public protocol BuildSystem {
/// Returns the settings for the given url and language mode, if known.
func settings(for: URL, _ language: Language) -> FileBuildSettings?

/// Returns the toolchain to use to compile this file
func toolchain(for: URL, _ language: Language) -> Toolchain?

// TODO: notifications when settings change.
}
4 changes: 4 additions & 0 deletions Sources/SKCore/BuildSystemList.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,8 @@ extension BuildSystemList: BuildSystem {
}
return nil
}

public func toolchain(for url: URL, _ language: Language) -> Toolchain? {
return providers.first?.toolchain(for: url, language)
}
}
3 changes: 2 additions & 1 deletion Sources/SKCore/CompilationDatabaseBuildSystem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,13 @@ extension CompilationDatabaseBuildSystem: BuildSystem {
guard let db = database(for: url),
let cmd = db[url].first else { return nil }
return FileBuildSettings(
preferredToolchain: nil, // FIXME: infer from path
compilerArguments: Array(cmd.commandLine.dropFirst()),
workingDirectory: cmd.directory
)
}

public func toolchain(for: URL, _ language: Language) -> Toolchain? { return nil }

func database(for url: URL) -> CompilationDatabase? {
if let path = try? AbsolutePath(validating: url.path) {
return database(for: path)
Expand Down
6 changes: 4 additions & 2 deletions Sources/SKCore/FallbackBuildSystem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ public final class FallbackBuildSystem: BuildSystem {
}
}

public func toolchain(for: URL, _ language: Language) -> Toolchain? { return nil }

func settingsSwift(_ path: AbsolutePath) -> FileBuildSettings {
var args: [String] = []
if let sdkpath = sdkpath {
Expand All @@ -59,7 +61,7 @@ public final class FallbackBuildSystem: BuildSystem {
]
}
args.append(path.pathString)
return FileBuildSettings(preferredToolchain: nil, compilerArguments: args)
return FileBuildSettings(compilerArguments: args)
}

func settingsClang(_ path: AbsolutePath, _ language: Language) -> FileBuildSettings {
Expand All @@ -71,6 +73,6 @@ public final class FallbackBuildSystem: BuildSystem {
]
}
args.append(path.pathString)
return FileBuildSettings(preferredToolchain: nil, compilerArguments: args)
return FileBuildSettings(compilerArguments: args)
}
}
10 changes: 2 additions & 8 deletions Sources/SKCore/FileBuildSettings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,20 @@

/// Build settings for a single file.
///
/// Encapsulates all the settings needed to compile a single file, including the compiler arguments,
/// working directory, and preferred toolchain if any. FileBuildSettings are typically the result
/// of a BuildSystem query.
/// Encapsulates all the settings needed to compile a single file, including the compiler arguments
/// and working directory. FileBuildSettings are typically the result of a BuildSystem query.
public struct FileBuildSettings {

/// The Toolchain that is preferred for compiling this file, if any.
public var preferredToolchain: Toolchain? = nil

/// The compiler arguments to use for this file.
public var compilerArguments: [String]

/// The working directory to resolve any relative paths in `compilerArguments`.
public var workingDirectory: String? = nil

public init(
preferredToolchain: Toolchain? = nil,
compilerArguments: [String],
workingDirectory: String? = nil)
{
self.preferredToolchain = preferredToolchain
self.compilerArguments = compilerArguments
self.workingDirectory = workingDirectory
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/SKSupport/Logging.swift
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public final class Logger {
}

public func setLogLevel(environmentVariable: String) {
if let string = Process.env[environmentVariable],
if let string = ProcessEnv.vars[environmentVariable],
let level = try? LogLevel(argument: string)
{
currentLevel = level
Expand Down
11 changes: 5 additions & 6 deletions Sources/SKSwiftPMWorkspace/SwiftPMWorkspace.swift
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,10 @@ extension SwiftPMWorkspace: BuildSystem {
return nil
}

public func toolchain(for: LanguageServerProtocol.URL, _ language: Language) -> SKCore.Toolchain? {
return nil
}

/// Returns the resolved target description for the given file, if one is known.
func targetDescription(for file: AbsolutePath) -> TargetBuildDescription? {
if let td = fileToTarget[file] {
Expand Down Expand Up @@ -257,10 +261,7 @@ extension SwiftPMWorkspace {
func impl(_ path: AbsolutePath) -> FileBuildSettings? {
for package in packageGraph.packages where path == package.manifest.path {
let compilerArgs = workspace.interpreterFlags(for: package.path) + [path.pathString]
return FileBuildSettings(
preferredToolchain: nil,
compilerArguments: compilerArgs
)
return FileBuildSettings(compilerArguments: compilerArgs)
}
return nil
}
Expand Down Expand Up @@ -319,7 +320,6 @@ extension SwiftPMWorkspace {
args += td.compileArguments()

return FileBuildSettings(
preferredToolchain: nil,
compilerArguments: args,
workingDirectory: workspacePath.pathString)
}
Expand Down Expand Up @@ -382,7 +382,6 @@ extension SwiftPMWorkspace {
}

return FileBuildSettings(
preferredToolchain: nil,
compilerArguments: args,
workingDirectory: workspacePath.pathString)
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/SourceKit/SourceKitServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public final class SourceKitServer: LanguageServer {
}

func toolchain(for url: URL, _ language: Language) -> Toolchain? {
if let toolchain = workspace?.buildSettings.settings(for: url, language)?.preferredToolchain {
if let toolchain = workspace?.buildSettings.toolchain(for: url, language) {
return toolchain
}

Expand Down
2 changes: 0 additions & 2 deletions Tests/SKCoreTests/FallbackBuildSystemTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ final class FallbackBuildSystemTests: XCTestCase {
XCTAssertNil(bs.indexDatabasePath)

let settings = bs.settings(for: source.asURL, .swift)!
XCTAssertNil(settings.preferredToolchain)
XCTAssertNil(settings.workingDirectory)

let args = settings.compilerArguments
Expand All @@ -53,7 +52,6 @@ final class FallbackBuildSystemTests: XCTestCase {
bs.sdkpath = sdk

let settings = bs.settings(for: source.asURL, .cpp)!
XCTAssertNil(settings.preferredToolchain)
XCTAssertNil(settings.workingDirectory)

let args = settings.compilerArguments
Expand Down