-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Pass the "available modules" for a target down to Swift commands #7581
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
Draft
DougGregor
wants to merge
1
commit into
swiftlang:main
Choose a base branch
from
DougGregor:available-modules
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Swift open source project | ||
// | ||
// Copyright (c) 2015-2023 Apple Inc. and the Swift project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See http://swift.org/LICENSE.txt for license information | ||
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
import Basics | ||
|
||
/// Describes the set of modules that are available to targets within a | ||
/// given package along with the target dependency that's required to import | ||
/// that module into a given | ||
package struct AvailableModules: Codable { | ||
/// A description of the target dependency that would be needed to | ||
/// import a given module. | ||
package enum TargetDependency: Codable { | ||
case target(name: String) | ||
case product(name: String, package: String?) | ||
|
||
static func <(lhs: TargetDependency, rhs: TargetDependency) -> Bool { | ||
switch (lhs, rhs) { | ||
case (.target(name: let lhsName), .target(name: let rhsName)): | ||
lhsName < rhsName | ||
case (.product(name: let lhsName, package: nil), | ||
.product(name: let rhsName, package: nil)): | ||
lhsName < rhsName | ||
case (.product(name: _, package: nil), | ||
.product(name: _, package: _?)): | ||
true | ||
case (.product(name: _, package: _?), | ||
.product(name: _, package: nil)): | ||
false | ||
case (.product(name: let lhsName, package: let lhsPackage?), | ||
.product(name: let rhsName, package: let rhsPackage?)): | ||
(lhsPackage, lhsName) < (rhsPackage, rhsName) | ||
case (.product, .target): | ||
false | ||
case (.target, .product): | ||
true | ||
} | ||
} | ||
} | ||
|
||
/// The set of modules that are available within the package described by | ||
/// the manifest, along with the target dependency required to reference | ||
/// the module. | ||
package var modules: [String: TargetDependency] = [:] | ||
} | ||
|
||
extension ModulesGraph { | ||
/// A flat list of available modules, used as an intermediary for the | ||
/// creation of an `AvailableModules` instance. | ||
fileprivate typealias AvailableModulesList = | ||
[(String, AvailableModules.TargetDependency)] | ||
|
||
/// Collect the module names that are made available by all of the products | ||
/// in this package, along with how the target dependency should be | ||
/// expressed to make the corresponding modules importable. | ||
/// | ||
/// The resulting module names are available to any package with a | ||
/// dependency on this package. | ||
fileprivate func productsAsAvailableModules( | ||
from package: ResolvedPackage | ||
) -> AvailableModulesList { | ||
package.products.flatMap { product in | ||
let productDependency: AvailableModules.TargetDependency = .product( | ||
name: product.name, | ||
package: product.packageIdentity.description | ||
) | ||
|
||
return product.targets.map { target in | ||
(target.c99name, productDependency) | ||
} | ||
} | ||
} | ||
|
||
/// Collect the module names that are made available by all of the targets | ||
/// in this package, along with how the target dependency should be | ||
/// expressed to make the corresponding module importable. | ||
/// | ||
/// The resulting module names are available within the targets of this | ||
/// package. | ||
fileprivate func targetsAsAvailableModules( | ||
in package: ResolvedPackage | ||
) -> AvailableModulesList { | ||
package.targets.map { target in | ||
(target.c99name, .target(name: target.name)) | ||
} | ||
} | ||
|
||
/// Produce the complete set of modules that are available within the | ||
/// given resolved package. | ||
package func availableModules( | ||
in package: ResolvedPackage | ||
) -> AvailableModules { | ||
var availableModules = AvailableModules() | ||
|
||
// Add available modules from targets within this package. | ||
availableModules.modules.merge( | ||
targetsAsAvailableModules(in: package), | ||
uniquingKeysWith: uniqueTargetDependency | ||
) | ||
|
||
// Add available modules from the products of any package this package | ||
// depends on. | ||
for dependencyID in package.dependencies { | ||
guard let dependencyPackage = self.package(for: dependencyID) else { | ||
continue | ||
} | ||
|
||
availableModules.modules.merge( | ||
productsAsAvailableModules(from: dependencyPackage), | ||
uniquingKeysWith: uniqueTargetDependency | ||
) | ||
} | ||
|
||
return availableModules | ||
} | ||
} | ||
|
||
/// "Unique" two target dependencies by picking the target dependency that we | ||
/// prefer. | ||
fileprivate func uniqueTargetDependency( | ||
lhs: AvailableModules.TargetDependency, | ||
rhs: AvailableModules.TargetDependency | ||
) -> AvailableModules.TargetDependency { | ||
lhs < rhs ? lhs : rhs | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this is guaranteed to be correct for C targets or binary targets, and it should exclude any plugin and macro targets
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For binary targets we at least tell people to match the target name and module name, but we aren't verifying it.