-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
[NFC] Remove SupportedPlatforms
, add PlatformVersionProvider
#7161
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
7430175
Filter out duplicate package manifest conditions
MaxDesiatov 6fd1606
Add doc comment to the new type
MaxDesiatov 456959e
Fix tests regression
MaxDesiatov 420f01a
Remove custom `ResolvedTarget.Dependency` conformance
MaxDesiatov 76121f4
Roll back to arrays and reduce the diff
MaxDesiatov 4bd4bb4
Update comments and reduce the diff
MaxDesiatov d6c0c8a
Reduce the diff
MaxDesiatov f3004a7
Reduce the diff
MaxDesiatov a6760d9
Reduce the diff to track the regression
MaxDesiatov f8aee43
[NFC] Remove `SupportedPlatforms`, add `PlatformVersionProvider`
MaxDesiatov 9387af3
Merge branch 'main' into maxd/platform-version-provider
MaxDesiatov b777e12
Rename `getDerived` to `getSupportedPlatform`
MaxDesiatov 5b6670e
Merge branch 'main' into maxd/platform-version-provider
MaxDesiatov 20b84c9
Address PR feedback
MaxDesiatov 49e0955
Fix build error
MaxDesiatov 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 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 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 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 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 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
114 changes: 114 additions & 0 deletions
114
Sources/PackageGraph/Resolution/PlatformVersionProvider.swift
This file contains 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,114 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Swift open source project | ||
// | ||
// Copyright (c) 2014-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 struct PackageModel.MinimumDeploymentTarget | ||
import struct PackageModel.Platform | ||
import struct PackageModel.PlatformVersion | ||
import struct PackageModel.SupportedPlatform | ||
|
||
/// Merging two sets of supported platforms, preferring the max constraint | ||
func merge(into partial: inout [SupportedPlatform], platforms: [SupportedPlatform]) { | ||
for platformSupport in platforms { | ||
if let existing = partial.firstIndex(where: { $0.platform == platformSupport.platform }) { | ||
if partial[existing].version < platformSupport.version { | ||
partial.remove(at: existing) | ||
partial.append(platformSupport) | ||
} | ||
} else { | ||
partial.append(platformSupport) | ||
} | ||
} | ||
} | ||
|
||
public struct PlatformVersionProvider: Hashable { | ||
public enum Implementation: Hashable { | ||
case mergingFromTargets([ResolvedTarget]) | ||
case customXCTestMinimumDeploymentTargets([PackageModel.Platform: PlatformVersion]) | ||
case minimumDeploymentTargetDefault | ||
} | ||
|
||
private let implementation: Implementation | ||
|
||
public init(implementation: Implementation) { | ||
self.implementation = implementation | ||
} | ||
|
||
func derivedXCTestPlatformProvider(_ declared: PackageModel.Platform) -> PlatformVersion? { | ||
switch self.implementation { | ||
case .mergingFromTargets(let targets): | ||
let platforms = targets.reduce(into: [SupportedPlatform]()) { partial, item in | ||
merge( | ||
into: &partial, | ||
platforms: [item.getSupportedPlatform(for: declared, usingXCTest: item.type == .test)] | ||
) | ||
} | ||
return platforms.first!.version | ||
|
||
case .customXCTestMinimumDeploymentTargets(let customXCTestMinimumDeploymentTargets): | ||
return customXCTestMinimumDeploymentTargets[declared] | ||
|
||
case .minimumDeploymentTargetDefault: | ||
return MinimumDeploymentTarget.default.computeXCTestMinimumDeploymentTarget(for: declared) | ||
} | ||
} | ||
|
||
/// Returns the supported platform instance for the given platform. | ||
func getDerived(declared: [SupportedPlatform], for platform: Platform, usingXCTest: Bool) -> SupportedPlatform { | ||
// derived platform based on known minimum deployment target logic | ||
if let declaredPlatform = declared.first(where: { $0.platform == platform }) { | ||
var version = declaredPlatform.version | ||
|
||
if usingXCTest, | ||
let xcTestMinimumDeploymentTarget = self.derivedXCTestPlatformProvider(platform), | ||
version < xcTestMinimumDeploymentTarget | ||
{ | ||
version = xcTestMinimumDeploymentTarget | ||
} | ||
|
||
// If the declared version is smaller than the oldest supported one, we raise the derived version to that. | ||
if version < platform.oldestSupportedVersion { | ||
version = platform.oldestSupportedVersion | ||
} | ||
|
||
return SupportedPlatform( | ||
platform: declaredPlatform.platform, | ||
version: version, | ||
options: declaredPlatform.options | ||
) | ||
} else { | ||
let minimumSupportedVersion: PlatformVersion | ||
if usingXCTest, | ||
let xcTestMinimumDeploymentTarget = self.derivedXCTestPlatformProvider(platform), | ||
xcTestMinimumDeploymentTarget > platform.oldestSupportedVersion | ||
{ | ||
minimumSupportedVersion = xcTestMinimumDeploymentTarget | ||
} else { | ||
minimumSupportedVersion = platform.oldestSupportedVersion | ||
} | ||
|
||
let oldestSupportedVersion: PlatformVersion | ||
if platform == .macCatalyst { | ||
let iOS = self.getDerived(declared: declared, for: .iOS, usingXCTest: usingXCTest) | ||
// If there was no deployment target specified for Mac Catalyst, fall back to the iOS deployment target. | ||
oldestSupportedVersion = max(minimumSupportedVersion, iOS.version) | ||
} else { | ||
oldestSupportedVersion = minimumSupportedVersion | ||
} | ||
|
||
return SupportedPlatform( | ||
platform: platform, | ||
version: oldestSupportedVersion, | ||
options: [] | ||
) | ||
} | ||
} | ||
} |
Oops, something went wrong.
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.
Since we are creating the implementation enum here, I wonder if we should make custom and default two separate cases?
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.
Good catch, I also found that
Implementation.empty
case is unused. Ready for review 🙂