Skip to content

Fix experimental-api-diff command and add a test #3359

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions Sources/Build/BuildPlan.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1853,11 +1853,22 @@ public class BuildPlan {
}
}

public func createAPIDigesterArgs() -> [String] {
public func createAPIToolCommonArgs(includeLibrarySearchPaths: Bool) -> [String] {
let buildPath = buildParameters.buildPath.pathString
var arguments = ["-I", buildPath]

arguments += buildParameters.toolchain.extraSwiftCFlags
var extraSwiftCFlags = buildParameters.toolchain.extraSwiftCFlags
if !includeLibrarySearchPaths {
for index in extraSwiftCFlags.indices.dropLast().reversed() {
if extraSwiftCFlags[index] == "-L" {
// Remove the flag
extraSwiftCFlags.remove(at: index)
// Remove the argument
extraSwiftCFlags.remove(at: index)
}
}
}
arguments += extraSwiftCFlags

// Add the search path to the directory containing the modulemap file.
for target in targets {
Expand Down
18 changes: 10 additions & 8 deletions Sources/Commands/APIDigester.swift
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ struct APIDigesterBaselineDumper {
try apiDigesterTool.dumpSDKJSON(
at: sdkJSON,
modules: graph.apiDigesterModules,
additionalArgs: buildOp.buildPlan!.createAPIDigesterArgs()
additionalArgs: buildOp.buildPlan!.createAPIToolCommonArgs(includeLibrarySearchPaths: false)
)

return sdkJSON
Expand Down Expand Up @@ -160,16 +160,18 @@ public struct SwiftAPIDigester {
}

public func diagnoseSDK(
currentSDKJSON: AbsolutePath,
baselineSDKJSON: AbsolutePath
baselineSDKJSON: AbsolutePath,
apiToolArgs: [String],
modules: [String]
) throws {
let args = [
var args = [
"-diagnose-sdk",
"--input-paths",
baselineSDKJSON.pathString,
"-input-paths",
currentSDKJSON.pathString,
"-baseline-path", baselineSDKJSON.pathString,
]
args.append(contentsOf: apiToolArgs)
for module in modules {
args.append(contentsOf: ["-module", module])
}

try runTool(args)
}
Expand Down
23 changes: 5 additions & 18 deletions Sources/Commands/SwiftPackageTool.swift
Original file line number Diff line number Diff line change
Expand Up @@ -287,11 +287,8 @@ extension SwiftPackageTool {
@OptionGroup()
var swiftOptions: SwiftToolOptions

@Argument(help: "The baseline treeish")
@Argument(help: "The baseline treeish to compare to (e.g. a commit hash, branch name, tag, etc.)")
var treeish: String

@Flag(help: "Invert the baseline which is helpful for determining API additions")
var invertBaseline: Bool = false

func run(_ swiftTool: SwiftTool) throws {
let apiDigesterPath = try swiftTool.getToolchain().getSwiftAPIDigester()
Expand All @@ -303,23 +300,12 @@ extension SwiftPackageTool {
let buildOp = try swiftTool.createBuildOperation(cacheBuildManifest: false)
try buildOp.build()

// Dump JSON for the current package.
let buildParameters = buildOp.buildParameters
let currentSDKJSON = buildParameters.apiDiff.appending(component: "current.json")
let packageGraph = try buildOp.getPackageGraph()

try apiDigesterTool.dumpSDKJSON(
at: currentSDKJSON,
modules: packageGraph.apiDigesterModules,
additionalArgs: buildOp.buildPlan!.createAPIDigesterArgs()
)

// Dump JSON for the baseline package.
let workspace = try swiftTool.getActiveWorkspace()
let baselineDumper = try APIDigesterBaselineDumper(
baselineTreeish: treeish,
packageRoot: swiftTool.getPackageRoot(),
buildParameters: buildParameters,
buildParameters: buildOp.buildParameters,
manifestLoader: workspace.manifestLoader,
repositoryManager: workspace.repositoryManager,
apiDigesterTool: apiDigesterTool,
Expand All @@ -329,8 +315,9 @@ extension SwiftPackageTool {

// Run the diagnose tool which will print the diff.
try apiDigesterTool.diagnoseSDK(
currentSDKJSON: invertBaseline ? baselineSDKJSON : currentSDKJSON,
baselineSDKJSON: invertBaseline ? currentSDKJSON : baselineSDKJSON
baselineSDKJSON: baselineSDKJSON,
apiToolArgs: buildOp.buildPlan!.createAPIToolCommonArgs(includeLibrarySearchPaths: false),
modules: try buildOp.getPackageGraph().apiDigesterModules
)
}
}
Expand Down
3 changes: 1 addition & 2 deletions Sources/Commands/SymbolGraphExtract.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ public struct SymbolGraphExtract {
args += ["-module-name", target.c99name]
args += buildParameters.targetTripleArgs(for: target)

// FIXME: We should rename this to common Swift tools args or something.
args += buildPlan.createAPIDigesterArgs()
args += buildPlan.createAPIToolCommonArgs(includeLibrarySearchPaths: true)
args += ["-module-cache-path", buildParameters.moduleCache.pathString]

args += ["-output-dir", symbolGraphDirectory.pathString]
Expand Down
19 changes: 19 additions & 0 deletions Tests/CommandsTests/PackageToolTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -982,6 +982,25 @@ final class PackageToolTests: XCTestCase {
#endif
}

func testAPIDiff() throws {
#if os(macOS)
guard (try? Resources.default.toolchain.getSwiftAPIDigester()) != nil else {
throw XCTSkip("swift-api-digester not available")
}
fixture(name: "DependencyResolution/External/Simple") { prefix in
let packageRoot = prefix.appending(component: "Foo")
// Overwrite the existing decl.
try localFileSystem.writeFileContents(packageRoot.appending(component: "Foo.swift")) {
$0 <<< "public let foo = 42"
}
let (_, stderr) = try execute(["experimental-api-diff", "1.2.3"], packagePath: packageRoot)
XCTAssertTrue(stderr.contains("Func foo() has been removed"))
}
#else
throw XCTSkip("Test unsupported on current platform")
#endif
}

func testArchiveSource() throws {
fixture(name: "DependencyResolution/External/Simple") { prefix in
let packageRoot = prefix.appending(component: "Bar")
Expand Down