Skip to content

Commit

Permalink
Driver: forwarding driver invocations to the new driver when SWIFT_US…
Browse files Browse the repository at this point in the history
…E_NEW_DRIVER is defined

This allows us to experiment with the new swift-driver without changing build systems.
  • Loading branch information
nkcsgexi committed Oct 2, 2020
1 parent 01c434c commit 11b2251
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
2 changes: 2 additions & 0 deletions include/swift/AST/DiagnosticsDriver.def
Expand Up @@ -190,5 +190,7 @@ WARNING(warn_drv_darwin_sdk_invalid_settings, none,
"SDK settings were ignored because 'SDKSettings.json' could not be parsed",
())

REMARK(remark_forwarding_to_new_driver, none, "new Swift driver will be used", ())

#define UNDEFINE_DIAGNOSTIC_MACROS
#include "DefineDiagnosticMacros.h"
30 changes: 30 additions & 0 deletions tools/driver/driver.cpp
Expand Up @@ -15,6 +15,7 @@
//===----------------------------------------------------------------------===//

#include "swift/AST/DiagnosticEngine.h"
#include "swift/AST/DiagnosticsDriver.h"
#include "swift/Basic/LLVMInitialize.h"
#include "swift/Basic/PrettyStackTrace.h"
#include "swift/Basic/Program.h"
Expand Down Expand Up @@ -159,6 +160,35 @@ static int run_driver(StringRef ExecName,
DiagnosticEngine Diags(SM);
Diags.addConsumer(PDC);

// Forwarding calls to the swift driver if the C++ driver is invoked as `swift`
// or `swiftc`, and an environment variable SWIFT_USE_NEW_DRIVER is defined.
if (llvm::sys::Process::GetEnv("SWIFT_USE_NEW_DRIVER") &&
(ExecName == "swift" || ExecName == "swiftc")) {
SmallString<256> NewDriverPath(llvm::sys::path::parent_path(Path));
llvm::sys::path::append(NewDriverPath, "swift-driver");
SmallVector<const char *, 256> subCommandArgs;
// Rewrite the program argument.
subCommandArgs.push_back(NewDriverPath.c_str());
if (ExecName == "swiftc") {
subCommandArgs.push_back("--driver-mode=swiftc");
} else {
assert(ExecName == "swift");
subCommandArgs.push_back("--driver-mode=swift");
}
subCommandArgs.insert(subCommandArgs.end(), argv.begin() + 1, argv.end());

// Execute the subcommand.
subCommandArgs.push_back(nullptr);
Diags.diagnose(SourceLoc(), diag::remark_forwarding_to_new_driver);
ExecuteInPlace(NewDriverPath.c_str(), subCommandArgs.data());

// If we reach here then an error occurred (typically a missing path).
std::string ErrorString = llvm::sys::StrError();
llvm::errs() << "error: unable to invoke subcommand: " << subCommandArgs[0]
<< " (" << ErrorString << ")\n";
return 2;
}

Driver TheDriver(Path, ExecName, argv, Diags);
switch (TheDriver.getDriverKind()) {
case Driver::DriverKind::AutolinkExtract:
Expand Down

0 comments on commit 11b2251

Please sign in to comment.