Skip to content

Align swift sdk configuration subcommand with proposal text #7535

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 10 commits into from
May 8, 2024
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
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ Swift 6.0
`--experimental-swift-sdks-path` options on `swift build` are deprecated with replacements that don't have the
`experimental` prefix.

* [#7535] The `swift sdk configuration` subcommand is deprecated with a replacement named `configure` that has options that exactly match
[SE-0387 proposal text].

* [#7202]

Package manifests can now access information about the Git repository the given package is in via the context object's
Expand Down Expand Up @@ -360,6 +363,7 @@ Swift 3.0
[SE-0386]: https://github.com/apple/swift-evolution/blob/main/proposals/0386-package-access-modifier.md
[SE-0387]: https://github.com/apple/swift-evolution/blob/main/proposals/0387-cross-compilation-destinations.md
[SE-0391]: https://github.com/apple/swift-evolution/blob/main/proposals/0391-package-registry-publish.md
[SE-0387 proposal text]: https://github.com/apple/swift-evolution/blob/main/proposals/0387-cross-compilation-destinations.md#swift-sdk-installation-and-configuration

[SR-5918]: https://bugs.swift.org/browse/SR-5918
[SR-6978]: https://bugs.swift.org/browse/SR-6978
Expand Down Expand Up @@ -409,4 +413,5 @@ Swift 3.0
[#7118]: https://github.com/apple/swift-package-manager/pull/7118
[#7201]: https://github.com/apple/swift-package-manager/pull/7201
[#7202]: https://github.com/apple/swift-package-manager/pull/7202
[#7505]: https://github.com/apple/swift-package-manager/pull/7507
[#7507]: https://github.com/apple/swift-package-manager/pull/7507
[#7535]: https://github.com/apple/swift-package-manager/pull/7535
Binary file modified Fixtures/SwiftSDKs/test-sdk.artifactbundle.tar.gz
Binary file not shown.
Binary file modified Fixtures/SwiftSDKs/test-sdk.artifactbundle.zip
Binary file not shown.
8 changes: 5 additions & 3 deletions Sources/PackageModel/SwiftSDKs/SwiftSDKBundle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,18 @@ extension [SwiftSDKBundle] {
/// - hostTriple: triple of the machine on which the Swift SDK is building.
/// - targetTriple: triple of the machine for which the Swift SDK is building.
/// - Returns: ``SwiftSDK`` value with a given artifact ID, `nil` if none found.
public func selectSwiftSDK(id: String, hostTriple: Triple, targetTriple: Triple) -> SwiftSDK? {
public func selectSwiftSDK(id: String, hostTriple: Triple?, targetTriple: Triple) -> SwiftSDK? {
for bundle in self {
for (artifactID, variants) in bundle.artifacts {
guard artifactID == id else {
continue
}

for variant in variants {
guard variant.isSupporting(hostTriple: hostTriple) else {
continue
if let hostTriple {
guard variant.isSupporting(hostTriple: hostTriple) else {
continue
}
}

return variant.swiftSDKs.first { $0.targetTriple == targetTriple }
Expand Down
7 changes: 4 additions & 3 deletions Sources/PackageModel/SwiftSDKs/SwiftSDKBundleStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -239,9 +239,10 @@ public final class SwiftSDKBundleStore {

try await archiver.extract(from: bundlePath, to: extractionResultsDirectory)

guard let bundleName = try fileSystem.getDirectoryContents(extractionResultsDirectory).first,
bundleName.hasSuffix(".\(artifactBundleExtension)")
else {
guard let bundleName = try fileSystem.getDirectoryContents(extractionResultsDirectory).first(where: {
$0.hasSuffix(".\(artifactBundleExtension)") &&
fileSystem.isDirectory(extractionResultsDirectory.appending($0))
}) else {
throw SwiftSDKError.invalidBundleArchive(bundlePath)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public final class SwiftSDKConfigurationStore {

guard var swiftSDK = swiftSDKs.selectSwiftSDK(
id: sdkID,
hostTriple: hostTriple,
hostTriple: nil,
targetTriple: targetTriple
) else {
return nil
Expand Down
3 changes: 2 additions & 1 deletion Sources/SwiftSDKCommand/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@

add_library(SwiftSDKCommand
Configuration/ConfigurationSubcommand.swift
Configuration/ConfigureSwiftSDK.swift
Configuration/DeprecatedSwiftSDKConfigurationCommand.swift
Configuration/ResetConfiguration.swift
Configuration/SetConfiguration.swift
Configuration/ShowConfiguration.swift
ConfigureSwiftSDK.swift
SwiftSDKSubcommand.swift
InstallSwiftSDK.swift
ListSwiftSDKs.swift
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import ArgumentParser
import Basics
import Foundation
import PackageModel

protocol ConfigurationSubcommand: SwiftSDKSubcommand {
Expand Down Expand Up @@ -47,6 +48,8 @@ extension ConfigurationSubcommand {
_ swiftSDKsDirectory: AbsolutePath,
_ observabilityScope: ObservabilityScope
) throws {
fputs("warning: `swift sdk configuration` command is deprecated and will be removed in a future version of SwiftPM. Use `swift sdk configure` instead.\n", stderr)

let bundleStore = SwiftSDKBundleStore(
swiftSDKsDirectory: swiftSDKsDirectory,
fileSystem: self.fileSystem,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,15 @@
//===----------------------------------------------------------------------===//

import ArgumentParser
import Basics
import PackageModel

package struct ConfigureSwiftSDK: ParsableCommand {
package static let configuration = CommandConfiguration(
struct DeprecatedSwiftSDKConfigurationCommand: ParsableCommand {
static let configuration = CommandConfiguration(
commandName: "configuration",
abstract: """
Deprecated: use `swift sdk configure` instead.

Manages configuration options for installed Swift SDKs.
""",
subcommands: [
Expand All @@ -24,6 +28,4 @@ package struct ConfigureSwiftSDK: ParsableCommand {
ShowConfiguration.self,
]
)

package init() {}
}
252 changes: 252 additions & 0 deletions Sources/SwiftSDKCommand/ConfigureSwiftSDK.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,252 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift open source project
//
// Copyright (c) 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 ArgumentParser
import Basics
import CoreCommands
import Dispatch
import PackageModel

import var TSCBasic.stdoutStream

struct ConfigureSwiftSDK: AsyncParsableCommand {
static let configuration = CommandConfiguration(
commandName: "configure",
abstract: """
Manages configuration options for installed Swift SDKs.
"""
)

@OptionGroup(visibility: .hidden)
var locations: LocationOptions

@Option(help: "A path to a directory containing the SDK root.")
var sdkRootPath: String? = nil

@Option(help: "A path to a directory containing Swift resources for dynamic linking.")
var swiftResourcesPath: String? = nil

@Option(help: "A path to a directory containing Swift resources for static linking.")
var swiftStaticResourcesPath: String? = nil

@Option(
parsing: .singleValue,
help: """
A path to a directory containing headers. Multiple paths can be specified by providing this option multiple \
times to the command.
"""
)
var includeSearchPath: [String] = []

@Option(
parsing: .singleValue,
help: """
"A path to a directory containing libraries. Multiple paths can be specified by providing this option multiple \
times to the command.
"""
)
var librarySearchPath: [String] = []

@Option(
parsing: .singleValue,
help: """
"A path to a toolset file. Multiple paths can be specified by providing this option multiple times to the command.
"""
)
var toolsetPath: [String] = []

@Flag(
name: .customLong("reset"),
help: """
Resets configuration properties currently applied to a given Swift SDK and target triple. If no specific \
property is specified, all of them are reset for the Swift SDK.
"""
)
var shouldReset: Bool = false

@Flag(
name: .customLong("show-configuration"),
help: """
Prints all configuration properties currently applied to a given Swift SDK and target triple.
"""
)
var shouldShowConfiguration: Bool = false

@Argument(
help: """
An identifier of an already installed Swift SDK. Use the `list` subcommand to see all available \
identifiers.
"""
)
var sdkID: String

@Argument(help: "The target triple of the Swift SDK to configure.")
var targetTriple: String

/// The file system used by default by this command.
private var fileSystem: FileSystem { localFileSystem }

/// Parses Swift SDKs directory option if provided or uses the default path for Swift SDKs
/// on the file system. A new directory at this path is created if one doesn't exist already.
/// - Returns: existing or a newly created directory at the computed location.
private func getOrCreateSwiftSDKsDirectory() throws -> AbsolutePath {
var swiftSDKsDirectory = try fileSystem.getSharedSwiftSDKsDirectory(
explicitDirectory: locations.swiftSDKsDirectory
)

if !self.fileSystem.exists(swiftSDKsDirectory) {
swiftSDKsDirectory = try self.fileSystem.getOrCreateSwiftPMSwiftSDKsDirectory()
}

return swiftSDKsDirectory
}

func run() async throws {
let observabilityHandler = SwiftCommandObservabilityHandler(outputStream: stdoutStream, logLevel: .info)
let observabilitySystem = ObservabilitySystem(observabilityHandler)
let observabilityScope = observabilitySystem.topScope
let swiftSDKsDirectory = try self.getOrCreateSwiftSDKsDirectory()

let hostToolchain = try UserToolchain(swiftSDK: SwiftSDK.hostSwiftSDK())
let triple = try Triple.getHostTriple(usingSwiftCompiler: hostToolchain.swiftCompilerPath)

var commandError: Error? = nil
do {
let bundleStore = SwiftSDKBundleStore(
swiftSDKsDirectory: swiftSDKsDirectory,
fileSystem: self.fileSystem,
observabilityScope: observabilityScope,
outputHandler: { print($0) }
)
let configurationStore = try SwiftSDKConfigurationStore(
hostTimeTriple: triple,
swiftSDKBundleStore: bundleStore
)
let targetTriple = try Triple(self.targetTriple)

guard let swiftSDK = try configurationStore.readConfiguration(
sdkID: sdkID,
targetTriple: targetTriple
) else {
throw SwiftSDKError.swiftSDKNotFound(
artifactID: sdkID,
hostTriple: triple,
targetTriple: targetTriple
)
}

if self.shouldShowConfiguration {
print(swiftSDK.pathsConfiguration)
return
}

var configuration = swiftSDK.pathsConfiguration
if self.shouldReset {
if try !configurationStore.resetConfiguration(sdkID: sdkID, targetTriple: targetTriple) {
observabilityScope.emit(
warning: "No configuration for Swift SDK `\(sdkID)`"
)
} else {
observabilityScope.emit(
info: """
All configuration properties of Swift SDK `\(sdkID)` for target triple \
`\(targetTriple)` were successfully reset.
"""
)
}
} else {
var updatedProperties = [String]()

let currentWorkingDirectory: AbsolutePath? = fileSystem.currentWorkingDirectory

if let sdkRootPath {
configuration.sdkRootPath = try AbsolutePath(validating: sdkRootPath, relativeTo: currentWorkingDirectory)
updatedProperties.append(CodingKeys.sdkRootPath.stringValue)
}

if let swiftResourcesPath {
configuration.swiftResourcesPath =
try AbsolutePath(validating: swiftResourcesPath, relativeTo: currentWorkingDirectory)
updatedProperties.append(CodingKeys.swiftResourcesPath.stringValue)
}

if let swiftStaticResourcesPath {
configuration.swiftResourcesPath =
try AbsolutePath(validating: swiftStaticResourcesPath, relativeTo: currentWorkingDirectory)
updatedProperties.append(CodingKeys.swiftStaticResourcesPath.stringValue)
}

if !includeSearchPath.isEmpty {
configuration.includeSearchPaths =
try includeSearchPath.map { try AbsolutePath(validating: $0, relativeTo: currentWorkingDirectory) }
updatedProperties.append(CodingKeys.includeSearchPath.stringValue)
}

if !librarySearchPath.isEmpty {
configuration.librarySearchPaths =
try librarySearchPath.map { try AbsolutePath(validating: $0, relativeTo: currentWorkingDirectory) }
updatedProperties.append(CodingKeys.librarySearchPath.stringValue)
}

if !toolsetPath.isEmpty {
configuration.toolsetPaths =
try toolsetPath.map { try AbsolutePath(validating: $0, relativeTo: currentWorkingDirectory) }
updatedProperties.append(CodingKeys.toolsetPath.stringValue)
}

guard !updatedProperties.isEmpty else {
observabilityScope.emit(
error: """
No properties of Swift SDK `\(sdkID)` for target triple `\(targetTriple)` were updated \
since none were specified. Pass `--help` flag to see the list of all available properties.
"""
)
return
}

var swiftSDK = swiftSDK
swiftSDK.pathsConfiguration = configuration
try configurationStore.updateConfiguration(sdkID: sdkID, swiftSDK: swiftSDK)

observabilityScope.emit(
info: """
These properties of Swift SDK `\(sdkID)` for target triple \
`\(targetTriple)` were successfully updated: \(updatedProperties.joined(separator: ", ")).
"""
)
}

if observabilityScope.errorsReported {
throw ExitCode.failure
}
} catch {
commandError = error
}

// wait for all observability items to process
observabilityHandler.wait(timeout: .now() + 5)

if let commandError {
throw commandError
}
}
}

extension AbsolutePath {
fileprivate init(validating string: String, relativeTo basePath: AbsolutePath?) throws {
if let basePath {
try self.init(validating: string, relativeTo: basePath)
} else {
try self.init(validating: string)
}
}
}
6 changes: 2 additions & 4 deletions Sources/SwiftSDKCommand/InstallSwiftSDK.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import PackageModel

import var TSCBasic.stdoutStream

package struct InstallSwiftSDK: SwiftSDKSubcommand {
package static let configuration = CommandConfiguration(
struct InstallSwiftSDK: SwiftSDKSubcommand {
static let configuration = CommandConfiguration(
commandName: "install",
abstract: """
Installs a given Swift SDK bundle to a location discoverable by SwiftPM. If the artifact bundle \
Expand All @@ -33,8 +33,6 @@ package struct InstallSwiftSDK: SwiftSDKSubcommand {
@Argument(help: "A local filesystem path or a URL of a Swift SDK bundle to install.")
var bundlePathOrURL: String

package init() {}

func run(
hostTriple: Triple,
_ swiftSDKsDirectory: AbsolutePath,
Expand Down
Loading