Skip to content

add customization point for host toolchain #4127

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions Sources/Commands/SwiftTool.swift
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,7 @@ public class SwiftTool {
fingerprintCheckingMode: self.options.resolverFingerprintCheckingMode
),
initializationWarningHandler: { self.observabilityScope.emit(warning: $0) },
customHostToolchain: self.getHostToolchain(), // FIXME: ideally we would not customize the host toolchain
customManifestLoader: self.getManifestLoader(), // FIXME: ideally we would not customize the manifest loader
customRepositoryProvider: repositoryProvider, // FIXME: ideally we would not customize the repository provider. its currently done for shutdown handling which can be better abstracted
delegate: delegate
Expand Down Expand Up @@ -688,7 +689,7 @@ public class SwiftTool {
let cacheDir = pluginsDir.appending(component: "cache")
let pluginScriptRunner = try DefaultPluginScriptRunner(
cacheDir: cacheDir,
toolchain: self._hostToolchain.get().configuration,
toolchain: self.getHostToolchain().configuration,
enableSandbox: !self.options.shouldDisableSandbox)
return pluginScriptRunner
}
Expand All @@ -698,6 +699,10 @@ public class SwiftTool {
return try _destinationToolchain.get()
}

func getHostToolchain() throws -> UserToolchain {
return try _hostToolchain.get()
}

