diff --git a/lib/Driver/Driver.cpp b/lib/Driver/Driver.cpp index f8dc71f43c3e9..869a6b0f57674 100644 --- a/lib/Driver/Driver.cpp +++ b/lib/Driver/Driver.cpp @@ -99,6 +99,8 @@ void Driver::parseDriverKind(ArrayRef Args) { llvm::StringSwitch>(DriverName) .Case("swift", DriverKind::Interactive) .Case("swiftc", DriverKind::Batch) + .Case("swift-legacy-driver", DriverKind::Interactive) + .Case("swiftc-legacy-driver", DriverKind::Batch) .Case("sil-opt", DriverKind::SILOpt) .Case("sil-func-extractor", DriverKind::SILFuncExtractor) .Case("sil-nm", DriverKind::SILNM) diff --git a/lib/DriverTool/driver.cpp b/lib/DriverTool/driver.cpp index 0b3bd77a032c0..3e149d6c4ae8c 100644 --- a/lib/DriverTool/driver.cpp +++ b/lib/DriverTool/driver.cpp @@ -133,7 +133,7 @@ static bool shouldRunAsSubcommand(StringRef ExecName, // If we are not run as 'swift', don't do anything special. This doesn't work // with symlinks with alternate names, but we can't detect 'swift' vs 'swiftc' // if we try and resolve using the actual executable path. - if (ExecName != "swift") + if (ExecName != "swift" && ExecName != "swift-legacy-driver") return false; // If there are no program arguments, always invoke as normal. @@ -167,14 +167,30 @@ static bool shouldRunAsSubcommand(StringRef ExecName, static bool shouldDisallowNewDriver(DiagnosticEngine &diags, StringRef ExecName, const ArrayRef argv) { - // We are not invoking the driver, so don't forward. - if (ExecName != "swift" && ExecName != "swiftc") { - return true; + // We are expected to use the legacy driver to `exec` an overload + // for testing purposes. + if (llvm::sys::Process::GetEnv("SWIFT_OVERLOAD_DRIVER").has_value()) { + return false; } StringRef disableArg = "-disallow-use-new-driver"; StringRef disableEnv = "SWIFT_USE_OLD_DRIVER"; auto shouldWarn = !llvm::sys::Process:: GetEnv("SWIFT_AVOID_WARNING_USING_OLD_DRIVER").has_value(); + + // We explicitly are on the fallback to the legacy driver from the new driver. + // Do not forward. + if (ExecName == "swift-legacy-driver" || + ExecName == "swiftc-legacy-driver") { + if (shouldWarn) + diags.diagnose(SourceLoc(), diag::old_driver_deprecated, disableArg); + return true; + } + + // We are not invoking the driver, so don't forward. + if (ExecName != "swift" && ExecName != "swiftc") { + return true; + } + // If user specified using the old driver, don't forward. if (llvm::find_if(argv, [&](const char* arg) { return StringRef(arg) == disableArg; @@ -193,7 +209,7 @@ static bool shouldDisallowNewDriver(DiagnosticEngine &diags, static bool appendSwiftDriverName(SmallString<256> &buffer) { assert(llvm::sys::fs::exists(buffer)); - if (auto driverNameOp = llvm::sys::Process::GetEnv("SWIFT_USE_NEW_DRIVER")) { + if (auto driverNameOp = llvm::sys::Process::GetEnv("SWIFT_OVERLOAD_DRIVER")) { llvm::sys::path::append(buffer, *driverNameOp); return true; } @@ -312,8 +328,7 @@ static int run_driver(StringRef ExecName, subCommandArgs.push_back(DriverModeArg.data()); } else if (ExecName == "swiftc") { subCommandArgs.push_back("--driver-mode=swiftc"); - } else { - assert(ExecName == "swift"); + } else if (ExecName == "swift") { subCommandArgs.push_back("--driver-mode=swift"); } // Push these non-op frontend arguments so the build log can indicate @@ -344,7 +359,16 @@ static int run_driver(StringRef ExecName, return 2; } } - + + // We are in the fallback to legacy driver mode. + // Now that we have determined above that we are not going to + // forward the invocation to the new driver, ensure the rest of the + // downstream driver execution refers to itself by the appropriate name. + if (ExecName == "swift-legacy-driver") + ExecName = "swift"; + else if (ExecName == "swiftc-legacy-driver") + ExecName = "swiftc"; + Driver TheDriver(Path, ExecName, argv, Diags); switch (TheDriver.getDriverKind()) { case Driver::DriverKind::SILOpt: diff --git a/test/Driver/LegacyDriver/legacy-driver-propagates-response-file-to-new-driver.swift b/test/Driver/LegacyDriver/legacy-driver-propagates-response-file-to-new-driver.swift index 11014ea1e4d5f..420310879ee0a 100644 --- a/test/Driver/LegacyDriver/legacy-driver-propagates-response-file-to-new-driver.swift +++ b/test/Driver/LegacyDriver/legacy-driver-propagates-response-file-to-new-driver.swift @@ -6,7 +6,7 @@ // REQUIRES: shell // RUN: %{python} -c 'for i in range(500001): print("-DTEST5_" + str(i))' > %t.resp // RUN: cp %S/Inputs/print-args.sh %swift-bin-dir/legacy-driver-propagates-response-file.sh -// RUN: env SWIFT_USE_NEW_DRIVER=legacy-driver-propagates-response-file.sh %swiftc_driver_plain %s @%t.resp | %FileCheck %s +// RUN: env SWIFT_OVERLOAD_DRIVER=legacy-driver-propagates-response-file.sh %swiftc_driver_plain -disallow-use-new-driver %s @%t.resp | %FileCheck %s // RUN: rm %swift-bin-dir/legacy-driver-propagates-response-file.sh // CHECK: -Xfrontend diff --git a/test/Driver/options-repl-darwin.swift b/test/Driver/options-repl-darwin.swift index f32ee91e9dcac..45040b1f8972b 100644 --- a/test/Driver/options-repl-darwin.swift +++ b/test/Driver/options-repl-darwin.swift @@ -6,7 +6,7 @@ // RUN: %empty-directory(%t/usr/bin/) // RUN: %empty-directory(%t/usr/lib/) -// RUN: %hardlink-or-copy(from: %swift_driver_plain, to: %t/usr/bin/swift) +// RUN: %hardlink-or-copy(from: %swift_driver_plain-legacy-driver, to: %t/usr/bin/swift) // RUN: %host-library-env %t/usr/bin/swift -repl -### | %FileCheck -check-prefix=INTEGRATED %s // RUN: %host-library-env %t/usr/bin/swift -### | %FileCheck -check-prefix=INTEGRATED %s diff --git a/test/Driver/subcommands.swift b/test/Driver/subcommands.swift index f34119c3f50bb..e7b6a7b72a021 100644 --- a/test/Driver/subcommands.swift +++ b/test/Driver/subcommands.swift @@ -34,5 +34,5 @@ // RUN: echo "#!/bin/sh" > %t.dir/swift-foo // RUN: echo "echo \"exec: \$0\"" >> %t.dir/swift-foo // RUN: chmod +x %t.dir/swift-foo -// RUN: env PATH=%t.dir %swift_driver_plain foo | %FileCheck -check-prefix=CHECK-SWIFT-SUBCOMMAND %s +// RUN: env PATH=%t.dir SWIFT_USE_OLD_DRIVER=1 %swift_driver_plain foo | %FileCheck -check-prefix=CHECK-SWIFT-SUBCOMMAND %s // CHECK-SWIFT-SUBCOMMAND: exec: {{.*}}/swift-foo diff --git a/tools/driver/CMakeLists.txt b/tools/driver/CMakeLists.txt index bccffb38207eb..5f81c6cd17d70 100644 --- a/tools/driver/CMakeLists.txt +++ b/tools/driver/CMakeLists.txt @@ -77,15 +77,40 @@ add_swift_parser_link_libraries(swift-frontend) # to ensure that `swiftc` forwards to the standalone driver when invoked. swift_create_early_driver_copies(swift-frontend) -swift_create_post_build_symlink(swift-frontend - SOURCE "swift-frontend${CMAKE_EXECUTABLE_SUFFIX}" - DESTINATION "swift${CMAKE_EXECUTABLE_SUFFIX}" - WORKING_DIRECTORY "${SWIFT_RUNTIME_OUTPUT_INTDIR}") +# If a `swift-driver` executable adjacent to the `swift-frontend` executable exists +# then the `swift` and `swiftc` symlinks should point to it by-default +if(EXISTS "${SWIFT_RUNTIME_OUTPUT_INTDIR}/swift-driver${CMAKE_EXECUTABLE_SUFFIX}" AND EXISTS "${SWIFT_RUNTIME_OUTPUT_INTDIR}/swift-help${CMAKE_EXECUTABLE_SUFFIX}") + message(STATUS "Pointing 'swift' and 'swiftc' symlinks at 'swift-driver'.") + swift_create_post_build_symlink(swift-frontend + SOURCE "swift-driver${CMAKE_EXECUTABLE_SUFFIX}" + DESTINATION "swift${CMAKE_EXECUTABLE_SUFFIX}" + WORKING_DIRECTORY "${SWIFT_RUNTIME_OUTPUT_INTDIR}") + swift_create_post_build_symlink(swift-frontend + SOURCE "swift-driver${CMAKE_EXECUTABLE_SUFFIX}" + DESTINATION "swiftc${CMAKE_EXECUTABLE_SUFFIX}" + WORKING_DIRECTORY "${SWIFT_RUNTIME_OUTPUT_INTDIR}") -swift_create_post_build_symlink(swift-frontend - SOURCE "swift-frontend${CMAKE_EXECUTABLE_SUFFIX}" - DESTINATION "swiftc${CMAKE_EXECUTABLE_SUFFIX}" - WORKING_DIRECTORY "${SWIFT_RUNTIME_OUTPUT_INTDIR}") + message(STATUS "Pointing 'swift-legacy-driver' and 'swiftc-legacy-driver' symlinks at 'swift-frontend'.") + swift_create_post_build_symlink(swift-frontend + SOURCE "swift-frontend${CMAKE_EXECUTABLE_SUFFIX}" + DESTINATION "swift-legacy-driver${CMAKE_EXECUTABLE_SUFFIX}" + WORKING_DIRECTORY "${SWIFT_RUNTIME_OUTPUT_INTDIR}") + swift_create_post_build_symlink(swift-frontend + SOURCE "swift-frontend${CMAKE_EXECUTABLE_SUFFIX}" + DESTINATION "swiftc-legacy-driver${CMAKE_EXECUTABLE_SUFFIX}" + WORKING_DIRECTORY "${SWIFT_RUNTIME_OUTPUT_INTDIR}") +else() + message(STATUS "Pointing 'swift' and 'swiftc' symlinks at 'swift-frontend' - no early SwiftDriver build found.") + swift_create_post_build_symlink(swift-frontend + SOURCE "swift-frontend${CMAKE_EXECUTABLE_SUFFIX}" + DESTINATION "swift${CMAKE_EXECUTABLE_SUFFIX}" + WORKING_DIRECTORY "${SWIFT_RUNTIME_OUTPUT_INTDIR}") + + swift_create_post_build_symlink(swift-frontend + SOURCE "swift-frontend${CMAKE_EXECUTABLE_SUFFIX}" + DESTINATION "swiftc${CMAKE_EXECUTABLE_SUFFIX}" + WORKING_DIRECTORY "${SWIFT_RUNTIME_OUTPUT_INTDIR}") +endif() swift_create_post_build_symlink(swift-frontend SOURCE "swift-frontend${CMAKE_EXECUTABLE_SUFFIX}" @@ -157,15 +182,6 @@ swift_create_post_build_symlink(swift-frontend DESTINATION "swift-parse-test${CMAKE_EXECUTABLE_SUFFIX}" WORKING_DIRECTORY "${SWIFT_RUNTIME_OUTPUT_INTDIR}") -add_swift_tool_symlink(swift swift-frontend compiler) -add_swift_tool_symlink(swiftc swift-frontend compiler) -add_swift_tool_symlink(swift-symbolgraph-extract swift-frontend compiler) -add_swift_tool_symlink(swift-api-extract swift-frontend compiler) -add_swift_tool_symlink(swift-autolink-extract swift-frontend autolink-driver) -add_swift_tool_symlink(swift-indent swift-frontend editor-integration) -add_swift_tool_symlink(swift-api-digester swift-frontend compiler) -add_swift_tool_symlink(swift-cache-tool swift-frontend compiler) - add_dependencies(compiler swift-frontend) swift_install_in_component(FILES "${SWIFT_RUNTIME_OUTPUT_INTDIR}/swift${CMAKE_EXECUTABLE_SUFFIX}" DESTINATION "bin" @@ -193,4 +209,17 @@ add_dependencies(editor-integration swift-frontend) swift_install_in_component(FILES "${SWIFT_RUNTIME_OUTPUT_INTDIR}/swift-indent${CMAKE_EXECUTABLE_SUFFIX}" DESTINATION "bin" COMPONENT editor-integration) - +if(EXISTS "${SWIFT_RUNTIME_OUTPUT_INTDIR}/swift-driver${CMAKE_EXECUTABLE_SUFFIX}" AND EXISTS "${SWIFT_RUNTIME_OUTPUT_INTDIR}/swift-help${CMAKE_EXECUTABLE_SUFFIX}") + swift_install_in_component(PROGRAMS "${SWIFT_RUNTIME_OUTPUT_INTDIR}/swift-driver${CMAKE_EXECUTABLE_SUFFIX}" + DESTINATION "bin" + COMPONENT compiler) + swift_install_in_component(PROGRAMS "${SWIFT_RUNTIME_OUTPUT_INTDIR}/swift-help${CMAKE_EXECUTABLE_SUFFIX}" + DESTINATION "bin" + COMPONENT compiler) + swift_install_in_component(FILES "${SWIFT_RUNTIME_OUTPUT_INTDIR}/swiftc-legacy-driver${CMAKE_EXECUTABLE_SUFFIX}" + DESTINATION "bin" + COMPONENT compiler) + swift_install_in_component(FILES "${SWIFT_RUNTIME_OUTPUT_INTDIR}/swift-legacy-driver${CMAKE_EXECUTABLE_SUFFIX}" + DESTINATION "bin" + COMPONENT compiler) +endif()