Skip to content

[Darwin] Add external plugin paths into platform specified by PLATFORM_DIR #1726

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
Nov 7, 2024
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
62 changes: 43 additions & 19 deletions Sources/SwiftDriver/Toolchains/DarwinToolchain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -456,29 +456,53 @@ public final class DarwinToolchain: Toolchain {
}

if driver.isFrontendArgSupported(.externalPluginPath) {
// Determine the platform path. For simulator platforms, look into the
// corresponding device platform instance.
let origPlatformPath = VirtualPath.lookup(sdkPath)
.parentDirectory
.parentDirectory
.parentDirectory
let platformPath: VirtualPath
if let simulatorRange = origPlatformPath.basename.range(of: "Simulator.platform") {
let devicePlatform = origPlatformPath.basename.replacingCharacters(in: simulatorRange, with: "OS.platform")
platformPath = origPlatformPath.parentDirectory.appending(component: devicePlatform)
} else {
platformPath = origPlatformPath
// If the PLATFORM_DIR environment variable is set, also add plugin
// paths into it. Since this is a user override, it comes beore the
// default platform path that's based on the SDK.
if let platformDir = env["PLATFORM_DIR"],
let platformPath = try? VirtualPath(path: platformDir) {
addPluginPaths(
forPlatform: platformPath,
commandLine: &commandLine
)
}

// Default paths for compiler plugins within the platform (accessed via that
// platform's plugin server).
let platformPathRoot = platformPath.appending(components: "Developer", "usr")
commandLine.appendFlag(.externalPluginPath)
commandLine.appendFlag("\(platformPathRoot.pluginPath.name)#\(platformPathRoot.pluginServerPath.name)")
// Determine the platform path based on the SDK path.
addPluginPaths(
forPlatform: VirtualPath.lookup(sdkPath)
.parentDirectory
.parentDirectory
.parentDirectory,
commandLine: &commandLine
)
}
}

commandLine.appendFlag(.externalPluginPath)
commandLine.appendFlag("\(platformPathRoot.localPluginPath.name)#\(platformPathRoot.pluginServerPath.name)")
/// Given the platform path (e.g., a path into something like iPhoneOS.platform),
/// add external plugin path arguments for compiler plugins that are distributed
/// within that path.
func addPluginPaths(
forPlatform origPlatformPath: VirtualPath,
commandLine: inout [Job.ArgTemplate]
) {
// For simulator platforms, look into the corresponding device platform instance,
// because they share compiler plugins.
let platformPath: VirtualPath
if let simulatorRange = origPlatformPath.basename.range(of: "Simulator.platform") {
let devicePlatform = origPlatformPath.basename.replacingCharacters(in: simulatorRange, with: "OS.platform")
platformPath = origPlatformPath.parentDirectory.appending(component: devicePlatform)
} else {
platformPath = origPlatformPath
}

// Default paths for compiler plugins within the platform (accessed via that
// platform's plugin server).
let platformPathRoot = platformPath.appending(components: "Developer", "usr")
commandLine.appendFlag(.externalPluginPath)
commandLine.appendFlag("\(platformPathRoot.pluginPath.name)#\(platformPathRoot.pluginServerPath.name)")

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

Expand Down
19 changes: 18 additions & 1 deletion Tests/SwiftDriverTests/SwiftDriverTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7749,7 +7749,12 @@ final class SwiftDriverTests: XCTestCase {
func pluginPathTest(platform: String, sdk: String, searchPlatform: String) throws {
let sdkRoot = try testInputsPath.appending(
components: ["Platform Checks", "\(platform).platform", "Developer", "SDKs", "\(sdk).sdk"])
var driver = try Driver(args: ["swiftc", "-typecheck", "foo.swift", "-sdk", VirtualPath.absolute(sdkRoot).name, "-plugin-path", "PluginA", "-external-plugin-path", "Plugin~B#Bexe", "-load-plugin-library", "PluginB2", "-plugin-path", "PluginC", "-working-directory", "/tmp"])
var env = ProcessEnv.vars
env["PLATFORM_DIR"] = "/tmp/PlatformDir/\(platform).platform"
var driver = try Driver(
args: ["swiftc", "-typecheck", "foo.swift", "-sdk", VirtualPath.absolute(sdkRoot).name, "-plugin-path", "PluginA", "-external-plugin-path", "Plugin~B#Bexe", "-load-plugin-library", "PluginB2", "-plugin-path", "PluginC", "-working-directory", "/tmp"],
env: env
)
guard driver.isFrontendArgSupported(.pluginPath) && driver.isFrontendArgSupported(.externalPluginPath) else {
return
}
Expand Down Expand Up @@ -7791,6 +7796,18 @@ final class SwiftDriverTests: XCTestCase {
XCTAssertNotNil(platformLocalPluginPathIndex)
XCTAssertLessThan(platformPluginPathIndex!, platformLocalPluginPathIndex!)

// Plugin paths that come from the PLATFORM_DIR environment variable.
let envOrigPlatformPath = try AbsolutePath(validating: "/tmp/PlatformDir/\(searchPlatform).platform")
let envPlatformPath = envOrigPlatformPath.appending(components: "Developer", "usr")
let envPlatformServerPath = envPlatformPath.appending(components: "bin", "swift-plugin-server").pathString
let envPlatformPluginPath = envPlatformPath.appending(components: "lib", "swift", "host", "plugins")
let envPlatformPluginPathIndex = job.commandLine.firstIndex(of: .flag("\(envPlatformPluginPath)#\(envPlatformServerPath)"))
XCTAssertNotNil(envPlatformPluginPathIndex)

if let platformPluginPathIndex, let envPlatformPluginPathIndex {
XCTAssertLessThan(envPlatformPluginPathIndex, platformPluginPathIndex)
}

let toolchainPluginPathIndex = job.commandLine.firstIndex(of: .path(.absolute(try driver.toolchain.executableDir.parentDirectory.appending(components: "lib", "swift", "host", "plugins"))))
XCTAssertNotNil(toolchainPluginPathIndex)

Expand Down