Skip to content

[6.0][Macros] In-process plugin server #74740

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

Closed
Closed
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
57 changes: 23 additions & 34 deletions cmake/modules/AddPureSwift.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ include(macCatalystUtils)
function(force_add_dependencies TARGET)
foreach(DEPENDENCY ${ARGN})
string(REGEX REPLACE [<>:\"/\\|?*] _ sanitized ${DEPENDENCY})
add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/forced-${sanitized}-dep.swift
COMMAND ${CMAKE_COMMAND} -E touch ${CMAKE_CURRENT_BINARY_DIR}/forced-${sanitized}-dep.swift
set(depfile "${CMAKE_CURRENT_BINARY_DIR}/forced-${sanitized}-dep.swift")
add_custom_command(OUTPUT ${depfile}
COMMAND ${CMAKE_COMMAND} -E touch ${depfile}
DEPENDS ${DEPENDENCY}
)
target_sources(${TARGET} PRIVATE
${CMAKE_CURRENT_BINARY_DIR}/forced-${sanitized}-dep.swift
)
target_sources(${TARGET} PRIVATE ${depfile})
endforeach()
endfunction()

Expand Down Expand Up @@ -198,15 +197,6 @@ function(add_pure_swift_host_library name)
# Depends on all '*.h' files in 'include/module.modulemap'.
force_add_dependencies(${name} importedHeaderDependencies)

# Workaround to touch the library and its objects so that we don't
# continually rebuild (again, see corresponding change in swift-syntax).
add_custom_command(
TARGET ${name}
POST_BUILD
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"
COMMAND_EXPAND_LISTS
COMMENT "Update mtime of library outputs workaround")

# Link against dependencies.
target_link_libraries(${name} PUBLIC
${APSHL_DEPENDENCIES}
Expand All @@ -216,15 +206,7 @@ function(add_pure_swift_host_library name)
${APSHL_SWIFT_DEPENDENCIES}
)

# Make sure we can use the host libraries.
target_include_directories(${name} PUBLIC
"${SWIFT_HOST_LIBRARIES_DEST_DIR}")
target_link_directories(${name} PUBLIC
"${SWIFT_HOST_LIBRARIES_DEST_DIR}")

if(APSHL_EMIT_MODULE)
# Determine where Swift modules will be built and installed.

set(module_triple "${SWIFT_HOST_MODULE_TRIPLE}")
set(module_dir "${SWIFT_HOST_LIBRARIES_DEST_DIR}")
set(module_base "${module_dir}/${name}.swiftmodule")
Expand All @@ -233,14 +215,6 @@ function(add_pure_swift_host_library name)
set(module_private_interface_file "${module_base}/${module_triple}.private.swiftinterface")
set(module_sourceinfo_file "${module_base}/${module_triple}.swiftsourceinfo")

set_target_properties(${name} PROPERTIES
# Set the default module name to the target name.
Swift_MODULE_NAME ${name}
# Install the Swift module into the appropriate location.
Swift_MODULE_DIRECTORY ${module_dir}
# NOTE: workaround for CMake not setting up include flags.
INTERFACE_INCLUDE_DIRECTORIES ${module_dir})

# Create the module directory.
add_custom_command(
TARGET ${name}
Expand All @@ -260,12 +234,27 @@ function(add_pure_swift_host_library name)
>)
else()
# Emit a swiftmodule in the current directory.
set_target_properties(${name} PROPERTIES
Swift_MODULE_NAME ${name}
Swift_MODULE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
set(module_file "${CMAKE_CURRENT_BINARY_DIR}/${name}.swiftmodule")
set(module_dir "${CMAKE_CURRENT_BINARY_DIR}/modules")
set(module_file "${module_dir}/${name}.swiftmodule")
endif()

set_target_properties(${name} PROPERTIES
# Set the default module name to the target name.
Swift_MODULE_NAME ${name}
# Install the Swift module into the appropriate location.
Swift_MODULE_DIRECTORY ${module_dir}
# NOTE: workaround for CMake not setting up include flags.
INTERFACE_INCLUDE_DIRECTORIES ${module_dir})

# Workaround to touch the library and its objects so that we don't
# continually rebuild (again, see corresponding change in swift-syntax).
add_custom_command(
TARGET ${name}
POST_BUILD
COMMAND "${CMAKE_COMMAND}" -E touch_nocreate $<TARGET_FILE:${name}> $<TARGET_OBJECTS:${name}> "${module_file}"
COMMAND_EXPAND_LISTS
COMMENT "Update mtime of library outputs workaround")

# Downstream linking should include the swiftmodule in debug builds to allow lldb to
# work correctly. Only do this on Darwin since neither gold (currently used by default
# on Linux), nor the default Windows linker 'link' support '-add_ast_path'.
Expand Down
4 changes: 2 additions & 2 deletions cmake/modules/AddSwift.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -1008,12 +1008,12 @@ function(add_swift_host_tool executable)
set_property(
TARGET ${executable}
APPEND PROPERTY INSTALL_RPATH
"@executable_path/../${extra_relative_rpath}lib/swift/host")
"@executable_path/../${extra_relative_rpath}lib/swift/host/compiler")
else()
set_property(
TARGET ${executable}
APPEND PROPERTY INSTALL_RPATH
"$ORIGIN/../${extra_relative_rpath}lib/swift/host")
"$ORIGIN/../${extra_relative_rpath}lib/swift/host/compiler")
endif()
endif()

Expand Down
4 changes: 2 additions & 2 deletions cmake/modules/AddSwiftUnittests.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,11 @@ function(add_swift_unittest test_dirname)
if(SWIFT_HOST_VARIANT_SDK IN_LIST SWIFT_DARWIN_PLATFORMS)
set_property(
TARGET ${test_dirname}
APPEND PROPERTY INSTALL_RPATH "@executable_path/${relative_lib_path}/swift/host")
APPEND PROPERTY INSTALL_RPATH "@executable_path/${relative_lib_path}/swift/host/compiler")
elseif(SWIFT_HOST_VARIANT_SDK MATCHES "LINUX|ANDROID|OPENBSD|FREEBSD")
set_property(
TARGET ${test_dirname}
APPEND PROPERTY INSTALL_RPATH "$ORIGIN/${relative_lib_path}/swift/host")
APPEND PROPERTY INSTALL_RPATH "$ORIGIN/${relative_lib_path}/swift/host/compiler")
endif()
endif()
endfunction()
Expand Down
3 changes: 0 additions & 3 deletions include/swift/AST/ASTBridging.h
Original file line number Diff line number Diff line change
Expand Up @@ -1863,9 +1863,6 @@ void Plugin_setCapability(PluginHandle handle,
/// Get a capability data set by \c Plugin_setCapability .
PluginCapabilityPtr _Nullable Plugin_getCapability(PluginHandle handle);

/// Get the executable file path of the plugin.
const char *Plugin_getExecutableFilePath(PluginHandle handle);

/// Lock the plugin. Clients should lock it during sending and recving the
/// response.
void Plugin_lock(PluginHandle handle);
Expand Down
2 changes: 0 additions & 2 deletions include/swift/AST/ASTContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,6 @@ namespace swift {
class DifferentiableAttr;
class ExtensionDecl;
struct ExternalSourceLocs;
class LoadedExecutablePlugin;
class LoadedLibraryPlugin;
class ForeignRepresentationInfo;
class FuncDecl;
class GenericContext;
Expand Down
32 changes: 23 additions & 9 deletions include/swift/AST/MacroDefinition.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,38 @@ namespace swift {
class ASTContext;

/// A reference to an external macro definition that is understood by ASTGen.
struct ExternalMacroDefinition {
enum class PluginKind : int8_t {
InProcess = 0,
Executable = 1,
Error = -1,
class ExternalMacroDefinition {
enum class Status : int8_t {
Success = 0,
Error,
};
PluginKind kind;
Status status;
/// ASTGen's notion of an macro definition, which is opaque to the C++ part
/// of the compiler. If 'kind' is 'PluginKind::Error', this is a C-string to
/// the error message
const void *opaqueHandle = nullptr;
const void *opaqueHandle;

ExternalMacroDefinition(Status status, const void *opaqueHandle)
: status(status), opaqueHandle(opaqueHandle) {}

public:
static ExternalMacroDefinition success(const void *opaqueHandle) {
return ExternalMacroDefinition{Status::Success, opaqueHandle};
}

static ExternalMacroDefinition error(NullTerminatedStringRef message) {
return ExternalMacroDefinition{PluginKind::Error,
return ExternalMacroDefinition{Status::Error,
static_cast<const void *>(message.data())};
}
bool isError() const { return kind == PluginKind::Error; }

const void *get() {
if (status != Status::Success)
return nullptr;
return opaqueHandle;
}
bool isError() const { return status == Status::Error; }
NullTerminatedStringRef getErrorMessage() const {
assert(isError());
return static_cast<const char *>(opaqueHandle);
}
};
Expand Down
5 changes: 2 additions & 3 deletions include/swift/AST/PluginLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,14 @@ class PluginLoader {
/// returns a nullptr.
/// NOTE: This method is idempotent. If the plugin is already loaded, the same
/// instance is simply returned.
llvm::Expected<LoadedLibraryPlugin *> loadLibraryPlugin(llvm::StringRef path);
llvm::Expected<CompilerPlugin *> getInProcessPlugins();

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

/// Add the specified plugin associated with the module name to the dependency
/// tracker if needed.
Expand Down
Loading