Skip to content

Commit 05c59ab

Browse files
committed
Add SDK and platform paths to plugin executables
1 parent 9e78be1 commit 05c59ab

File tree

2 files changed

+59
-9
lines changed

2 files changed

+59
-9
lines changed

Sources/SwiftDriver/Jobs/FrontendJobHelpers.swift

Lines changed: 55 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,26 @@ fileprivate func shouldColorDiagnostics() -> Bool {
2727
return TerminalController.isTTY(stderrStream)
2828
}
2929

30+
extension VirtualPath {
31+
// Given a virtual path pointing into a toolchain/SDK/platform, produce the
32+
// path to `swift-plugin-server`.
33+
fileprivate var pluginServerPath: VirtualPath {
34+
self.appending(components: "usr", "swift-plugin-server")
35+
}
36+
37+
// Given a virtual path pointing into a toolchain/SDK/platform, produce the
38+
// path to the plugins.
39+
fileprivate var pluginPath: VirtualPath {
40+
self.appending(components: "lib", "swift", "host", "plugins")
41+
}
42+
43+
// Given a virtual path pointing into a toolchain/SDK/platform, produce the
44+
// path to the plugins.
45+
fileprivate var localPluginPath: VirtualPath {
46+
self.appending(component: "local").pluginPath
47+
}
48+
}
49+
3050
extension Driver {
3151
/// How the bridging header should be handled.
3252
enum BridgingHeaderHandling {
@@ -261,18 +281,46 @@ extension Driver {
261281
try commandLine.appendLast(.emitMacroExpansionFiles, from: &parsedOptions)
262282
}
263283

264-
if isFrontendArgSupported(.pluginPath) {
284+
// Emit user-provided plugin paths, in order.
285+
if isFrontendArgSupported(.externalPluginPath) {
286+
try commandLine.appendAll(.pluginPath, .externalPluginPath, from: &parsedOptions)
287+
} else if isFrontendArgSupported(.pluginPath) {
265288
try commandLine.appendAll(.pluginPath, from: &parsedOptions)
289+
}
290+
291+
if isFrontendArgSupported(.externalPluginPath), let sdkPath = frontendTargetInfo.sdkPath?.path {
292+
// Default paths for compiler plugins found within an SDK (accessed via
293+
// that SDK's plugin server).
294+
let sdkPathRoot = VirtualPath.lookup(sdkPath).appending(components: "usr")
295+
commandLine.appendFlag(.externalPluginPath)
296+
commandLine.appendFlag("\(sdkPathRoot.pluginServerPath.name.spm_shellEscaped())#\(sdkPathRoot.pluginPath.name)")
297+
298+
commandLine.appendFlag(.externalPluginPath)
299+
commandLine.appendFlag("\(sdkPathRoot.pluginServerPath.name.spm_shellEscaped())#\(sdkPathRoot.localPluginPath.name)")
266300

267-
let defaultPluginPath = try toolchain.executableDir.parentDirectory
268-
.appending(components: "lib", "swift", "host", "plugins")
301+
// Default paths for compiler plugins within the platform (accessed via that
302+
// platform's plugin server).
303+
let platformPathRoot = VirtualPath.lookup(sdkPath)
304+
.parentDirectory
305+
.parentDirectory
306+
.parentDirectory
307+
.appending(components: "Developer", "usr")
308+
commandLine.appendFlag(.externalPluginPath)
309+
commandLine.appendFlag("\(platformPathRoot.pluginServerPath.name.spm_shellEscaped())#\(platformPathRoot.pluginPath.name)")
310+
311+
commandLine.appendFlag(.externalPluginPath)
312+
commandLine.appendFlag("\(platformPathRoot.pluginServerPath.name.spm_shellEscaped())#\(platformPathRoot.localPluginPath.name)")
313+
}
314+
315+
if isFrontendArgSupported(.pluginPath) {
316+
// Default paths for compiler plugins found within the toolchain
317+
// (loaded as shared libraries).
318+
let pluginPathRoot = VirtualPath.absolute(try toolchain.executableDir.parentDirectory)
269319
commandLine.appendFlag(.pluginPath)
270-
commandLine.appendPath(defaultPluginPath)
320+
commandLine.appendPath(pluginPathRoot.pluginPath)
271321

272-
let localPluginPath = try toolchain.executableDir.parentDirectory
273-
.appending(components: "local", "lib", "swift", "host", "plugins")
274322
commandLine.appendFlag(.pluginPath)
275-
commandLine.appendPath(localPluginPath)
323+
commandLine.appendPath(pluginPathRoot.localPluginPath)
276324
}
277325

278326
if isFrontendArgSupported(.externalPluginPath) {

Tests/SwiftDriverTests/SwiftDriverTests.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6798,14 +6798,16 @@ final class SwiftDriverTests: XCTestCase {
67986798
}
67996799

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

68066807
let jobs = try driver.planBuild().removingAutolinkExtractJobs()
68076808
XCTAssertEqual(jobs.count, 1)
68086809
let job = jobs.first!
6810+
XCTAssertTrue(job.commandLine.contains(.flag("-external-plugin-path")))
68096811
XCTAssertTrue(job.commandLine.contains(.flag("-plugin-path")))
68106812
XCTAssertTrue(job.commandLine.contains(.path(.absolute(try driver.toolchain.executableDir.parentDirectory.appending(components: "lib", "swift", "host", "plugins")))))
68116813
XCTAssertTrue(job.commandLine.contains(.path(.absolute(try driver.toolchain.executableDir.parentDirectory.appending(components: "local", "lib", "swift", "host", "plugins")))))

0 commit comments

Comments
 (0)