Skip to content

Commit 9e02f5b

Browse files
neonichuaciidgh
authored andcommitted
Allow a custom env for various xcrun invocations
For applications using libSwiftPM it may be appropriate to use a different environment than the one of the calling process.
1 parent 0165d69 commit 9e02f5b

File tree

2 files changed

+8
-7
lines changed

2 files changed

+8
-7
lines changed

Sources/Workspace/Destination.swift

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ public struct Destination {
8484
/// The destination describing the host OS.
8585
public static func hostDestination(
8686
_ binDir: AbsolutePath? = nil,
87-
originalWorkingDirectory: AbsolutePath? = localFileSystem.currentWorkingDirectory
87+
originalWorkingDirectory: AbsolutePath? = localFileSystem.currentWorkingDirectory,
88+
environment: [String:String] = Process.env
8889
) throws -> Destination {
8990
// Select the correct binDir.
9091
let binDir = binDir ?? Destination.hostBinDir(
@@ -98,7 +99,7 @@ public struct Destination {
9899
} else {
99100
// No value in env, so search for it.
100101
let sdkPathStr = try Process.checkNonZeroExit(
101-
args: "xcrun", "--sdk", "macosx", "--show-sdk-path").chomp()
102+
arguments: ["xcrun", "--sdk", "macosx", "--show-sdk-path"], environment: environment).chomp()
102103
guard !sdkPathStr.isEmpty else {
103104
throw DestinationError.invalidInstallation("default SDK not found")
104105
}
@@ -107,7 +108,7 @@ public struct Destination {
107108

108109
// Compute common arguments for clang and swift.
109110
// This is currently just frameworks path.
110-
let commonArgs = Destination.sdkPlatformFrameworkPath().map({ ["-F", $0.asString] }) ?? []
111+
let commonArgs = Destination.sdkPlatformFrameworkPath(environment: environment).map({ ["-F", $0.asString] }) ?? []
111112

112113
return Destination(
113114
target: hostTargetTriple,
@@ -132,12 +133,12 @@ public struct Destination {
132133
}
133134

134135
/// Returns macosx sdk platform framework path.
135-
public static func sdkPlatformFrameworkPath() -> AbsolutePath? {
136+
public static func sdkPlatformFrameworkPath(environment: [String:String] = Process.env) -> AbsolutePath? {
136137
if let path = _sdkPlatformFrameworkPath {
137138
return path
138139
}
139140
let platformPath = try? Process.checkNonZeroExit(
140-
args: "xcrun", "--sdk", "macosx", "--show-sdk-platform-path").chomp()
141+
arguments: ["xcrun", "--sdk", "macosx", "--show-sdk-platform-path"], environment: environment).chomp()
141142

142143
if let platformPath = platformPath, !platformPath.isEmpty {
143144
_sdkPlatformFrameworkPath = AbsolutePath(platformPath).appending(

Sources/Workspace/UserToolchain.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ public final class UserToolchain: Toolchain {
157157
return clangCompiler
158158
}
159159

160-
public init(destination: Destination) throws {
160+
public init(destination: Destination, environment: [String:String] = Process.env) throws {
161161
self.destination = destination
162162

163163
// Get the search paths from PATH.
@@ -183,7 +183,7 @@ public final class UserToolchain: Toolchain {
183183
#if os(macOS)
184184
// FIXME: We should have some general utility to find tools.
185185
let xctestFindArgs = ["xcrun", "--sdk", "macosx", "--find", "xctest"]
186-
self.xctest = try AbsolutePath(validating: Process.checkNonZeroExit(arguments: xctestFindArgs).chomp())
186+
self.xctest = try AbsolutePath(validating: Process.checkNonZeroExit(arguments: xctestFindArgs, environment: environment).chomp())
187187
#else
188188
self.xctest = nil
189189
#endif

0 commit comments

Comments
 (0)