Skip to content

Commit 06b4c91

Browse files
authored
Merge pull request #845 from nkcsgexi/json-feature
Add support for loading supported features from share/swift/features.json
2 parents 0c88dd3 + 6d3a6da commit 06b4c91

File tree

6 files changed

+53
-6
lines changed

6 files changed

+53
-6
lines changed

Sources/SwiftDriver/Driver/Driver.swift

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,9 +330,15 @@ 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

339+
enum KnownCompilerFeature: String {
340+
case emit_abi_descriptor = "emit-abi-descriptor"
341+
}
336342

337343
lazy var sdkPath: VirtualPath? = {
338344
guard let rawSdkPath = frontendTargetInfo.sdkPath?.path else {
@@ -364,6 +370,10 @@ public struct Driver {
364370
}
365371
}
366372

373+
func isFeatureSupported(_ feature: KnownCompilerFeature) -> Bool {
374+
return supportedFrontendFeatures.contains(feature.rawValue)
375+
}
376+
367377
/// Handler for emitting diagnostics to stderr.
368378
public static let stderrDiagnosticsHandler: DiagnosticsEngine.DiagnosticsHandler = { diagnostic in
369379
stdErrQueue.sync {
@@ -606,11 +616,12 @@ public struct Driver {
606616
outputFileMap: outputFileMap)
607617

608618
self.supportedFrontendFlags =
609-
try Self.computeSupportedCompilerFeatures(of: self.toolchain, hostTriple: self.hostTriple,
619+
try Self.computeSupportedCompilerArgs(of: self.toolchain, hostTriple: self.hostTriple,
610620
parsedOptions: &self.parsedOptions,
611621
diagnosticsEngine: diagnosticEngine,
612622
fileSystem: fileSystem, executor: executor,
613623
env: env)
624+
self.supportedFrontendFeatures = try Self.computeSupportedCompilerFeatures(of: self.toolchain, env: env)
614625

615626
self.enabledSanitizers = try Self.parseSanitizerArgValues(
616627
&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/EmitModuleJob.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,13 @@ extension Driver {
8787
commandLine.appendFlag(.parseAsLibrary)
8888
}
8989

90+
let outputPath = VirtualPath.lookup(moduleOutputPath)
9091
commandLine.appendFlag(.o)
91-
commandLine.appendPath(VirtualPath.lookup(moduleOutputPath))
92-
92+
commandLine.appendPath(outputPath)
93+
if isFeatureSupported(.emit_abi_descriptor) {
94+
commandLine.appendFlag(.emitAbiDescriptorPath)
95+
commandLine.appendPath(outputPath.replacingExtension(with: .jsonABIBaseline))
96+
}
9397
return Job(
9498
moduleName: moduleOutputInfo.name,
9599
kind: .emitModule,

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
}

Sources/SwiftDriver/Jobs/MergeModuleJob.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,14 @@ extension Driver {
7272
try commandLine.appendLast(.disableIncrementalImports, from: &parsedOptions)
7373
}
7474

75+
let outputPath = VirtualPath.lookup(moduleOutputInfo.output!.outputPath)
7576
commandLine.appendFlag(.o)
76-
commandLine.appendPath(VirtualPath.lookup(moduleOutputInfo.output!.outputPath))
77+
commandLine.appendPath(outputPath)
7778

79+
if isFeatureSupported(.emit_abi_descriptor) {
80+
commandLine.appendFlag(.emitAbiDescriptorPath)
81+
commandLine.appendPath(outputPath.replacingExtension(with: .jsonABIBaseline))
82+
}
7883
return Job(
7984
moduleName: moduleOutputInfo.name,
8085
kind: .mergeModule,

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)