diff --git a/Sources/PackageModel/UserToolchain.swift b/Sources/PackageModel/UserToolchain.swift index 2520787428b..69edefd7132 100644 --- a/Sources/PackageModel/UserToolchain.swift +++ b/Sources/PackageModel/UserToolchain.swift @@ -42,11 +42,18 @@ public final class UserToolchain: Toolchain { /// An array of paths to search for libraries at link time. public let librarySearchPaths: [AbsolutePath] + /// Thread-safe cached runtime library paths + private let _runtimeLibraryPaths = ThreadSafeBox<[AbsolutePath]>() + /// An array of paths to use with binaries produced by this toolchain at run time. - public lazy var runtimeLibraryPaths: [AbsolutePath] = { - guard let targetInfo else { return [] } - return (try? Self.computeRuntimeLibraryPaths(targetInfo: targetInfo)) ?? [] - }() + public var runtimeLibraryPaths: [AbsolutePath] { + return _runtimeLibraryPaths.memoize { + guard let targetInfo = self.targetInfo else { + return [] + } + return (try? Self.computeRuntimeLibraryPaths(targetInfo: targetInfo)) ?? [] + } + } /// Path containing Swift resources for dynamic linking. public var swiftResourcesPath: AbsolutePath? { @@ -82,16 +89,29 @@ public final class UserToolchain: Toolchain { public let targetTriple: Basics.Triple private let _targetInfo: JSON? - private lazy var targetInfo: JSON? = { - // Only call out to the swift compiler to fetch the target info when necessary - try? _targetInfo ?? Self.getTargetInfo(swiftCompiler: swiftCompilerPath) - }() + + /// Thread-safe cached target info that allows for lazy instantiation + private let _cachedTargetInfo = ThreadSafeBox() + + private var targetInfo: JSON? { + return _cachedTargetInfo.memoize { + // Only call out to the swift compiler to fetch the target info when necessary + return try? _targetInfo ?? Self.getTargetInfo(swiftCompiler: swiftCompilerPath) + } + } + + /// Thread-safe cached swift compiler version that allows for lazy instantiation + private let _swiftCompilerVersion = ThreadSafeBox() // A version string that can be used to identify the swift compiler version - public lazy var swiftCompilerVersion: String? = { - guard let targetInfo else { return nil } - return Self.computeSwiftCompilerVersion(targetInfo: targetInfo) - }() + public var swiftCompilerVersion: String? { + return _swiftCompilerVersion.memoize { + guard let targetInfo = self.targetInfo else { + return nil + } + return Self.computeSwiftCompilerVersion(targetInfo: targetInfo) + } + } /// The list of CPU architectures to build for. public let architectures: [String]?