Skip to content

Commit 7c0946f

Browse files
committed
[Macros] Set -external-plugin-path when the toolchain is not Xcode
1 parent cf0429d commit 7c0946f

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

Sources/SwiftDriver/Jobs/FrontendJobHelpers.swift

+29
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,35 @@ extension Driver {
280280
commandLine.appendPath(localPluginPath)
281281
}
282282

283+
// Pass -external-plugin-path if the current toolchain is not a Xcode
284+
// default toolchain.
285+
if
286+
isFrontendArgSupported(.externalPluginPath),
287+
let darwinToolchain = toolchain as? DarwinToolchain,
288+
let xcodeDir = try darwinToolchain.findCurrentSelectedXcodeDir(),
289+
try !toolchain.executableDir.isDescendant(of: xcodeDir),
290+
let xcodeExecutableDir = try darwinToolchain.findXcodeExecutableDir()
291+
{
292+
try commandLine.appendAll(.externalPluginPath, from: &parsedOptions)
293+
294+
let xcodePluginServerPath = xcodeExecutableDir
295+
.appending(component: "swift-plugin-server")
296+
297+
if fileSystem.isExecutableFile(xcodePluginServerPath) {
298+
let xcodeToolchainUsrPath = xcodeExecutableDir.parentDirectory
299+
300+
let xcodePluginPath = xcodeToolchainUsrPath
301+
.appending(components: "lib", "swift", "host", "plugins")
302+
commandLine.appendFlag(.externalPluginPath)
303+
commandLine.appendFlag(xcodePluginPath.pathString + "#" + xcodePluginServerPath.pathString)
304+
305+
let xcodeLocalPluginPath = xcodeToolchainUsrPath
306+
.appending(components: "local", "lib", "swift", "host", "plugins")
307+
commandLine.appendFlag(.externalPluginPath)
308+
commandLine.appendFlag(xcodeLocalPluginPath.pathString + "#" + xcodePluginServerPath.pathString)
309+
}
310+
}
311+
283312
// Pass down -user-module-version if we are working with a compiler that
284313
// supports it.
285314
if let ver = parsedOptions.getLastArgument(.userModuleVersion)?.asSingle,

Sources/SwiftDriver/Toolchains/DarwinToolchain.swift

+35
Original file line numberDiff line numberDiff line change
@@ -421,3 +421,38 @@ private extension Version {
421421
return self.description
422422
}
423423
}
424+
425+
extension DarwinToolchain {
426+
func findXcodeExecutableDir() throws -> AbsolutePath? {
427+
#if os(macOS)
428+
let result = try executor.checkNonZeroExit(
429+
args: "xcrun", "-toolchain", "default", "-f", "swiftc",
430+
environment: env
431+
).trimmingCharacters(in: .whitespacesAndNewlines)
432+
433+
guard !result.isEmpty else {
434+
return nil
435+
}
436+
return try AbsolutePath(validating: result)
437+
.parentDirectory // swiftc
438+
#else
439+
return nil
440+
#endif
441+
}
442+
443+
func findCurrentSelectedXcodeDir() throws -> AbsolutePath? {
444+
#if os(macOS)
445+
let result = try executor.checkNonZeroExit(
446+
args: "xcode-select", "-p",
447+
environment: env
448+
).trimmingCharacters(in: .whitespacesAndNewlines)
449+
450+
guard !result.isEmpty else {
451+
return nil
452+
}
453+
return try AbsolutePath(validating: result)
454+
#else
455+
return nil
456+
#endif
457+
}
458+
}

0 commit comments

Comments
 (0)