Skip to content

Commit 6a181fb

Browse files
committed
Add symlinks for the legacy driver invocation (for emergency fallback invoked from 'swift-driver')
1 parent 7f6152a commit 6a181fb

File tree

6 files changed

+53
-12
lines changed

6 files changed

+53
-12
lines changed

lib/Driver/Driver.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ void Driver::parseDriverKind(ArrayRef<const char *> Args) {
9999
llvm::StringSwitch<llvm::Optional<DriverKind>>(DriverName)
100100
.Case("swift", DriverKind::Interactive)
101101
.Case("swiftc", DriverKind::Batch)
102+
.Case("swift-legacy-driver", DriverKind::Interactive)
103+
.Case("swiftc-legacy-driver", DriverKind::Batch)
102104
.Case("sil-opt", DriverKind::SILOpt)
103105
.Case("sil-func-extractor", DriverKind::SILFuncExtractor)
104106
.Case("sil-nm", DriverKind::SILNM)

lib/DriverTool/driver.cpp

+32-8
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ static bool shouldRunAsSubcommand(StringRef ExecName,
133133
// If we are not run as 'swift', don't do anything special. This doesn't work
134134
// with symlinks with alternate names, but we can't detect 'swift' vs 'swiftc'
135135
// if we try and resolve using the actual executable path.
136-
if (ExecName != "swift")
136+
if (ExecName != "swift" && ExecName != "swift-legacy-driver")
137137
return false;
138138

139139
// If there are no program arguments, always invoke as normal.
@@ -167,14 +167,30 @@ static bool shouldRunAsSubcommand(StringRef ExecName,
167167
static bool shouldDisallowNewDriver(DiagnosticEngine &diags,
168168
StringRef ExecName,
169169
const ArrayRef<const char *> argv) {
170-
// We are not invoking the driver, so don't forward.
171-
if (ExecName != "swift" && ExecName != "swiftc") {
172-
return true;
170+
// We are expected to use the legacy driver to `exec` an overload
171+
// for testing purposes.
172+
if (llvm::sys::Process::GetEnv("SWIFT_OVERLOAD_DRIVER").has_value()) {
173+
return false;
173174
}
174175
StringRef disableArg = "-disallow-use-new-driver";
175176
StringRef disableEnv = "SWIFT_USE_OLD_DRIVER";
176177
auto shouldWarn = !llvm::sys::Process::
177178
GetEnv("SWIFT_AVOID_WARNING_USING_OLD_DRIVER").has_value();
179+
180+
// We explicitly are on the fallback to the legacy driver from the new driver.
181+
// Do not forward.
182+
if (ExecName == "swift-legacy-driver" ||
183+
ExecName == "swiftc-legacy-driver") {
184+
if (shouldWarn)
185+
diags.diagnose(SourceLoc(), diag::old_driver_deprecated, disableArg);
186+
return true;
187+
}
188+
189+
// We are not invoking the driver, so don't forward.
190+
if (ExecName != "swift" && ExecName != "swiftc") {
191+
return true;
192+
}
193+
178194
// If user specified using the old driver, don't forward.
179195
if (llvm::find_if(argv, [&](const char* arg) {
180196
return StringRef(arg) == disableArg;
@@ -193,7 +209,7 @@ static bool shouldDisallowNewDriver(DiagnosticEngine &diags,
193209

194210
static bool appendSwiftDriverName(SmallString<256> &buffer) {
195211
assert(llvm::sys::fs::exists(buffer));
196-
if (auto driverNameOp = llvm::sys::Process::GetEnv("SWIFT_USE_NEW_DRIVER")) {
212+
if (auto driverNameOp = llvm::sys::Process::GetEnv("SWIFT_OVERLOAD_DRIVER")) {
197213
llvm::sys::path::append(buffer, *driverNameOp);
198214
return true;
199215
}
@@ -312,8 +328,7 @@ static int run_driver(StringRef ExecName,
312328
subCommandArgs.push_back(DriverModeArg.data());
313329
} else if (ExecName == "swiftc") {
314330
subCommandArgs.push_back("--driver-mode=swiftc");
315-
} else {
316-
assert(ExecName == "swift");
331+
} else if (ExecName == "swift") {
317332
subCommandArgs.push_back("--driver-mode=swift");
318333
}
319334
// Push these non-op frontend arguments so the build log can indicate
@@ -344,7 +359,16 @@ static int run_driver(StringRef ExecName,
344359
return 2;
345360
}
346361
}
347-
362+
363+
// We are in the fallback to legacy driver mode.
364+
// Now that we have determined above that we are not going to
365+
// forward the invocation to the new driver, ensure the rest of the
366+
// downstream driver execution refers to itself by the appropriate name.
367+
if (ExecName == "swift-legacy-driver")
368+
ExecName = "swift";
369+
else if (ExecName == "swiftc-legacy-driver")
370+
ExecName = "swiftc";
371+
348372
Driver TheDriver(Path, ExecName, argv, Diags);
349373
switch (TheDriver.getDriverKind()) {
350374
case Driver::DriverKind::SILOpt:

test/Driver/LegacyDriver/legacy-driver-propagates-response-file-to-new-driver.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
// REQUIRES: shell
77
// RUN: %{python} -c 'for i in range(500001): print("-DTEST5_" + str(i))' > %t.resp
88
// RUN: cp %S/Inputs/print-args.sh %swift-bin-dir/legacy-driver-propagates-response-file.sh
9-
// RUN: env SWIFT_USE_NEW_DRIVER=legacy-driver-propagates-response-file.sh %swiftc_driver_plain %s @%t.resp | %FileCheck %s
9+
// RUN: env SWIFT_OVERLOAD_DRIVER=legacy-driver-propagates-response-file.sh %swiftc_driver_plain -disallow-use-new-driver %s @%t.resp | %FileCheck %s
1010
// RUN: rm %swift-bin-dir/legacy-driver-propagates-response-file.sh
1111

1212
// CHECK: -Xfrontend

test/Driver/options-repl-darwin.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
// RUN: %empty-directory(%t/usr/bin/)
88
// RUN: %empty-directory(%t/usr/lib/)
9-
// RUN: %hardlink-or-copy(from: %swift_driver_plain, to: %t/usr/bin/swift)
9+
// RUN: %hardlink-or-copy(from: %swift_driver_plain-legacy-driver, to: %t/usr/bin/swift)
1010

1111
// RUN: %host-library-env %t/usr/bin/swift -repl -### | %FileCheck -check-prefix=INTEGRATED %s
1212
// RUN: %host-library-env %t/usr/bin/swift -### | %FileCheck -check-prefix=INTEGRATED %s

test/Driver/subcommands.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,5 @@
3434
// RUN: echo "#!/bin/sh" > %t.dir/swift-foo
3535
// RUN: echo "echo \"exec: \$0\"" >> %t.dir/swift-foo
3636
// RUN: chmod +x %t.dir/swift-foo
37-
// RUN: env PATH=%t.dir %swift_driver_plain foo | %FileCheck -check-prefix=CHECK-SWIFT-SUBCOMMAND %s
37+
// RUN: env PATH=%t.dir SWIFT_USE_OLD_DRIVER=1 %swift_driver_plain foo | %FileCheck -check-prefix=CHECK-SWIFT-SUBCOMMAND %s
3838
// CHECK-SWIFT-SUBCOMMAND: exec: {{.*}}/swift-foo

tools/driver/CMakeLists.txt

+16-1
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,20 @@ if(EXISTS "${SWIFT_RUNTIME_OUTPUT_INTDIR}/swift-driver${CMAKE_EXECUTABLE_SUFFIX}
8585
SOURCE "swift-driver${CMAKE_EXECUTABLE_SUFFIX}"
8686
DESTINATION "swift${CMAKE_EXECUTABLE_SUFFIX}"
8787
WORKING_DIRECTORY "${SWIFT_RUNTIME_OUTPUT_INTDIR}")
88-
8988
swift_create_post_build_symlink(swift-frontend
9089
SOURCE "swift-driver${CMAKE_EXECUTABLE_SUFFIX}"
9190
DESTINATION "swiftc${CMAKE_EXECUTABLE_SUFFIX}"
9291
WORKING_DIRECTORY "${SWIFT_RUNTIME_OUTPUT_INTDIR}")
92+
93+
message(STATUS "Pointing 'swift-legacy-driver' and 'swiftc-legacy-driver' symlinks at 'swift-frontend'.")
94+
swift_create_post_build_symlink(swift-frontend
95+
SOURCE "swift-frontend${CMAKE_EXECUTABLE_SUFFIX}"
96+
DESTINATION "swift-legacy-driver${CMAKE_EXECUTABLE_SUFFIX}"
97+
WORKING_DIRECTORY "${SWIFT_RUNTIME_OUTPUT_INTDIR}")
98+
swift_create_post_build_symlink(swift-frontend
99+
SOURCE "swift-frontend${CMAKE_EXECUTABLE_SUFFIX}"
100+
DESTINATION "swiftc-legacy-driver${CMAKE_EXECUTABLE_SUFFIX}"
101+
WORKING_DIRECTORY "${SWIFT_RUNTIME_OUTPUT_INTDIR}")
93102
else()
94103
message(STATUS "Pointing 'swift' and 'swiftc' symlinks at 'swift-frontend' - no early SwiftDriver build found.")
95104
swift_create_post_build_symlink(swift-frontend
@@ -207,4 +216,10 @@ if(EXISTS "${SWIFT_RUNTIME_OUTPUT_INTDIR}/swift-driver${CMAKE_EXECUTABLE_SUFFIX}
207216
swift_install_in_component(PROGRAMS "${SWIFT_RUNTIME_OUTPUT_INTDIR}/swift-help${CMAKE_EXECUTABLE_SUFFIX}"
208217
DESTINATION "bin"
209218
COMPONENT compiler)
219+
swift_install_in_component(FILES "${SWIFT_RUNTIME_OUTPUT_INTDIR}/swiftc-legacy-driver${CMAKE_EXECUTABLE_SUFFIX}"
220+
DESTINATION "bin"
221+
COMPONENT compiler)
222+
swift_install_in_component(FILES "${SWIFT_RUNTIME_OUTPUT_INTDIR}/swift-legacy-driver${CMAKE_EXECUTABLE_SUFFIX}"
223+
DESTINATION "bin"
224+
COMPONENT compiler)
210225
endif()

0 commit comments

Comments
 (0)