|
15 | 15 | //===----------------------------------------------------------------------===//
|
16 | 16 |
|
17 | 17 | #include "swift/AST/DiagnosticEngine.h"
|
| 18 | +#include "swift/AST/DiagnosticsDriver.h" |
18 | 19 | #include "swift/Basic/LLVMInitialize.h"
|
19 | 20 | #include "swift/Basic/PrettyStackTrace.h"
|
20 | 21 | #include "swift/Basic/Program.h"
|
@@ -159,6 +160,35 @@ static int run_driver(StringRef ExecName,
|
159 | 160 | DiagnosticEngine Diags(SM);
|
160 | 161 | Diags.addConsumer(PDC);
|
161 | 162 |
|
| 163 | + // Forwarding calls to the swift driver if the C++ driver is invoked as `swift` |
| 164 | + // or `swiftc`, and an environment variable SWIFT_USE_NEW_DRIVER is defined. |
| 165 | + if (llvm::sys::Process::GetEnv("SWIFT_USE_NEW_DRIVER") && |
| 166 | + (ExecName == "swift" || ExecName == "swiftc")) { |
| 167 | + SmallString<256> NewDriverPath(llvm::sys::path::parent_path(Path)); |
| 168 | + llvm::sys::path::append(NewDriverPath, "swift-driver"); |
| 169 | + SmallVector<const char *, 256> subCommandArgs; |
| 170 | + // Rewrite the program argument. |
| 171 | + subCommandArgs.push_back(NewDriverPath.c_str()); |
| 172 | + if (ExecName == "swiftc") { |
| 173 | + subCommandArgs.push_back("--driver-mode=swiftc"); |
| 174 | + } else { |
| 175 | + assert(ExecName == "swift"); |
| 176 | + subCommandArgs.push_back("--driver-mode=swift"); |
| 177 | + } |
| 178 | + subCommandArgs.insert(subCommandArgs.end(), argv.begin() + 1, argv.end()); |
| 179 | + |
| 180 | + // Execute the subcommand. |
| 181 | + subCommandArgs.push_back(nullptr); |
| 182 | + Diags.diagnose(SourceLoc(), diag::remark_forwarding_to_new_driver); |
| 183 | + ExecuteInPlace(NewDriverPath.c_str(), subCommandArgs.data()); |
| 184 | + |
| 185 | + // If we reach here then an error occurred (typically a missing path). |
| 186 | + std::string ErrorString = llvm::sys::StrError(); |
| 187 | + llvm::errs() << "error: unable to invoke subcommand: " << subCommandArgs[0] |
| 188 | + << " (" << ErrorString << ")\n"; |
| 189 | + return 2; |
| 190 | + } |
| 191 | + |
162 | 192 | Driver TheDriver(Path, ExecName, argv, Diags);
|
163 | 193 | switch (TheDriver.getDriverKind()) {
|
164 | 194 | case Driver::DriverKind::AutolinkExtract:
|
|
0 commit comments