Skip to content

Commit

Permalink
[Macros] In-process plugin server library tied to compiler host, not …
Browse files Browse the repository at this point in the history
…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)
  • Loading branch information
drodriguez authored and rintaro committed Jul 1, 2024
1 parent b5bacff commit 1093743
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 96 deletions.
6 changes: 3 additions & 3 deletions include/swift/Driver/ToolChain.h
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,9 @@ class ToolChain {
const InvocationInfo &invocationInfo,
const JobContext &context) const;

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

public:
virtual ~ToolChain() = default;

Expand Down Expand Up @@ -341,9 +344,6 @@ class ToolChain {
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
31 changes: 0 additions & 31 deletions lib/Driver/DarwinToolChains.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,37 +128,6 @@ 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);

// In-process plugin server path.
auto inProcPluginServerPath = pluginPath;
llvm::sys::path::append(inProcPluginServerPath, "host",
"libSwiftInProcPluginServer.dylib");
Arguments.push_back("-in-process-plugin-server-path");
Arguments.push_back(Args.MakeArgString(inProcPluginServerPath));

// Default plugin path.
auto defaultPluginPath = pluginPath;
llvm::sys::path::append(defaultPluginPath, "host", "plugins");
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
62 changes: 62 additions & 0 deletions lib/Driver/ToolChains.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1466,6 +1466,68 @@ void ToolChain::addLinkRuntimeLib(const ArgList &Args, ArgStringList &Arguments,
Arguments.push_back(Args.MakeArgString(P));
}

static void appendInProcPluginServerPath(StringRef PluginPathRoot,
llvm::SmallVectorImpl<char> &InProcPluginServerPath) {
InProcPluginServerPath.append(PluginPathRoot.begin(), PluginPathRoot.end());
#if defined(_WIN32)
llvm::sys::path::append(InProcPluginServerPath, "bin", "SwiftInProcPluginServer.dll");
#elif defined(__APPLE__)
llvm::sys::path::append(InProcPluginServerPath, "lib", "swift", "host", "libSwiftInProcPluginServer.dylib");
#elif defined(__unix__)
llvm::sys::path::append(InProcPluginServerPath, "lib", "swift", "host", "libSwiftInProcPluginServer.so");
#else
#error Unknown compiler host
#endif
}

static void appendPluginsPath(StringRef PluginPathRoot,
llvm::SmallVectorImpl<char> &PluginsPath) {
PluginsPath.append(PluginPathRoot.begin(), PluginPathRoot.end());
#if defined(_WIN32)
llvm::sys::path::append(PluginsPath, "bin");
#elif defined(__APPLE__) || defined(__unix__)
llvm::sys::path::append(PluginsPath, "lib", "swift", "host", "plugins");
#else
#error Unknown compiler host
#endif
}

#if defined(__APPLE__) || defined(__unix__)
static void appendLocalPluginsPath(StringRef PluginPathRoot,
llvm::SmallVectorImpl<char> &LocalPluginsPath) {
SmallString<261> localPluginPathRoot = PluginPathRoot;
llvm::sys::path::append(localPluginPathRoot, "local");
appendPluginsPath(localPluginPathRoot, LocalPluginsPath);
}
#endif

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

// In-process plugin server path.
SmallString<261> inProcPluginServerPath;
appendInProcPluginServerPath(pluginPathRoot, inProcPluginServerPath);
Arguments.push_back("-in-process-plugin-server-path");
Arguments.push_back(Args.MakeArgString(inProcPluginServerPath));

// Default plugin path.
SmallString<261> defaultPluginPath;
appendPluginsPath(pluginPathRoot, defaultPluginPath);
Arguments.push_back("-plugin-path");
Arguments.push_back(Args.MakeArgString(defaultPluginPath));

// Local plugin path.
#if defined(__APPLE__) || defined(__unix__)
SmallString<261> localPluginPath;
appendLocalPluginsPath(pluginPathRoot, localPluginPath);
Arguments.push_back("-plugin-path");
Arguments.push_back(Args.MakeArgString(localPluginPath));
#endif
}

void ToolChain::getClangLibraryPath(const ArgList &Args,
SmallString<128> &LibPath) const {
const llvm::Triple &T = getTriple();
Expand Down
9 changes: 0 additions & 9 deletions lib/Driver/ToolChains.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,6 @@ class LLVM_LIBRARY_VISIBILITY Darwin : public ToolChain {
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 @@ -117,9 +114,6 @@ class LLVM_LIBRARY_VISIBILITY Windows : public ToolChain {

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 @@ -173,9 +167,6 @@ 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
31 changes: 0 additions & 31 deletions lib/Driver/UnixToolChains.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,37 +47,6 @@ 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);

// In-process plugin server path.
auto inProcPluginServerPath = pluginPath;
llvm::sys::path::append(inProcPluginServerPath, "host",
"libSwiftInProcPluginServer.so");
Arguments.push_back("-in-process-plugin-server-path");
Arguments.push_back(Args.MakeArgString(inProcPluginServerPath));

// Default plugin path.
auto defaultPluginPath = pluginPath;
llvm::sys::path::append(defaultPluginPath, "host", "plugins");
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
18 changes: 0 additions & 18 deletions lib/Driver/WindowsToolChains.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,24 +43,6 @@ 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`

// In-process plugin server path.
SmallString<261> InProcPluginServerPath = LibraryPath;
llvm::sys::path::append(InProcPluginServerPath,
"SwiftInProcPluginServer.dll");
Arguments.push_back("-in-process-plugin-server-path");
Arguments.push_back(Args.MakeArgString(InProcPluginServerPath));

// 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
7 changes: 3 additions & 4 deletions test/Driver/compiler_plugin_path.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// RUN: %swiftc_driver -driver-print-jobs -target x86_64-apple-macosx10.9 %s 2>^1 | %FileCheck %s

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

// CHECK-SAME: -plugin-path
// CHECK-SAME: {{(/|\\\\)}}local{{(/|\\\\)}}lib{{(/|\\\\)}}swift{{(/|\\\\)}}host{{(/|\\\\)}}plugins
// CHECK: -plugin-path {{[^ ]+}}/lib/swift/host/plugins
// CHECK-SAME: -plugin-path {{[^ ]+}}/local/lib/swift/host/plugins
5 changes: 5 additions & 0 deletions test/Driver/compiler_plugin_path_windows.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// RUN: %swiftc_driver -driver-print-jobs -target x86_64-apple-macosx10.9 %s 2>^1 | %FileCheck %s

// REQUIRES: OS=windows

// CHECK: -plugin-path {{[^ ]+}}\bin

0 comments on commit 1093743

Please sign in to comment.