Skip to content

Commit abc1390

Browse files
committed
[Macros] In-process plugin server
Separate swift-syntax libs for the compiler and for the library plugins. Compiler communicates with library plugins using serialized messages just like executable plugins. * `libswiftSyntaxUnfied`(`lib/CompilerSwiftSyntax`): swift-syntax libraries for compiler. This is a single shared library that bundles all swift-syntax libraries. Library evolution is disabled. * Compiler (`ASTGen` and `swiftIDEUtilsBridging`) only depends on `libswiftSyntaxUnfied`. * `SwiftInProcPluginServer`: In-process plugin server shared library. This has one `swift_inproc_plugins_handle_message` entry point that receives a message and return the response. * In the compiler * Add `-in-process-plugin-server-path` front-end option, which specifies the `SwiftInProcPluginServer` shared library path. * Remove `LoadedLibraryPlugin`, because all library plugins are managed by `SwiftInProcPluginServer` * Introduce abstract `CompilerPlugin` class that has 2 subclasses: * `LoadedExecutablePlugin` existing class that represents an executable plugin * `InProcessPlugins` wraps `dlopen`ed `SwiftInProcPluginServer` * Unified the code path in `TypeCheckMacros.cpp` and `ASTGen`, the difference between executable plugins and library plugins are now abstracted by `CompilerPlugin`
1 parent e1a82f6 commit abc1390

35 files changed

+666
-845
lines changed

cmake/modules/AddPureSwift.cmake

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -218,15 +218,6 @@ function(add_pure_swift_host_library name)
218218
# Depends on all '*.h' files in 'include/module.modulemap'.
219219
force_add_dependencies(${name} importedHeaderDependencies)
220220

221-
# Workaround to touch the library and its objects so that we don't
222-
# continually rebuild (again, see corresponding change in swift-syntax).
223-
add_custom_command(
224-
TARGET ${name}
225-
POST_BUILD
226-
COMMAND "${CMAKE_COMMAND}" -E touch_nocreate $<TARGET_FILE:${name}> $<TARGET_OBJECTS:${name}> "${SWIFT_HOST_LIBRARIES_DEST_DIR}/${name}.swiftmodule" "${CMAKE_CURRENT_BINARY_DIR}/${name}.swiftmodule"
227-
COMMAND_EXPAND_LISTS
228-
COMMENT "Update mtime of library outputs workaround")
229-
230221
# Link against dependencies.
231222
target_link_libraries(${name} PUBLIC
232223
${APSHL_DEPENDENCIES}
@@ -236,15 +227,7 @@ function(add_pure_swift_host_library name)
236227
${APSHL_SWIFT_DEPENDENCIES}
237228
)
238229

239-
# Make sure we can use the host libraries.
240-
target_include_directories(${name} PUBLIC
241-
"${SWIFT_HOST_LIBRARIES_DEST_DIR}")
242-
target_link_directories(${name} PUBLIC
243-
"${SWIFT_HOST_LIBRARIES_DEST_DIR}")
244-
245230
if(APSHL_EMIT_MODULE)
246-
# Determine where Swift modules will be built and installed.
247-
248231
set(module_triple "${SWIFT_HOST_MODULE_TRIPLE}")
249232
set(module_dir "${SWIFT_HOST_LIBRARIES_DEST_DIR}")
250233
set(module_base "${module_dir}/${name}.swiftmodule")
@@ -253,14 +236,6 @@ function(add_pure_swift_host_library name)
253236
set(module_private_interface_file "${module_base}/${module_triple}.private.swiftinterface")
254237
set(module_sourceinfo_file "${module_base}/${module_triple}.swiftsourceinfo")
255238

