Skip to content

Commit 3cb4c9a

Browse files
authored
add customization point for host toolchain (#4127)
motivation: some complext integrations may require customization of the host toolchain changes: * add customization point at workspace initializer to pass in a custom host toolchain * adjust call sites and tests rdar://74700642
1 parent bec0287 commit 3cb4c9a

File tree

3 files changed

+29
-15
lines changed

3 files changed

+29
-15
lines changed

Sources/Commands/SwiftTool.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,7 @@ public class SwiftTool {
553553
fingerprintCheckingMode: self.options.resolverFingerprintCheckingMode
554554
),
555555
initializationWarningHandler: { self.observabilityScope.emit(warning: $0) },
556+
customHostToolchain: self.getHostToolchain(), // FIXME: ideally we would not customize the host toolchain
556557
customManifestLoader: self.getManifestLoader(), // FIXME: ideally we would not customize the manifest loader
557558
customRepositoryProvider: repositoryProvider, // FIXME: ideally we would not customize the repository provider. its currently done for shutdown handling which can be better abstracted
558559
delegate: delegate
@@ -688,7 +689,7 @@ public class SwiftTool {
688689
let cacheDir = pluginsDir.appending(component: "cache")
689690
let pluginScriptRunner = try DefaultPluginScriptRunner(
690691
cacheDir: cacheDir,
691-
toolchain: self._hostToolchain.get().configuration,
692+
toolchain: self.getHostToolchain().configuration,
692693
enableSandbox: !self.options.shouldDisableSandbox)
693694
return pluginScriptRunner
694695
}
@@ -698,6 +699,10 @@ public class SwiftTool {
698699
return try _destinationToolchain.get()
699700
}
700701

702+
func getHostToolchain() throws -> UserToolchain {
703+
return try _hostToolchain.get()
704+
}
705+
701706
func getManifestLoader() throws -> ManifestLoader {
702707
return try _manifestLoader.get()
703708
}
@@ -912,7 +917,7 @@ public class SwiftTool {
912917

913918
return try ManifestLoader(
914919
// Always use the host toolchain's resources for parsing manifest.
915-
toolchain: self._hostToolchain.get().configuration,
920+
toolchain: self.getHostToolchain().configuration,
916921
isManifestSandboxEnabled: !self.options.shouldDisableSandbox,
917922
cacheDir: cachePath,
918923
extraManifestFlags: extraManifestFlags

Sources/Workspace/Workspace.swift

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,9 @@ public class Workspace {
221221
/// The file system on which the workspace will operate.
222222
fileprivate let fileSystem: FileSystem
223223

224+
/// The host toolchain to use.
225+
fileprivate let hostToolchain: UserToolchain
226+
224227
/// The manifest loader to use.
225228
fileprivate let manifestLoader: ManifestLoaderProtocol
226229

@@ -290,6 +293,7 @@ public class Workspace {
290293
/// - authorizationProvider: Provider of authentication information for outbound network requests.
291294
/// - configuration: Configuration to fine tune the dependency resolution behavior.
292295
/// - initializationWarningHandler: Initialization warnings handler
296+
/// - customHostToolchain: Custom host toolchain. Used to create a customized ManifestLoader, customizing how manifest are loaded.
293297
/// - customManifestLoader: Custom manifest loader. Used to customize how manifest are loaded.
294298
/// - customPackageContainerProvider: Custom package container provider. Used to provide specialized package providers.
295299
/// - customRepositoryProvider: Custom repository provider. Used to customize source control access.
@@ -301,6 +305,7 @@ public class Workspace {
301305
configuration: WorkspaceConfiguration? = .none,
302306
initializationWarningHandler: ((String) -> Void)? = .none,
303307
// optional customization used for advanced integration situations
308+
customHostToolchain: UserToolchain? = .none,
304309
customManifestLoader: ManifestLoaderProtocol? = .none,
305310
customPackageContainerProvider: PackageContainerProvider? = .none,
306311
customRepositoryProvider: RepositoryProvider? = .none,
@@ -317,6 +322,7 @@ public class Workspace {
317322
customFingerprints: .none,
318323
customMirrors: .none,
319324
customToolsVersion: .none,
325+
customHostToolchain: customHostToolchain,
320326
customManifestLoader: customManifestLoader,
321327
customPackageContainerProvider: customPackageContainerProvider,
322328
customRepositoryManager: .none,
@@ -384,7 +390,7 @@ public class Workspace {
384390
/// - authorizationProvider: Provider of authentication information for outbound network requests.
385391
/// - configuration: Configuration to fine tune the dependency resolution behavior.
386392
/// - initializationWarningHandler: Initialization warnings handler
387-
/// - customToolchain: Custom toolchain. Used to create a customized ManifestLoader, customizing how manifest are loaded.
393+
/// - customHostToolchain: Custom host toolchain. Used to create a customized ManifestLoader, customizing how manifest are loaded.
388394
/// - customPackageContainerProvider: Custom package container provider. Used to provide specialized package providers.
389395
/// - customRepositoryProvider: Custom repository provider. Used to customize source control access.
390396
/// - delegate: Delegate for workspace events
@@ -395,7 +401,7 @@ public class Workspace {
395401
configuration: WorkspaceConfiguration? = .none,
396402
initializationWarningHandler: ((String) -> Void)? = .none,
397403
// optional customization used for advanced integration situations
398-
customToolchain: UserToolchain,
404+
customHostToolchain: UserToolchain,
399405
customPackageContainerProvider: PackageContainerProvider? = .none,
400406
customRepositoryProvider: RepositoryProvider? = .none,
401407
// delegate
@@ -404,15 +410,16 @@ public class Workspace {
404410
let fileSystem = fileSystem ?? localFileSystem
405411
let location = Location(forRootPackage: packagePath, fileSystem: fileSystem)
406412
let manifestLoader = ManifestLoader(
407-
toolchain: customToolchain.configuration,
413+
toolchain: customHostToolchain.configuration,
408414
cacheDir: location.sharedManifestsCacheDirectory
409415
)
410416
try self.init(
411417
fileSystem: fileSystem,
412-
forRootPackage: packagePath,
418+
location: location,
413419
authorizationProvider: authorizationProvider,
414420
configuration: configuration,
415421
initializationWarningHandler: initializationWarningHandler,
422+
customHostToolchain: customHostToolchain,
416423
customManifestLoader: manifestLoader,
417424
customPackageContainerProvider: customPackageContainerProvider,
418425
customRepositoryProvider: customRepositoryProvider,
@@ -464,6 +471,7 @@ public class Workspace {
464471
customFingerprints: customFingerprintStorage,
465472
customMirrors: mirrors,
466473
customToolsVersion: customToolsVersion,
474+
customHostToolchain: .none,
467475
customManifestLoader: customManifestLoader,
468476
customPackageContainerProvider: customPackageContainerProvider,
469477
customRepositoryManager: customRepositoryManager,
@@ -581,6 +589,7 @@ public class Workspace {
581589
customFingerprints: PackageFingerprintStorage? = .none,
582590
customMirrors: DependencyMirrors? = .none,
583591
customToolsVersion: ToolsVersion? = .none,
592+
customHostToolchain: UserToolchain? = .none,
584593
customManifestLoader: ManifestLoaderProtocol? = .none,
585594
customPackageContainerProvider: PackageContainerProvider? = .none,
586595
customRepositoryManager: RepositoryManager? = .none,
@@ -603,6 +612,7 @@ public class Workspace {
603612
customFingerprints: customFingerprints,
604613
customMirrors: customMirrors,
605614
customToolsVersion: customToolsVersion,
615+
customHostToolchain: customHostToolchain,
606616
customManifestLoader: customManifestLoader,
607617
customPackageContainerProvider: customPackageContainerProvider,
608618
customRepositoryManager: customRepositoryManager,
@@ -628,6 +638,7 @@ public class Workspace {
628638
customFingerprints: PackageFingerprintStorage?,
629639
customMirrors: DependencyMirrors?,
630640
customToolsVersion: ToolsVersion?,
641+
customHostToolchain: UserToolchain?,
631642
customManifestLoader: ManifestLoaderProtocol?,
632643
customPackageContainerProvider: PackageContainerProvider?,
633644
customRepositoryManager: RepositoryManager?,
@@ -648,8 +659,9 @@ public class Workspace {
648659

649660
let currentToolsVersion = customToolsVersion ?? ToolsVersion.currentToolsVersion
650661
let toolsVersionLoader = ToolsVersionLoader(currentToolsVersion: currentToolsVersion)
651-
let manifestLoader = try customManifestLoader ?? ManifestLoader(
652-
toolchain: UserToolchain(destination: .hostDestination()).configuration,
662+
let hostToolchain = try customHostToolchain ?? UserToolchain(destination: .hostDestination())
663+
let manifestLoader = customManifestLoader ?? ManifestLoader(
664+
toolchain: hostToolchain.configuration,
653665
cacheDir: location.sharedManifestsCacheDirectory
654666
)
655667

@@ -711,6 +723,7 @@ public class Workspace {
711723
self.delegate = delegate
712724
self.mirrors = mirrors
713725
self.authorizationProvider = authorizationProvider
726+
self.hostToolchain = hostToolchain
714727
self.manifestLoader = manifestLoader
715728
self.currentToolsVersion = currentToolsVersion
716729
self.toolsVersionLoader = toolsVersionLoader
@@ -2443,7 +2456,6 @@ extension Workspace {
24432456
let indexFiles = artifacts.filter { $0.url.pathExtension.lowercased() == "artifactbundleindex" }
24442457
if !indexFiles.isEmpty {
24452458
let errors = ThreadSafeArrayStore<Error>()
2446-
let hostToolchain = try UserToolchain(destination: .hostDestination())
24472459
let jsonDecoder = JSONDecoder.makeWithDefaults()
24482460
for indexFile in indexFiles {
24492461
group.enter()
@@ -2468,8 +2480,8 @@ extension Workspace {
24682480
}
24692481
let metadata = try jsonDecoder.decode(ArchiveIndexFile.self, from: body)
24702482
// FIXME: this filter needs to become more sophisticated
2471-
guard let supportedArchive = metadata.archives.first(where: { $0.fileName.lowercased().hasSuffix(".zip") && $0.supportedTriples.contains(hostToolchain.triple) }) else {
2472-
throw StringError("No supported archive was found for '\(hostToolchain.triple.tripleString)'")
2483+
guard let supportedArchive = metadata.archives.first(where: { $0.fileName.lowercased().hasSuffix(".zip") && $0.supportedTriples.contains(self.hostToolchain.triple) }) else {
2484+
throw StringError("No supported archive was found for '\(self.hostToolchain.triple.tripleString)'")
24732485
}
24742486
// add relevant archive
24752487
zipArtifacts.append(

Tests/WorkspaceTests/WorkspaceTests.swift

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4154,10 +4154,7 @@ final class WorkspaceTests: XCTestCase {
41544154

41554155
// Load the workspace.
41564156
let observability = ObservabilitySystem.makeForTesting()
4157-
let workspace = try Workspace(
4158-
forRootPackage: packagePath,
4159-
customToolchain: UserToolchain.default
4160-
)
4157+
let workspace = try Workspace(forRootPackage: packagePath)
41614158

41624159
// From here the API should be simple and straightforward:
41634160
let manifest = try tsc_await {

0 commit comments

Comments
 (0)