Skip to content

Commands: add experimental-destination command #5911

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 6 commits into from
Dec 15, 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
25 changes: 24 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,18 @@ let package = Package(
exclude: ["CMakeLists.txt", "README.md"]
),

.target(
/** Interacts with cross-compilation destinations */
name: "CrossCompilationDestinationsTool",
dependencies: [
.product(name: "ArgumentParser", package: "swift-argument-parser"),
"Basics",
"CoreCommands",
"SPMBuildCore",
"PackageModel",
]
),

.target(
/** Interacts with package collections */
name: "PackageCollectionsTool",
Expand Down Expand Up @@ -417,6 +429,11 @@ let package = Package(
dependencies: ["CoreCommands"],
exclude: ["CMakeLists.txt"]
),
.executableTarget(
/** Interacts with cross-compilation destinations */
name: "swift-experimental-destination",
dependencies: ["Commands", "CrossCompilationDestinationsTool"]
),
.executableTarget(
/** Runs package tests */
name: "swift-test",
Expand All @@ -437,7 +454,13 @@ let package = Package(
.executableTarget(
/** Multi-tool entry point for SwiftPM. */
name: "swift-package-manager",
dependencies: ["Commands", "Basics", "PackageCollectionsTool", "PackageRegistryTool"]
dependencies: [
"Basics",
"Commands",
"CrossCompilationDestinationsTool",
"PackageCollectionsTool",
"PackageRegistryTool"
]
),
.executableTarget(
/** Interact with package registry */
Expand Down
47 changes: 43 additions & 4 deletions Sources/Basics/FileSystem+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -224,26 +224,65 @@ extension FileSystem {
}
}

// MARK: - cross-compilation SDKs
// MARK: - cross-compilation destinations

private let crossCompilationDestinationsDirectoryName = "destinations"

extension FileSystem {
/// SwiftPM cross-compilation destinations directory (if exists)
public var swiftPMCrossCompilationDestinationsDirectory: AbsolutePath {
get throws {
if let path = try self.idiomaticSwiftPMDirectory {
if let path = try idiomaticSwiftPMDirectory {
return path.appending(component: crossCompilationDestinationsDirectoryName)
} else {
return try self.dotSwiftPMCrossCompilationDestinationsDirectory
return try dotSwiftPMCrossCompilationDestinationsDirectory
}
}
}

fileprivate var dotSwiftPMCrossCompilationDestinationsDirectory: AbsolutePath {
get throws {
return try self.dotSwiftPM.appending(component: crossCompilationDestinationsDirectoryName)
return try dotSwiftPM.appending(component: crossCompilationDestinationsDirectoryName)
Copy link
Contributor

@neonichu neonichu Dec 7, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure this feels right. I think on macOS, we probably want this to be inside ~/Library/Application\ Support/org.swift.swiftpm and not sure what a canonical place for other OSes would be.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm modeling this after all other path options boilerplate, for which a symlink is created to idiomaticSwiftPMDirectory where possible.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, but isn't the primary location ~/Library/org.swift.swiftpm right now?

Copy link
Contributor Author

@MaxDesiatov MaxDesiatov Dec 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's correct. Is your point that dotSwiftPMCrossCompilationDestinationsDirectory property should be deleted altogether? It's only called from swiftPMCrossCompilationDestinationsDirectory, which checks if ~/Library/org.swift.swiftpm is a valid path on the host platform and prefers that one.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was wondering whether these should be in "Application Support" instead since they're more "data" than "configuration", but now looking at it, we should probably not have a ~/Library/org.swift.swiftpm in the first place, but rather place things into subdirectories of "Preferences" and "Application Support"? Let's not worry about it for this PR.

}
}

public func getSharedCrossCompilationDestinationsDirectory(
explicitDirectory: AbsolutePath?
) throws -> AbsolutePath? {
if let explicitDestinationsDirectory = explicitDirectory {
// Create the explicit SDKs path if necessary
if !exists(explicitDestinationsDirectory) {
try createDirectory(explicitDestinationsDirectory, recursive: true)
}
return explicitDestinationsDirectory
} else {
return try swiftPMCrossCompilationDestinationsDirectory
}
}

public func getOrCreateSwiftPMCrossCompilationDestinationsDirectory() throws -> AbsolutePath {
let idiomaticDestinationsDirectory = try swiftPMCrossCompilationDestinationsDirectory

// Create idiomatic if necessary
if !exists(idiomaticDestinationsDirectory) {
try createDirectory(idiomaticDestinationsDirectory, recursive: true)
}
// Create ~/.swiftpm if necessary
if !exists(try dotSwiftPM) {
try createDirectory(dotSwiftPM, recursive: true)
}
// Create ~/.swiftpm/destinations symlink if necessary
// locking ~/.swiftpm to protect from concurrent access
try withLock(on: dotSwiftPM, type: .exclusive) {
if !exists(try dotSwiftPMCrossCompilationDestinationsDirectory, followSymlink: false) {
try createSymbolicLink(
dotSwiftPMCrossCompilationDestinationsDirectory,
pointingAt: idiomaticDestinationsDirectory,
relative: false
)
}
}
return idiomaticDestinationsDirectory
}
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/CoreCommands/Options.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public struct LocationOptions: ParsableArguments {
public var customCompileDestination: AbsolutePath?

@Option(name: .customLong("experimental-destinations-path"), help: .hidden, completion: .directory)
var crossCompilationDestinationsDirectory: AbsolutePath?
public var crossCompilationDestinationsDirectory: AbsolutePath?
}

public struct CachingOptions: ParsableArguments {
Expand Down
21 changes: 4 additions & 17 deletions Sources/CoreCommands/SwiftTool.swift
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public final class SwiftTool {
/// Path to the shared configuration directory
public let sharedConfigurationDirectory: AbsolutePath?

/// Path to the cross-compilation SDK directory.
/// Path to the cross-compilation destinations directory.
public let sharedCrossCompilationDestinationsDirectory: AbsolutePath?

/// Cancellator to handle cancellation of outstanding work when handling SIGINT
Expand Down Expand Up @@ -299,7 +299,9 @@ public final class SwiftTool {
self.sharedSecurityDirectory = try getSharedSecurityDirectory(options: self.options, fileSystem: fileSystem)
self.sharedConfigurationDirectory = try getSharedConfigurationDirectory(options: self.options, fileSystem: fileSystem)
self.sharedCacheDirectory = try getSharedCacheDirectory(options: self.options, fileSystem: fileSystem)
self.sharedCrossCompilationDestinationsDirectory = try getSharedCrossCompilationDestinationsDirectory(options: self.options, fileSystem: fileSystem)
self.sharedCrossCompilationDestinationsDirectory = try fileSystem.getSharedCrossCompilationDestinationsDirectory(
explicitDirectory: self.options.locations.crossCompilationDestinationsDirectory
)

// set global process logging handler
Process.loggingHandler = { self.observabilityScope.emit(debug: $0) }
Expand Down Expand Up @@ -772,21 +774,6 @@ private func getSharedCacheDirectory(options: GlobalOptions, fileSystem: FileSys
}
}

private func getSharedCrossCompilationDestinationsDirectory(
options: GlobalOptions,
fileSystem: FileSystem
) throws -> AbsolutePath? {
if let explicitDestinationsDirectory = options.locations.crossCompilationDestinationsDirectory {
// Create the explicit destinations path if necessary
if !fileSystem.exists(explicitDestinationsDirectory) {
try fileSystem.createDirectory(explicitDestinationsDirectory, recursive: true)
}
return explicitDestinationsDirectory
} else {
return try fileSystem.swiftPMCrossCompilationDestinationsDirectory
}
}

extension Basics.Diagnostic {
static func unsupportedFlag(_ flag: String) -> Self {
.warning("\(flag) is an *unsupported* option which can be removed at any time; do not rely on it")
Expand Down
78 changes: 78 additions & 0 deletions Sources/CrossCompilationDestinationsTool/ListDestinations.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift open source project
//
// Copyright (c) 2014-2022 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 SPMBuildCore
import PackageModel
import TSCBasic

struct ListDestinations: ParsableCommand {
static let configuration = CommandConfiguration(
commandName: "list",
abstract:
"""
Print a list of IDs of available cross-compilation destinations available on the filesystem.
"""
)

@OptionGroup()
var locations: LocationOptions

func run() throws {
let fileSystem = localFileSystem
let observabilitySystem = ObservabilitySystem(
SwiftToolObservabilityHandler(outputStream: stdoutStream, logLevel: .warning)
)
let observabilityScope = observabilitySystem.topScope

guard var destinationsDirectory = try fileSystem.getSharedCrossCompilationDestinationsDirectory(
explicitDirectory: locations.crossCompilationDestinationsDirectory
) else {
let expectedPath = try fileSystem.swiftPMCrossCompilationDestinationsDirectory
throw StringError(
"Couldn't find or create a directory where cross-compilation destinations are stored: \(expectedPath)"
)
}

if !fileSystem.exists(destinationsDirectory) {
destinationsDirectory = try fileSystem.getOrCreateSwiftPMCrossCompilationDestinationsDirectory()
}

// Get absolute paths to available destination bundles.
let destinationBundles = try fileSystem.getDirectoryContents(destinationsDirectory).filter {
$0.hasSuffix(BinaryTarget.Kind.artifactsArchive.fileExtension)
}.map {
destinationsDirectory.appending(components: [$0])
}

// Enumerate available bundles and parse manifests for each of them, then validate supplied destinations.
for bundlePath in destinationBundles {
do {
let destinationsBundle = try DestinationsBundle.parseAndValidate(
bundlePath: bundlePath,
fileSystem: fileSystem,
observabilityScope: observabilityScope
)

destinationsBundle.artifacts.keys.forEach { print($0) }
} catch {
observabilityScope.emit(
.warning(
"Couldn't parse `info.json` manifest of a destination bundle at \(bundlePath): \(error)"
)
)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift open source project
//
// Copyright (c) 2014-2022 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

public struct SwiftDestinationTool: ParsableCommand {
public static let configuration = CommandConfiguration(
commandName: "experimental-destination",
_superCommandName: "swift",
abstract: "Perform operations on Swift cross-compilation destinations.",
version: SwiftVersion.current.completeDisplayString,
subcommands: [
ListDestinations.self,
],
helpNames: [.short, .long, .customLong("help", withSingleDash: true)])

public init() {}
}
16 changes: 15 additions & 1 deletion Sources/SPMBuildCore/ArtifactsArchiveMetadata.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import PackageModel
import TSCBasic

import struct TSCUtility.Triple
import struct TSCUtility.Version

public struct ArtifactsArchiveMetadata: Equatable {
public let schemaVersion: String
Expand Down Expand Up @@ -42,6 +43,7 @@ public struct ArtifactsArchiveMetadata: Equatable {
// This can also support resource-only artifacts as well. For example, 3d models along with associated textures, or fonts, etc.
public enum ArtifactType: String, RawRepresentable, Decodable {
case executable
case crossCompilationDestination
}

public struct Variant: Equatable {
Expand All @@ -65,7 +67,19 @@ extension ArtifactsArchiveMetadata {
do {
let data: Data = try fileSystem.readFileContents(path)
let decoder = JSONDecoder.makeWithDefaults()
return try decoder.decode(ArtifactsArchiveMetadata.self, from: data)
let decodedMetadata = try decoder.decode(ArtifactsArchiveMetadata.self, from: data)
let version = try Version(
versionString: decodedMetadata.schemaVersion,
usesLenientParsing: true
)

switch (version.major, version.minor) {
case (1, 1), (1, 0):
return decodedMetadata
default:
throw StringError("invalid `schemaVersion` of bundle manifest at `\(path)`: \(decodedMetadata.schemaVersion)")
}

} catch {
throw StringError("failed parsing ArtifactsArchive info.json at '\(path)': \(error)")
}
Expand Down
1 change: 1 addition & 0 deletions Sources/SPMBuildCore/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ add_library(SPMBuildCore
BuildSystemCommand.swift
BuildSystemDelegate.swift
BuiltTestProduct.swift
DestinationsBundle.swift
PluginContextSerializer.swift
PluginInvocation.swift
PluginMessages.swift
Expand Down
Loading