256-
set_target_properties(${name} PROPERTIES
257-
# Set the default module name to the target name.
258-
Swift_MODULE_NAME ${name}
259-
# Install the Swift module into the appropriate location.
260-
Swift_MODULE_DIRECTORY ${module_dir}
261-
# NOTE: workaround for CMake not setting up include flags.
262-
INTERFACE_INCLUDE_DIRECTORIES ${module_dir})
263-
264239
# Create the module directory.
265240
add_custom_command(
266241
TARGET ${name}
@@ -280,12 +255,27 @@ function(add_pure_swift_host_library name)
280255
>)
281256
else()
282257
# Emit a swiftmodule in the current directory.
283-
set_target_properties(${name} PROPERTIES
284-
Swift_MODULE_NAME ${name}
285-
Swift_MODULE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
286-
set(module_file "${CMAKE_CURRENT_BINARY_DIR}/${name}.swiftmodule")
258+
set(module_dir "${CMAKE_CURRENT_BINARY_DIR}")
259+
set(module_file "${module_dir}/${name}.swiftmodule")
287260
endif()
288261

262+
set_target_properties(${name} PROPERTIES
263+
# Set the default module name to the target name.
264+
Swift_MODULE_NAME ${name}
265+
# Install the Swift module into the appropriate location.
266+
Swift_MODULE_DIRECTORY ${module_dir}
267+
# NOTE: workaround for CMake not setting up include flags.
268+
INTERFACE_INCLUDE_DIRECTORIES ${module_dir})
269+
270+
# Workaround to touch the library and its objects so that we don't
271+
# continually rebuild (again, see corresponding change in swift-syntax).
272+
add_custom_command(
273+
TARGET ${name}
274+
POST_BUILD
275+
COMMAND "${CMAKE_COMMAND}" -E touch_nocreate $<TARGET_FILE:${name}> $<TARGET_OBJECTS:${name}> "${module_file}"
276+
COMMAND_EXPAND_LISTS
277+
COMMENT "Update mtime of library outputs workaround")
278+
289279
# Downstream linking should include the swiftmodule in debug builds to allow lldb to
290280
# work correctly. Only do this on Darwin since neither gold (currently used by default
291281
# on Linux), nor the default Windows linker 'link' support '-add_ast_path'.

cmake/modules/AddSwift.cmake

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,12 +1012,12 @@ function(add_swift_host_tool executable)
10121012
set_property(
10131013
TARGET ${executable}
10141014
APPEND PROPERTY INSTALL_RPATH
1015-
"@executable_path/../${extra_relative_rpath}lib/swift/host")
1015+
"@executable_path/../${extra_relative_rpath}lib")
10161016
else()
10171017
set_property(
10181018
TARGET ${executable}
10191019
APPEND PROPERTY INSTALL_RPATH
1020-
"$ORIGIN/../${extra_relative_rpath}lib/swift/host")
1020+
"$ORIGIN/../${extra_relative_rpath}lib")
10211021
endif()
10221022
endif()
10231023

include/swift/AST/ASTContext.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,6 @@ namespace swift {
8383
class DifferentiableAttr;
8484
class ExtensionDecl;
8585
struct ExternalSourceLocs;
86-
class LoadedExecutablePlugin;
87-
class LoadedLibraryPlugin;
8886
class ForeignRepresentationInfo;
8987
class FuncDecl;
9088
class GenericContext;

include/swift/AST/MacroDefinition.h

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,38 @@ namespace swift {
2626
class ASTContext;
2727

2828
/// A reference to an external macro definition that is understood by ASTGen.
29-
struct ExternalMacroDefinition {
30-
enum class PluginKind : int8_t {
31-
InProcess = 0,
32-
Executable = 1,
33-
Error = -1,
29+
class ExternalMacroDefinition {
30+
enum class Status : int8_t {
31+
Success = 0,
32+
Error,
3433
};
35-
PluginKind kind;
34+
Status kind;
3635
/// ASTGen's notion of an macro definition, which is opaque to the C++ part
3736
/// of the compiler. If 'kind' is 'PluginKind::Error', this is a C-string to
3837
/// the error message
39-
const void *opaqueHandle = nullptr;
38+
const void *opaqueHandle;
39+
40+
ExternalMacroDefinition(Status kind, const void *opaqueHandle)
41+
: kind(kind), opaqueHandle(opaqueHandle) {}
42+
43+
public:
44+
static ExternalMacroDefinition success(const void *opaqueHandle) {
45+
return ExternalMacroDefinition{Status::Success, opaqueHandle};
46+
}
4047

4148
static ExternalMacroDefinition error(NullTerminatedStringRef message) {
42-
return ExternalMacroDefinition{PluginKind::Error,
49+
return ExternalMacroDefinition{Status::Error,
4350
static_cast<const void *>(message.data())};
4451
}
45-
bool isError() const { return kind == PluginKind::Error; }
52+
53+
const void *get() {
54+
if (kind != Status::Success)
55+
return nullptr;
56+
return opaqueHandle;
57+
}
58+
bool isError() const { return kind == Status::Error; }
4659
NullTerminatedStringRef getErrorMessage() const {
60+
assert(isError());
4761
return static_cast<const char *>(opaqueHandle);
4862
}
4963
};

include/swift/AST/PluginLoader.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,14 @@ class PluginLoader {
7878
/// returns a nullptr.
7979
/// NOTE: This method is idempotent. If the plugin is already loaded, the same
8080
/// instance is simply returned.
81-
llvm::Expected<LoadedLibraryPlugin *> loadLibraryPlugin(llvm::StringRef path);
81+
llvm::Expected<CompilerPlugin *> getInProcessPlugins();
8282

8383
/// Launch the specified executable plugin path resolving the path with the
8484
/// current VFS. If it fails to load the plugin, a diagnostic is emitted, and
8585
/// returns a nullptr.
8686
/// NOTE: This method is idempotent. If the plugin is already loaded, the same
8787
/// instance is simply returned.
88-
llvm::Expected<LoadedExecutablePlugin *>
89-
loadExecutablePlugin(llvm::StringRef path);
88+
llvm::Expected<CompilerPlugin *> loadExecutablePlugin(llvm::StringRef path);
9089

9190
/// Add the specified plugin associated with the module name to the dependency
9291
/// tracker if needed.

0 commit comments

Comments
 (0)