Skip to content

Add SDK and platform paths to plugin executables #1377

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
Jun 16, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
62 changes: 55 additions & 7 deletions Sources/SwiftDriver/Jobs/FrontendJobHelpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,26 @@ fileprivate func shouldColorDiagnostics() -> Bool {
return TerminalController.isTTY(stderrStream)
}

extension VirtualPath {
// Given a virtual path pointing into a toolchain/SDK/platform, produce the
// path to `swift-plugin-server`.
fileprivate var pluginServerPath: VirtualPath {
self.appending(components: "usr", "swift-plugin-server")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

usr/bin?

Copy link
Contributor

@bnbarham bnbarham Jun 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Though seems like the rest don't have usr, so is this actually intended to be bin?

EDIT: Yeah, the root path below is "Developer", "usr" so I assume this should be bin.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're absolutely right.

}

// Given a virtual path pointing into a toolchain/SDK/platform, produce the
// path to the plugins.
fileprivate var pluginPath: VirtualPath {
self.appending(components: "lib", "swift", "host", "plugins")
}

// Given a virtual path pointing into a toolchain/SDK/platform, produce the
// path to the plugins.
fileprivate var localPluginPath: VirtualPath {
self.appending(component: "local").pluginPath
}
}

extension Driver {
/// How the bridging header should be handled.
enum BridgingHeaderHandling {
Expand Down Expand Up @@ -261,18 +281,46 @@ extension Driver {
try commandLine.appendLast(.emitMacroExpansionFiles, from: &parsedOptions)
}

if isFrontendArgSupported(.pluginPath) {
// Emit user-provided plugin paths, in order.
if isFrontendArgSupported(.externalPluginPath) {
try commandLine.appendAll(.pluginPath, .externalPluginPath, from: &parsedOptions)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aren't we passing load-plugin-library and load-plugin-executable? 🤔

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think those also need to be handled in this same group, yes.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Woah, those aren't being passed at all!

} else if isFrontendArgSupported(.pluginPath) {
try commandLine.appendAll(.pluginPath, from: &parsedOptions)
}

if isFrontendArgSupported(.externalPluginPath), let sdkPath = frontendTargetInfo.sdkPath?.path {
// Default paths for compiler plugins found within an SDK (accessed via
// that SDK's plugin server).
let sdkPathRoot = VirtualPath.lookup(sdkPath).appending(components: "usr")
commandLine.appendFlag(.externalPluginPath)
commandLine.appendFlag("\(sdkPathRoot.pluginServerPath.name.spm_shellEscaped())#\(sdkPathRoot.pluginPath.name)")

commandLine.appendFlag(.externalPluginPath)
commandLine.appendFlag("\(sdkPathRoot.pluginServerPath.name.spm_shellEscaped())#\(sdkPathRoot.localPluginPath.name)")

let defaultPluginPath = try toolchain.executableDir.parentDirectory
.appending(components: "lib", "swift", "host", "plugins")
// Default paths for compiler plugins within the platform (accessed via that
// platform's plugin server).
let platformPathRoot = VirtualPath.lookup(sdkPath)
.parentDirectory
.parentDirectory
.parentDirectory
.appending(components: "Developer", "usr")
commandLine.appendFlag(.externalPluginPath)
commandLine.appendFlag("\(platformPathRoot.pluginServerPath.name.spm_shellEscaped())#\(platformPathRoot.pluginPath.name)")

commandLine.appendFlag(.externalPluginPath)
commandLine.appendFlag("\(platformPathRoot.pluginServerPath.name.spm_shellEscaped())#\(platformPathRoot.localPluginPath.name)")
}

if isFrontendArgSupported(.pluginPath) {
// Default paths for compiler plugins found within the toolchain
// (loaded as shared libraries).
let pluginPathRoot = VirtualPath.absolute(try toolchain.executableDir.parentDirectory)
commandLine.appendFlag(.pluginPath)
commandLine.appendPath(defaultPluginPath)
commandLine.appendPath(pluginPathRoot.pluginPath)

let localPluginPath = try toolchain.executableDir.parentDirectory
.appending(components: "local", "lib", "swift", "host", "plugins")
commandLine.appendFlag(.pluginPath)
commandLine.appendPath(localPluginPath)
commandLine.appendPath(pluginPathRoot.localPluginPath)
}

if isFrontendArgSupported(.externalPluginPath) {
Expand Down
6 changes: 4 additions & 2 deletions Tests/SwiftDriverTests/SwiftDriverTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6798,14 +6798,16 @@ final class SwiftDriverTests: XCTestCase {
}

func testPluginPaths() throws {
var driver = try Driver(args: ["swiftc", "-typecheck", "foo.swift"])
guard driver.isFrontendArgSupported(.pluginPath) else {
let sdkRoot = testInputsPath.appending(component: "SDKChecks").appending(component: "iPhoneOS.sdk")
var driver = try Driver(args: ["swiftc", "-typecheck", "foo.swift", "-sdk", VirtualPath.absolute(sdkRoot).name])
guard driver.isFrontendArgSupported(.pluginPath) && driver.isFrontendArgSupported(.externalPluginPath) else {
return
}

let jobs = try driver.planBuild().removingAutolinkExtractJobs()
XCTAssertEqual(jobs.count, 1)
let job = jobs.first!
XCTAssertTrue(job.commandLine.contains(.flag("-external-plugin-path")))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be worth checking the path here 😅

XCTAssertTrue(job.commandLine.contains(.flag("-plugin-path")))
XCTAssertTrue(job.commandLine.contains(.path(.absolute(try driver.toolchain.executableDir.parentDirectory.appending(components: "lib", "swift", "host", "plugins")))))
XCTAssertTrue(job.commandLine.contains(.path(.absolute(try driver.toolchain.executableDir.parentDirectory.appending(components: "local", "lib", "swift", "host", "plugins")))))
Expand Down