Skip to content

[Commands] Migrate: Avoid injecting feature settings into every Swift… #8700

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
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
45 changes: 32 additions & 13 deletions Sources/Commands/PackageCommands/Migrate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ extension SwiftPackageCommand {

let buildSystem = try await createBuildSystem(
swiftCommandState,
targets: self.options.targets,
features: features
)

Expand Down Expand Up @@ -150,21 +151,35 @@ extension SwiftPackageCommand {

private func createBuildSystem(
_ swiftCommandState: SwiftCommandState,
targets: Set<String>? = .none,
features: [SwiftCompilerFeature]
) async throws -> BuildSystem {
let toolsBuildParameters = try swiftCommandState.toolsBuildParameters
var destinationBuildParameters = try swiftCommandState.productsBuildParameters

// Inject feature settings as flags. This is safe and not as invasive
// as trying to update manifest because in adoption mode the features
// can only produce warnings.
for feature in features {
destinationBuildParameters.flags.swiftCompilerFlags.append(contentsOf: [
"-Xfrontend",
"-enable-\(feature.upcoming ? "upcoming" : "experimental")-feature",
"-Xfrontend",
"\(feature.name):migrate",
])
let destinationBuildParameters = try swiftCommandState.productsBuildParameters

let modulesGraph = try await swiftCommandState.loadPackageGraph()

let addFeaturesToModule = { (module: ResolvedModule) in
for feature in features {
module.underlying.buildSettings.add(.init(values: [
"-Xfrontend",
"-enable-\(feature.upcoming ? "upcoming" : "experimental")-feature",
"-Xfrontend",
"\(feature.name):migrate",
]), for: .OTHER_SWIFT_FLAGS)
}
}

if let targets {
targets.lazy.compactMap {
modulesGraph.module(for: $0)
}.forEach(addFeaturesToModule)
} else {
for package in modulesGraph.rootPackages {
package.modules.filter {
$0.type != .plugin
}.forEach(addFeaturesToModule)
}
}

return try await swiftCommandState.createBuildSystem(
Expand All @@ -173,7 +188,11 @@ extension SwiftPackageCommand {
toolsBuildParameters: toolsBuildParameters,
// command result output goes on stdout
// ie "swift build" should output to stdout
outputStream: TSCBasic.stdoutStream
packageGraphLoader: {
modulesGraph
},
outputStream: TSCBasic.stdoutStream,
observabilityScope: swiftCommandState.observabilityScope
)
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/PackageModel/Module/Module.swift
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ public class Module {
public let others: [AbsolutePath]

/// The build settings assignments of this module.
public let buildSettings: BuildSettings.AssignmentTable
public package(set) var buildSettings: BuildSettings.AssignmentTable

@_spi(SwiftPMInternal)
public let buildSettingsDescription: [TargetBuildSettingDescription.Setting]
Expand Down