Skip to content

Commit 943ca4b

Browse files
authored
Only fetch swiftc target info when needed. (#8992)
We added fetching of the swift compiler version using swiftc -print-target-info in the init of UserToolchain so that it can be passed to the PrebuiltsManager to calculate the manifest and prebuilts zip file names. However, there are cases where we pass in the triple for the current SDK which was intended to avoid the call to swiftc. We now call it all the time which causes an extra call to swiftc that isn't always needed. To address this, we make the calculation of the target info lazy and move the call to fetch the swift version to the point we need it in the PrebuiltsManager. If prebuilts aren't required, the call is avoided. To properly test this we need to run a test that calls out to swiftc. However, we don't have integration tests yet for prebuilts since they would take a long time to execute. I have instead manually tested this using a staged version of the prebuilts.
1 parent 485f566 commit 943ca4b

File tree

3 files changed

+41
-19
lines changed

3 files changed

+41
-19
lines changed

Sources/PackageModel/UserToolchain.swift

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@ public final class UserToolchain: Toolchain {
4343
public let librarySearchPaths: [AbsolutePath]
4444

4545
/// An array of paths to use with binaries produced by this toolchain at run time.
46-
public let runtimeLibraryPaths: [AbsolutePath]
46+
public lazy var runtimeLibraryPaths: [AbsolutePath] = {
47+
guard let targetInfo else { return [] }
48+
return (try? Self.computeRuntimeLibraryPaths(targetInfo: targetInfo)) ?? []
49+
}()
4750

4851
/// Path containing Swift resources for dynamic linking.
4952
public var swiftResourcesPath: AbsolutePath? {
@@ -78,8 +81,17 @@ public final class UserToolchain: Toolchain {
7881

7982
public let targetTriple: Basics.Triple
8083

84+
private let _targetInfo: JSON?
85+
private lazy var targetInfo: JSON? = {
86+
// Only call out to the swift compiler to fetch the target info when necessary
87+
try? _targetInfo ?? Self.getTargetInfo(swiftCompiler: swiftCompilerPath)
88+
}()
89+
8190
// A version string that can be used to identify the swift compiler version
82-
public let swiftCompilerVersion: String?
91+
public lazy var swiftCompilerVersion: String? = {
92+
guard let targetInfo else { return nil }
93+
return Self.computeSwiftCompilerVersion(targetInfo: targetInfo)
94+
}()
8395

8496
/// The list of CPU architectures to build for.
8597
public let architectures: [String]?
@@ -721,17 +733,16 @@ public final class UserToolchain: Toolchain {
721733
default: InstalledSwiftPMConfiguration.default)
722734
}
723735

724-
// targetInfo from the compiler
725-
let targetInfo = try customTargetInfo ?? Self.getTargetInfo(swiftCompiler: swiftCompilers.compile)
726-
727-
// Get compiler version information from target info
728-
self.swiftCompilerVersion = Self.computeSwiftCompilerVersion(targetInfo: targetInfo)
729-
730-
// Get the list of runtime libraries from the target info
731-
self.runtimeLibraryPaths = try Self.computeRuntimeLibraryPaths(targetInfo: targetInfo)
732-
733-
// Use the triple from Swift SDK or compute the host triple from the target info
734-
var triple = try swiftSDK.targetTriple ?? Self.getHostTriple(targetInfo: targetInfo)
736+
var triple: Basics.Triple
737+
if let targetTriple = swiftSDK.targetTriple {
738+
self._targetInfo = nil
739+
triple = targetTriple
740+
} else {
741+
// targetInfo from the compiler
742+
let targetInfo = try customTargetInfo ?? Self.getTargetInfo(swiftCompiler: swiftCompilers.compile)
743+
self._targetInfo = targetInfo
744+
triple = try swiftSDK.targetTriple ?? Self.getHostTriple(targetInfo: targetInfo)
745+
}
735746

736747
// Change the triple to the specified arch if there's exactly one of them.
737748
// The Triple property is only looked at by the native build system currently.

Sources/Workspace/Workspace+Prebuilts.swift

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,6 @@ extension Workspace {
165165
public typealias Delegate = PrebuiltsManagerDelegate
166166

167167
private let fileSystem: FileSystem
168-
private let swiftVersion: String
169168
private let authorizationProvider: AuthorizationProvider?
170169
private let httpClient: HTTPClient
171170
private let archiver: Archiver
@@ -175,15 +174,16 @@ extension Workspace {
175174
private let hashAlgorithm: HashAlgorithm = SHA256()
176175
private let prebuiltsDownloadURL: URL
177176
private let rootCertPath: AbsolutePath?
177+
private let customSwiftCompilerVersion: String?
178178
let hostPlatform: PrebuiltsManifest.Platform
179179

180180
init(
181181
fileSystem: FileSystem,
182182
hostPlatform: PrebuiltsManifest.Platform,
183-
swiftCompilerVersion: String,
184183
authorizationProvider: AuthorizationProvider?,
185184
scratchPath: AbsolutePath,
186185
cachePath: AbsolutePath?,
186+
customSwiftCompilerVersion: String?,
187187
customHTTPClient: HTTPClient?,
188188
customArchiver: Archiver?,
189189
delegate: Delegate?,
@@ -192,9 +192,9 @@ extension Workspace {
192192
) {
193193
self.fileSystem = fileSystem
194194
self.hostPlatform = hostPlatform
195-
self.swiftVersion = swiftCompilerVersion
196195
self.authorizationProvider = authorizationProvider
197196
self.httpClient = customHTTPClient ?? HTTPClient()
197+
self.customSwiftCompilerVersion = customSwiftCompilerVersion
198198

199199
#if os(Linux)
200200
self.archiver = customArchiver ?? TarArchiver(fileSystem: fileSystem)
@@ -276,10 +276,15 @@ extension Workspace {
276276
}
277277

278278
func downloadManifest(
279+
workspace: Workspace,
279280
package: PrebuiltPackage,
280281
version: Version,
281282
observabilityScope: ObservabilityScope
282283
) async throws -> PrebuiltsManifest? {
284+
guard let swiftVersion = customSwiftCompilerVersion ?? workspace.hostToolchain.swiftCompilerVersion else {
285+
return nil
286+
}
287+
283288
let manifestFile = swiftVersion + "-manifest.json"
284289
let manifestPath = try RelativePath(validating: "\(package.identity)/\(version)/\(manifestFile)")
285290
let destination = scratchPath.appending(manifestPath)
@@ -418,12 +423,17 @@ extension Workspace {
418423
}
419424

420425
func downloadPrebuilt(
426+
workspace: Workspace,
421427
package: PrebuiltPackage,
422428
version: Version,
423429
library: PrebuiltsManifest.Library,
424430
artifact: PrebuiltsManifest.Library.Artifact,
425431
observabilityScope: ObservabilityScope
426432
) async throws -> AbsolutePath? {
433+
guard let swiftVersion = customSwiftCompilerVersion ?? workspace.hostToolchain.swiftCompilerVersion else {
434+
return nil
435+
}
436+
427437
let artifactName = "\(swiftVersion)-\(library.name)-\(artifact.platform.rawValue)"
428438
let scratchDir = scratchPath.appending(components: package.identity.description, version.description)
429439

@@ -597,6 +607,7 @@ extension Workspace {
597607
let packageVersion = manifest.manifest.version,
598608
let prebuiltManifest = try await prebuiltsManager
599609
.downloadManifest(
610+
workspace: self,
600611
package: prebuilt,
601612
version: packageVersion,
602613
observabilityScope: observabilityScope
@@ -611,6 +622,7 @@ extension Workspace {
611622
for artifact in library.artifacts ?? [] where artifact.platform == hostPlatform {
612623
if let path = try await prebuiltsManager
613624
.downloadPrebuilt(
625+
workspace: self,
614626
package: prebuilt,
615627
version: packageVersion,
616628
library: library,

Sources/Workspace/Workspace.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -578,8 +578,7 @@ public class Workspace {
578578

579579
var prebuiltsManager: PrebuiltsManager?
580580
if configuration.usePrebuilts,
581-
let hostPlatform = customPrebuiltsManager?.hostPlatform ?? PrebuiltsManifest.Platform.hostPlatform,
582-
let swiftCompilerVersion = hostToolchain.swiftCompilerVersion
581+
let hostPlatform = customPrebuiltsManager?.hostPlatform ?? PrebuiltsManifest.Platform.hostPlatform
583582
{
584583
let rootCertPath: AbsolutePath?
585584
if let path = configuration.prebuiltsRootCertPath {
@@ -591,10 +590,10 @@ public class Workspace {
591590
let prebuiltsManagerObj = PrebuiltsManager(
592591
fileSystem: fileSystem,
593592
hostPlatform: hostPlatform,
594-
swiftCompilerVersion: customPrebuiltsManager?.swiftVersion ?? swiftCompilerVersion,
595593
authorizationProvider: authorizationProvider,
596594
scratchPath: location.prebuiltsDirectory,
597595
cachePath: customPrebuiltsManager?.useCache == false || !configuration.sharedDependenciesCacheEnabled ? .none : location.sharedPrebuiltsCacheDirectory,
596+
customSwiftCompilerVersion: customPrebuiltsManager?.swiftVersion,
598597
customHTTPClient: customPrebuiltsManager?.httpClient,
599598
customArchiver: customPrebuiltsManager?.archiver,
600599
delegate: delegate.map(WorkspacePrebuiltsManagerDelegate.init(workspaceDelegate:)),

0 commit comments

Comments
 (0)