Skip to content

Commit 7e74ff6

Browse files
authored
Merge pull request #1726 from DougGregor/darwin-platform-dir-env-var
2 parents 086c822 + cea7c11 commit 7e74ff6

File tree

2 files changed

+61
-20
lines changed

2 files changed

+61
-20
lines changed

Sources/SwiftDriver/Toolchains/DarwinToolchain.swift

+43-19
Original file line numberDiff line numberDiff line change
@@ -456,29 +456,53 @@ public final class DarwinToolchain: Toolchain {
456456
}
457457

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

473-
// Default paths for compiler plugins within the platform (accessed via that
474-
// platform's plugin server).
475-
let platformPathRoot = platformPath.appending(components: "Developer", "usr")
476-
commandLine.appendFlag(.externalPluginPath)
477-
commandLine.appendFlag("\(platformPathRoot.pluginPath.name)#\(platformPathRoot.pluginServerPath.name)")
470+
// Determine the platform path based on the SDK path.
471+
addPluginPaths(
472+
forPlatform: VirtualPath.lookup(sdkPath)
473+
.parentDirectory
474+
.parentDirectory
475+
.parentDirectory,
476+
commandLine: &commandLine
477+
)
478+
}
479+
}
478480

479-
commandLine.appendFlag(.externalPluginPath)
480-
commandLine.appendFlag("\(platformPathRoot.localPluginPath.name)#\(platformPathRoot.pluginServerPath.name)")
481+
/// Given the platform path (e.g., a path into something like iPhoneOS.platform),
482+
/// add external plugin path arguments for compiler plugins that are distributed
483+
/// within that path.
484+
func addPluginPaths(
485+
forPlatform origPlatformPath: VirtualPath,
486+
commandLine: inout [Job.ArgTemplate]
487+
) {
488+
// For simulator platforms, look into the corresponding device platform instance,
489+
// because they share compiler plugins.
490+
let platformPath: VirtualPath
491+
if let simulatorRange = origPlatformPath.basename.range(of: "Simulator.platform") {
492+
let devicePlatform = origPlatformPath.basename.replacingCharacters(in: simulatorRange, with: "OS.platform")
493+
platformPath = origPlatformPath.parentDirectory.appending(component: devicePlatform)
494+
} else {
495+
platformPath = origPlatformPath
481496
}
497+
498+
// Default paths for compiler plugins within the platform (accessed via that
499+
// platform's plugin server).
500+
let platformPathRoot = platformPath.appending(components: "Developer", "usr")
501+
commandLine.appendFlag(.externalPluginPath)
502+
commandLine.appendFlag("\(platformPathRoot.pluginPath.name)#\(platformPathRoot.pluginServerPath.name)")
503+
504+
commandLine.appendFlag(.externalPluginPath)
505+
commandLine.appendFlag("\(platformPathRoot.localPluginPath.name)#\(platformPathRoot.pluginServerPath.name)")
482506
}
483507
}
484508

Tests/SwiftDriverTests/SwiftDriverTests.swift

+18-1
Original file line numberDiff line numberDiff line change
@@ -7749,7 +7749,12 @@ final class SwiftDriverTests: XCTestCase {
77497749
func pluginPathTest(platform: String, sdk: String, searchPlatform: String) throws {
77507750
let sdkRoot = try testInputsPath.appending(
77517751
components: ["Platform Checks", "\(platform).platform", "Developer", "SDKs", "\(sdk).sdk"])
7752-
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"])
7752+
var env = ProcessEnv.vars
7753+
env["PLATFORM_DIR"] = "/tmp/PlatformDir/\(platform).platform"
7754+
var driver = try Driver(
7755+
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"],
7756+
env: env
7757+
)
77537758
guard driver.isFrontendArgSupported(.pluginPath) && driver.isFrontendArgSupported(.externalPluginPath) else {
77547759
return
77557760
}
@@ -7791,6 +7796,18 @@ final class SwiftDriverTests: XCTestCase {
77917796
XCTAssertNotNil(platformLocalPluginPathIndex)
77927797
XCTAssertLessThan(platformPluginPathIndex!, platformLocalPluginPathIndex!)
77937798

7799+
// Plugin paths that come from the PLATFORM_DIR environment variable.
7800+
let envOrigPlatformPath = try AbsolutePath(validating: "/tmp/PlatformDir/\(searchPlatform).platform")
7801+
let envPlatformPath = envOrigPlatformPath.appending(components: "Developer", "usr")
7802+
let envPlatformServerPath = envPlatformPath.appending(components: "bin", "swift-plugin-server").pathString
7803+
let envPlatformPluginPath = envPlatformPath.appending(components: "lib", "swift", "host", "plugins")
7804+
let envPlatformPluginPathIndex = job.commandLine.firstIndex(of: .flag("\(envPlatformPluginPath)#\(envPlatformServerPath)"))
7805+
XCTAssertNotNil(envPlatformPluginPathIndex)
7806+
7807+
if let platformPluginPathIndex, let envPlatformPluginPathIndex {
7808+
XCTAssertLessThan(envPlatformPluginPathIndex, platformPluginPathIndex)
7809+
}
7810+
77947811
let toolchainPluginPathIndex = job.commandLine.firstIndex(of: .path(.absolute(try driver.toolchain.executableDir.parentDirectory.appending(components: "lib", "swift", "host", "plugins"))))
77957812
XCTAssertNotNil(toolchainPluginPathIndex)
77967813

0 commit comments

Comments
 (0)