Skip to content

Commit f9821a1

Browse files
authored
Merge pull request #885 from nkcsgexi/84595482
ABIChecking: use the always-generated-abi-descriptor to diagnose ABI breakages
2 parents 506af05 + 5a62863 commit f9821a1

File tree

7 files changed

+66
-20
lines changed

7 files changed

+66
-20
lines changed

Sources/SwiftDriver/Driver/Driver.swift

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,17 @@ public struct Driver {
356356
.appending(component: "Frameworks")
357357
} ()
358358

359+
lazy var abiDescriptorPath: TypedVirtualPath? = {
360+
guard isFeatureSupported(.emit_abi_descriptor) else {
361+
return nil
362+
}
363+
guard let moduleOutput = moduleOutputInfo.output else {
364+
return nil
365+
}
366+
return TypedVirtualPath(file: VirtualPath.lookup(moduleOutput.outputPath)
367+
.replacingExtension(with: .jsonABIBaseline).intern(), type: .jsonABIBaseline)
368+
}()
369+
359370
public func isFrontendArgSupported(_ opt: Option) -> Bool {
360371
var current = opt.spelling
361372
while(true) {
@@ -694,6 +705,7 @@ public struct Driver {
694705
compilerMode: compilerMode,
695706
outputFileMap: self.outputFileMap,
696707
moduleName: moduleOutputInfo.name)
708+
697709
let projectDirectory = Self.computeProjectDirectoryPath(
698710
moduleOutputPath: self.moduleOutputInfo.output?.outputPath,
699711
fileSystem: self.fileSystem)
@@ -721,7 +733,6 @@ public struct Driver {
721733
compilerMode: compilerMode,
722734
outputFileMap: self.outputFileMap,
723735
moduleName: moduleOutputInfo.name)
724-
725736
self.swiftPrivateInterfacePath = try Self.computeSupplementaryOutputPath(
726737
&parsedOptions, type: .privateSwiftInterface, isOutputOptions: [],
727738
outputPath: .emitPrivateModuleInterfacePath,

Sources/SwiftDriver/Jobs/APIDigesterJobs.swift

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,50 @@ extension Driver {
6565
)
6666
}
6767

68+
mutating func digesterDiagnosticsJob(modulePath: VirtualPath.Handle, baselinePath:
69+
VirtualPath.Handle, mode: DigesterMode) throws -> Job {
70+
func getDescriptorPath(for mode: DigesterMode) -> TypedVirtualPath? {
71+
switch mode {
72+
case .api:
73+
return nil
74+
case .abi:
75+
return abiDescriptorPath
76+
}
77+
}
78+
guard let currentABI = getDescriptorPath(for: mode) else {
79+
// we don't have existing descriptor to use so we have to load the module from interface/swiftmodule
80+
return try digesterCompareToBaselineJob(modulePath: modulePath, baselinePath: baselinePath, mode: digesterMode)
81+
}
82+
var commandLine = [Job.ArgTemplate]()
83+
commandLine.appendFlag("-diagnose-sdk")
84+
commandLine.appendFlag("-input-paths")
85+
commandLine.appendPath(VirtualPath.lookup(baselinePath))
86+
commandLine.appendFlag("-input-paths")
87+
commandLine.appendPath(currentABI.file)
88+
if mode == .abi {
89+
commandLine.appendFlag("-abi")
90+
}
91+
if let arg = parsedOptions.getLastArgument(.digesterBreakageAllowlistPath)?.asSingle {
92+
let path = try VirtualPath(path: arg)
93+
commandLine.appendFlag("-breakage-allowlist-path")
94+
commandLine.appendPath(path)
95+
}
96+
commandLine.appendFlag("-serialize-diagnostics-path")
97+
let diag = TypedVirtualPath(file: currentABI.file.parentDirectory.appending(component: currentABI.file.basename + ".dia").intern(), type: .diagnostics)
98+
commandLine.appendPath(diag.file)
99+
let inputs: [TypedVirtualPath] = [currentABI]
100+
return Job(
101+
moduleName: moduleOutputInfo.name,
102+
kind: .compareABIBaseline,
103+
tool: .absolute(try toolchain.getToolPath(.swiftAPIDigester)),
104+
commandLine: commandLine,
105+
inputs: inputs,
106+
primaryInputs: [],
107+
outputs: [diag],
108+
supportsResponseFiles: true
109+
)
110+
}
111+
68112
mutating func digesterCompareToBaselineJob(modulePath: VirtualPath.Handle, baselinePath: VirtualPath.Handle, mode: DigesterMode) throws -> Job {
69113
var commandLine = [Job.ArgTemplate]()
70114
commandLine.appendFlag("-diagnose-sdk")

Sources/SwiftDriver/Jobs/EmitModuleJob.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,10 @@ extension Driver {
9393
let outputPath = VirtualPath.lookup(moduleOutputPath)
9494
commandLine.appendFlag(.o)
9595
commandLine.appendPath(outputPath)
96-
if isFeatureSupported(.emit_abi_descriptor) {
96+
if let abiPath = abiDescriptorPath {
9797
commandLine.appendFlag(.emitAbiDescriptorPath)
98-
let abiOutput = outputPath.replacingExtension(with: .jsonABIBaseline)
99-
commandLine.appendPath(abiOutput)
100-
outputs.append(TypedVirtualPath(file: abiOutput.intern(), type: .jsonABIBaseline))
98+
commandLine.appendPath(abiPath.file)
99+
outputs.append(abiPath)
101100
}
102101
return Job(
103102
moduleName: moduleOutputInfo.name,

Sources/SwiftDriver/Jobs/Job.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public struct Job: Codable, Equatable, Hashable {
4141
case generateAPIBaseline = "generate-api-baseline"
4242
case generateABIBaseline = "generate-abi-baseline"
4343
case compareAPIBaseline = "compare-api-baseline"
44-
case compareABIBaseline = "compare-abi-baseline"
44+
case compareABIBaseline = "Check ABI stability"
4545
}
4646

4747
public enum ArgTemplate: Equatable, Hashable {

Sources/SwiftDriver/Jobs/MergeModuleJob.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,10 @@ extension Driver {
7676
commandLine.appendFlag(.o)
7777
commandLine.appendPath(outputPath)
7878

79-
if isFeatureSupported(.emit_abi_descriptor) {
79+
if let abiPath = abiDescriptorPath {
8080
commandLine.appendFlag(.emitAbiDescriptorPath)
81-
let abiOutput = outputPath.replacingExtension(with: .jsonABIBaseline)
82-
commandLine.appendPath(abiOutput)
83-
outputs.append(TypedVirtualPath(file: abiOutput.intern(), type: .jsonABIBaseline))
81+
commandLine.appendPath(abiPath.file)
82+
outputs.append(abiPath)
8483
}
8584
return Job(
8685
moduleName: moduleOutputInfo.name,

Sources/SwiftDriver/Jobs/Planning.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,7 @@ extension Driver {
510510
}
511511
if let baselineArg = parsedOptions.getLastArgument(.compareToBaselinePath)?.asSingle,
512512
let baselinePath = try? VirtualPath.intern(path: baselineArg) {
513-
addJob(try digesterCompareToBaselineJob(modulePath: moduleOutputPath, baselinePath: baselinePath, mode: digesterMode))
513+
addJob(try digesterDiagnosticsJob(modulePath: moduleOutputPath, baselinePath: baselinePath, mode: digesterMode))
514514
}
515515
}
516516

Tests/SwiftDriverTests/APIDigesterTests.swift

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -252,18 +252,11 @@ class APIDigesterTests: XCTestCase {
252252
"-digester-breakage-allowlist-path", "allowlist/path"])
253253
let digesterJob = try XCTUnwrap(driver.planBuild().first { $0.kind == .compareABIBaseline })
254254
XCTAssertTrue(digesterJob.commandLine.contains("-diagnose-sdk"))
255-
XCTAssertTrue(digesterJob.commandLine.contains(subsequence: ["-module", "foo"]))
256-
XCTAssertTrue(digesterJob.commandLine.contains(subsequence: ["-baseline-path", .path(.absolute(.init("/baseline/path")))]))
257-
XCTAssertTrue(digesterJob.commandLine.contains(subsequence: ["-I", .path(.relative(.init(".")))]))
258-
XCTAssertTrue(digesterJob.commandLine.contains(subsequence: ["-sdk", .path(.absolute(.init("/path/to/sdk")))]))
259-
XCTAssertTrue(digesterJob.commandLine.contains(subsequence: ["-I", .path(.absolute(.init("/some/path")))]))
260-
XCTAssertTrue(digesterJob.commandLine.contains(subsequence: ["-F", .path(.relative(.init("framework/path")))]))
261-
XCTAssertTrue(digesterJob.commandLine.contains(subsequence: ["-serialize-diagnostics-path",
262-
.path(.relative(.init("breaking-changes.dia")))]))
255+
XCTAssertTrue(digesterJob.commandLine.contains(subsequence: ["-input-paths", .path(.absolute(.init("/baseline/path")))]))
263256
XCTAssertTrue(digesterJob.commandLine.contains(subsequence: ["-breakage-allowlist-path",
264257
.path(.relative(.init("allowlist/path")))]))
265-
266258
XCTAssertTrue(digesterJob.commandLine.contains("-abi"))
259+
XCTAssertTrue(digesterJob.commandLine.contains("-serialize-diagnostics-path"))
267260
}
268261
}
269262

0 commit comments

Comments
 (0)