Skip to content

Commit 76d7a2c

Browse files
authored
Merge pull request swiftlang#65 from krzyzanowskim/cli-arguments
Command line arguments
2 parents 8017a63 + 17e82ff commit 76d7a2c

File tree

9 files changed

+247
-36
lines changed

9 files changed

+247
-36
lines changed

Sources/SKCore/BuildSetup.swift

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2019 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+
import Foundation
14+
import Utility
15+
import SKSupport
16+
17+
/// Build configuration
18+
public struct BuildSetup {
19+
20+
/// Default configuration
21+
public static let `default` = BuildSetup(configuration: .debug,
22+
path: "./.build",
23+
flags: BuildFlags())
24+
25+
/// Build configuration
26+
public let configuration: BuildConfiguration
27+
28+
/// Build artefacts directory path
29+
public let path: String
30+
31+
/// Additional build flags
32+
public let flags: BuildFlags
33+
34+
public init(configuration: BuildConfiguration, path: String, flags: BuildFlags) {
35+
self.configuration = configuration
36+
self.path = path
37+
self.flags = flags
38+
}
39+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2019 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+
import Foundation
14+
import Utility
15+
16+
public enum BuildConfiguration: String {
17+
case debug
18+
case release
19+
}
20+
21+
extension BuildConfiguration: ArgumentKind {
22+
public init(argument: String) throws {
23+
self = BuildConfiguration(rawValue: argument) ?? .debug
24+
}
25+
26+
/// Type of shell completion to provide for this argument.
27+
public static var completion: ShellCompletion {
28+
return ShellCompletion.none
29+
}
30+
}

Sources/SKSupport/Logging.swift

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
import Basic
1414
import Foundation
15+
import Utility
1516

1617
#if canImport(os)
1718
import os // os_log
@@ -26,6 +27,45 @@ public enum LogLevel: Int, Equatable {
2627
public static let `default`: LogLevel = .info
2728
}
2829

30+
extension LogLevel: CustomStringConvertible {
31+
32+
public var description: String {
33+
switch self {
34+
case .error:
35+
return "error"
36+
case .warning:
37+
return "warning"
38+
case .info:
39+
return "info"
40+
case .debug:
41+
return "debug"
42+
}
43+
}
44+
}
45+
46+
extension LogLevel: ArgumentKind {
47+
48+
public init(argument: String) throws {
49+
switch argument {
50+
case "error":
51+
self = .error
52+
case "warning":
53+
self = .warning
54+
case "info":
55+
self = .info
56+
case "debug":
57+
self = .debug
58+
default:
59+
self = LogLevel.default
60+
}
61+
}
62+
63+
/// Type of shell completion to provide for this argument.
64+
public static var completion: ShellCompletion {
65+
return ShellCompletion.none
66+
}
67+
}
68+
2969
/// Log the given message.
3070
///
3171
/// If `level >= Logger.shared.currentLevel`, it will be emitted. However, the converse is not necessarily true: on platforms that provide `os_log`, the message may be emitted by `os_log` according to its own rules about log level.

Sources/SKSwiftPMWorkspace/SwiftPMWorkspace.swift

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ public final class SwiftPMWorkspace {
5858
public init(
5959
workspacePath: AbsolutePath,
6060
toolchainRegistry: ToolchainRegistry,
61-
fileSystem: FileSystem = localFileSystem) throws
61+
fileSystem: FileSystem = localFileSystem,
62+
buildSetup: BuildSetup) throws
6263
{
6364
self.workspacePath = workspacePath
6465
self.toolchainRegistry = toolchainRegistry
@@ -112,7 +113,13 @@ public final class SwiftPMWorkspace {
112113
swiftPMToolchain.extraSwiftCFlags = extraSwiftFlags
113114
swiftPMToolchain.extraCPPFlags = extraClangFlags
114115

115-
let buildPath = packageRoot.appending(component: ".build")
116+
117+
let buildPath: AbsolutePath
118+
if let absoluteBuildPath = try? AbsolutePath(validating: buildSetup.path) {
119+
buildPath = absoluteBuildPath
120+
} else {
121+
buildPath = packageRoot.appending(component: buildSetup.path)
122+
}
116123

117124
self.workspace = Workspace(
118125
dataPath: buildPath,
@@ -125,12 +132,19 @@ public final class SwiftPMWorkspace {
125132

126133
let triple = Triple.hostTriple
127134

128-
// FIXME: make these configurable
135+
let swiftPMConfiguration: PackageModel.BuildConfiguration
136+
switch buildSetup.configuration {
137+
case .debug:
138+
swiftPMConfiguration = .debug
139+
case .release:
140+
swiftPMConfiguration = .release
141+
}
142+
129143
self.buildParameters = BuildParameters(
130144
dataPath: buildPath.appending(component: triple.tripleString),
131-
configuration: .debug,
145+
configuration: swiftPMConfiguration,
132146
toolchain: swiftPMToolchain,
133-
flags: BuildFlags())
147+
flags: buildSetup.flags)
134148

135149
self.packageGraph = PackageGraph(rootPackages: [])
136150

@@ -140,13 +154,16 @@ public final class SwiftPMWorkspace {
140154
/// Creates a build system using the Swift Package Manager, if this workspace is a package.
141155
///
142156
/// - Returns: nil if `workspacePath` is not part of a package or there is an error.
143-
public convenience init?(url: LanguageServerProtocol.URL, toolchainRegistry: ToolchainRegistry) {
157+
public convenience init?(url: LanguageServerProtocol.URL,
158+
toolchainRegistry: ToolchainRegistry,
159+
buildSetup: BuildSetup)
160+
{
144161
do {
145162
try self.init(
146163
workspacePath: try AbsolutePath(validating: url.path),
147164
toolchainRegistry: toolchainRegistry,
148-
fileSystem: localFileSystem)
149-
165+
fileSystem: localFileSystem,
166+
buildSetup: buildSetup)
150167
} catch Error.noManifest(let path) {
151168
log("could not find manifest, or not a SwiftPM package: \(path.asString)", level: .warning)
152169
return nil

Sources/SKTestSupport/TestServer.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
import SKSupport
14+
import SKCore
15+
import Utility
1416
import LanguageServerProtocol
1517
import LanguageServerProtocolJSONRPC
1618
import SourceKit
@@ -32,6 +34,10 @@ public struct TestSourceKitServer {
3234
serverConnection: JSONRPCConection)
3335
}
3436

37+
public static let buildSetup: BuildSetup = BuildSetup(configuration: .debug,
38+
path: ".build",
39+
flags: BuildFlags())
40+
3541
public let client: TestClient
3642
let connImpl: ConnectionImpl
3743

@@ -46,7 +52,7 @@ public struct TestSourceKitServer {
4652
let clientConnection = LocalConnection()
4753
let serverConnection = LocalConnection()
4854
client = TestClient(server: serverConnection)
49-
server = SourceKitServer(client: clientConnection, onExit: {
55+
server = SourceKitServer(client: clientConnection, buildSetup: TestSourceKitServer.buildSetup, onExit: {
5056
clientConnection.close()
5157
})
5258

@@ -74,7 +80,7 @@ public struct TestSourceKitServer {
7480
)
7581

7682
client = TestClient(server: clientConnection)
77-
server = SourceKitServer(client: serverConnection, onExit: {
83+
server = SourceKitServer(client: serverConnection, buildSetup: TestSourceKitServer.buildSetup, onExit: {
7884
serverConnection.close()
7985
})
8086

Sources/SourceKit/SourceKitServer.swift

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,13 @@ import SKCore
1515
import SKSupport
1616
import IndexStoreDB
1717
import Basic
18+
import Utility
1819
import Dispatch
20+
import Foundation
1921
import SPMLibc
2022

23+
public typealias URL = Foundation.URL
24+
2125
/// The SourceKit language server.
2226
///
2327
/// This is the client-facing language server implementation, providing indexing, multiple-toolchain
@@ -30,6 +34,8 @@ public final class SourceKitServer: LanguageServer {
3034
var language: Language
3135
}
3236

37+
let buildSetup: BuildSetup
38+
3339
let toolchainRegistry: ToolchainRegistry
3440

3541
var languageService: [LanguageServiceKey: Connection] = [:]
@@ -41,10 +47,11 @@ public final class SourceKitServer: LanguageServer {
4147
let onExit: () -> Void
4248

4349
/// Creates a language server for the given client.
44-
public init(client: Connection, fileSystem: FileSystem = localFileSystem, onExit: @escaping () -> Void = {}) {
50+
public init(client: Connection, fileSystem: FileSystem = localFileSystem, buildSetup: BuildSetup, onExit: @escaping () -> Void = {}) {
4551

4652
self.fs = fileSystem
4753
self.toolchainRegistry = ToolchainRegistry.shared
54+
self.buildSetup = buildSetup
4855
self.onExit = onExit
4956

5057
super.init(client: client)
@@ -212,14 +219,14 @@ extension SourceKitServer {
212219
self.workspace = try? Workspace(
213220
url: url,
214221
clientCapabilities: req.params.capabilities,
215-
toolchainRegistry: toolchainRegistry
216-
)
222+
toolchainRegistry: self.toolchainRegistry,
223+
buildSetup: self.buildSetup)
217224
} else if let path = req.params.rootPath {
218225
self.workspace = try? Workspace(
219226
url: URL(fileURLWithPath: path),
220227
clientCapabilities: req.params.capabilities,
221-
toolchainRegistry: toolchainRegistry
222-
)
228+
toolchainRegistry: self.toolchainRegistry,
229+
buildSetup: self.buildSetup)
223230
}
224231

225232
if self.workspace == nil {
@@ -229,7 +236,8 @@ extension SourceKitServer {
229236
rootPath: nil,
230237
clientCapabilities: req.params.capabilities,
231238
buildSettings: BuildSystemList(),
232-
index: nil
239+
index: nil,
240+
buildSetup: self.buildSetup
233241
)
234242
}
235243

Sources/SourceKit/Workspace.swift

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import SKCore
1515
import SKSupport
1616
import IndexStoreDB
1717
import Basic
18+
import Utility
1819
import SKSwiftPMWorkspace
1920

2021
/// Represents the configuration and sate of a project or combination of projects being worked on
@@ -34,6 +35,9 @@ public final class Workspace {
3435
/// The build settings provider to use for documents in this workspace.
3536
public let buildSettings: BuildSystem
3637

38+
/// Build setup
39+
public let buildSetup: BuildSetup
40+
3741
/// The source code index, if available.
3842
public var index: IndexStoreDB? = nil
3943

@@ -47,12 +51,14 @@ public final class Workspace {
4751
rootPath: AbsolutePath?,
4852
clientCapabilities: ClientCapabilities,
4953
buildSettings: BuildSystem,
50-
index: IndexStoreDB?)
54+
index: IndexStoreDB?,
55+
buildSetup: BuildSetup)
5156
{
5257
self.rootPath = rootPath
5358
self.clientCapabilities = clientCapabilities
5459
self.buildSettings = buildSettings
5560
self.index = index
61+
self.buildSetup = buildSetup
5662
}
5763

5864
/// Creates a workspace for a given root `URL`, inferring the `ExternalWorkspace` if possible.
@@ -64,17 +70,23 @@ public final class Workspace {
6470
public init(
6571
url: URL,
6672
clientCapabilities: ClientCapabilities,
67-
toolchainRegistry: ToolchainRegistry
73+
toolchainRegistry: ToolchainRegistry,
74+
buildSetup: BuildSetup
6875
) throws {
6976

77+
self.buildSetup = buildSetup
78+
7079
self.rootPath = try AbsolutePath(validating: url.path)
7180
self.clientCapabilities = clientCapabilities
7281
let settings = BuildSystemList()
7382
self.buildSettings = settings
7483

7584
settings.providers.insert(CompilationDatabaseBuildSystem(projectRoot: rootPath), at: 0)
7685

77-
if let swiftpm = SwiftPMWorkspace(url: url, toolchainRegistry: toolchainRegistry) {
86+
if let swiftpm = SwiftPMWorkspace(url: url,
87+
toolchainRegistry: toolchainRegistry,
88+
buildSetup: buildSetup
89+
) {
7890
settings.providers.insert(swiftpm, at: 0)
7991
}
8092

0 commit comments

Comments
 (0)