Skip to content

Commit 1093743

Browse files
drodriguezrintaro
authored andcommitted
[Macros] In-process plugin server library tied to compiler host, not target
PR swiftlang#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. (cherry picked from commit 55d9e74)
1 parent b5bacff commit 1093743

File tree

8 files changed

+73
-96
lines changed

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
@@ -128,37 +128,6 @@ std::string toolchains::Darwin::sanitizerRuntimeLibName(StringRef Sanitizer,
128128
.str();
129129
}
130130

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

lib/Driver/ToolChains.cpp

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

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

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

lib/Driver/WindowsToolChains.cpp

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

46-
void
47-
toolchains::Windows::addPluginArguments(const ArgList &Args,
48-
ArgStringList &Arguments) const {
49-
SmallString<261> LibraryPath = StringRef(getDriver().getSwiftProgramPath());
50-
llvm::sys::path::remove_filename(LibraryPath); // Remove `swift`
51-
52-
// In-process plugin server path.
53-
SmallString<261> InProcPluginServerPath = LibraryPath;
54-
llvm::sys::path::append(InProcPluginServerPath,
55-
"SwiftInProcPluginServer.dll");
56-
Arguments.push_back("-in-process-plugin-server-path");
57-
Arguments.push_back(Args.MakeArgString(InProcPluginServerPath));
58-
59-
// Default plugin path.
60-
Arguments.push_back("-plugin-path");
61-
Arguments.push_back(Args.MakeArgString(LibraryPath));
62-
}
63-
6446
ToolChain::InvocationInfo
6547
toolchains::Windows::constructInvocation(const DynamicLinkJobAction &job,
6648
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)