Skip to content

Commit 35827d2

Browse files
committed
Allow a custom env for more xcrun invocations
Basically a continuation of #1766
1 parent 5f63b90 commit 35827d2

File tree

4 files changed

+14
-8
lines changed

4 files changed

+14
-8
lines changed

Sources/Build/Toolchain.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public protocol Toolchain {
1616
var swiftCompiler: AbsolutePath { get }
1717

1818
/// Path of the `clang` compiler.
19-
func getClangCompiler() throws -> AbsolutePath
19+
func getClangCompiler(environment: [String:String]) throws -> AbsolutePath
2020

2121
/// Additional flags to be passed to the C compiler.
2222
var extraCCFlags: [String] { get }
@@ -30,3 +30,9 @@ public protocol Toolchain {
3030
/// The dynamic library extension, for e.g. dylib, so.
3131
var dynamicLibraryExtension: String { get }
3232
}
33+
34+
extension Toolchain {
35+
func getClangCompiler() throws -> AbsolutePath {
36+
return try getClangCompiler(environment: Process.env)
37+
}
38+
}

Sources/Build/llbuild.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public struct LLBuildManifestGenerator {
8787
}
8888

8989
/// Generate manifest at the given path.
90-
public func generateManifest(at path: AbsolutePath) throws {
90+
public func generateManifest(at path: AbsolutePath, environment: [String:String] = Process.env) throws {
9191
var targets = Targets()
9292

9393
// Create commands for all target description in the plan.
@@ -99,7 +99,7 @@ public struct LLBuildManifestGenerator {
9999
buildByDefault: plan.graph.reachableTargets.contains(target),
100100
isTest: description.isTestTarget)
101101
case .clang(let description):
102-
targets.append(try createClangCompileTarget(description),
102+
targets.append(try createClangCompileTarget(description, environment: environment),
103103
buildByDefault: plan.graph.reachableTargets.contains(target),
104104
isTest: description.isTestTarget)
105105
}
@@ -215,7 +215,7 @@ public struct LLBuildManifestGenerator {
215215
}
216216

217217
/// Create a llbuild target for a Clang target description.
218-
private func createClangCompileTarget(_ target: ClangTargetBuildDescription) throws -> Target {
218+
private func createClangCompileTarget(_ target: ClangTargetBuildDescription, environment: [String:String] = Process.env) throws -> Target {
219219

220220
let standards = [
221221
(target.clangTarget.cxxLanguageStandard, SupportedLanguageExtension.cppExtensions),
@@ -241,7 +241,7 @@ public struct LLBuildManifestGenerator {
241241
//FIXME: Should we add build time dependency on dependent targets?
242242
inputs: [path.source.asString],
243243
outputs: [path.object.asString],
244-
args: [try plan.buildParameters.toolchain.getClangCompiler().asString] + args,
244+
args: [try plan.buildParameters.toolchain.getClangCompiler(environment: environment).asString] + args,
245245
deps: path.deps.asString)
246246
return Command(name: path.object.asString, tool: clang)
247247
})

Sources/Workspace/UserToolchain.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ public final class UserToolchain: Toolchain {
129129
return lookupExecutablePath(filename: getenv(variable), searchPaths: searchPaths)
130130
}
131131

132-
public func getClangCompiler() throws -> AbsolutePath {
132+
public func getClangCompiler(environment: [String:String]) throws -> AbsolutePath {
133133

134134
if let clangCompiler = _clangCompiler {
135135
return clangCompiler
@@ -142,7 +142,7 @@ public final class UserToolchain: Toolchain {
142142
clangCompiler = value
143143
} else {
144144
// No value in env, so search for `clang`.
145-
let foundPath = try Process.checkNonZeroExit(arguments: whichClangArgs).chomp()
145+
let foundPath = try Process.checkNonZeroExit(arguments: whichClangArgs, environment: environment).chomp()
146146
guard !foundPath.isEmpty else {
147147
throw InvalidToolchainDiagnostic("could not find `clang`")
148148
}

Tests/BuildTests/BuildPlanTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ private struct MockToolchain: Toolchain {
3131
#else
3232
let dynamicLibraryExtension = "so"
3333
#endif
34-
func getClangCompiler() throws -> AbsolutePath {
34+
func getClangCompiler(environment: [String:String]) throws -> AbsolutePath {
3535
return AbsolutePath("/fake/path/to/clang")
3636
}
3737
}

0 commit comments

Comments
 (0)