func getManifestLoader() throws -> ManifestLoader {
return try _manifestLoader.get()
}
Expand Down Expand Up @@ -912,7 +917,7 @@ public class SwiftTool {

return try ManifestLoader(
// Always use the host toolchain's resources for parsing manifest.
toolchain: self._hostToolchain.get().configuration,
toolchain: self.getHostToolchain().configuration,
isManifestSandboxEnabled: !self.options.shouldDisableSandbox,
cacheDir: cachePath,
extraManifestFlags: extraManifestFlags
Expand Down
30 changes: 21 additions & 9 deletions Sources/Workspace/Workspace.swift
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,9 @@ public class Workspace {
/// The file system on which the workspace will operate.
fileprivate let fileSystem: FileSystem

/// The host toolchain to use.
fileprivate let hostToolchain: UserToolchain

/// The manifest loader to use.
fileprivate let manifestLoader: ManifestLoaderProtocol

Expand Down Expand Up @@ -290,6 +293,7 @@ public class Workspace {
/// - authorizationProvider: Provider of authentication information for outbound network requests.
/// - configuration: Configuration to fine tune the dependency resolution behavior.
/// - initializationWarningHandler: Initialization warnings handler
/// - customHostToolchain: Custom host toolchain. Used to create a customized ManifestLoader, customizing how manifest are loaded.
/// - customManifestLoader: Custom manifest loader. Used to customize how manifest are loaded.
/// - customPackageContainerProvider: Custom package container provider. Used to provide specialized package providers.
/// - customRepositoryProvider: Custom repository provider. Used to customize source control access.
Expand All @@ -301,6 +305,7 @@ public class Workspace {
configuration: WorkspaceConfiguration? = .none,
initializationWarningHandler: ((String) -> Void)? = .none,
// optional customization used for advanced integration situations
customHostToolchain: UserToolchain? = .none,
customManifestLoader: ManifestLoaderProtocol? = .none,
customPackageContainerProvider: PackageContainerProvider? = .none,
customRepositoryProvider: RepositoryProvider? = .none,
Expand All @@ -317,6 +322,7 @@ public class Workspace {
customFingerprints: .none,
customMirrors: .none,
customToolsVersion: .none,
customHostToolchain: customHostToolchain,
customManifestLoader: customManifestLoader,
customPackageContainerProvider: customPackageContainerProvider,
customRepositoryManager: .none,
Expand Down Expand Up @@ -384,7 +390,7 @@ public class Workspace {
/// - authorizationProvider: Provider of authentication information for outbound network requests.
/// - configuration: Configuration to fine tune the dependency resolution behavior.
/// - initializationWarningHandler: Initialization warnings handler
/// - customToolchain: Custom toolchain. Used to create a customized ManifestLoader, customizing how manifest are loaded.
/// - customHostToolchain: Custom host toolchain. Used to create a customized ManifestLoader, customizing how manifest are loaded.
/// - customPackageContainerProvider: Custom package container provider. Used to provide specialized package providers.
/// - customRepositoryProvider: Custom repository provider. Used to customize source control access.
/// - delegate: Delegate for workspace events
Expand All @@ -395,7 +401,7 @@ public class Workspace {
configuration: WorkspaceConfiguration? = .none,
initializationWarningHandler: ((String) -> Void)? = .none,
// optional customization used for advanced integration situations
customToolchain: UserToolchain,
customHostToolchain: UserToolchain,
customPackageContainerProvider: PackageContainerProvider? = .none,
customRepositoryProvider: RepositoryProvider? = .none,
// delegate
Expand All @@ -404,15 +410,16 @@ public class Workspace {
let fileSystem = fileSystem ?? localFileSystem
let location = Location(forRootPackage: packagePath, fileSystem: fileSystem)
let manifestLoader = ManifestLoader(
toolchain: customToolchain.configuration,
toolchain: customHostToolchain.configuration,
cacheDir: location.sharedManifestsCacheDirectory
)
try self.init(
fileSystem: fileSystem,
forRootPackage: packagePath,
location: location,
authorizationProvider: authorizationProvider,
configuration: configuration,
initializationWarningHandler: initializationWarningHandler,
customHostToolchain: customHostToolchain,
customManifestLoader: manifestLoader,
customPackageContainerProvider: customPackageContainerProvider,
customRepositoryProvider: customRepositoryProvider,
Expand Down Expand Up @@ -464,6 +471,7 @@ public class Workspace {
customFingerprints: customFingerprintStorage,
customMirrors: mirrors,
customToolsVersion: customToolsVersion,
customHostToolchain: .none,
customManifestLoader: customManifestLoader,
customPackageContainerProvider: customPackageContainerProvider,
customRepositoryManager: customRepositoryManager,
Expand Down Expand Up @@ -581,6 +589,7 @@ public class Workspace {
customFingerprints: PackageFingerprintStorage? = .none,
customMirrors: DependencyMirrors? = .none,
customToolsVersion: ToolsVersion? = .none,
customHostToolchain: UserToolchain? = .none,
customManifestLoader: ManifestLoaderProtocol? = .none,
customPackageContainerProvider: PackageContainerProvider? = .none,
customRepositoryManager: RepositoryManager? = .none,
Expand All @@ -603,6 +612,7 @@ public class Workspace {
customFingerprints: customFingerprints,
customMirrors: customMirrors,
customToolsVersion: customToolsVersion,
customHostToolchain: customHostToolchain,
customManifestLoader: customManifestLoader,
customPackageContainerProvider: customPackageContainerProvider,
customRepositoryManager: customRepositoryManager,
Expand All @@ -628,6 +638,7 @@ public class Workspace {
customFingerprints: PackageFingerprintStorage?,
customMirrors: DependencyMirrors?,
customToolsVersion: ToolsVersion?,
customHostToolchain: UserToolchain?,
customManifestLoader: ManifestLoaderProtocol?,
customPackageContainerProvider: PackageContainerProvider?,
customRepositoryManager: RepositoryManager?,
Expand All @@ -648,8 +659,9 @@ public class Workspace {

let currentToolsVersion = customToolsVersion ?? ToolsVersion.currentToolsVersion
let toolsVersionLoader = ToolsVersionLoader(currentToolsVersion: currentToolsVersion)
let manifestLoader = try customManifestLoader ?? ManifestLoader(
toolchain: UserToolchain(destination: .hostDestination()).configuration,
let hostToolchain = try customHostToolchain ?? UserToolchain(destination: .hostDestination())
let manifestLoader = customManifestLoader ?? ManifestLoader(
toolchain: hostToolchain.configuration,
cacheDir: location.sharedManifestsCacheDirectory
)

Expand Down Expand Up @@ -711,6 +723,7 @@ public class Workspace {
self.delegate = delegate
self.mirrors = mirrors
self.authorizationProvider = authorizationProvider
self.hostToolchain = hostToolchain
self.manifestLoader = manifestLoader
self.currentToolsVersion = currentToolsVersion
self.toolsVersionLoader = toolsVersionLoader
Expand Down Expand Up @@ -2443,7 +2456,6 @@ extension Workspace {
let indexFiles = artifacts.filter { $0.url.pathExtension.lowercased() == "artifactbundleindex" }
if !indexFiles.isEmpty {
let errors = ThreadSafeArrayStore<Error>()
let hostToolchain = try UserToolchain(destination: .hostDestination())
let jsonDecoder = JSONDecoder.makeWithDefaults()
for indexFile in indexFiles {
group.enter()
Expand All @@ -2468,8 +2480,8 @@ extension Workspace {
}
let metadata = try jsonDecoder.decode(ArchiveIndexFile.self, from: body)
// FIXME: this filter needs to become more sophisticated
guard let supportedArchive = metadata.archives.first(where: { $0.fileName.lowercased().hasSuffix(".zip") && $0.supportedTriples.contains(hostToolchain.triple) }) else {
throw StringError("No supported archive was found for '\(hostToolchain.triple.tripleString)'")
guard let supportedArchive = metadata.archives.first(where: { $0.fileName.lowercased().hasSuffix(".zip") && $0.supportedTriples.contains(self.hostToolchain.triple) }) else {
throw StringError("No supported archive was found for '\(self.hostToolchain.triple.tripleString)'")
}
// add relevant archive
zipArtifacts.append(
Expand Down
5 changes: 1 addition & 4 deletions Tests/WorkspaceTests/WorkspaceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4154,10 +4154,7 @@ final class WorkspaceTests: XCTestCase {

// Load the workspace.
let observability = ObservabilitySystem.makeForTesting()
let workspace = try Workspace(
forRootPackage: packagePath,
customToolchain: UserToolchain.default
)
let workspace = try Workspace(forRootPackage: packagePath)

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