@@ -14,10 +14,6 @@ import PackageLoading
14
14
import SPMBuildCore
15
15
import Build
16
16
17
- #if !os(macOS)
18
- import class Foundation. FileManager
19
- #endif
20
-
21
17
#if os(Windows)
22
18
private let hostExecutableSuffix = " .exe "
23
19
#else
@@ -78,21 +74,27 @@ public final class UserToolchain: Toolchain {
78
74
79
75
return runtime
80
76
}
81
-
82
- private static func findProgram( _ name: String , envSearchPaths: [ AbsolutePath ] ) throws -> AbsolutePath {
77
+
78
+ private static func getTool( _ name: String , binDir: AbsolutePath ) throws -> AbsolutePath {
79
+ let executableName = " \( name) \( hostExecutableSuffix) "
80
+ let toolPath = binDir. appending ( component: executableName)
81
+ guard localFileSystem. isExecutableFile ( toolPath) else {
82
+ throw InvalidToolchainDiagnostic ( " could not find \( name) at expected path \( toolPath) " )
83
+ }
84
+ return toolPath
85
+ }
86
+
87
+ private static func findTool( _ name: String , envSearchPaths: [ AbsolutePath ] ) throws -> AbsolutePath {
83
88
#if os(macOS)
84
89
let foundPath = try Process . checkNonZeroExit ( arguments: [ " /usr/bin/xcrun " , " --find " , name] ) . spm_chomp ( )
85
90
return try AbsolutePath ( validating: foundPath)
86
91
#else
87
- let executableName = " \( name) \( hostExecutableSuffix) "
88
-
89
92
for folder in envSearchPaths {
90
- let path = folder. appending ( component: executableName)
91
- if FileManager . default. fileExists ( atPath: path. pathString) {
92
- return path
93
+ if let toolPath = try ? getTool ( name, binDir: folder) {
94
+ return toolPath
93
95
}
94
96
}
95
- throw InvalidToolchainDiagnostic ( " Missing tool \( name) " )
97
+ throw InvalidToolchainDiagnostic ( " could not find \( name) " )
96
98
#endif
97
99
}
98
100
@@ -116,15 +118,14 @@ public final class UserToolchain: Toolchain {
116
118
// We require there is at least one valid swift compiler, either in the
117
119
// bin dir or SWIFT_EXEC.
118
120
let resolvedBinDirCompiler : AbsolutePath
119
- let binDirCompiler = binDir. appending ( component: " swiftc " + hostExecutableSuffix)
120
- if localFileSystem. isExecutableFile ( binDirCompiler) {
121
+ if let binDirCompiler = try ? UserToolchain . getTool ( " swiftc " , binDir: binDir) {
121
122
resolvedBinDirCompiler = binDirCompiler
122
123
} else if let SWIFT_EXEC = SWIFT_EXEC {
123
124
resolvedBinDirCompiler = SWIFT_EXEC
124
125
} else {
125
126
// Try to lookup swift compiler on the system which is possible when
126
127
// we're built outside of the Swift toolchain.
127
- resolvedBinDirCompiler = try UserToolchain . findProgram ( " swiftc " , envSearchPaths: envSearchPaths)
128
+ resolvedBinDirCompiler = try UserToolchain . findTool ( " swiftc " , envSearchPaths: envSearchPaths)
128
129
}
129
130
130
131
// The compiler for compilation tasks is SWIFT_EXEC or the bin dir compiler.
@@ -154,15 +155,14 @@ public final class UserToolchain: Toolchain {
154
155
155
156
// Then, check the toolchain.
156
157
do {
157
- let toolPath = destination. binDir. appending ( component: " clang " + hostExecutableSuffix)
158
- if localFileSystem. exists ( toolPath) {
158
+ if let toolPath = try ? UserToolchain . getTool ( " clang " , binDir: destination. binDir) {
159
159
_clangCompiler = toolPath
160
160
return toolPath
161
161
}
162
162
}
163
163
164
164
// Otherwise, lookup it up on the system.
165
- let toolPath = try UserToolchain . findProgram ( " clang " , envSearchPaths: envSearchPaths)
165
+ let toolPath = try UserToolchain . findTool ( " clang " , envSearchPaths: envSearchPaths)
166
166
_clangCompiler = toolPath
167
167
return toolPath
168
168
}
@@ -180,46 +180,26 @@ public final class UserToolchain: Toolchain {
180
180
181
181
/// Returns the path to llvm-cov tool.
182
182
public func getLLVMCov( ) throws -> AbsolutePath {
183
- let toolPath = destination. binDir. appending ( component: " llvm-cov " )
184
- guard localFileSystem. isExecutableFile ( toolPath) else {
185
- throw InvalidToolchainDiagnostic ( " could not find llvm-cov at expected path \( toolPath) " )
186
- }
187
- return toolPath
183
+ return try UserToolchain . getTool ( " llvm-cov " , binDir: destination. binDir)
188
184
}
189
185
190
186
/// Returns the path to llvm-prof tool.
191
187
public func getLLVMProf( ) throws -> AbsolutePath {
192
- let toolPath = destination. binDir. appending ( component: " llvm-profdata " )
193
- guard localFileSystem. isExecutableFile ( toolPath) else {
194
- throw InvalidToolchainDiagnostic ( " could not find llvm-profdata at expected path \( toolPath) " )
195
- }
196
- return toolPath
188
+ return try UserToolchain . getTool ( " llvm-profdata " , binDir: destination. binDir)
197
189
}
198
190
199
191
public func getSwiftAPIDigester( ) throws -> AbsolutePath {
200
192
if let envValue = UserToolchain . lookup ( variable: " SWIFT_API_DIGESTER " , searchPaths: envSearchPaths) {
201
193
return envValue
202
194
}
203
-
204
- let candidate = swiftCompiler. parentDirectory. appending ( component: " swift-api-digester " )
205
- if localFileSystem. exists ( candidate) {
206
- return candidate
207
- }
208
-
209
- throw InvalidToolchainDiagnostic ( " could not find swift-api-digester " )
195
+ return try UserToolchain . getTool ( " swift-api-digester " , binDir: swiftCompiler. parentDirectory)
210
196
}
211
197
212
198
public func getSymbolGraphExtract( ) throws -> AbsolutePath {
213
199
if let envValue = UserToolchain . lookup ( variable: " SWIFT_SYMBOLGRAPH_EXTRACT " , searchPaths: envSearchPaths) {
214
200
return envValue
215
201
}
216
-
217
- let candidate = swiftCompiler. parentDirectory. appending ( component: " swift-symbolgraph-extract " )
218
- if localFileSystem. exists ( candidate) {
219
- return candidate
220
- }
221
-
222
- throw InvalidToolchainDiagnostic ( " could not find swift-api-digester " )
202
+ return try UserToolchain . getTool ( " swift-symbolgraph-extract " , binDir: swiftCompiler. parentDirectory)
223
203
}
224
204
225
205
public static func deriveSwiftCFlags( triple: Triple , destination: Destination ) -> [ String ] {
0 commit comments