Skip to content

Prepare SPM Targets for Process migration by specifying that callee should use TSCBasic.Process #5609

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 1 commit into from
Jun 26, 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
8 changes: 4 additions & 4 deletions Sources/Basics/Archiver+Zip.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ public struct ZipArchiver: Archiver, Cancellable {
}

#if os(Windows)
let process = Process(arguments: ["tar.exe", "xf", archivePath.pathString, "-C", destinationPath.pathString])
let process = TSCBasic.Process(arguments: ["tar.exe", "xf", archivePath.pathString, "-C", destinationPath.pathString])
#else
let process = Process(arguments: ["unzip", archivePath.pathString, "-d", destinationPath.pathString])
let process = TSCBasic.Process(arguments: ["unzip", archivePath.pathString, "-d", destinationPath.pathString])
#endif
guard let registrationKey = self.cancellator.register(process) else {
throw StringError("cancellation")
Expand Down Expand Up @@ -77,9 +77,9 @@ public struct ZipArchiver: Archiver, Cancellable {
}

#if os(Windows)
let process = Process(arguments: ["tar.exe", "tf", path.pathString])
let process = TSCBasic.Process(arguments: ["tar.exe", "tf", path.pathString])
#else
let process = Process(arguments: ["unzip", "-t", path.pathString])
let process = TSCBasic.Process(arguments: ["unzip", "-t", path.pathString])
#endif
guard let registrationKey = self.cancellator.register(process) else {
throw StringError("cancellation")
Expand Down
2 changes: 1 addition & 1 deletion Sources/Basics/Cancellator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public class Cancellator: Cancellable {
}

public func register(_ process: TSCBasic.Process) -> RegistrationKey? {
self.register(name: "\(process.arguments.joined(separator: " "))", handler: process.terminate)
self.register(name: "\(process.arguments.joined(separator: " "))", handler: process.terminate)
}

#if !os(iOS) && !os(watchOS) && !os(tvOS)
Expand Down
2 changes: 1 addition & 1 deletion Sources/Build/BuildOperation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ public final class BuildOperation: PackageStructureDelegate, SPMBuildCore.BuildS
if !self.disableSandboxForPluginCommands {
commandLine = Sandbox.apply(command: commandLine, strictness: .writableTemporaryDirectory, writableDirectories: [pluginResult.pluginOutputDirectory])
}
let processResult = try Process.popen(arguments: commandLine, environment: command.configuration.environment)
let processResult = try TSCBasic.Process.popen(arguments: commandLine, environment: command.configuration.environment)
let output = try processResult.utf8Output() + processResult.utf8stderrOutput()
if processResult.exitStatus != .terminated(code: 0) {
throw StringError("failed: \(command)\n\n\(output)")
Expand Down
140 changes: 70 additions & 70 deletions Sources/Build/SPMSwiftDriverExecutor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,78 +16,78 @@ import Foundation
@_implementationOnly import SwiftDriver

final class SPMSwiftDriverExecutor: DriverExecutor {

private enum Error: Swift.Error, CustomStringConvertible {
case inPlaceExecutionUnsupported

var description: String {
switch self {
case .inPlaceExecutionUnsupported:
return "the integrated Swift driver does not support in-place execution"
}

private enum Error: Swift.Error, CustomStringConvertible {
case inPlaceExecutionUnsupported

var description: String {
switch self {
case .inPlaceExecutionUnsupported:
return "the integrated Swift driver does not support in-place execution"
}
}
}
}

let resolver: ArgsResolver
let fileSystem: FileSystem
let env: EnvironmentVariables

init(resolver: ArgsResolver,
fileSystem: FileSystem,
env: EnvironmentVariables) {
self.resolver = resolver
self.fileSystem = fileSystem
self.env = env
}

func execute(job: Job,
forceResponseFiles: Bool,
recordedInputModificationDates: [TypedVirtualPath : Date]) throws -> ProcessResult {
let arguments: [String] = try resolver.resolveArgumentList(for: job,
forceResponseFiles: forceResponseFiles)

try job.verifyInputsNotModified(since: recordedInputModificationDates,
fileSystem: fileSystem)

if job.requiresInPlaceExecution {
throw Error.inPlaceExecutionUnsupported

let resolver: ArgsResolver
let fileSystem: FileSystem
let env: EnvironmentVariables

init(resolver: ArgsResolver,
fileSystem: FileSystem,
env: EnvironmentVariables) {
self.resolver = resolver
self.fileSystem = fileSystem
self.env = env
}

var childEnv = env
childEnv.merge(job.extraEnvironment, uniquingKeysWith: { (_, new) in new })

let process = try Process.launchProcess(arguments: arguments, env: childEnv)
return try process.waitUntilExit()
}

func execute(workload: DriverExecutorWorkload,
delegate: JobExecutionDelegate,
numParallelJobs: Int, forceResponseFiles: Bool,
recordedInputModificationDates: [TypedVirtualPath : Date]) throws {
throw InternalError("Multi-job build plans should be lifted into the SPM build graph.")
}

func checkNonZeroExit(args: String..., environment: [String : String]) throws -> String {
return try Process.checkNonZeroExit(arguments: args, environment: environment)
}

func description(of job: Job, forceResponseFiles: Bool) throws -> String {
// FIXME: This is duplicated from SwiftDriver, maybe it shouldn't be a protocol requirement.
let (args, usedResponseFile) = try resolver.resolveArgumentList(for: job,
forceResponseFiles: forceResponseFiles)
var result = args.joined(separator: " ")

if usedResponseFile {
// Print the response file arguments as a comment.
result += " # \(job.commandLine.joinedUnresolvedArguments)"

func execute(job: Job,
forceResponseFiles: Bool,
recordedInputModificationDates: [TypedVirtualPath : Date]) throws -> ProcessResult {
let arguments: [String] = try resolver.resolveArgumentList(for: job,
forceResponseFiles: forceResponseFiles)

try job.verifyInputsNotModified(since: recordedInputModificationDates,
fileSystem: fileSystem)

if job.requiresInPlaceExecution {
throw Error.inPlaceExecutionUnsupported
}

var childEnv = env
childEnv.merge(job.extraEnvironment, uniquingKeysWith: { (_, new) in new })

let process = try Process.launchProcess(arguments: arguments, env: childEnv)
return try process.waitUntilExit()
}

if !job.extraEnvironment.isEmpty {
result += " #"
for (envVar, val) in job.extraEnvironment {
result += " \(envVar)=\(val)"
}

func execute(workload: DriverExecutorWorkload,
delegate: JobExecutionDelegate,
numParallelJobs: Int, forceResponseFiles: Bool,
recordedInputModificationDates: [TypedVirtualPath : Date]) throws {
throw InternalError("Multi-job build plans should be lifted into the SPM build graph.")
}

func checkNonZeroExit(args: String..., environment: [String : String]) throws -> String {
return try TSCBasic.Process.checkNonZeroExit(arguments: args, environment: environment)
}

func description(of job: Job, forceResponseFiles: Bool) throws -> String {
// FIXME: This is duplicated from SwiftDriver, maybe it shouldn't be a protocol requirement.
let (args, usedResponseFile) = try resolver.resolveArgumentList(for: job,
forceResponseFiles: forceResponseFiles)
var result = args.joined(separator: " ")

if usedResponseFile {
// Print the response file arguments as a comment.
result += " # \(job.commandLine.joinedUnresolvedArguments)"
}

if !job.extraEnvironment.isEmpty {
result += " #"
for (envVar, val) in job.extraEnvironment {
result += " \(envVar)=\(val)"
}
}
return result
}
return result
}
}
4 changes: 2 additions & 2 deletions Sources/Commands/SwiftBuildTool.swift
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,9 @@ public struct SwiftBuildTool: SwiftCommand {

private func checkClangVersion(observabilityScope: ObservabilityScope) {
// We only care about this on Ubuntu 14.04
guard let uname = try? Process.checkNonZeroExit(args: "lsb_release", "-r").spm_chomp(),
guard let uname = try? TSCBasic.Process.checkNonZeroExit(args: "lsb_release", "-r").spm_chomp(),
uname.hasSuffix("14.04"),
let clangVersionOutput = try? Process.checkNonZeroExit(args: "clang", "--version").spm_chomp(),
let clangVersionOutput = try? TSCBasic.Process.checkNonZeroExit(args: "clang", "--version").spm_chomp(),
let clang = getClangVersion(versionOutput: clangVersionOutput) else {
return
}
Expand Down
6 changes: 3 additions & 3 deletions Sources/Commands/SwiftPackageTool.swift
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ extension SwiftPackageTool {
let args = [swiftFormat.pathString] + formatOptions + [packagePath.pathString] + paths
print("Running:", args.map{ $0.spm_shellEscaped() }.joined(separator: " "))

let result = try Process.popen(arguments: args)
let result = try TSCBasic.Process.popen(arguments: args)
let output = try (result.utf8Output() + result.utf8stderrOutput())

if result.exitStatus != .terminated(code: 0) {
Expand Down Expand Up @@ -1358,7 +1358,7 @@ final class PluginDelegate: PluginInvocationDelegate {
llvmProfCommand.append(filePath.pathString)
}
llvmProfCommand += ["-o", mergedCovFile.pathString]
try Process.checkNonZeroExit(arguments: llvmProfCommand)
try TSCBasic.Process.checkNonZeroExit(arguments: llvmProfCommand)

// Use `llvm-cov` to export the merged `.profdata` file contents in JSON form.
var llvmCovCommand = [try toolchain.getLLVMCov().pathString]
Expand All @@ -1368,7 +1368,7 @@ final class PluginDelegate: PluginInvocationDelegate {
llvmCovCommand.append(product.binaryPath.pathString)
}
// We get the output on stdout, and have to write it to a JSON ourselves.
let jsonOutput = try Process.checkNonZeroExit(arguments: llvmCovCommand)
let jsonOutput = try TSCBasic.Process.checkNonZeroExit(arguments: llvmCovCommand)
let jsonCovFile = buildParameters.codeCovDataFile.parentDirectory.appending(component: buildParameters.codeCovDataFile.basenameWithoutExt + ".json")
try swiftTool.fileSystem.writeFileContents(jsonCovFile, string: jsonOutput)

Expand Down
6 changes: 3 additions & 3 deletions Sources/Commands/SwiftTestTool.swift
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ public struct SwiftTestTool: SwiftCommand {
}
args += ["-o", buildParameters.codeCovDataFile.pathString]

try Process.checkNonZeroExit(arguments: args)
try TSCBasic.Process.checkNonZeroExit(arguments: args)
}

private func codeCovAsJSONPath(buildParameters: BuildParameters, packageName: String) -> AbsolutePath {
Expand All @@ -454,7 +454,7 @@ public struct SwiftTestTool: SwiftCommand {
"-instr-profile=\(buildParameters.codeCovDataFile)",
testBinary.pathString
]
let result = try Process.popen(arguments: args)
let result = try TSCBasic.Process.popen(arguments: args)

if result.exitStatus != .terminated(code: 0) {
let output = try result.utf8Output() + result.utf8stderrOutput()
Expand Down Expand Up @@ -621,7 +621,7 @@ final class TestRunner {
stdout: outputHandler,
stderr: outputHandler
)
let process = Process(arguments: try args(forTestAt: path), environment: self.testEnv, outputRedirection: outputRedirection)
let process = TSCBasic.Process(arguments: try args(forTestAt: path), environment: self.testEnv, outputRedirection: outputRedirection)
guard let terminationKey = self.cancellator.register(process) else {
return false // terminating
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/Commands/Utilities/APIDigester.swift
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ public struct SwiftAPIDigester {

private func runTool(_ args: [String]) throws {
let arguments = [tool.pathString] + args
let process = Process(
let process = TSCBasic.Process(
arguments: arguments,
outputRedirection: .collect
)
Expand Down
4 changes: 2 additions & 2 deletions Sources/Commands/Utilities/SymbolGraphExtract.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public struct SymbolGraphExtract {
public func extractSymbolGraph(
target: ResolvedTarget,
buildPlan: BuildPlan,
outputRedirection: Process.OutputRedirection = .none,
outputRedirection: TSCBasic.Process.OutputRedirection = .none,
outputDirectory: AbsolutePath,
verboseOutput: Bool
) throws {
Expand Down Expand Up @@ -80,7 +80,7 @@ public struct SymbolGraphExtract {
commandLine += ["-output-dir", outputDirectory.pathString]

// Run the extraction.
let process = Process(
let process = TSCBasic.Process(
arguments: commandLine,
outputRedirection: outputRedirection
)
Expand Down
2 changes: 1 addition & 1 deletion Sources/Commands/Utilities/TestingSupport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ enum TestingSupport {
env.appendPath("DYLD_FRAMEWORK_PATH", value: sdkPlatformFrameworksPath.fwk.pathString)
env.appendPath("DYLD_LIBRARY_PATH", value: sdkPlatformFrameworksPath.lib.pathString)
}
try Process.checkNonZeroExit(arguments: args, environment: env)
try TSCBasic.Process.checkNonZeroExit(arguments: args, environment: env)
// Read the temporary file's content.
return try swiftTool.fileSystem.readFileContents(tempFile.path)
}
Expand Down
6 changes: 3 additions & 3 deletions Sources/PackageLoading/ManifestLoader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,7 @@ public final class ManifestLoader: ManifestLoaderProtocol {
cmd += ["-o", compiledManifestFile.pathString]

// Compile the manifest.
Process.popen(arguments: cmd, environment: self.toolchain.swiftCompilerEnvironment, queue: callbackQueue) { result in
TSCBasic.Process.popen(arguments: cmd, environment: self.toolchain.swiftCompilerEnvironment, queue: callbackQueue) { result in
dispatchPrecondition(condition: .onQueue(callbackQueue))

var cleanupIfError = DelayableAction(target: tmpDir, action: cleanupTmpDir)
Expand Down Expand Up @@ -648,7 +648,7 @@ public final class ManifestLoader: ManifestLoaderProtocol {
#endif

let cleanupAfterRunning = cleanupIfError.delay()
Process.popen(arguments: cmd, environment: environment, queue: callbackQueue) { result in
TSCBasic.Process.popen(arguments: cmd, environment: environment, queue: callbackQueue) { result in
dispatchPrecondition(condition: .onQueue(callbackQueue))

defer { cleanupAfterRunning.perform() }
Expand Down Expand Up @@ -697,7 +697,7 @@ public final class ManifestLoader: ManifestLoaderProtocol {
var sdkRootPath: AbsolutePath? = nil
// Find SDKROOT on macOS using xcrun.
#if os(macOS)
let foundPath = try? Process.checkNonZeroExit(
let foundPath = try? TSCBasic.Process.checkNonZeroExit(
args: "/usr/bin/xcrun", "--sdk", "macosx", "--show-sdk-path")
guard let sdkRoot = foundPath?.spm_chomp(), !sdkRoot.isEmpty else {
return nil
Expand Down
2 changes: 1 addition & 1 deletion Sources/PackageLoading/PkgConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ internal struct PCFileFinder {
private init(pkgConfigPath: String) {
if PCFileFinder.pkgConfigPaths == nil {
do {
let searchPaths = try Process.checkNonZeroExit(args:
let searchPaths = try TSCBasic.Process.checkNonZeroExit(args:
pkgConfigPath, "--variable", "pc_path", "pkg-config"
).spm_chomp()

Expand Down
2 changes: 1 addition & 1 deletion Sources/PackageLoading/Target+PkgConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ extension SystemPackageProviderDescription {
// to the latest version. Instead use the version as symlinked
// in /usr/local/opt/(NAME)/lib/pkgconfig.
struct Static {
static let value = { try? Process.checkNonZeroExit(args: "brew", "--prefix").spm_chomp() }()
static let value = { try? TSCBasic.Process.checkNonZeroExit(args: "brew", "--prefix").spm_chomp() }()
}
if let value = Static.value {
brewPrefix = value
Expand Down
4 changes: 2 additions & 2 deletions Sources/PackageModel/Destination.swift
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public struct Destination: Encodable, Equatable {
sdkPath = value
} else {
// No value in env, so search for it.
let sdkPathStr = try Process.checkNonZeroExit(
let sdkPathStr = try TSCBasic.Process.checkNonZeroExit(
arguments: ["/usr/bin/xcrun", "--sdk", "macosx", "--show-sdk-path"], environment: environment).spm_chomp()
guard !sdkPathStr.isEmpty else {
throw DestinationError.invalidInstallation("default SDK not found")
Expand Down Expand Up @@ -177,7 +177,7 @@ public struct Destination: Encodable, Equatable {
if let path = _sdkPlatformFrameworkPath {
return path
}
let platformPath = try? Process.checkNonZeroExit(
let platformPath = try? TSCBasic.Process.checkNonZeroExit(
arguments: ["/usr/bin/xcrun", "--sdk", "macosx", "--show-sdk-platform-path"],
environment: environment).spm_chomp()

Expand Down
4 changes: 2 additions & 2 deletions Sources/PackageModel/UserToolchain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public final class UserToolchain: Toolchain {
private static func findTool(_ name: String, envSearchPaths: [AbsolutePath], useXcrun: Bool) throws -> AbsolutePath {
if useXcrun {
#if os(macOS)
let foundPath = try Process.checkNonZeroExit(arguments: ["/usr/bin/xcrun", "--find", name]).spm_chomp()
let foundPath = try TSCBasic.Process.checkNonZeroExit(arguments: ["/usr/bin/xcrun", "--find", name]).spm_chomp()
return try AbsolutePath(validating: foundPath)
#endif
}
Expand Down Expand Up @@ -477,7 +477,7 @@ public final class UserToolchain: Toolchain {
if triple.isDarwin() {
// XCTest is optional on macOS, for example when Xcode is not installed
let xctestFindArgs = ["/usr/bin/xcrun", "--sdk", "macosx", "--find", "xctest"]
if let path = try? Process.checkNonZeroExit(arguments: xctestFindArgs, environment: environment).spm_chomp() {
if let path = try? TSCBasic.Process.checkNonZeroExit(arguments: xctestFindArgs, environment: environment).spm_chomp() {
return try AbsolutePath(validating: path)
}
} else if triple.isWindows() {
Expand Down
4 changes: 2 additions & 2 deletions Sources/SourceControl/GitRepository.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ private struct GitShellHelper {
/// Private function to invoke the Git tool with its default environment and given set of arguments. The specified
/// failure message is used only in case of error. This function waits for the invocation to finish and returns the
/// output as a string.
func run(_ args: [String], environment: EnvironmentVariables = Git.environment, outputRedirection: Process.OutputRedirection = .collect) throws -> String {
let process = Process(arguments: [Git.tool] + args, environment: environment, outputRedirection: outputRedirection)
func run(_ args: [String], environment: EnvironmentVariables = Git.environment, outputRedirection: TSCBasic.Process.OutputRedirection = .collect) throws -> String {
let process = TSCBasic.Process(arguments: [Git.tool] + args, environment: environment, outputRedirection: outputRedirection)
let result: ProcessResult
do {
guard let terminationKey = self.cancellator.register(process) else {
Expand Down
Loading