Skip to content

Commit 5280cea

Browse files
authored
[Macros] In-process plugin server library tied to compiler host, not target (#74785)
PR #73725 introduced the in-process plugin server library, but the selection of the library depends on the selected toolchain, which depends on the compiler target, not the host. When cross-compiling (for example from macOS to a embedded Unix target), the compiler will incorrectly chose the `.so` file, not find it, and fail to compile things like the `@debugDescription` macro. Move the in-process plugin server library code from the platform toolchains into the parent type, and code it so it uses the right name depending on the compiler host at compilation time. This discards the target and only relies on the compiler host for selecting the right library.
1 parent 84a0779 commit 5280cea

8 files changed

+73
-96
lines changed

include/swift/Driver/ToolChain.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,9 @@ class ToolChain {
253253
const InvocationInfo &invocationInfo,
254254
const JobContext &context) const;
255255

256+
void addPluginArguments(const llvm::opt::ArgList &Args,
257+
llvm::opt::ArgStringList &Arguments) const;
258+
256259
public:
257260
virtual ~ToolChain() = default;
258261

@@ -341,9 +344,6 @@ class ToolChain {
341344
llvm::opt::ArgStringList &Arguments,
342345
StringRef LibName) const;
343346

344-
virtual void addPluginArguments(const llvm::opt::ArgList &Args,
345-
llvm::opt::ArgStringList &Arguments) const {}
346-
347347
/// Validates arguments passed to the toolchain.
348348
///
349349
/// An override point for platform-specific subclasses to customize the

lib/Driver/DarwinToolChains.cpp

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -129,37 +129,6 @@ std::string toolchains::Darwin::sanitizerRuntimeLibName(StringRef Sanitizer,
129129
.str();
130130
}
131131

132-
void
133-
toolchains::Darwin::addPluginArguments(const ArgList &Args,
134-
ArgStringList &Arguments) const {
135-
SmallString<64> pluginPath;
136-
auto programPath = getDriver().getSwiftProgramPath();
137-
CompilerInvocation::computeRuntimeResourcePathFromExecutablePath(
138-
programPath, /*shared=*/true, pluginPath);
139-
140-
// In-process plugin server path.
141-
auto inProcPluginServerPath = pluginPath;
142-
llvm::sys::path::append(inProcPluginServerPath, "host",
143-
"libSwiftInProcPluginServer.dylib");
144-
Arguments.push_back("-in-process-plugin-server-path");
145-
Arguments.push_back(Args.MakeArgString(inProcPluginServerPath));
146-
147-
// Default plugin path.
148-
auto defaultPluginPath = pluginPath;
149-
llvm::sys::path::append(defaultPluginPath, "host", "plugins");
150-
Arguments.push_back("-plugin-path");
151-
Arguments.push_back(Args.MakeArgString(defaultPluginPath));
152-
153-
// Local plugin path.
154-
llvm::sys::path::remove_filename(pluginPath); // Remove "swift"
155-
llvm::sys::path::remove_filename(pluginPath); // Remove "lib"
156-
llvm::sys::path::append(pluginPath, "local", "lib");
157-
llvm::sys::path::append(pluginPath, "swift");
158-
llvm::sys::path::append(pluginPath, "host", "plugins");
159-
Arguments.push_back("-plugin-path");
160-
Arguments.push_back(Args.MakeArgString(pluginPath));
161-
}
162-
163132
static void addLinkRuntimeLibRPath(const ArgList &Args,
164133
ArgStringList &Arguments,
165134
StringRef DarwinLibName,

lib/Driver/ToolChains.cpp

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1468,6 +1468,68 @@ void ToolChain::addLinkRuntimeLib(const ArgList &Args, ArgStringList &Arguments,
14681468
Arguments.push_back(Args.MakeArgString(P));
14691469
}
14701470

1471+
static void appendInProcPluginServerPath(StringRef PluginPathRoot,
1472+
llvm::SmallVectorImpl<char> &InProcPluginServerPath) {
1473+
InProcPluginServerPath.append(PluginPathRoot.begin(), PluginPathRoot.end());
1474+
#if defined(_WIN32)
1475+
llvm::sys::path::append(InProcPluginServerPath, "bin", "SwiftInProcPluginServer.dll");
1476+
#elif defined(__APPLE__)
1477+
llvm::sys::path::append(InProcPluginServerPath, "lib", "swift", "host", "libSwiftInProcPluginServer.dylib");
1478+
#elif defined(__unix__)
1479+
llvm::sys::path::append(InProcPluginServerPath, "lib", "swift", "host", "libSwiftInProcPluginServer.so");
1480+
#else
1481+
#error Unknown compiler host
1482+
#endif
1483+
}
1484+
1485+
static void appendPluginsPath(StringRef PluginPathRoot,
1486+
llvm::SmallVectorImpl<char> &PluginsPath) {
1487+
PluginsPath.append(PluginPathRoot.begin(), PluginPathRoot.end());
1488+
#if defined(_WIN32)
1489+
llvm::sys::path::append(PluginsPath, "bin");
1490+
#elif defined(__APPLE__) || defined(__unix__)
1491+
llvm::sys::path::append(PluginsPath, "lib", "swift", "host", "plugins");
1492+
#else
1493+
#error Unknown compiler host
1494+
#endif
1495+
}
1496+
1497+
#if defined(__APPLE__) || defined(__unix__)
1498+
static void appendLocalPluginsPath(StringRef PluginPathRoot,
1499+
llvm::SmallVectorImpl<char> &LocalPluginsPath) {
1500+
SmallString<261> localPluginPathRoot = PluginPathRoot;
1501+
llvm::sys::path::append(localPluginPathRoot, "local");
1502+
appendPluginsPath(localPluginPathRoot, LocalPluginsPath);
1503+
}
1504+
#endif
1505+
1506+
void ToolChain::addPluginArguments(const ArgList &Args,
1507+
ArgStringList &Arguments) const {
1508+
SmallString<261> pluginPathRoot = StringRef(getDriver().getSwiftProgramPath());
1509+
llvm::sys::path::remove_filename(pluginPathRoot); // Remove `swift`
1510+
llvm::sys::path::remove_filename(pluginPathRoot); // Remove `bin`
1511+
1512+
// In-process plugin server path.
1513+
SmallString<261> inProcPluginServerPath;
1514+
appendInProcPluginServerPath(pluginPathRoot, inProcPluginServerPath);
1515+
Arguments.push_back("-in-process-plugin-server-path");
1516+
Arguments.push_back(Args.MakeArgString(inProcPluginServerPath));
1517+
1518+
// Default plugin path.
1519+
SmallString<261> defaultPluginPath;
1520+
appendPluginsPath(pluginPathRoot, defaultPluginPath);
1521+
Arguments.push_back("-plugin-path");
1522+
Arguments.push_back(Args.MakeArgString(defaultPluginPath));
1523+
1524+
// Local plugin path.
1525+
#if defined(__APPLE__) || defined(__unix__)
1526+
SmallString<261> localPluginPath;
1527+
appendLocalPluginsPath(pluginPathRoot, localPluginPath);
1528+
Arguments.push_back("-plugin-path");
1529+
Arguments.push_back(Args.MakeArgString(localPluginPath));
1530+
#endif
1531+
}
1532+
14711533
void ToolChain::getClangLibraryPath(const ArgList &Args,
14721534
SmallString<128> &LibPath) const {
14731535
const llvm::Triple &T = getTriple();

lib/Driver/ToolChains.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,6 @@ class LLVM_LIBRARY_VISIBILITY Darwin : public ToolChain {
6666
InvocationInfo constructInvocation(const StaticLinkJobAction &job,
6767
const JobContext &context) const override;
6868

69-
void addPluginArguments(const llvm::opt::ArgList &Args,
70-
llvm::opt::ArgStringList &Arguments) const override;
71-
7269
void validateArguments(DiagnosticEngine &diags,
7370
const llvm::opt::ArgList &args,
7471
StringRef defaultTarget) const override;
@@ -117,9 +114,6 @@ class LLVM_LIBRARY_VISIBILITY Windows : public ToolChain {
117114

118115
std::string sanitizerRuntimeLibName(StringRef Sanitizer,
119116
bool shared = true) const override;
120-
121-
void addPluginArguments(const llvm::opt::ArgList &Args,
122-
llvm::opt::ArgStringList &Arguments) const override;
123117
};
124118

125119
class LLVM_LIBRARY_VISIBILITY WebAssembly : public ToolChain {
@@ -173,9 +167,6 @@ class LLVM_LIBRARY_VISIBILITY GenericUnix : public ToolChain {
173167
~GenericUnix() = default;
174168
std::string sanitizerRuntimeLibName(StringRef Sanitizer,
175169
bool shared = true) const override;
176-
177-
void addPluginArguments(const llvm::opt::ArgList &Args,
178-
llvm::opt::ArgStringList &Arguments) const override;
179170
};
180171

181172
class LLVM_LIBRARY_VISIBILITY Android : public GenericUnix {

lib/Driver/UnixToolChains.cpp

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -50,37 +50,6 @@ toolchains::GenericUnix::sanitizerRuntimeLibName(StringRef Sanitizer,
5050
.str();
5151
}
5252

53-
void
54-
toolchains::GenericUnix::addPluginArguments(const ArgList &Args,
55-
ArgStringList &Arguments) const {
56-
SmallString<64> pluginPath;
57-
auto programPath = getDriver().getSwiftProgramPath();
58-
CompilerInvocation::computeRuntimeResourcePathFromExecutablePath(
59-
programPath, /*shared=*/true, pluginPath);
60-
61-
// In-process plugin server path.
62-
auto inProcPluginServerPath = pluginPath;
63-
llvm::sys::path::append(inProcPluginServerPath, "host",
64-
"libSwiftInProcPluginServer.so");
65-
Arguments.push_back("-in-process-plugin-server-path");
66-
Arguments.push_back(Args.MakeArgString(inProcPluginServerPath));
67-
68-
// Default plugin path.
69-
auto defaultPluginPath = pluginPath;
70-
llvm::sys::path::append(defaultPluginPath, "host", "plugins");
71-
Arguments.push_back("-plugin-path");
72-
Arguments.push_back(Args.MakeArgString(defaultPluginPath));
73-
74-
// Local plugin path.
75-
llvm::sys::path::remove_filename(pluginPath); // Remove "swift"
76-
llvm::sys::path::remove_filename(pluginPath); // Remove "lib"
77-
llvm::sys::path::append(pluginPath, "local", "lib");
78-
llvm::sys::path::append(pluginPath, "swift");
79-
llvm::sys::path::append(pluginPath, "host", "plugins");
80-
Arguments.push_back("-plugin-path");
81-
Arguments.push_back(Args.MakeArgString(pluginPath));
82-
}
83-
8453
ToolChain::InvocationInfo
8554
toolchains::GenericUnix::constructInvocation(const InterpretJobAction &job,
8655
const JobContext &context) const {

lib/Driver/WindowsToolChains.cpp

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -44,24 +44,6 @@ std::string toolchains::Windows::sanitizerRuntimeLibName(StringRef Sanitizer,
4444
.str();
4545
}
4646

47-
void
48-
toolchains::Windows::addPluginArguments(const ArgList &Args,
49-
ArgStringList &Arguments) const {
50-
SmallString<261> LibraryPath = StringRef(getDriver().getSwiftProgramPath());
51-
llvm::sys::path::remove_filename(LibraryPath); // Remove `swift`
52-
53-
// In-process plugin server path.
54-
SmallString<261> InProcPluginServerPath = LibraryPath;
55-
llvm::sys::path::append(InProcPluginServerPath,
56-
"SwiftInProcPluginServer.dll");
57-
Arguments.push_back("-in-process-plugin-server-path");
58-
Arguments.push_back(Args.MakeArgString(InProcPluginServerPath));
59-
60-
// Default plugin path.
61-
Arguments.push_back("-plugin-path");
62-
Arguments.push_back(Args.MakeArgString(LibraryPath));
63-
}
64-
6547
ToolChain::InvocationInfo
6648
toolchains::Windows::constructInvocation(const DynamicLinkJobAction &job,
6749
const JobContext &context) const {
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// RUN: %swiftc_driver -driver-print-jobs -target x86_64-apple-macosx10.9 %s 2>^1 | %FileCheck %s
22

3-
// CHECK: -plugin-path
4-
// CHECK-SAME: {{(/|\\\\)}}lib{{(/|\\\\)}}swift{{(/|\\\\)}}host{{(/|\\\\)}}plugins
3+
// REQUIRES: OS=macosx || OS=linux-gnu
54

6-
// CHECK-SAME: -plugin-path
7-
// CHECK-SAME: {{(/|\\\\)}}local{{(/|\\\\)}}lib{{(/|\\\\)}}swift{{(/|\\\\)}}host{{(/|\\\\)}}plugins
5+
// CHECK: -plugin-path {{[^ ]+}}/lib/swift/host/plugins
6+
// CHECK-SAME: -plugin-path {{[^ ]+}}/local/lib/swift/host/plugins
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// RUN: %swiftc_driver -driver-print-jobs -target x86_64-apple-macosx10.9 %s 2>^1 | %FileCheck %s
2+
3+
// REQUIRES: OS=windows
4+
5+
// CHECK: -plugin-path {{[^ ]+}}\bin

0 commit comments

Comments
 (0)