Skip to content

Commit fef589a

Browse files
committed
Add support for loading supported features from share/swift/features.json
1 parent 0c88dd3 commit fef589a

File tree

4 files changed

+34
-3
lines changed

4 files changed

+34
-3
lines changed

Sources/SwiftDriver/Driver/Driver.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,9 @@ public struct Driver {
330330
/// A collection of all the flags the selected toolchain's `swift-frontend` supports
331331
public let supportedFrontendFlags: Set<String>
332332

333+
/// A collection of all the features the selected toolchain's `swift-frontend` supports
334+
public let supportedFrontendFeatures: Set<String>
335+
333336
/// A global queue for emitting non-interrupted messages into stderr
334337
public static let stdErrQueue = DispatchQueue(label: "org.swift.driver.emit-to-stderr")
335338

@@ -606,11 +609,12 @@ public struct Driver {
606609
outputFileMap: outputFileMap)
607610

608611
self.supportedFrontendFlags =
609-
try Self.computeSupportedCompilerFeatures(of: self.toolchain, hostTriple: self.hostTriple,
612+
try Self.computeSupportedCompilerArgs(of: self.toolchain, hostTriple: self.hostTriple,
610613
parsedOptions: &self.parsedOptions,
611614
diagnosticsEngine: diagnosticEngine,
612615
fileSystem: fileSystem, executor: executor,
613616
env: env)
617+
self.supportedFrontendFeatures = try Self.computeSupportedCompilerFeatures(of: self.toolchain, env: env)
614618

615619
self.enabledSanitizers = try Self.parseSanitizerArgValues(
616620
&parsedOptions,

Sources/SwiftDriver/ExplicitModuleBuilds/ModuleDependencyScanning.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ public extension Driver {
335335
.appending(component: libScanner)
336336
}
337337

338-
fileprivate static func getRootPath(of toolchain: Toolchain, env: [String: String])
338+
static func getRootPath(of toolchain: Toolchain, env: [String: String])
339339
throws -> AbsolutePath {
340340
if let overrideString = env["SWIFT_DRIVER_SWIFT_SCAN_TOOLCHAIN_PATH"] {
341341
return try AbsolutePath(validating: overrideString)

Sources/SwiftDriver/Jobs/EmitSupportedFeaturesJob.swift

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
import TSCBasic
1414
import SwiftOptions
15+
import Foundation
1516

1617
/// Describes information about the compiler's supported arguments and features
1718
@_spi(Testing) public struct SupportedCompilerFeatures: Codable {
@@ -52,7 +53,7 @@ extension Toolchain {
5253
}
5354

5455
extension Driver {
55-
static func computeSupportedCompilerFeatures(of toolchain: Toolchain, hostTriple: Triple,
56+
static func computeSupportedCompilerArgs(of toolchain: Toolchain, hostTriple: Triple,
5657
parsedOptions: inout ParsedOptions,
5758
diagnosticsEngine: DiagnosticsEngine,
5859
fileSystem: FileSystem,
@@ -85,4 +86,24 @@ extension Driver {
8586
recordedInputModificationDates: [:]).SupportedArguments
8687
return Set(decodedSupportedFlagList)
8788
}
89+
90+
static func computeSupportedCompilerFeatures(of toolchain: Toolchain,
91+
env: [String: String]) throws -> Set<String> {
92+
struct FeatureInfo: Codable {
93+
var name: String
94+
}
95+
struct FeatureList: Codable {
96+
var features: [FeatureInfo]
97+
}
98+
let jsonPath = try getRootPath(of: toolchain, env: env)
99+
.appending(component: "share")
100+
.appending(component: "swift")
101+
.appending(component: "features.json")
102+
guard localFileSystem.exists(jsonPath) else {
103+
return Set<String>()
104+
}
105+
let content = try localFileSystem.readFileContents(jsonPath)
106+
let result = try JSONDecoder().decode(FeatureList.self, from: Data(content.contents))
107+
return Set(result.features.map {$0.name})
108+
}
88109
}

Tests/SwiftDriverTests/SwiftDriverTests.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5015,6 +5015,12 @@ final class SwiftDriverTests: XCTestCase {
50155015
}
50165016
}
50175017

5018+
func testSupportedFeatureJson() throws {
5019+
let driver = try Driver(args: ["swiftc", "-emit-module", "foo.swift"])
5020+
XCTAssertFalse(driver.supportedFrontendFeatures.isEmpty)
5021+
XCTAssertTrue(driver.supportedFrontendFeatures.contains("experimental-skip-all-function-bodies"))
5022+
}
5023+
50185024
func testFilelist() throws {
50195025
do {
50205026
var driver = try Driver(args: ["swiftc", "-emit-module", "./a.swift", "./b.swift", "./c.swift", "-module-name", "main", "-target", "x86_64-apple-macosx10.9", "-driver-filelist-threshold=0", "-no-emit-module-separately"])

0 commit comments

Comments
 (0)