From d6facec080bdb3e926d73ed5a5122616a2acde6d Mon Sep 17 00:00:00 2001 From: stevapple Date: Tue, 10 Aug 2021 01:38:48 +0800 Subject: [PATCH 1/5] Add swift-script shortcut --- Package.resolved | 2 +- Sources/SwiftDriver/CMakeLists.txt | 4 +- Sources/SwiftDriver/Jobs/CheckScriptJob.swift | 56 ++++++++++++++++ Sources/SwiftDriver/Jobs/Job.swift | 18 +++-- Sources/SwiftDriver/Jobs/Planning.swift | 10 ++- Sources/SwiftDriver/Jobs/RunScriptJob.swift | 65 +++++++++++++++++++ 6 files changed, 147 insertions(+), 8 deletions(-) create mode 100644 Sources/SwiftDriver/Jobs/CheckScriptJob.swift create mode 100644 Sources/SwiftDriver/Jobs/RunScriptJob.swift diff --git a/Package.resolved b/Package.resolved index 5fd667d0f..39195a5e5 100644 --- a/Package.resolved +++ b/Package.resolved @@ -11,7 +11,7 @@ } }, { - "package": "swift-llbuild", + "package": "llbuild", "repositoryURL": "https://github.com/apple/swift-llbuild.git", "state": { "branch": "main", diff --git a/Sources/SwiftDriver/CMakeLists.txt b/Sources/SwiftDriver/CMakeLists.txt index 72cf9f2d1..2888f3b14 100644 --- a/Sources/SwiftDriver/CMakeLists.txt +++ b/Sources/SwiftDriver/CMakeLists.txt @@ -1,6 +1,6 @@ # This source file is part of the Swift.org open source project # -# Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors +# Copyright (c) 2014 - 2021 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 @@ -57,6 +57,7 @@ add_library(SwiftDriver Jobs/APIDigesterJobs.swift Jobs/AutolinkExtractJob.swift Jobs/BackendJob.swift + Jobs/CheckScriptJob.swift Jobs/CommandLineArguments.swift Jobs/CompileJob.swift Jobs/DarwinToolchain+LinkerSupport.swift @@ -75,6 +76,7 @@ add_library(SwiftDriver Jobs/Planning.swift Jobs/PrintTargetInfoJob.swift Jobs/ReplJob.swift + Jobs/RunScriptJob.swift Jobs/Toolchain+InterpreterSupport.swift Jobs/Toolchain+LinkerSupport.swift Jobs/VerifyDebugInfoJob.swift diff --git a/Sources/SwiftDriver/Jobs/CheckScriptJob.swift b/Sources/SwiftDriver/Jobs/CheckScriptJob.swift new file mode 100644 index 000000000..df82a4070 --- /dev/null +++ b/Sources/SwiftDriver/Jobs/CheckScriptJob.swift @@ -0,0 +1,56 @@ +//===-------- CheckScriptJob.swift - Swift Script Prechecking Job --------===// +// +// This source file is part of the Swift.org open source project +// +// Copyright (c) 2021 Apple Inc. and the Swift project authors +// Licensed under Apache License v2.0 with Runtime Library Exception +// +// See https://swift.org/LICENSE.txt for license information +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors +// +//===----------------------------------------------------------------------===// +import TSCBasic + +public enum ScriptingError: Error, DiagnosticData { + case tooManyInputFiles + case noSwiftScript + + public var description: String { + switch self { + case .tooManyInputFiles: + return "Scripting mode expect exactly 1 input file" + + case .noSwiftScript: + return "'swift-script' tool not found" + } + } +} + +extension Driver { + /// Link the given inputs. + func checkScriptJob(inputs: [TypedVirtualPath]) throws -> Job { + var commandLine: [Job.ArgTemplate] = [] + commandLine.appendFlags("-frontend", "-print-package-declarations") + + // Check and add the inputs. + guard inputs.count <= 1 else { + throw ScriptingError.tooManyInputFiles + } + for input in inputs { + commandLine.append(.path(input.file)) + } + print(commandLine) + + return Job( + moduleName: moduleOutputInfo.name, + kind: .checkScript, + tool: .absolute(try toolchain.getToolPath(.swiftCompiler)), + commandLine: commandLine, + inputs: inputs, + primaryInputs: [], + outputs: [], + requiresInPlaceExecution: false, + supportsResponseFiles: true + ) + } +} diff --git a/Sources/SwiftDriver/Jobs/Job.swift b/Sources/SwiftDriver/Jobs/Job.swift index c180041ef..313d87e72 100644 --- a/Sources/SwiftDriver/Jobs/Job.swift +++ b/Sources/SwiftDriver/Jobs/Job.swift @@ -2,7 +2,7 @@ // // This source file is part of the Swift.org open source project // -// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors +// Copyright (c) 2014 - 2021 Apple Inc. and the Swift project authors // Licensed under Apache License v2.0 with Runtime Library Exception // // See https://swift.org/LICENSE.txt for license information @@ -41,6 +41,9 @@ public struct Job: Codable, Equatable, Hashable { case generateABIBaseline = "generate-abi-baseline" case compareAPIBaseline = "compare-api-baseline" case compareABIBaseline = "compare-abi-baseline" + + case checkScript = "check-script" + case runScript = "run-script" } public enum ArgTemplate: Equatable, Hashable { @@ -236,6 +239,12 @@ extension Job : CustomStringConvertible { case .compareABIBaseline: return "Comparing ABI of \(moduleName) to baseline" + + case .checkScript: + return "Checking if \(displayInputs.first?.file.basename ?? "") requires scripting mode" + + case .runScript: + return "Running a script with SwiftPM" } } @@ -254,12 +263,13 @@ extension Job.Kind { public var isSwiftFrontend: Bool { switch self { case .backend, .compile, .mergeModule, .emitModule, .generatePCH, - .generatePCM, .dumpPCM, .interpret, .repl, .printTargetInfo, + .generatePCM, .dumpPCM, .interpret, .repl, .printTargetInfo, .checkScript, .versionRequest, .emitSupportedFeatures, .scanDependencies, .verifyModuleInterface: return true case .autolinkExtract, .generateDSYM, .help, .link, .verifyDebugInfo, .moduleWrap, - .generateAPIBaseline, .generateABIBaseline, .compareAPIBaseline, .compareABIBaseline: + .generateAPIBaseline, .generateABIBaseline, .compareAPIBaseline, .compareABIBaseline, + .runScript: return false } } @@ -275,7 +285,7 @@ extension Job.Kind { .help, .link, .verifyDebugInfo, .scanDependencies, .emitSupportedFeatures, .moduleWrap, .verifyModuleInterface, .generateAPIBaseline, .generateABIBaseline, .compareAPIBaseline, - .compareABIBaseline: + .compareABIBaseline, .checkScript, .runScript: return false } } diff --git a/Sources/SwiftDriver/Jobs/Planning.swift b/Sources/SwiftDriver/Jobs/Planning.swift index a2499e72f..d82ac1574 100644 --- a/Sources/SwiftDriver/Jobs/Planning.swift +++ b/Sources/SwiftDriver/Jobs/Planning.swift @@ -2,7 +2,7 @@ // // This source file is part of the Swift.org open source project // -// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors +// Copyright (c) 2014 - 2021 Apple Inc. and the Swift project authors // Licensed under Apache License v2.0 with Runtime Library Exception // // See https://swift.org/LICENSE.txt for license information @@ -687,7 +687,13 @@ extension Driver { case .immediate: var jobs: [Job] = [] try addPrecompileModuleDependenciesJobs(addJob: { jobs.append($0) }) - jobs.append(try interpretJob(inputs: inputFiles)) + let result = try executor.execute(job: checkScriptJob(inputs: inputFiles), forceResponseFiles: true, + recordedInputModificationDates: recordedInputModificationDates) + if try result.utf8Output().hasPrefix("[]") { + jobs.append(try interpretJob(inputs: inputFiles)) + } else { + jobs.append(try runScriptJob(inputs: inputFiles)) + } return (jobs, nil) case .standardCompile, .batchCompile, .singleCompile: diff --git a/Sources/SwiftDriver/Jobs/RunScriptJob.swift b/Sources/SwiftDriver/Jobs/RunScriptJob.swift new file mode 100644 index 000000000..abcae76a3 --- /dev/null +++ b/Sources/SwiftDriver/Jobs/RunScriptJob.swift @@ -0,0 +1,65 @@ +//===--------------- RunScriptJob.swift - Swift Scripting Mode ------------===// +// +// This source file is part of the Swift.org open source project +// +// Copyright (c) 2021 Apple Inc. and the Swift project authors +// Licensed under Apache License v2.0 with Runtime Library Exception +// +// See https://swift.org/LICENSE.txt for license information +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors +// +//===----------------------------------------------------------------------===// + +extension Driver { + mutating func runScriptJob(inputs allInputs: [TypedVirtualPath]) throws -> Job { + let swiftScriptTool = try toolchain.getToolPath(.swiftCompiler).parentDirectory.appending(component: "swift-script") + guard fileSystem.isExecutableFile(swiftScriptTool) else { + throw ScriptingError.noSwiftScript + } + var rawCommandline: [Job.ArgTemplate] = swiftCompilerPrefixArgs.map { Job.ArgTemplate.flag($0) } + var inputs: [TypedVirtualPath] = [] + + // Add the inputs. + for input in allInputs { + rawCommandline.append(.path(input.file)) + inputs.append(input) + } + + if parsedOptions.hasArgument(.parseStdlib) { + rawCommandline.appendFlag(.disableObjcAttrRequiresFoundationModule) + } + + try addCommonFrontendOptions(commandLine: &rawCommandline, inputs: &inputs) + // FIXME: MSVC runtime flags + + try rawCommandline.appendLast(.parseSil, from: &parsedOptions) + try rawCommandline.appendAll(.l, .framework, from: &parsedOptions) + + // The immediate arguments must be last. + try rawCommandline.appendLast(.DASHDASH, from: &parsedOptions) + + var commandline: [Job.ArgTemplate] = [] + for arg in rawCommandline { + commandline.appendFlag("-Xfrontend") + commandline.append(arg) + } + commandline.appendFlag("--quiet") + + let extraEnvironment = try toolchain.platformSpecificInterpreterEnvironmentVariables( + env: self.env, parsedOptions: &parsedOptions, + sdkPath: frontendTargetInfo.sdkPath?.path, targetInfo: self.frontendTargetInfo) + + return Job( + moduleName: moduleOutputInfo.name, + kind: .runScript, + tool: .absolute(swiftScriptTool), + commandLine: rawCommandline, + inputs: inputs, + primaryInputs: [], + outputs: [], + extraEnvironment: extraEnvironment, + requiresInPlaceExecution: true, + supportsResponseFiles: true + ) + } +} From 1b3ee22fdb49c7f8b61a01ec0af79cf77756d553 Mon Sep 17 00:00:00 2001 From: stevapple Date: Thu, 12 Aug 2021 17:46:28 +0800 Subject: [PATCH 2/5] Minor fixes --- Sources/SwiftDriver/Jobs/CheckScriptJob.swift | 17 ++++---- Sources/SwiftDriver/Jobs/Job.swift | 42 +++++++++---------- Sources/SwiftDriver/Jobs/RunScriptJob.swift | 4 +- 3 files changed, 31 insertions(+), 32 deletions(-) diff --git a/Sources/SwiftDriver/Jobs/CheckScriptJob.swift b/Sources/SwiftDriver/Jobs/CheckScriptJob.swift index df82a4070..147c8dae9 100644 --- a/Sources/SwiftDriver/Jobs/CheckScriptJob.swift +++ b/Sources/SwiftDriver/Jobs/CheckScriptJob.swift @@ -13,7 +13,7 @@ import TSCBasic public enum ScriptingError: Error, DiagnosticData { case tooManyInputFiles - case noSwiftScript + case noSwiftScript public var description: String { switch self { @@ -21,7 +21,7 @@ public enum ScriptingError: Error, DiagnosticData { return "Scripting mode expect exactly 1 input file" case .noSwiftScript: - return "'swift-script' tool not found" + return "'swift-script' tool not found" } } } @@ -33,13 +33,12 @@ extension Driver { commandLine.appendFlags("-frontend", "-print-package-declarations") // Check and add the inputs. - guard inputs.count <= 1 else { - throw ScriptingError.tooManyInputFiles - } - for input in inputs { - commandLine.append(.path(input.file)) - } - print(commandLine) + guard inputs.count <= 1 else { + throw ScriptingError.tooManyInputFiles + } + for input in inputs { + commandLine.append(.path(input.file)) + } return Job( moduleName: moduleOutputInfo.name, diff --git a/Sources/SwiftDriver/Jobs/Job.swift b/Sources/SwiftDriver/Jobs/Job.swift index 313d87e72..69d37c1ba 100644 --- a/Sources/SwiftDriver/Jobs/Job.swift +++ b/Sources/SwiftDriver/Jobs/Job.swift @@ -169,52 +169,52 @@ extension Job : CustomStringConvertible { public var description: String { switch kind { case .compile: - return "Compiling \(moduleName) \(displayInputs.first?.file.basename ?? "")" + return "Compiling \(moduleName) \(displayInputs.first?.file.basename ?? "")" case .mergeModule: - return "Merging module \(moduleName)" + return "Merging module \(moduleName)" case .link: - return "Linking \(moduleName)" + return "Linking \(moduleName)" case .generateDSYM: - return "Generating dSYM for module \(moduleName)" + return "Generating dSYM for module \(moduleName)" case .autolinkExtract: - return "Extracting autolink information for module \(moduleName)" + return "Extracting autolink information for module \(moduleName)" case .emitModule: - return "Emitting module for \(moduleName)" + return "Emitting module for \(moduleName)" case .generatePCH: - return "Compiling bridging header \(displayInputs.first?.file.basename ?? "")" + return "Compiling bridging header \(displayInputs.first?.file.basename ?? "")" case .moduleWrap: return "Wrapping Swift module \(moduleName)" case .generatePCM: - return "Compiling Clang module \(moduleName)" + return "Compiling Clang module \(moduleName)" case .dumpPCM: - return "Dump information about Clang module \(displayInputs.first?.file.name ?? "")" + return "Dump information about Clang module \(displayInputs.first?.file.name ?? "")" case .interpret: - return "Interpreting \(displayInputs.first?.file.name ?? "")" + return "Interpreting \(displayInputs.first?.file.name ?? "")" case .repl: - return "Executing Swift REPL" + return "Executing Swift REPL" case .verifyDebugInfo: - return "Verifying debug information for module \(moduleName)" + return "Verifying debug information for module \(moduleName)" case .printTargetInfo: - return "Gathering target information for module \(moduleName)" + return "Gathering target information for module \(moduleName)" case .versionRequest: - return "Getting Swift version information" + return "Getting Swift version information" case .help: - return "Swift help" + return "Swift help" case .backend: return "Embedding bitcode for \(moduleName) \(displayInputs.first?.file.basename ?? "")" @@ -241,10 +241,10 @@ extension Job : CustomStringConvertible { return "Comparing ABI of \(moduleName) to baseline" case .checkScript: - return "Checking if \(displayInputs.first?.file.basename ?? "") requires scripting mode" + return "Checking if \(displayInputs.first?.file.basename ?? "") requires scripting mode" case .runScript: - return "Running a script with SwiftPM" + return "Running a script with SwiftPM" } } @@ -263,14 +263,14 @@ extension Job.Kind { public var isSwiftFrontend: Bool { switch self { case .backend, .compile, .mergeModule, .emitModule, .generatePCH, - .generatePCM, .dumpPCM, .interpret, .repl, .printTargetInfo, .checkScript, - .versionRequest, .emitSupportedFeatures, .scanDependencies, .verifyModuleInterface: - return true + .generatePCM, .dumpPCM, .interpret, .repl, .printTargetInfo, .checkScript, + .versionRequest, .emitSupportedFeatures, .scanDependencies, .verifyModuleInterface: + return true case .autolinkExtract, .generateDSYM, .help, .link, .verifyDebugInfo, .moduleWrap, .generateAPIBaseline, .generateABIBaseline, .compareAPIBaseline, .compareABIBaseline, .runScript: - return false + return false } } diff --git a/Sources/SwiftDriver/Jobs/RunScriptJob.swift b/Sources/SwiftDriver/Jobs/RunScriptJob.swift index abcae76a3..f6fdee414 100644 --- a/Sources/SwiftDriver/Jobs/RunScriptJob.swift +++ b/Sources/SwiftDriver/Jobs/RunScriptJob.swift @@ -40,8 +40,8 @@ extension Driver { var commandline: [Job.ArgTemplate] = [] for arg in rawCommandline { - commandline.appendFlag("-Xfrontend") - commandline.append(arg) + commandline.appendFlag("-Xfrontend") + commandline.append(arg) } commandline.appendFlag("--quiet") From 1e5d49594f87ab627322dd9c22bfee8968baa418 Mon Sep 17 00:00:00 2001 From: stevapple Date: Fri, 13 Aug 2021 17:19:46 +0800 Subject: [PATCH 3/5] Minor fixes --- Sources/SwiftDriver/Jobs/CheckScriptJob.swift | 2 +- Sources/SwiftDriver/Jobs/Planning.swift | 10 ++++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/Sources/SwiftDriver/Jobs/CheckScriptJob.swift b/Sources/SwiftDriver/Jobs/CheckScriptJob.swift index 147c8dae9..649a9b226 100644 --- a/Sources/SwiftDriver/Jobs/CheckScriptJob.swift +++ b/Sources/SwiftDriver/Jobs/CheckScriptJob.swift @@ -33,7 +33,7 @@ extension Driver { commandLine.appendFlags("-frontend", "-print-package-declarations") // Check and add the inputs. - guard inputs.count <= 1 else { + guard inputs.count == 1 else { throw ScriptingError.tooManyInputFiles } for input in inputs { diff --git a/Sources/SwiftDriver/Jobs/Planning.swift b/Sources/SwiftDriver/Jobs/Planning.swift index d82ac1574..82f65e7c0 100644 --- a/Sources/SwiftDriver/Jobs/Planning.swift +++ b/Sources/SwiftDriver/Jobs/Planning.swift @@ -687,12 +687,14 @@ extension Driver { case .immediate: var jobs: [Job] = [] try addPrecompileModuleDependenciesJobs(addJob: { jobs.append($0) }) - let result = try executor.execute(job: checkScriptJob(inputs: inputFiles), forceResponseFiles: true, + let result = try executor.execute(job: checkScriptJob(inputs: inputFiles), + forceResponseFiles: true, recordedInputModificationDates: recordedInputModificationDates) - if try result.utf8Output().hasPrefix("[]") { - jobs.append(try interpretJob(inputs: inputFiles)) - } else { + if result.exitStatus == .terminated(code: 0), + try !result.utf8Output().hasPrefix("[]") { jobs.append(try runScriptJob(inputs: inputFiles)) + } else { + jobs.append(try interpretJob(inputs: inputFiles)) } return (jobs, nil) From b89740ab0a844a0c4b2877d9ff35b58cd907a1f0 Mon Sep 17 00:00:00 2001 From: stevapple Date: Sat, 14 Aug 2021 05:20:04 +0800 Subject: [PATCH 4/5] Fix Driver.runScriptJob --- Sources/SwiftDriver/Jobs/RunScriptJob.swift | 49 +++++++++++---------- 1 file changed, 25 insertions(+), 24 deletions(-) diff --git a/Sources/SwiftDriver/Jobs/RunScriptJob.swift b/Sources/SwiftDriver/Jobs/RunScriptJob.swift index f6fdee414..637c7813d 100644 --- a/Sources/SwiftDriver/Jobs/RunScriptJob.swift +++ b/Sources/SwiftDriver/Jobs/RunScriptJob.swift @@ -9,6 +9,7 @@ // See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors // //===----------------------------------------------------------------------===// +import TSCBasic extension Driver { mutating func runScriptJob(inputs allInputs: [TypedVirtualPath]) throws -> Job { @@ -16,48 +17,48 @@ extension Driver { guard fileSystem.isExecutableFile(swiftScriptTool) else { throw ScriptingError.noSwiftScript } - var rawCommandline: [Job.ArgTemplate] = swiftCompilerPrefixArgs.map { Job.ArgTemplate.flag($0) } + var commandLine: [Job.ArgTemplate] = [.flag("run")] var inputs: [TypedVirtualPath] = [] // Add the inputs. for input in allInputs { - rawCommandline.append(.path(input.file)) + commandLine.append(.path(input.file)) inputs.append(input) } - if parsedOptions.hasArgument(.parseStdlib) { - rawCommandline.appendFlag(.disableObjcAttrRequiresFoundationModule) - } - - try addCommonFrontendOptions(commandLine: &rawCommandline, inputs: &inputs) - // FIXME: MSVC runtime flags - - try rawCommandline.appendLast(.parseSil, from: &parsedOptions) - try rawCommandline.appendAll(.l, .framework, from: &parsedOptions) + var frontendArgs: [Job.ArgTemplate] = swiftCompilerPrefixArgs.map { Job.ArgTemplate.flag($0) } + try frontendArgs.appendAll(.l, .framework, from: &parsedOptions) - // The immediate arguments must be last. - try rawCommandline.appendLast(.DASHDASH, from: &parsedOptions) - - var commandline: [Job.ArgTemplate] = [] - for arg in rawCommandline { - commandline.appendFlag("-Xfrontend") - commandline.append(arg) + for arg in frontendArgs { + commandLine.appendFlag("-Xfrontend") + commandLine.append(arg) + } + + // Appending extra arguments + // FIXME: Is there a more elegant solution? + try commandLine.appendLast(.DASHDASH, from: &parsedOptions) + if let idx = commandLine.firstIndex(of: .flag("--")) { + commandLine.remove(at: idx) } - commandline.appendFlag("--quiet") - let extraEnvironment = try toolchain.platformSpecificInterpreterEnvironmentVariables( - env: self.env, parsedOptions: &parsedOptions, - sdkPath: frontendTargetInfo.sdkPath?.path, targetInfo: self.frontendTargetInfo) + // Explicitly handle -v flag + // FIXME: Is there a more elegant solution? + if let idx = commandLine.firstIndex(of: .flag("-v")) { + commandLine.remove(at: idx) + stderrStream <<< "\(swiftScriptTool.pathString) \(commandLine.joinedUnresolvedArguments)\n" + stderrStream.flush() + } else { + commandLine.insert(.flag("--quiet"), at: 1) + } return Job( moduleName: moduleOutputInfo.name, kind: .runScript, tool: .absolute(swiftScriptTool), - commandLine: rawCommandline, + commandLine: commandLine, inputs: inputs, primaryInputs: [], outputs: [], - extraEnvironment: extraEnvironment, requiresInPlaceExecution: true, supportsResponseFiles: true ) From e5bcf88e22cb36e03fc64d83997cfb948f1b2508 Mon Sep 17 00:00:00 2001 From: stevapple Date: Mon, 16 Aug 2021 23:42:03 +0800 Subject: [PATCH 5/5] Add documents and comments --- Sources/SwiftDriver/Jobs/CheckScriptJob.swift | 4 ++-- Sources/SwiftDriver/Jobs/Planning.swift | 2 ++ Sources/SwiftDriver/Jobs/RunScriptJob.swift | 12 +++++++----- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/Sources/SwiftDriver/Jobs/CheckScriptJob.swift b/Sources/SwiftDriver/Jobs/CheckScriptJob.swift index 649a9b226..5189499eb 100644 --- a/Sources/SwiftDriver/Jobs/CheckScriptJob.swift +++ b/Sources/SwiftDriver/Jobs/CheckScriptJob.swift @@ -27,12 +27,12 @@ public enum ScriptingError: Error, DiagnosticData { } extension Driver { - /// Link the given inputs. + /// Check if the script requires scripting mode. func checkScriptJob(inputs: [TypedVirtualPath]) throws -> Job { var commandLine: [Job.ArgTemplate] = [] commandLine.appendFlags("-frontend", "-print-package-declarations") - // Check and add the inputs. + // Check and add the input file. guard inputs.count == 1 else { throw ScriptingError.tooManyInputFiles } diff --git a/Sources/SwiftDriver/Jobs/Planning.swift b/Sources/SwiftDriver/Jobs/Planning.swift index 82f65e7c0..f9adf939a 100644 --- a/Sources/SwiftDriver/Jobs/Planning.swift +++ b/Sources/SwiftDriver/Jobs/Planning.swift @@ -687,9 +687,11 @@ extension Driver { case .immediate: var jobs: [Job] = [] try addPrecompileModuleDependenciesJobs(addJob: { jobs.append($0) }) + // Check if the script requires scripting mode. let result = try executor.execute(job: checkScriptJob(inputs: inputFiles), forceResponseFiles: true, recordedInputModificationDates: recordedInputModificationDates) + // Only use scripting mode when package dependencies are known. if result.exitStatus == .terminated(code: 0), try !result.utf8Output().hasPrefix("[]") { jobs.append(try runScriptJob(inputs: inputFiles)) diff --git a/Sources/SwiftDriver/Jobs/RunScriptJob.swift b/Sources/SwiftDriver/Jobs/RunScriptJob.swift index 637c7813d..d1eac5f8b 100644 --- a/Sources/SwiftDriver/Jobs/RunScriptJob.swift +++ b/Sources/SwiftDriver/Jobs/RunScriptJob.swift @@ -12,28 +12,30 @@ import TSCBasic extension Driver { + /// Run a script with the `swift-script` tool. mutating func runScriptJob(inputs allInputs: [TypedVirtualPath]) throws -> Job { + // Find and use `swift-script run`. let swiftScriptTool = try toolchain.getToolPath(.swiftCompiler).parentDirectory.appending(component: "swift-script") guard fileSystem.isExecutableFile(swiftScriptTool) else { throw ScriptingError.noSwiftScript } var commandLine: [Job.ArgTemplate] = [.flag("run")] - var inputs: [TypedVirtualPath] = [] // Add the inputs. + var inputs: [TypedVirtualPath] = [] for input in allInputs { commandLine.append(.path(input.file)) inputs.append(input) } + // Add frontend arguments. var frontendArgs: [Job.ArgTemplate] = swiftCompilerPrefixArgs.map { Job.ArgTemplate.flag($0) } try frontendArgs.appendAll(.l, .framework, from: &parsedOptions) - - for arg in frontendArgs { + frontendArgs.forEach { commandLine.appendFlag("-Xfrontend") - commandLine.append(arg) + commandLine.append($0) } - + // Appending extra arguments // FIXME: Is there a more elegant solution? try commandLine.appendLast(.DASHDASH, from: &parsedOptions)