Skip to content

Commit 0df5552

Browse files
committed
[Workspace] Fix lookup of llvm tools and clang
<rdar://problem/45577364> swiftpm should look for the llvm tools next to its binary instead of using xcrun/which 😩
1 parent f3ba910 commit 0df5552

File tree

10 files changed

+25
-122
lines changed

10 files changed

+25
-122
lines changed

Fixtures/Miscellaneous/CodeCoverage/.gitignore

Lines changed: 0 additions & 4 deletions
This file was deleted.

Fixtures/Miscellaneous/CodeCoverage/Package.swift

Lines changed: 0 additions & 28 deletions
This file was deleted.

Fixtures/Miscellaneous/CodeCoverage/README.md

Lines changed: 0 additions & 3 deletions
This file was deleted.

Fixtures/Miscellaneous/CodeCoverage/Sources/CodeCoverage/CodeCoverage.swift

Lines changed: 0 additions & 3 deletions
This file was deleted.

Fixtures/Miscellaneous/CodeCoverage/Tests/CodeCoverageTests/CodeCoverageTests.swift

Lines changed: 0 additions & 15 deletions
This file was deleted.

Fixtures/Miscellaneous/CodeCoverage/Tests/CodeCoverageTests/XCTestManifests.swift

Lines changed: 0 additions & 9 deletions
This file was deleted.

Fixtures/Miscellaneous/CodeCoverage/Tests/LinuxMain.swift

Lines changed: 0 additions & 7 deletions
This file was deleted.

Sources/Workspace/UserToolchain.swift

Lines changed: 25 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,6 @@ public final class UserToolchain: Toolchain {
4545
/// The manifest resource provider.
4646
public let manifestResources: ManifestResourceProvider
4747

48-
/// Cache for storing paths of tools after first lookup.
49-
///
50-
/// Key -> name of the tool.
51-
private var toolsPathCache: [String: AbsolutePath]
52-
5348
/// Path of the `swiftc` compiler.
5449
public let swiftCompiler: AbsolutePath
5550

@@ -144,60 +139,51 @@ public final class UserToolchain: Toolchain {
144139
/// Environment to use when looking up tools.
145140
private let processEnvironment: [String: String]
146141

147-
private func lookup(tool toolName: String) throws -> AbsolutePath {
148-
// Check the cache.
149-
if let toolPath = toolsPathCache[toolName] {
142+
/// Returns the path to clang compiler tool.
143+
public func getClangCompiler() throws -> AbsolutePath {
144+
// Check if we already computed.
145+
if let clang = _clangCompiler {
146+
return clang
147+
}
148+
149+
// Check in the environment variable first.
150+
if let toolPath = UserToolchain.lookup(variable: "CC", searchPaths: envSearchPaths) {
151+
_clangCompiler = toolPath
150152
return toolPath
151153
}
152154

153155
// Otherwise, lookup the tool on the system.
154-
let arguments = whichArgs + [toolName]
156+
let arguments = whichArgs + ["clang"]
155157
let foundPath = try Process.checkNonZeroExit(arguments: arguments, environment: processEnvironment).spm_chomp()
156158
guard !foundPath.isEmpty else {
157-
throw InvalidToolchainDiagnostic("could not find \(toolName)")
159+
throw InvalidToolchainDiagnostic("could not find clang")
158160
}
161+
let toolPath = try AbsolutePath(validating: foundPath)
159162

160-
let toolPath = AbsolutePath(foundPath)
161-
162-
// Check that it's valid in the file system.
163-
guard localFileSystem.isExecutableFile(toolPath) else {
164-
throw InvalidToolchainDiagnostic("could not find \(toolName) at expected path \(toolPath.asString)")
165-
}
166-
toolsPathCache[toolName] = toolPath
163+
_clangCompiler = toolPath
167164
return toolPath
168165
}
169-
170-
/// Returns the path to llvm-cov tool.
171-
public func getClangCompiler() throws -> AbsolutePath {
172-
let clangToolName = "clang"
173-
let toolPath: AbsolutePath
174-
175-
// Check in the environment variable first.
176-
if let value = UserToolchain.lookup(variable: "CC", searchPaths: envSearchPaths) {
177-
toolPath = value
178-
guard localFileSystem.isExecutableFile(toolPath) else {
179-
throw InvalidToolchainDiagnostic("could not find clang at expected path \(toolPath.asString)")
180-
}
181-
toolsPathCache[clangToolName] = toolPath
182-
return toolPath
183-
}
184-
185-
// Otherwise, just do a regular lookup.
186-
return try lookup(tool: clangToolName)
187-
}
166+
private var _clangCompiler: AbsolutePath?
188167

189168
/// Returns the path to llvm-cov tool.
190169
public func getLLVMCov() throws -> AbsolutePath {
191-
return try lookup(tool: "llvm-cov")
170+
let toolPath = destination.binDir.appending(component: "llvm-cov")
171+
guard localFileSystem.isExecutableFile(toolPath) else {
172+
throw InvalidToolchainDiagnostic("could not find llvm-cov at expected path \(toolPath.asString)")
173+
}
174+
return toolPath
192175
}
193176

194177
/// Returns the path to llvm-prof tool.
195178
public func getLLVMProf() throws -> AbsolutePath {
196-
return try lookup(tool: "llvm-profdata")
179+
let toolPath = destination.binDir.appending(component: "llvm-profdata")
180+
guard localFileSystem.isExecutableFile(toolPath) else {
181+
throw InvalidToolchainDiagnostic("could not find llvm-profdata at expected path \(toolPath.asString)")
182+
}
183+
return toolPath
197184
}
198185

199186
public init(destination: Destination, environment: [String: String] = Process.env) throws {
200-
self.toolsPathCache = [:]
201187
self.destination = destination
202188
self.processEnvironment = environment
203189

Tests/FunctionalTests/MiscellaneousTests.swift

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -360,19 +360,6 @@ class MiscellaneousTestCase: XCTestCase {
360360
#endif
361361
}
362362

363-
func testCodeCoverageCompile() throws {
364-
#if os(macOS)
365-
fixture(name: "Miscellaneous/CodeCoverage") { prefix in
366-
_ = try SwiftPMProduct.SwiftTest.executeProcess(["--enable-code-coverage"], packagePath: prefix)
367-
let codeCov = prefix.appending(components: ".build", "debug", "codecov")
368-
369-
let codeCovContents = try localFileSystem.getDirectoryContents(codeCov)
370-
XCTAssertTrue(codeCovContents.contains(where: { $0.contains("profraw") }), "No profraw file")
371-
XCTAssertTrue(codeCovContents.contains("default.profdata"), "No profdata file")
372-
}
373-
#endif
374-
}
375-
376363
func testReportingErrorFromGitCommand() throws {
377364
fixture(name: "Miscellaneous/MissingDependency") { prefix in
378365
// This fixture has a setup that is intentionally missing a local

Tests/FunctionalTests/XCTestManifests.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ extension MiscellaneousTestCase {
3434
static let __allTests__MiscellaneousTestCase = [
3535
("testCanBuildMoreThanTwiceWithExternalDependencies", testCanBuildMoreThanTwiceWithExternalDependencies),
3636
("testCanKillSubprocessOnSigInt", testCanKillSubprocessOnSigInt),
37-
("testCodeCoverageCompile", testCodeCoverageCompile),
3837
("testCompileFailureExitsGracefully", testCompileFailureExitsGracefully),
3938
("testExternalDependencyEdges1", testExternalDependencyEdges1),
4039
("testExternalDependencyEdges2", testExternalDependencyEdges2),

0 commit comments

Comments
 (0)