Skip to content

Commit 2f7aa42

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. * `lib/swift/host/compiler/lib_Compiler*.dylib`(`lib/CompilerSwiftSyntax`): swift-syntax libraries for compiler. Library evolution is disabled. * Compiler (`ASTGen` and `swiftIDEUtilsBridging`) only depends on `lib/swift/host/compiler` libraries. * `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 af1d601 commit 2f7aa42

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+695
-889
lines changed

cmake/modules/AddPureSwift.cmake

+23-34
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,12 @@ include(macCatalystUtils)
44
function(force_add_dependencies TARGET)
55
foreach(DEPENDENCY ${ARGN})
66
string(REGEX REPLACE [<>:\"/\\|?*] _ sanitized ${DEPENDENCY})
7-
add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/forced-${sanitized}-dep.swift
8-
COMMAND ${CMAKE_COMMAND} -E touch ${CMAKE_CURRENT_BINARY_DIR}/forced-${sanitized}-dep.swift
7+
set(depfile "${CMAKE_CURRENT_BINARY_DIR}/forced-${sanitized}-dep.swift")
8+
add_custom_command(OUTPUT ${depfile}
9+
COMMAND ${CMAKE_COMMAND} -E touch ${depfile}
910
DEPENDS ${DEPENDENCY}
1011
)
11-
target_sources(${TARGET} PRIVATE
12-
${CMAKE_CURRENT_BINARY_DIR}/forced-${sanitized}-dep.swift
13-
)
12+
target_sources(${TARGET} PRIVATE ${depfile})
1413
endforeach()
1514
endfunction()
1615

@@ -218,15 +217,6 @@ function(add_pure_swift_host_library name)
218217
# Depends on all '*.h' files in 'include/module.modulemap'.
219218
force_add_dependencies(${name} importedHeaderDependencies)
220219

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-
230220
# Link against dependencies.
231221
target_link_libraries(${name} PUBLIC
232222
${APSHL_DEPENDENCIES}
@@ -236,15 +226,7 @@ function(add_pure_swift_host_library name)
236226
${APSHL_SWIFT_DEPENDENCIES}
237227
)
238228

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-
245229
if(APSHL_EMIT_MODULE)
246-
# Determine where Swift modules will be built and installed.
247-
248230
set(module_triple "${SWIFT_HOST_MODULE_TRIPLE}")
249231
set(module_dir "${SWIFT_HOST_LIBRARIES_DEST_DIR}")
250232
set(module_base "${module_dir}/${name}.swiftmodule")
@@ -253,14 +235,6 @@ function(add_pure_swift_host_library name)
253235
set(module_private_interface_file "${module_base}/${module_triple}.private.swiftinterface")
254236
set(module_sourceinfo_file "${module_base}/${module_triple}.swiftsourceinfo")
255237

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-
264238
# Create the module directory.
265239
add_custom_command(
266240
TARGET ${name}
@@ -280,12 +254,27 @@ function(add_pure_swift_host_library name)
280254
>)
281255
else()
282256
# 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")
257+
set(module_dir "${CMAKE_CURRENT_BINARY_DIR}/modules")
258+
set(module_file "${module_dir}/${name}.swiftmodule")
287259
endif()
288260

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

cmake/modules/AddSwift.cmake

+2-2
Original file line numberDiff line numberDiff line change
@@ -1016,12 +1016,12 @@ function(add_swift_host_tool executable)
10161016
set_property(
10171017
TARGET ${executable}
10181018
APPEND PROPERTY INSTALL_RPATH
1019-
"@executable_path/../${extra_relative_rpath}lib/swift/host")
1019+
"@executable_path/../${extra_relative_rpath}lib/swift/host/compiler")
10201020
else()
10211021
set_property(
10221022
TARGET ${executable}
10231023
APPEND PROPERTY INSTALL_RPATH
1024-
"$ORIGIN/../${extra_relative_rpath}lib/swift/host")
1024+
"$ORIGIN/../${extra_relative_rpath}lib/swift/host/compiler")
10251025
endif()
10261026
endif()
10271027

cmake/modules/AddSwiftUnittests.cmake

+2-2
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,11 @@ function(add_swift_unittest test_dirname)
133133
if(SWIFT_HOST_VARIANT_SDK IN_LIST SWIFT_DARWIN_PLATFORMS)
134134
set_property(
135135
TARGET ${test_dirname}
136-
APPEND PROPERTY INSTALL_RPATH "@executable_path/${relative_lib_path}/swift/host")
136+
APPEND PROPERTY INSTALL_RPATH "@executable_path/${relative_lib_path}/swift/host/compiler")
137137
elseif(SWIFT_HOST_VARIANT_SDK MATCHES "LINUX|ANDROID|OPENBSD|FREEBSD")
138138
set_property(
139139
TARGET ${test_dirname}
140-
APPEND PROPERTY INSTALL_RPATH "$ORIGIN/${relative_lib_path}/swift/host")
140+
APPEND PROPERTY INSTALL_RPATH "$ORIGIN/${relative_lib_path}/swift/host/compiler")
141141
endif()
142142
endif()
143143
endfunction()

include/swift/AST/ASTBridging.h

-3
Original file line numberDiff line numberDiff line change
@@ -1862,9 +1862,6 @@ void Plugin_setCapability(PluginHandle handle,
18621862
/// Get a capability data set by \c Plugin_setCapability .
18631863
PluginCapabilityPtr _Nullable Plugin_getCapability(PluginHandle handle);
18641864

1865-
/// Get the executable file path of the plugin.
1866-
const char *Plugin_getExecutableFilePath(PluginHandle handle);
1867-
18681865
/// Lock the plugin. Clients should lock it during sending and recving the
18691866
/// response.
18701867
void Plugin_lock(PluginHandle handle);

include/swift/AST/ASTContext.h

-2
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,6 @@ namespace swift {
8484
class DifferentiableAttr;
8585
class ExtensionDecl;
8686
struct ExternalSourceLocs;
87-
class LoadedExecutablePlugin;
88-
class LoadedLibraryPlugin;
8987
class ForeignRepresentationInfo;
9088
class FuncDecl;
9189
class GenericContext;

include/swift/AST/MacroDefinition.h

+23-9
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 status;
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 status, const void *opaqueHandle)
41+
: status(status), 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 (status != Status::Success)
55+
return nullptr;
56+
return opaqueHandle;
57+
}
58+
bool isError() const { return status == Status::Error; }
4659
NullTerminatedStringRef getErrorMessage() const {
60+
assert(isError());
4761
return static_cast<const char *>(opaqueHandle);
4862
}
4963
};

include/swift/AST/PluginLoader.h

+2-3
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)