Skip to content
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

Implement destination.json schema v3.0 #6186

Merged
merged 6 commits into from
Feb 28, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
3 changes: 2 additions & 1 deletion .swiftformat
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,5 @@
## Rules

# Prefer `&&` over `,` comma in `if`, `guard` or `while` conditions.
--disable andOperator
# Preserve `nil` default value (Optional `var`s are `nil` by default).
--disable andOperator,redundantNilInit
126 changes: 99 additions & 27 deletions Sources/CoreCommands/SwiftTool.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,22 @@ public struct ToolWorkspaceConfiguration {
let wantsMultipleTestProducts: Bool
let wantsREPLProduct: Bool

public init(wantsMultipleTestProducts: Bool = false,
wantsREPLProduct: Bool = false)
{
public init(
wantsMultipleTestProducts: Bool = false,
wantsREPLProduct: Bool = false
) {
self.wantsMultipleTestProducts = wantsMultipleTestProducts
self.wantsREPLProduct = wantsREPLProduct
}
}

public typealias WorkspaceDelegateProvider = (_ observabilityScope: ObservabilityScope, _ outputHandler: @escaping (String, Bool) -> Void, _ progressHandler: @escaping (Int64, Int64, String?) -> Void) -> WorkspaceDelegate
public typealias WorkspaceLoaderProvider = (_ fileSystem: FileSystem, _ observabilityScope: ObservabilityScope) -> WorkspaceLoader
public typealias WorkspaceDelegateProvider = (
_ observabilityScope: ObservabilityScope,
_ outputHandler: @escaping (String, Bool) -> Void,
_ progressHandler: @escaping (Int64, Int64, String?) -> Void
) -> WorkspaceDelegate
public typealias WorkspaceLoaderProvider = (_ fileSystem: FileSystem, _ observabilityScope: ObservabilityScope)
-> WorkspaceLoader

public protocol SwiftCommand: ParsableCommand {
var globalOptions: GlobalOptions { get }
Expand Down Expand Up @@ -201,17 +207,34 @@ public final class SwiftTool {
/// Create an instance of this tool.
///
/// - parameter options: The command line options to be passed to this tool.
public convenience init(options: GlobalOptions, toolWorkspaceConfiguration: ToolWorkspaceConfiguration = .init(), workspaceDelegateProvider: @escaping WorkspaceDelegateProvider, workspaceLoaderProvider: @escaping WorkspaceLoaderProvider) throws {
public convenience init(
options: GlobalOptions,
toolWorkspaceConfiguration: ToolWorkspaceConfiguration = .init(),
workspaceDelegateProvider: @escaping WorkspaceDelegateProvider,
workspaceLoaderProvider: @escaping WorkspaceLoaderProvider
) throws {
// output from background activities goes to stderr, this includes diagnostics and output from build operations,
// package resolution that take place as part of another action
// CLI commands that have user facing output, use stdout directly to emit the final result
// this means that the build output from "swift build" goes to stdout
// but the build output from "swift test" goes to stderr, while the tests output go to stdout
try self.init(outputStream: TSCBasic.stderrStream, options: options, toolWorkspaceConfiguration: toolWorkspaceConfiguration, workspaceDelegateProvider: workspaceDelegateProvider, workspaceLoaderProvider: workspaceLoaderProvider)
try self.init(
outputStream: TSCBasic.stderrStream,
options: options,
toolWorkspaceConfiguration: toolWorkspaceConfiguration,
workspaceDelegateProvider: workspaceDelegateProvider,
workspaceLoaderProvider: workspaceLoaderProvider
)
}

// marked internal for testing
internal init(outputStream: OutputByteStream, options: GlobalOptions, toolWorkspaceConfiguration: ToolWorkspaceConfiguration, workspaceDelegateProvider: @escaping WorkspaceDelegateProvider, workspaceLoaderProvider: @escaping WorkspaceLoaderProvider) throws {
internal init(
outputStream: OutputByteStream,
options: GlobalOptions,
toolWorkspaceConfiguration: ToolWorkspaceConfiguration,
workspaceDelegateProvider: @escaping WorkspaceDelegateProvider,
workspaceLoaderProvider: @escaping WorkspaceLoaderProvider
) throws {
self.fileSystem = localFileSystem
// first, bootstrap the observability system
self.logLevel = options.logging.logLevel
Expand Down Expand Up @@ -283,7 +306,8 @@ public final class SwiftTool {
var action = sigaction()
action.__sigaction_handler = unsafeBitCast(
SIG_DFL,
to: sigaction.__Unnamed_union___sigaction_handler.self)
to: sigaction.__Unnamed_union___sigaction_handler.self
)
sigaction(SIGINT, &action, nil)
kill(getpid(), SIGINT)
#endif
Expand All @@ -308,11 +332,15 @@ public final class SwiftTool {

// make sure common directories are created
self.sharedSecurityDirectory = try getSharedSecurityDirectory(options: options, fileSystem: fileSystem)
self.sharedConfigurationDirectory = try getSharedConfigurationDirectory(options: options, fileSystem: fileSystem)
self.sharedCacheDirectory = try getSharedCacheDirectory(options: options, fileSystem: fileSystem)
self.sharedCrossCompilationDestinationsDirectory = try fileSystem.getSharedCrossCompilationDestinationsDirectory(
explicitDirectory: options.locations.crossCompilationDestinationsDirectory
self.sharedConfigurationDirectory = try getSharedConfigurationDirectory(
options: options,
fileSystem: fileSystem
)
self.sharedCacheDirectory = try getSharedCacheDirectory(options: options, fileSystem: fileSystem)
self.sharedCrossCompilationDestinationsDirectory = try fileSystem
.getSharedCrossCompilationDestinationsDirectory(
explicitDirectory: options.locations.crossCompilationDestinationsDirectory
)

// set global process logging handler
Process.loggingHandler = { self.observabilityScope.emit(debug: $0) }
Expand All @@ -334,12 +362,18 @@ public final class SwiftTool {
// --enable-test-discovery should never be called on darwin based platforms
#if canImport(Darwin)
if options.build.enableTestDiscovery {
observabilityScope.emit(warning: "'--enable-test-discovery' option is deprecated; tests are automatically discovered on all platforms")
observabilityScope
.emit(
warning: "'--enable-test-discovery' option is deprecated; tests are automatically discovered on all platforms"
)
}
#endif

if options.caching.shouldDisableManifestCaching {
observabilityScope.emit(warning: "'--disable-package-manifest-caching' option is deprecated; use '--manifest-caching' instead")
observabilityScope
.emit(
warning: "'--disable-package-manifest-caching' option is deprecated; use '--manifest-caching' instead"
)
}

if let _ = options.security.netrcFilePath, options.security.netrc == false {
Expand All @@ -357,7 +391,11 @@ public final class SwiftTool {
return workspace
}

let delegate = self.workspaceDelegateProvider(self.observabilityScope, self.observabilityHandler.print, self.observabilityHandler.progress)
let delegate = self.workspaceDelegateProvider(
self.observabilityScope,
self.observabilityHandler.print,
self.observabilityHandler.progress
)
let isXcodeBuildSystemEnabled = self.options.build.buildSystem == .xcode
let workspace = try Workspace(
fileSystem: self.fileSystem,
Expand Down Expand Up @@ -417,13 +455,25 @@ public final class SwiftTool {
if let multiRootPackageDataFile = options.locations.multirootPackageDataFile {
// migrate from legacy location
let legacyPath = multiRootPackageDataFile.appending(components: "xcshareddata", "swiftpm", "config")
let newPath = Workspace.DefaultLocations.mirrorsConfigurationFile(at: multiRootPackageDataFile.appending(components: "xcshareddata", "swiftpm", "configuration"))
return try Workspace.migrateMirrorsConfiguration(from: legacyPath, to: newPath, observabilityScope: observabilityScope)
let newPath = Workspace.DefaultLocations
.mirrorsConfigurationFile(
at: multiRootPackageDataFile
.appending(components: "xcshareddata", "swiftpm", "configuration")
)
return try Workspace.migrateMirrorsConfiguration(
from: legacyPath,
to: newPath,
observabilityScope: observabilityScope
)
} else {
// migrate from legacy location
let legacyPath = try self.getPackageRoot().appending(components: ".swiftpm", "config")
let newPath = try Workspace.DefaultLocations.mirrorsConfigurationFile(forRootPackage: self.getPackageRoot())
return try Workspace.migrateMirrorsConfiguration(from: legacyPath, to: newPath, observabilityScope: observabilityScope)
return try Workspace.migrateMirrorsConfiguration(
from: legacyPath,
to: newPath,
observabilityScope: observabilityScope
)
}
}

Expand All @@ -441,7 +491,10 @@ public final class SwiftTool {
authorization.keychain = self.options.security.keychain ? .enabled : .disabled
#endif

return try authorization.makeAuthorizationProvider(fileSystem: self.fileSystem, observabilityScope: self.observabilityScope)
return try authorization.makeAuthorizationProvider(
fileSystem: self.fileSystem,
observabilityScope: self.observabilityScope
)
}

public func getRegistryAuthorizationProvider() throws -> AuthorizationProvider? {
Expand All @@ -457,7 +510,10 @@ public final class SwiftTool {
authorization.keychain = self.options.security.forceNetrc ? .disabled : .enabled
#endif

return try authorization.makeRegistryAuthorizationProvider(fileSystem: self.fileSystem, observabilityScope: self.observabilityScope)
return try authorization.makeRegistryAuthorizationProvider(
fileSystem: self.fileSystem,
observabilityScope: self.observabilityScope
)
}

/// Resolve the dependencies.
Expand All @@ -481,7 +537,8 @@ public final class SwiftTool {
/// Fetch and load the complete package graph.
///
/// - Parameters:
/// - explicitProduct: The product specified on the command line to a “swift run” or “swift build” command. This allows executables from dependencies to be run directly without having to hook them up to any particular target.
/// - explicitProduct: The product specified on the command line to a “swift run” or “swift build” command. This
/// allows executables from dependencies to be run directly without having to hook them up to any particular target.
@discardableResult
public func loadPackageGraph(
explicitProduct: String? = nil,
Expand Down Expand Up @@ -654,17 +711,32 @@ public final class SwiftTool {

// Create custom toolchain if present.
if let customDestination = options.locations.customCompileDestination {
destination = try Destination(fromFile: customDestination, fileSystem: fileSystem)
} else if let target = options.build.customCompileTriple,
let targetDestination = Destination.defaultDestination(for: target, host: hostDestination) {
let destinations = try Destination.decode(
fromFile: customDestination,
fileSystem: fileSystem,
observability: observabilityScope
)
if destinations.count == 1 {
destination = destinations[0]
} else if destinations.count > 1,
let triple = options.build.customCompileTriple,
let matchingDestination = destinations.first(where: { $0.targetTriple == triple })
{
destination = matchingDestination
} else {
return .failure(DestinationError.noDestinationsDecoded(customDestination))
}
} else if let triple = options.build.customCompileTriple,
let targetDestination = Destination.defaultDestination(for: triple, host: hostDestination)
{
destination = targetDestination
} else if let destinationSelector = options.build.crossCompilationDestinationSelector {
destination = try DestinationsBundle.selectDestination(
fromBundlesAt: sharedCrossCompilationDestinationsDirectory,
fileSystem: fileSystem,
matching: destinationSelector,
hostTriple: hostTriple,
observabilityScope: observabilityScope
observability: observabilityScope
MaxDesiatov marked this conversation as resolved.
Show resolved Hide resolved
)
} else {
// Otherwise use the host toolchain.
Expand All @@ -678,7 +750,7 @@ public final class SwiftTool {
destination.targetTriple = triple
}
if let binDir = options.build.customCompileToolchain {
destination.toolchainBinDir = binDir.appending(components: "usr", "bin")
destination.add(toolsetRootPath: binDir.appending(components: "usr", "bin"))
}
if let sdk = options.build.customCompileSDK {
destination.sdkRootDir = sdk
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@
import ArgumentParser
import Basics
import CoreCommands
import SPMBuildCore
import PackageModel
import SPMBuildCore
import TSCBasic

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

@OptionGroup()
Expand All @@ -34,7 +34,7 @@ public struct ListDestinations: ParsableCommand {
public func run() throws {
let fileSystem = localFileSystem
let observabilitySystem = ObservabilitySystem.swiftTool()
let observabilityScope = observabilitySystem.topScope
let scope = observabilitySystem.topScope
MaxDesiatov marked this conversation as resolved.
Show resolved Hide resolved

guard var destinationsDirectory = try fileSystem.getSharedCrossCompilationDestinationsDirectory(
explicitDirectory: locations.crossCompilationDestinationsDirectory
Expand All @@ -52,7 +52,7 @@ public struct ListDestinations: ParsableCommand {
let validBundles = try DestinationsBundle.getAllValidBundles(
destinationsDirectory: destinationsDirectory,
fileSystem: fileSystem,
observabilityScope: observabilityScope
observability: scope
)

for bundle in validBundles {
Expand Down
31 changes: 15 additions & 16 deletions Sources/PackageModel/BuildFlags.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,33 +12,32 @@

/// Build-tool independent flags.
public struct BuildFlags: Equatable, Encodable {

/// Flags to pass to the C compiler.
public var cCompilerFlags: [String]

/// Flags to pass to the C++ compiler.
public var cxxCompilerFlags: [String]

/// Flags to pass to the Swift compiler.
public var swiftCompilerFlags: [String]

/// Flags to pass to the linker.
public var linkerFlags: [String]

/// Flags to pass to xcbuild.
public var xcbuildFlags: [String]?

public init(
cCompilerFlags: [String]? = .none,
cxxCompilerFlags: [String]? = .none,
swiftCompilerFlags: [String]? = .none,
linkerFlags: [String]? = .none,
xcbuildFlags: [String]? = .none
cCompilerFlags: [String] = [],
cxxCompilerFlags: [String] = [],
swiftCompilerFlags: [String] = [],
linkerFlags: [String] = [],
xcbuildFlags: [String] = []
) {
self.cCompilerFlags = cCompilerFlags ?? []
self.cxxCompilerFlags = cxxCompilerFlags ?? []
self.swiftCompilerFlags = swiftCompilerFlags ?? []
self.linkerFlags = linkerFlags ?? []
self.xcbuildFlags = xcbuildFlags ?? []
self.cCompilerFlags = cCompilerFlags
self.cxxCompilerFlags = cxxCompilerFlags
self.swiftCompilerFlags = swiftCompilerFlags
self.linkerFlags = linkerFlags
self.xcbuildFlags = xcbuildFlags
}
}
Loading