Skip to content

Commit 4665413

Browse files
authored
Merge pull request swiftlang#68322 from compnerd/virtual-path-handling
Driver: virtualise the plugin path handling
2 parents 3065ab4 + fcd7b9e commit 4665413

File tree

8 files changed

+96
-39
lines changed

8 files changed

+96
-39
lines changed

include/swift/Driver/ToolChain.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,10 @@ class ToolChain {
340340
void addLinkRuntimeLib(const llvm::opt::ArgList &Args,
341341
llvm::opt::ArgStringList &Arguments,
342342
StringRef LibName) const;
343-
343+
344+
virtual void addPluginArguments(const llvm::opt::ArgList &Args,
345+
llvm::opt::ArgStringList &Arguments) const {}
346+
344347
/// Validates arguments passed to the toolchain.
345348
///
346349
/// An override point for platform-specific subclasses to customize the

lib/Driver/DarwinToolChains.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "swift/Driver/Compilation.h"
2525
#include "swift/Driver/Driver.h"
2626
#include "swift/Driver/Job.h"
27+
#include "swift/IDETool/CompilerInvocation.h"
2728
#include "swift/Option/Options.h"
2829
#include "clang/Basic/DarwinSDKInfo.h"
2930
#include "clang/Basic/Version.h"
@@ -123,6 +124,31 @@ std::string toolchains::Darwin::sanitizerRuntimeLibName(StringRef Sanitizer,
123124
.str();
124125
}
125126

127+
void
128+
toolchains::Darwin::addPluginArguments(const ArgList &Args,
129+
ArgStringList &Arguments) const {
130+
SmallString<64> pluginPath;
131+
auto programPath = getDriver().getSwiftProgramPath();
132+
CompilerInvocation::computeRuntimeResourcePathFromExecutablePath(
133+
programPath, /*shared=*/true, pluginPath);
134+
135+
auto defaultPluginPath = pluginPath;
136+
llvm::sys::path::append(defaultPluginPath, "host", "plugins");
137+
138+
// Default plugin path.
139+
Arguments.push_back("-plugin-path");
140+
Arguments.push_back(Args.MakeArgString(defaultPluginPath));
141+
142+
// Local plugin path.
143+
llvm::sys::path::remove_filename(pluginPath); // Remove "swift"
144+
llvm::sys::path::remove_filename(pluginPath); // Remove "lib"
145+
llvm::sys::path::append(pluginPath, "local", "lib");
146+
llvm::sys::path::append(pluginPath, "swift");
147+
llvm::sys::path::append(pluginPath, "host", "plugins");
148+
Arguments.push_back("-plugin-path");
149+
Arguments.push_back(Args.MakeArgString(pluginPath));
150+
}
151+
126152
static void addLinkRuntimeLibRPath(const ArgList &Args,
127153
ArgStringList &Arguments,
128154
StringRef DarwinLibName,

lib/Driver/ToolChains.cpp

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -372,30 +372,8 @@ void ToolChain::addCommonFrontendArgs(const OutputInfo &OI,
372372
// options.
373373
inputArgs.AddAllArgs(arguments, options::OPT_plugin_search_Group);
374374
addPlatformSpecificPluginFrontendArgs(OI, output, inputArgs, arguments);
375-
376-
// Toolchain-relative plugin paths
377-
{
378-
SmallString<64> pluginPath;
379-
auto programPath = getDriver().getSwiftProgramPath();
380-
CompilerInvocation::computeRuntimeResourcePathFromExecutablePath(
381-
programPath, /*shared=*/true, pluginPath);
382375

383-
auto defaultPluginPath = pluginPath;
384-
llvm::sys::path::append(defaultPluginPath, "host", "plugins");
385-
386-
// Default plugin path.
387-
arguments.push_back("-plugin-path");
388-
arguments.push_back(inputArgs.MakeArgString(defaultPluginPath));
389-
390-
// Local plugin path.
391-
llvm::sys::path::remove_filename(pluginPath); // Remove "swift"
392-
llvm::sys::path::remove_filename(pluginPath); // Remove "lib"
393-
llvm::sys::path::append(pluginPath, "local", "lib");
394-
llvm::sys::path::append(pluginPath, "swift");
395-
llvm::sys::path::append(pluginPath, "host", "plugins");
396-
arguments.push_back("-plugin-path");
397-
arguments.push_back(inputArgs.MakeArgString(pluginPath));
398-
}
376+
addPluginArguments(inputArgs, arguments);
399377

400378
// Pass through any subsystem flags.
401379
inputArgs.AddAllArgs(arguments, options::OPT_Xllvm);

lib/Driver/ToolChains.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,10 @@ class LLVM_LIBRARY_VISIBILITY Darwin : public ToolChain {
6565
const JobContext &context) const override;
6666
InvocationInfo constructInvocation(const StaticLinkJobAction &job,
6767
const JobContext &context) const override;
68-
68+
69+
void addPluginArguments(const llvm::opt::ArgList &Args,
70+
llvm::opt::ArgStringList &Arguments) const override;
71+
6972
void validateArguments(DiagnosticEngine &diags,
7073
const llvm::opt::ArgList &args,
7174
StringRef defaultTarget) const override;
@@ -113,8 +116,12 @@ class LLVM_LIBRARY_VISIBILITY Windows : public ToolChain {
113116
public:
114117
Windows(const Driver &D, const llvm::Triple &Triple) : ToolChain(D, Triple) {}
115118
~Windows() = default;
119+
116120
std::string sanitizerRuntimeLibName(StringRef Sanitizer,
117121
bool shared = true) const override;
122+
123+
void addPluginArguments(const llvm::opt::ArgList &Args,
124+
llvm::opt::ArgStringList &Arguments) const override;
118125
};
119126

120127
class LLVM_LIBRARY_VISIBILITY WebAssembly : public ToolChain {
@@ -168,6 +175,9 @@ class LLVM_LIBRARY_VISIBILITY GenericUnix : public ToolChain {
168175
~GenericUnix() = default;
169176
std::string sanitizerRuntimeLibName(StringRef Sanitizer,
170177
bool shared = true) const override;
178+
179+
void addPluginArguments(const llvm::opt::ArgList &Args,
180+
llvm::opt::ArgStringList &Arguments) const override;
171181
};
172182

173183
class LLVM_LIBRARY_VISIBILITY Android : public GenericUnix {

lib/Driver/UnixToolChains.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "swift/Driver/Compilation.h"
2222
#include "swift/Driver/Driver.h"
2323
#include "swift/Driver/Job.h"
24+
#include "swift/IDETool/CompilerInvocation.h"
2425
#include "swift/Option/Options.h"
2526
#include "swift/Option/SanitizerOptions.h"
2627
#include "clang/Basic/Version.h"
@@ -47,6 +48,31 @@ toolchains::GenericUnix::sanitizerRuntimeLibName(StringRef Sanitizer,
4748
.str();
4849
}
4950

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

lib/Driver/WindowsToolChains.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,17 @@ 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+
// Default plugin path.
54+
Arguments.push_back("-plugin-path");
55+
Arguments.push_back(Args.MakeArgString(LibraryPath));
56+
}
57+
4758
ToolChain::InvocationInfo
4859
toolchains::Windows::constructInvocation(const DynamicLinkJobAction &job,
4960
const JobContext &context) const {

test/Macros/lit.local.cfg

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,21 @@ def get_target_os():
1010
return run_os
1111

1212
if get_target_os() in ['windows-msvc']:
13-
config.substitutions.insert(
14-
0,
15-
(
16-
'%swift-build-cxx-plugin',
17-
'%clang -isysroot %host_sdk -I %swift_src_root/include -L %swift-lib-dir -l_swiftMockPlugin'
13+
config.substitutions.insert(0, ('%target-abi', 'WIN'))
14+
config.substitutions.insert(
15+
0,
16+
(
17+
'%swift-build-cxx-plugin',
18+
'%clang -isysroot %host_sdk -I %swift_src_root/include -L %swift-lib-dir -l_swiftMockPlugin'
19+
)
1820
)
19-
)
2021
else:
21-
config.substitutions.insert(
22-
0,
23-
(
24-
'%swift-build-cxx-plugin',
25-
'%clang -isysroot %host_sdk -I %swift_src_root/include -L %swift-lib-dir -l_swiftMockPlugin -Xlinker -rpath -Xlinker %swift-lib-dir'
22+
# FIXME(compnerd) do all the targets we currently support use SysV ABI?
23+
config.substitutions.insert(0, ('%target-abi', 'SYSV'))
24+
config.substitutions.insert(
25+
0,
26+
(
27+
'%swift-build-cxx-plugin',
28+
'%clang -isysroot %host_sdk -I %swift_src_root/include -L %swift-lib-dir -l_swiftMockPlugin -Xlinker -rpath -Xlinker %swift-lib-dir'
29+
)
2630
)
27-
)

test/Macros/serialize_plugin_search_paths.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,5 @@
1616
// CHECK: -external-plugin-path {{.*}}plugins#{{.*}}swift-plugin-server
1717
// CHECK: -load-plugin-library {{.*}}MacroDefinition.{{dylib|so|dll}}
1818
// CHECK: -load-plugin-executable {{.*}}mock-plugin#TestPlugin
19-
// CHECK: -plugin-path {{.*}}plugins
20-
// CHECK: -plugin-path {{.*}}plugins
19+
// CHECK-SYSV:-plugin-path {{.*}}plugins
20+
// CHECK-SYSV:-plugin-path {{.*}}plugins

0 commit comments

Comments
 (0)