Skip to content

Commit

Permalink
fix(plugins): load plugins in relative path
Browse files Browse the repository at this point in the history
in cmake config set RIME_PLUGINS_DIR as relative to libdir
  • Loading branch information
lotem committed Mar 6, 2024
1 parent d345b41 commit a791879
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 8 deletions.
10 changes: 5 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ option(ENABLE_EXTERNAL_PLUGINS "Enable loading of externally built Rime plugins
option(ENABLE_THREADING "Enable threading for deployer" ON)
option(ENABLE_TIMESTAMP "Embed timestamp to schema artifacts" ON)

set(RIME_DATA_DIR "${CMAKE_INSTALL_FULL_DATADIR}/rime-data" CACHE STRING "Target directory for Rime data")
set(RIME_PLUGINS_DIR "${CMAKE_INSTALL_FULL_LIBDIR}/rime-plugins" CACHE STRING "Target directory for externally built Rime plugins")
set(RIME_DATA_DIR "rime-data" CACHE STRING "Target directory for Rime data")
set(RIME_PLUGINS_DIR "rime-plugins" CACHE STRING "Target directory for externally built Rime plugins")

if(WIN32)
set(ext ".exe")
Expand Down Expand Up @@ -205,8 +205,8 @@ if(${CMAKE_SYSTEM_NAME} MATCHES "Linux|FreeBSD|DragonFly|GNU|Darwin" OR MINGW)
set(exec_prefix "${CMAKE_INSTALL_PREFIX}")
set(bindir "${CMAKE_INSTALL_FULL_BINDIR}")
set(libdir "${CMAKE_INSTALL_FULL_LIBDIR}")
set(pkgdatadir "${RIME_DATA_DIR}")
set(pluginsdir "${RIME_PLUGINS_DIR}")
set(pkgdatadir "${CMAKE_INSTALL_FULL_DATADIR}/${RIME_DATA_DIR}")
set(pluginsdir "${CMAKE_INSTALL_FULL_LIBDIR}/${RIME_PLUGINS_DIR}")
set(includedir "${CMAKE_INSTALL_FULL_INCLUDEDIR}")
configure_file(
${PROJECT_SOURCE_DIR}/rime.pc.in
Expand Down Expand Up @@ -238,7 +238,7 @@ endif()

if(BUILD_DATA)
file(GLOB rime_preset_data_files ${PROJECT_SOURCE_DIR}/data/preset/*.yaml)
install(FILES ${rime_preset_data_files} DESTINATION ${RIME_DATA_DIR})
install(FILES ${rime_preset_data_files} DESTINATION ${CMAKE_INSTALL_FULL_DATADIR}/${RIME_DATA_DIR})
endif()

if(BUILD_SHARED_LIBS)
Expand Down
7 changes: 5 additions & 2 deletions plugins/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
set(RIME_SOURCE_DIR ${PROJECT_SOURCE_DIR})
set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib)

# work around CMake build issues on macOS
set(rime_plugin_boilerplate_src "plugin.cc")
Expand Down Expand Up @@ -55,10 +54,14 @@ foreach(plugin ${plugins})
message(STATUS "Plugin ${plugin_name} provides modules: ${plugin_modules}")
add_library(${plugin_name} ${rime_plugin_boilerplate_src} ${plugin_objs})
target_link_libraries(${plugin_name} ${plugin_deps})
set_target_properties(${plugin_name}
PROPERTIES
LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib/${RIME_PLUGINS_DIR}
RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/bin/${RIME_PLUGINS_DIR})
if(XCODE_VERSION)
set_target_properties(${plugin_name} PROPERTIES INSTALL_NAME_DIR "@rpath")
endif(XCODE_VERSION)
install(TARGETS ${plugin_name} DESTINATION ${RIME_PLUGINS_DIR})
install(TARGETS ${plugin_name} DESTINATION ${CMAKE_INSTALL_FULL_LIBDIR}/${RIME_PLUGINS_DIR})
endif()
endforeach(plugin)

Expand Down
29 changes: 28 additions & 1 deletion plugins/plugins_module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,35 @@ PluginManager& PluginManager::instance() {

} // namespace rime

#ifdef _WIN32
// TODO: implement this when ready to support DLL plugins on Windows.
inline static rime::path current_module_path() {
return rime::path{};
}
#else
#include <dlfcn.h>

inline static rime::path symbol_location(const void* symbol) {
Dl_info info;
// Some of the libc headers miss `const` in `dladdr(const void*, Dl_info*)`
const int res = dladdr(const_cast<void*>(symbol), &info);
if (res) {
return rime::path{info.dli_fname};
} else {
return rime::path{};
}
}

inline static rime::path current_module_path() {
void rime_require_module_plugins();
return symbol_location(
reinterpret_cast<const void*>(&rime_require_module_plugins));
}
#endif

static void rime_plugins_initialize() {
rime::PluginManager::instance().LoadPlugins(rime::path(RIME_PLUGINS_DIR));
rime::PluginManager::instance().LoadPlugins(
current_module_path().remove_filename() / RIME_PLUGINS_DIR);
}

static void rime_plugins_finalize() {}
Expand Down

0 comments on commit a791879

Please sign in to comment.