Skip to content

Commit 2b68778

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

File tree

4 files changed

+32
-2
lines changed

4 files changed

+32
-2
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: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,4 +344,24 @@ public extension Driver {
344344
.parentDirectory // bin
345345
.parentDirectory // toolchain root
346346
}
347+
348+
static func computeSupportedCompilerFeatures(of toolchain: Toolchain,
349+
env: [String: String]) throws -> Set<String> {
350+
struct FeatureInfo: Codable {
351+
var name: String
352+
}
353+
struct FeatureList: Codable {
354+
var features: [FeatureInfo]
355+
}
356+
let jsonPath = try getRootPath(of: toolchain, env: env)
357+
.appending(component: "share")
358+
.appending(component: "swift")
359+
.appending(component: "features.json")
360+
guard localFileSystem.exists(jsonPath) else {
361+
return Set<String>()
362+
}
363+
let content = try localFileSystem.readFileContents(jsonPath)
364+
let result = try JSONDecoder().decode(FeatureList.self, from: Data(content.contents))
365+
return Set(result.features.map {$0.name})
366+
}
347367
}

Sources/SwiftDriver/Jobs/EmitSupportedFeaturesJob.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ extension Toolchain {
5252
}
5353

5454
extension Driver {
55-
static func computeSupportedCompilerFeatures(of toolchain: Toolchain, hostTriple: Triple,
55+
static func computeSupportedCompilerArgs(of toolchain: Toolchain, hostTriple: Triple,
5656
parsedOptions: inout ParsedOptions,
5757
diagnosticsEngine: DiagnosticsEngine,
5858
fileSystem: FileSystem,

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("emit-localized-strings"))
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)