Skip to content

Commit bf4c8bc

Browse files
authored
Merge pull request swiftlang#31 from benlangmuir/build-system-cleanup1
[build-system] Various minor refactoring and cleanups
2 parents 676a81c + c17ae69 commit bf4c8bc

13 files changed

+159
-144
lines changed

Sources/SKCore/BuildSystem.swift

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,35 +11,26 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
import LanguageServerProtocol
14+
import Basic
1415

15-
/// Provider of build settings.
16-
public protocol BuildSettingsProvider {
16+
/// Provider of FileBuildSettings and other build-related information.
17+
///
18+
/// The primary role of the build system is to answer queries for FileBuildSettings and (TODO) to
19+
/// notify clients when they change. The BuildSystem is also the source of related informatino,
20+
/// such as where the index datastore is located.
21+
///
22+
/// For example, a SwiftPMWorkspace provides compiler arguments for the files contained in a
23+
/// SwiftPM package root directory.
24+
public protocol BuildSystem {
1725

18-
/// Returns the settings for the given url and language mode, if known.
19-
func settings(for: URL, language: Language) -> FileBuildSettings?
20-
21-
// TODO: notifications when settings change.
22-
}
26+
/// The path to the raw index store data, if any.
27+
var indexStorePath: AbsolutePath? { get }
2328

24-
/// Build settings for a single file.
25-
public struct FileBuildSettings {
29+
/// The path to put the index database, if any.
30+
var indexDatabasePath: AbsolutePath? { get }
2631

27-
/// The identifier of the toolchain that is preferred for compiling this file, if any.
28-
public var preferredToolchain: String? = nil
29-
30-
/// The compiler arguments to use for this file.
31-
public var compilerArguments: [String]
32-
33-
/// The working directory to resolve any relative paths in `compilerArguments`.
34-
public var workingDirectory: String? = nil
32+
/// Returns the settings for the given url and language mode, if known.
33+
func settings(for: URL, _ language: Language) -> FileBuildSettings?
3534

36-
public init(
37-
preferredToolchain: String? = nil,
38-
compilerArguments: [String],
39-
workingDirectory: String? = nil
40-
) {
41-
self.preferredToolchain = preferredToolchain
42-
self.compilerArguments = compilerArguments
43-
self.workingDirectory = workingDirectory
44-
}
35+
// TODO: notifications when settings change.
4536
}

Sources/SKCore/BuildSettingsProviderList.swift renamed to Sources/SKCore/BuildSystemList.swift

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,29 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13+
import Basic
1314
import LanguageServerProtocol
1415

15-
/// Provides build settings from a list of providers in priority order.
16-
public final class BuildSettingsProviderList: BuildSettingsProvider {
16+
/// Provides build settings from a list of build systems in priority order.
17+
public final class BuildSystemList {
1718

18-
/// The build settings providers to try (in order).
19-
public var providers: [BuildSettingsProvider] = [
20-
FallbackBuildSettingsProvider()
19+
/// The build systems to try (in order).
20+
public var providers: [BuildSystem] = [
21+
FallbackBuildSystem()
2122
]
2223

2324
public init() {}
25+
}
26+
27+
extension BuildSystemList: BuildSystem {
28+
29+
public var indexStorePath: AbsolutePath? { return providers.first?.indexStorePath }
30+
31+
public var indexDatabasePath: AbsolutePath? { return providers.first?.indexDatabasePath }
2432

25-
public func settings(for url: URL, language: Language) -> FileBuildSettings? {
33+
public func settings(for url: URL, _ language: Language) -> FileBuildSettings? {
2634
for provider in providers {
27-
if let settings = provider.settings(for: url, language: language) {
35+
if let settings = provider.settings(for: url, language) {
2836
return settings
2937
}
3038
}

Sources/SKCore/CompilationDatabaseBuildSystem.swift

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import LanguageServerProtocol
1818
///
1919
/// Provides build settings from a `CompilationDatabase` found by searching a project. For now, only
2020
/// one compilation database, located at the project root.
21-
public final class CompilationDatabaseBuildSystem: BuildSettingsProvider {
21+
public final class CompilationDatabaseBuildSystem {
2222

2323
/// The compilation database.
2424
var compdb: CompilationDatabase? = nil
@@ -31,8 +31,15 @@ public final class CompilationDatabaseBuildSystem: BuildSettingsProvider {
3131
self.compdb = tryLoadCompilationDatabase(directory: path, fileSystem)
3232
}
3333
}
34+
}
35+
36+
extension CompilationDatabaseBuildSystem: BuildSystem {
37+
38+
// FIXME: derive from the compiler arguments.
39+
public var indexStorePath: AbsolutePath? { return nil }
40+
public var indexDatabasePath: AbsolutePath? { return nil }
3441

35-
public func settings(for url: URL, language: Language) -> FileBuildSettings? {
42+
public func settings(for url: URL, _ language: Language) -> FileBuildSettings? {
3643
guard let db = database(for: url),
3744
let cmd = db[url].first else { return nil }
3845
return FileBuildSettings(

Sources/SKCore/ExternalWorkspace.swift

Lines changed: 0 additions & 29 deletions
This file was deleted.

Sources/SKCore/FallbackBuildSettingsProvider.swift renamed to Sources/SKCore/FallbackBuildSystem.swift

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,34 +14,41 @@ import LanguageServerProtocol
1414
import Basic
1515
import enum Utility.Platform
1616

17-
/// A simple build settings provider suitable as a fallback when accurate settings are unknown.
18-
public final class FallbackBuildSettingsProvider: BuildSettingsProvider {
17+
/// A simple BuildSystem suitable as a fallback when accurate settings are unknown.
18+
public final class FallbackBuildSystem: BuildSystem {
1919

20+
/// The path to the SDK.
2021
lazy var sdkpath: AbsolutePath? = {
21-
if case .darwin? = Platform.currentPlatform {
22-
if let str = try? Process.checkNonZeroExit(args: "/usr/bin/xcrun", "--show-sdk-path", "--sdk", "macosx"), let path = try? AbsolutePath(validating: str.spm_chomp()) {
23-
return path
24-
}
22+
if case .darwin? = Platform.currentPlatform,
23+
let str = try? Process.checkNonZeroExit(
24+
args: "/usr/bin/xcrun", "--show-sdk-path", "--sdk", "macosx"),
25+
let path = try? AbsolutePath(validating: str.spm_chomp())
26+
{
27+
return path
2528
}
2629
return nil
2730
}()
2831

29-
public func settings(for url: URL, language: Language) -> FileBuildSettings? {
32+
public var indexStorePath: AbsolutePath? { return nil }
33+
34+
public var indexDatabasePath: AbsolutePath? { return nil }
35+
36+
public func settings(for url: URL, _ language: Language) -> FileBuildSettings? {
3037
guard let path = try? AbsolutePath(validating: url.path) else {
3138
return nil
3239
}
3340

3441
switch language {
3542
case .swift:
36-
return settingsSwift(path: path)
43+
return settingsSwift(path)
3744
case .c, .cpp, .objective_c, .objective_cpp:
38-
return settingsClang(path: path, language: language)
45+
return settingsClang(path, language)
3946
default:
4047
return nil
4148
}
4249
}
4350

44-
func settingsSwift(path: AbsolutePath) -> FileBuildSettings {
51+
func settingsSwift(_ path: AbsolutePath) -> FileBuildSettings {
4552
var args: [String] = []
4653
if let sdkpath = sdkpath {
4754
args += [
@@ -53,7 +60,7 @@ public final class FallbackBuildSettingsProvider: BuildSettingsProvider {
5360
return FileBuildSettings(preferredToolchain: nil, compilerArguments: args)
5461
}
5562

56-
func settingsClang(path: AbsolutePath, language: Language) -> FileBuildSettings {
63+
func settingsClang(_ path: AbsolutePath, _ language: Language) -> FileBuildSettings {
5764
var args: [String] = []
5865
if let sdkpath = sdkpath {
5966
args += [
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
/// Build settings for a single file.
14+
///
15+
/// Encapsulates all the settings needed to compile a single file, including the compiler arguments,
16+
/// working directory, and preferred toolchain if any. FileBuildSettings are typically the result
17+
/// of a BuildSystem query.
18+
public struct FileBuildSettings {
19+
20+
/// The identifier of the toolchain that is preferred for compiling this file, if any.
21+
public var preferredToolchain: String? = nil
22+
23+
/// The compiler arguments to use for this file.
24+
public var compilerArguments: [String]
25+
26+
/// The working directory to resolve any relative paths in `compilerArguments`.
27+
public var workingDirectory: String? = nil
28+
29+
public init(
30+
preferredToolchain: String? = nil,
31+
compilerArguments: [String],
32+
workingDirectory: String? = nil)
33+
{
34+
self.preferredToolchain = preferredToolchain
35+
self.compilerArguments = compilerArguments
36+
self.workingDirectory = workingDirectory
37+
}
38+
}

Sources/SKSwiftPMWorkspace/SwiftPMWorkspace.swift

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public final class SwiftPMWorkspace {
4040
var fileToTarget: [AbsolutePath: TargetBuildDescription] = [:]
4141
var sourceDirToTarget: [AbsolutePath: TargetBuildDescription] = [:]
4242

43-
/// Creates a `BuildSettingsProvider` using the Swift Package Manager, if this workspace is part of a package.
43+
/// Creates a `BuildSystem` using the Swift Package Manager, if this workspace is part of a package.
4444
///
4545
/// - returns: nil if `workspacePath` is not part of a package or there is an error.
4646
public convenience init?(url: LanguageServerProtocol.URL, toolchainRegistry: ToolchainRegistry) {
@@ -52,7 +52,7 @@ public final class SwiftPMWorkspace {
5252
}
5353
}
5454

55-
/// Creates a `BuildSettingsProvider` using the Swift Package Manager, if this workspace is part of a package.
55+
/// Creates a `BuildSystem` using the Swift Package Manager, if this workspace is part of a package.
5656
///
5757
/// - returns: nil if `workspacePath` is not part of a package.
5858
/// - throws: If there is an error loading the package.
@@ -163,9 +163,7 @@ public final class SwiftPMWorkspace {
163163
}
164164
}
165165

166-
extension SwiftPMWorkspace: ExternalWorkspace, BuildSettingsProvider {
167-
168-
public var buildSystem: BuildSettingsProvider { return self }
166+
extension SwiftPMWorkspace: BuildSystem {
169167

170168
public var buildPath: AbsolutePath {
171169
return buildParameters.buildPath
@@ -179,21 +177,24 @@ extension SwiftPMWorkspace: ExternalWorkspace, BuildSettingsProvider {
179177
return buildPath.appending(components: "index", "db")
180178
}
181179

182-
public func settings(for url: LanguageServerProtocol.URL, language: Language) -> FileBuildSettings? {
180+
public func settings(
181+
for url: LanguageServerProtocol.URL,
182+
_ language: Language) -> FileBuildSettings?
183+
{
183184
guard let path = try? AbsolutePath(validating: url.path) else {
184185
return nil
185186
}
186187

187188
if let td = self.fileToTarget[path] {
188-
return settings(for: path, language: language, targetDescription: td)
189+
return settings(for: path, language, td)
189190
}
190191

191192
if path.basename == "Package.swift" {
192193
return packageDescriptionSettings(path)
193194
}
194195

195196
if path.extension == "h" {
196-
return settings(forHeader: path, language: language)
197+
return settings(forHeader: path, language)
197198
}
198199

199200
return nil
@@ -206,8 +207,8 @@ extension SwiftPMWorkspace {
206207

207208
public func settings(
208209
for path: AbsolutePath,
209-
language: Language,
210-
targetDescription td: TargetBuildDescription
210+
_ language: Language,
211+
_ td: TargetBuildDescription
211212
) -> FileBuildSettings? {
212213

213214
let buildPath = self.buildPath
@@ -315,11 +316,11 @@ extension SwiftPMWorkspace {
315316
return nil
316317
}
317318

318-
public func settings(forHeader path: AbsolutePath, language: Language) -> FileBuildSettings? {
319+
public func settings(forHeader path: AbsolutePath, _ language: Language) -> FileBuildSettings? {
319320
var dir = path.parentDirectory
320321
while !dir.isRoot {
321322
if let td = sourceDirToTarget[dir] {
322-
return settings(for: path, language: language, targetDescription: td)
323+
return settings(for: path, language, td)
323324
}
324325
dir = dir.parentDirectory
325326
}

0 commit comments

Comments
 (0)