Skip to content

Driver: virtualise the plugin path handling #68322

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion include/swift/Driver/ToolChain.h
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,10 @@ class ToolChain {
void addLinkRuntimeLib(const llvm::opt::ArgList &Args,
llvm::opt::ArgStringList &Arguments,
StringRef LibName) const;


virtual void addPluginArguments(const llvm::opt::ArgList &Args,
llvm::opt::ArgStringList &Arguments) const {}

/// Validates arguments passed to the toolchain.
///
/// An override point for platform-specific subclasses to customize the
Expand Down
26 changes: 26 additions & 0 deletions lib/Driver/DarwinToolChains.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "swift/Driver/Compilation.h"
#include "swift/Driver/Driver.h"
#include "swift/Driver/Job.h"
#include "swift/IDETool/CompilerInvocation.h"
#include "swift/Option/Options.h"
#include "clang/Basic/DarwinSDKInfo.h"
#include "clang/Basic/Version.h"
Expand Down Expand Up @@ -123,6 +124,31 @@ std::string toolchains::Darwin::sanitizerRuntimeLibName(StringRef Sanitizer,
.str();
}

void
toolchains::Darwin::addPluginArguments(const ArgList &Args,
ArgStringList &Arguments) const {
SmallString<64> pluginPath;
auto programPath = getDriver().getSwiftProgramPath();
CompilerInvocation::computeRuntimeResourcePathFromExecutablePath(
programPath, /*shared=*/true, pluginPath);

auto defaultPluginPath = pluginPath;
llvm::sys::path::append(defaultPluginPath, "host", "plugins");

// Default plugin path.
Arguments.push_back("-plugin-path");
Arguments.push_back(Args.MakeArgString(defaultPluginPath));

// Local plugin path.
llvm::sys::path::remove_filename(pluginPath); // Remove "swift"
llvm::sys::path::remove_filename(pluginPath); // Remove "lib"
llvm::sys::path::append(pluginPath, "local", "lib");
llvm::sys::path::append(pluginPath, "swift");
llvm::sys::path::append(pluginPath, "host", "plugins");
Arguments.push_back("-plugin-path");
Arguments.push_back(Args.MakeArgString(pluginPath));
}

static void addLinkRuntimeLibRPath(const ArgList &Args,
ArgStringList &Arguments,
StringRef DarwinLibName,
Expand Down
24 changes: 1 addition & 23 deletions lib/Driver/ToolChains.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -372,30 +372,8 @@ void ToolChain::addCommonFrontendArgs(const OutputInfo &OI,
// options.
inputArgs.AddAllArgs(arguments, options::OPT_plugin_search_Group);
addPlatformSpecificPluginFrontendArgs(OI, output, inputArgs, arguments);

// Toolchain-relative plugin paths
{
SmallString<64> pluginPath;
auto programPath = getDriver().getSwiftProgramPath();
CompilerInvocation::computeRuntimeResourcePathFromExecutablePath(
programPath, /*shared=*/true, pluginPath);

auto defaultPluginPath = pluginPath;
llvm::sys::path::append(defaultPluginPath, "host", "plugins");

// Default plugin path.
arguments.push_back("-plugin-path");
arguments.push_back(inputArgs.MakeArgString(defaultPluginPath));

// Local plugin path.
llvm::sys::path::remove_filename(pluginPath); // Remove "swift"
llvm::sys::path::remove_filename(pluginPath); // Remove "lib"
llvm::sys::path::append(pluginPath, "local", "lib");
llvm::sys::path::append(pluginPath, "swift");
llvm::sys::path::append(pluginPath, "host", "plugins");
arguments.push_back("-plugin-path");
arguments.push_back(inputArgs.MakeArgString(pluginPath));
}
addPluginArguments(inputArgs, arguments);

// Pass through any subsystem flags.
inputArgs.AddAllArgs(arguments, options::OPT_Xllvm);
Expand Down
12 changes: 11 additions & 1 deletion lib/Driver/ToolChains.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,10 @@ class LLVM_LIBRARY_VISIBILITY Darwin : public ToolChain {
const JobContext &context) const override;
InvocationInfo constructInvocation(const StaticLinkJobAction &job,
const JobContext &context) const override;


void addPluginArguments(const llvm::opt::ArgList &Args,
llvm::opt::ArgStringList &Arguments) const override;

void validateArguments(DiagnosticEngine &diags,
const llvm::opt::ArgList &args,
StringRef defaultTarget) const override;
Expand Down Expand Up @@ -113,8 +116,12 @@ class LLVM_LIBRARY_VISIBILITY Windows : public ToolChain {
public:
Windows(const Driver &D, const llvm::Triple &Triple) : ToolChain(D, Triple) {}
~Windows() = default;

std::string sanitizerRuntimeLibName(StringRef Sanitizer,
bool shared = true) const override;

void addPluginArguments(const llvm::opt::ArgList &Args,
llvm::opt::ArgStringList &Arguments) const override;
};

class LLVM_LIBRARY_VISIBILITY WebAssembly : public ToolChain {
Expand Down Expand Up @@ -168,6 +175,9 @@ class LLVM_LIBRARY_VISIBILITY GenericUnix : public ToolChain {
~GenericUnix() = default;
std::string sanitizerRuntimeLibName(StringRef Sanitizer,
bool shared = true) const override;

void addPluginArguments(const llvm::opt::ArgList &Args,
llvm::opt::ArgStringList &Arguments) const override;
};

class LLVM_LIBRARY_VISIBILITY Android : public GenericUnix {
Expand Down
26 changes: 26 additions & 0 deletions lib/Driver/UnixToolChains.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "swift/Driver/Compilation.h"
#include "swift/Driver/Driver.h"
#include "swift/Driver/Job.h"
#include "swift/IDETool/CompilerInvocation.h"
#include "swift/Option/Options.h"
#include "swift/Option/SanitizerOptions.h"
#include "clang/Basic/Version.h"
Expand All @@ -47,6 +48,31 @@ toolchains::GenericUnix::sanitizerRuntimeLibName(StringRef Sanitizer,
.str();
}

void
toolchains::GenericUnix::addPluginArguments(const ArgList &Args,
ArgStringList &Arguments) const {
SmallString<64> pluginPath;
auto programPath = getDriver().getSwiftProgramPath();
CompilerInvocation::computeRuntimeResourcePathFromExecutablePath(
programPath, /*shared=*/true, pluginPath);

auto defaultPluginPath = pluginPath;
llvm::sys::path::append(defaultPluginPath, "host", "plugins");

// Default plugin path.
Arguments.push_back("-plugin-path");
Arguments.push_back(Args.MakeArgString(defaultPluginPath));

// Local plugin path.
llvm::sys::path::remove_filename(pluginPath); // Remove "swift"
llvm::sys::path::remove_filename(pluginPath); // Remove "lib"
llvm::sys::path::append(pluginPath, "local", "lib");
llvm::sys::path::append(pluginPath, "swift");
llvm::sys::path::append(pluginPath, "host", "plugins");
Arguments.push_back("-plugin-path");
Arguments.push_back(Args.MakeArgString(pluginPath));
}

ToolChain::InvocationInfo
toolchains::GenericUnix::constructInvocation(const InterpretJobAction &job,
const JobContext &context) const {
Expand Down
11 changes: 11 additions & 0 deletions lib/Driver/WindowsToolChains.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,17 @@ std::string toolchains::Windows::sanitizerRuntimeLibName(StringRef Sanitizer,
.str();
}

void
toolchains::Windows::addPluginArguments(const ArgList &Args,
ArgStringList &Arguments) const {
SmallString<261> LibraryPath = StringRef(getDriver().getSwiftProgramPath());
llvm::sys::path::remove_filename(LibraryPath); // Remove `swift`

// Default plugin path.
Arguments.push_back("-plugin-path");
Arguments.push_back(Args.MakeArgString(LibraryPath));
}

ToolChain::InvocationInfo
toolchains::Windows::constructInvocation(const DynamicLinkJobAction &job,
const JobContext &context) const {
Expand Down
27 changes: 15 additions & 12 deletions test/Macros/lit.local.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,21 @@ def get_target_os():
return run_os

if get_target_os() in ['windows-msvc']:
config.substitutions.insert(
0,
(
'%swift-build-cxx-plugin',
'%clang -isysroot %host_sdk -I %swift_src_root/include -L %swift-lib-dir -l_swiftMockPlugin'
config.substitutions.insert(0, ('%target-abi', 'WIN'))
config.substitutions.insert(
0,
(
'%swift-build-cxx-plugin',
'%clang -isysroot %host_sdk -I %swift_src_root/include -L %swift-lib-dir -l_swiftMockPlugin'
)
)
)
else:
config.substitutions.insert(
0,
(
'%swift-build-cxx-plugin',
'%clang -isysroot %host_sdk -I %swift_src_root/include -L %swift-lib-dir -l_swiftMockPlugin -Xlinker -rpath -Xlinker %swift-lib-dir'
# FIXME(compnerd) do all the targets we currently support use SysV ABI?
config.substitutions.insert(0, ('%target-abi', 'SYSV'))
config.substitutions.insert(
0,
(
'%swift-build-cxx-plugin',
'%clang -isysroot %host_sdk -I %swift_src_root/include -L %swift-lib-dir -l_swiftMockPlugin -Xlinker -rpath -Xlinker %swift-lib-dir'
)
)
)
4 changes: 2 additions & 2 deletions test/Macros/serialize_plugin_search_paths.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@
// CHECK: -external-plugin-path {{.*}}plugins#{{.*}}swift-plugin-server
// CHECK: -load-plugin-library {{.*}}MacroDefinition.{{dylib|so|dll}}
// CHECK: -load-plugin-executable {{.*}}mock-plugin#TestPlugin
// CHECK: -plugin-path {{.*}}plugins
// CHECK: -plugin-path {{.*}}plugins
// CHECK-SYSV:-plugin-path {{.*}}plugins
// CHECK-SYSV:-plugin-path {{.*}}plugins