Skip to content

Commit 17a195e

Browse files
authored
Merge pull request #847 from nkcsgexi/emit-abi-5.5
[5.5] Emit ABI descriptor while emitting the final module
2 parents 18ab0ce + d368101 commit 17a195e

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
@@ -327,9 +327,15 @@ public struct Driver {
327327
/// A collection of all the flags the selected toolchain's `swift-frontend` supports
328328
public let supportedFrontendFlags: Set<String>
329329

330+
/// A collection of all the features the selected toolchain's `swift-frontend` supports
331+
public let supportedFrontendFeatures: Set<String>
332+
330333
/// A global queue for emitting non-interrupted messages into stderr
331334
public static let stdErrQueue = DispatchQueue(label: "org.swift.driver.emit-to-stderr")
332335

336+
enum KnownCompilerFeature: String {
337+
case emit_abi_descriptor = "emit-abi-descriptor"
338+
}
333339

334340
lazy var sdkPath: VirtualPath? = {
335341
guard let rawSdkPath = frontendTargetInfo.sdkPath?.path else {
@@ -361,6 +367,10 @@ public struct Driver {
361367
}
362368
}
363369

370+
func isFeatureSupported(_ feature: KnownCompilerFeature) -> Bool {
371+
return supportedFrontendFeatures.contains(feature.rawValue)
372+
}
373+
364374
/// Handler for emitting diagnostics to stderr.
365375
public static let stderrDiagnosticsHandler: DiagnosticsEngine.DiagnosticsHandler = { diagnostic in
366376
stdErrQueue.sync {
@@ -602,11 +612,12 @@ public struct Driver {
602612
outputFileMap: outputFileMap)
603613

604614
self.supportedFrontendFlags =
605-
try Self.computeSupportedCompilerFeatures(of: self.toolchain, hostTriple: self.hostTriple,
615+
try Self.computeSupportedCompilerArgs(of: self.toolchain, hostTriple: self.hostTriple,
606616
parsedOptions: &self.parsedOptions,
607617
diagnosticsEngine: diagnosticEngine,
608618
fileSystem: fileSystem, executor: executor,
609619
env: env)
620+
self.supportedFrontendFeatures = try Self.computeSupportedCompilerFeatures(of: self.toolchain, env: env)
610621

611622
self.enabledSanitizers = try Self.parseSanitizerArgValues(
612623
&parsedOptions,

Sources/SwiftDriver/ExplicitModuleBuilds/ModuleDependencyScanning.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ public extension Driver {
348348
.appending(component: "lib_InternalSwiftScan" + sharedLibExt)
349349
}
350350

351-
fileprivate static func getRootPath(of toolchain: Toolchain, env: [String: String])
351+
static func getRootPath(of toolchain: Toolchain, env: [String: String])
352352
throws -> AbsolutePath {
353353
if let overrideString = env["SWIFT_DRIVER_SWIFT_SCAN_TOOLCHAIN_PATH"] {
354354
return try AbsolutePath(validating: overrideString)

Sources/SwiftDriver/Jobs/EmitModuleJob.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,13 @@ extension Driver {
8484
commandLine.appendFlag(.parseAsLibrary)
8585
}
8686

87+
let outputPath = VirtualPath.lookup(moduleOutputPath)
8788
commandLine.appendFlag(.o)
88-
commandLine.appendPath(VirtualPath.lookup(moduleOutputPath))
89-
89+
commandLine.appendPath(outputPath)
90+
if isFeatureSupported(.emit_abi_descriptor) {
91+
commandLine.appendFlag(.emitAbiDescriptorPath)
92+
commandLine.appendPath(outputPath.replacingExtension(with: .jsonABIBaseline))
93+
}
9094
return Job(
9195
moduleName: moduleOutputInfo.name,
9296
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
@@ -4742,6 +4742,12 @@ final class SwiftDriverTests: XCTestCase {
47424742
}
47434743
}
47444744

4745+
func testSupportedFeatureJson() throws {
4746+
let driver = try Driver(args: ["swiftc", "-emit-module", "foo.swift"])
4747+
XCTAssertFalse(driver.supportedFrontendFeatures.isEmpty)
4748+
XCTAssertTrue(driver.supportedFrontendFeatures.contains("experimental-skip-all-function-bodies"))
4749+
}
4750+
47454751
func testFilelist() throws {
47464752
do {
47474753
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)