|
| 1 | +//===------- ToolingUtil.swift - Swift Driver Source Version--------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +import class TSCBasic.DiagnosticsEngine |
| 14 | +import struct TSCBasic.Diagnostic |
| 15 | +import class TSCBasic.ProcessSet |
| 16 | +import enum TSCBasic.ProcessEnv |
| 17 | +import var TSCBasic.localFileSystem |
| 18 | +import SwiftOptions |
| 19 | + |
| 20 | +/// Generates the list of arguments that would be passed to the compiler |
| 21 | +/// frontend from the given driver arguments. |
| 22 | +/// |
| 23 | +/// \param ArgList The driver arguments (i.e. normal arguments for \c swiftc). |
| 24 | +/// \param ForceNoOutputs If true, override the output mode to "-typecheck" and |
| 25 | +/// produce no outputs. For example, this disables "-emit-module" and "-c" and |
| 26 | +/// prevents the creation of temporary files. |
| 27 | +/// \param outputFrontendArgs Contains the resulting frontend invocation command |
| 28 | +/// \param emittedDiagnostics Contains the diagnostics emitted by the driver |
| 29 | +/// |
| 30 | +/// \returns true on error |
| 31 | +/// |
| 32 | +/// \note This function is not intended to create invocations which are |
| 33 | +/// suitable for use in REPL or immediate modes. |
| 34 | +public func getSingleFrontendInvocationFromDriverArguments(argList: [String], |
| 35 | + outputFrontendArgs: inout [String], |
| 36 | + emittedDiagnostics: inout [Diagnostic], |
| 37 | + forceNoOutputs: Bool = false) -> Bool { |
| 38 | + var args: [String] = [] |
| 39 | + args.append(contentsOf: argList) |
| 40 | + |
| 41 | + // When creating a CompilerInvocation, ensure that the driver creates a single |
| 42 | + // frontend command. |
| 43 | + args.append("-whole-module-optimization") |
| 44 | + |
| 45 | + // Explicitly disable batch mode to avoid a spurious warning when combining |
| 46 | + // -enable-batch-mode with -whole-module-optimization. This is an |
| 47 | + // implementation detail. |
| 48 | + args.append("-disable-batch-mode"); |
| 49 | + |
| 50 | + // Prevent having a separate job for emit-module, we would like |
| 51 | + // to just have one job |
| 52 | + args.append("-no-emit-module-separately-wmo") |
| 53 | + |
| 54 | + // Avoid using filelists |
| 55 | + args.append("-driver-filelist-threshold"); |
| 56 | + args.append(String(Int.max)); |
| 57 | + |
| 58 | + let diagnosticsEngine = DiagnosticsEngine() |
| 59 | + defer { emittedDiagnostics = diagnosticsEngine.diagnostics } |
| 60 | + |
| 61 | + do { |
| 62 | + args = try ["swiftc"] + Driver.expandResponseFiles(args, |
| 63 | + fileSystem: localFileSystem, |
| 64 | + diagnosticsEngine: diagnosticsEngine) |
| 65 | + |
| 66 | + let optionTable = OptionTable() |
| 67 | + var parsedOptions = try optionTable.parse(Array(args), for: .batch, delayThrows: true) |
| 68 | + if forceNoOutputs { |
| 69 | + // Clear existing output modes and supplementary outputs. |
| 70 | + parsedOptions.eraseAllArguments(in: .modes) |
| 71 | + parsedOptions.eraseSupplementaryOutputs() |
| 72 | + parsedOptions.addOption(.typecheck, argument: .none) |
| 73 | + } |
| 74 | + |
| 75 | + // Instantiate the driver, setting up the toolchain in the process, etc. |
| 76 | + let resolver = try ArgsResolver(fileSystem: localFileSystem) |
| 77 | + let executor = SimpleExecutor(resolver: resolver, |
| 78 | + fileSystem: localFileSystem, |
| 79 | + env: ProcessEnv.vars) |
| 80 | + var driver = try Driver(args: parsedOptions.commandLine, |
| 81 | + diagnosticsEngine: diagnosticsEngine, |
| 82 | + executor: executor) |
| 83 | + if diagnosticsEngine.hasErrors { |
| 84 | + return true |
| 85 | + } |
| 86 | + |
| 87 | + |
| 88 | + let buildPlan = try driver.planBuild() |
| 89 | + if diagnosticsEngine.hasErrors { |
| 90 | + return true |
| 91 | + } |
| 92 | + let compileJobs = buildPlan.filter({ $0.kind == .compile }) |
| 93 | + guard let compileJob = compileJobs.spm_only else { |
| 94 | + diagnosticsEngine.emit(.error_expected_one_frontend_job()) |
| 95 | + return true |
| 96 | + } |
| 97 | + if !compileJob.commandLine.starts(with: [.flag("-frontend")]) { |
| 98 | + diagnosticsEngine.emit(.error_expected_frontend_command()) |
| 99 | + return true |
| 100 | + } |
| 101 | + outputFrontendArgs = try executor.description(of: compileJob, |
| 102 | + forceResponseFiles: false).components(separatedBy: " ") |
| 103 | + } catch { |
| 104 | + print("Unexpected error: \(error).") |
| 105 | + return true |
| 106 | + } |
| 107 | + |
| 108 | + return false |
| 109 | +} |
0 commit comments