[Macros] Add swift-plugin-server executable

This executable is intended to be installed in the toolchain and act as
an executable compiler plugin just like other 'macro' plugins.

This plugin server has an optional method 'loadPluginLibrary' that
dynamically loads dylib plugins.
The compiler has a newly added option '-external-plugin-path'. This
option receives a pair of the plugin library search path (just like
'-plugin-path') and the corresponding "plugin server" path, separated
by '#'. i.e.

  -external-plugin-path
    <plugin library search path>#<plugin server executable path>

For exmaple, when there's a macro decl:

  @freestanding(expression)
  macro stringify<T>(T) -> (T, String) =
      #externalMacro(module: "BasicMacro", type: "StringifyMacro")

The compiler look for 'libBasicMacro.dylib' in '-plugin-path' paths,
if not found, it falls back to '-external-plugin-path' and tries to find
'libBasicMacro.dylib' in them. If it's found, the "plugin server" path
is launched just like an executable plugin, then 'loadPluginLibrary'
method is invoked via IPC, which 'dlopen' the library path in the plugin
server. At the actual macro expansion, the mangled name for
'BasicMacro.StringifyMacro' is used to resolve the macro  just like
dylib plugins in the compiler.

This is useful for
 * Isolating the plugin process, so the plugin crashes doesn't result
   the compiler crash
 * Being able to use library plugins linked with other `swift-syntax`
   versions

rdar://105104850
This commit is contained in:
Rintaro Ishizaki
2023-03-13 12:17:24 -07:00
parent aeab985647
commit c4b3edd6df
24 changed files with 710 additions and 55 deletions

View File

@@ -1492,6 +1492,15 @@ static bool ParseSearchPathArgs(SearchPathOptions &Opts,
Opts.PluginSearchPaths.push_back(resolveSearchPath(A->getValue()));
}
for (const Arg *A : Args.filtered(OPT_external_plugin_path)) {
// '<plugin directory>#<plugin server executable path>'.
StringRef dylibPath;
StringRef serverPath;
std::tie(dylibPath, serverPath) = StringRef(A->getValue()).split('#');
Opts.ExternalPluginSearchPaths.push_back(
resolveSearchPath(dylibPath) + "#" + resolveSearchPath(serverPath));
}
for (const Arg *A : Args.filtered(OPT_L)) {
Opts.LibrarySearchPaths.push_back(resolveSearchPath(A->getValue()));
}