From 9123da9388d32f16bc780ae2ea92883ecfcc533b Mon Sep 17 00:00:00 2001 From: Arfrever Frehtes Taifersar Arahesis Date: Sat, 12 Dec 2020 00:00:00 +0000 Subject: [PATCH 1/5] Support installation of private headers (#431). Add INSTALL_PRIVATE_HEADERS option to enable installation of private headers, which remains disabled by default. Signed-off-by: Arfrever Frehtes Taifersar Arahesis --- CMakeLists.txt | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index b1766c5..79f0177 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -22,6 +22,7 @@ option(ENABLE_LOGGING "Enable logging with google-glog library" ON) option(BOOST_USE_CXX11 "Boost has been built with C++11 support" OFF) option(BOOST_USE_SIGNALS2 "Boost use signals2 instead of signals" ON) option(ENABLE_ASAN "Enable Address Sanitizer (Unix Only)" OFF) +option(INSTALL_PRIVATE_HEADERS "Install private headers (usually needed for externally built Rime plugins)" OFF) set(rime_data_dir "/share/rime-data" CACHE STRING "Target directory for Rime data") @@ -189,6 +190,14 @@ install(FILES cmake/RimeConfig.cmake DESTINATION share/cmake/rime) file(GLOB rime_public_header_files ${PROJECT_SOURCE_DIR}/src/*.h) install(FILES ${rime_public_header_files} DESTINATION include) +if(INSTALL_PRIVATE_HEADERS) + file(GLOB rime_private_header_files ${PROJECT_SOURCE_DIR}/src/rime/*.h) + install(FILES ${rime_private_header_files} DESTINATION include/rime) + foreach(rime_private_header_files_dir algo config dict gear lever) + file(GLOB rime_private_header_files ${PROJECT_SOURCE_DIR}/src/rime/${rime_private_header_files_dir}/*.h) + install(FILES ${rime_private_header_files} DESTINATION include/rime/${rime_private_header_files_dir}) + endforeach() +endif() if(BUILD_DATA) file(GLOB rime_preset_data_files ${PROJECT_SOURCE_DIR}/data/preset/*.yaml) -- 2.29.2 From f1f93a7c2215c318c9c5e8ee084f483166aad7ec Mon Sep 17 00:00:00 2001 From: Arfrever Frehtes Taifersar Arahesis Date: Sat, 12 Dec 2020 00:00:00 +0000 Subject: [PATCH 2/5] Set BIN_INSTALL_DIR and LIB_INSTALL_DIR earlier in CMakeLists.txt (#431). LIB_INSTALL_DIR will be used for RIME_PLUGINS_DIR. Signed-off-by: Arfrever Frehtes Taifersar Arahesis --- CMakeLists.txt | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 79f0177..737b636 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,6 +11,14 @@ add_definitions(-DRIME_VERSION="${rime_version}") include(GNUInstallDirs) +if(NOT DEFINED BIN_INSTALL_DIR) + set(BIN_INSTALL_DIR ${CMAKE_INSTALL_BINDIR}) +endif() + +if(NOT DEFINED LIB_INSTALL_DIR) + set(LIB_INSTALL_DIR ${CMAKE_INSTALL_LIBDIR}) +endif() + option(BUILD_SHARED_LIBS "Build Rime as shared library" ON) option(BUILD_MERGED_PLUGINS "Merge plugins into one Rime library" ON) option(BUILD_STATIC "Build with dependencies as static libraries" OFF) @@ -155,14 +163,6 @@ if(UNIX) set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14") endif() -if(NOT DEFINED LIB_INSTALL_DIR) - set(LIB_INSTALL_DIR ${CMAKE_INSTALL_LIBDIR}) -endif() - -if(NOT DEFINED BIN_INSTALL_DIR) - set(BIN_INSTALL_DIR ${CMAKE_INSTALL_BINDIR}) -endif() - # uninstall target configure_file( "${CMAKE_CURRENT_SOURCE_DIR}/cmake/cmake_uninstall.cmake.in" -- 2.29.2 From dd1dd32f784ca4e401b9d0935f0c608624397a01 Mon Sep 17 00:00:00 2001 From: Arfrever Frehtes Taifersar Arahesis Date: Sat, 12 Dec 2020 00:00:00 +0000 Subject: [PATCH 3/5] Rename rime_data_dir variable to RIME_DATA_DIR (#431). This variable can be overriden during invocation of cmake. For consistency with other cmake options, it should be uppercase. Drop leading "/" from value of this variable and add it in lines using this variable. Signed-off-by: Arfrever Frehtes Taifersar Arahesis --- CMakeLists.txt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 737b636..c37735c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -32,7 +32,7 @@ option(BOOST_USE_SIGNALS2 "Boost use signals2 instead of signals" ON) option(ENABLE_ASAN "Enable Address Sanitizer (Unix Only)" OFF) option(INSTALL_PRIVATE_HEADERS "Install private headers (usually needed for externally built Rime plugins)" OFF) -set(rime_data_dir "/share/rime-data" CACHE STRING "Target directory for Rime data") +set(RIME_DATA_DIR "share/rime-data" CACHE STRING "Target directory for Rime data") if(WIN32) set(ext ".exe") @@ -176,7 +176,7 @@ if(${CMAKE_SYSTEM_NAME} MATCHES "Linux|FreeBSD|DragonFly|GNU") set(exec_prefix "${CMAKE_INSTALL_PREFIX}") set(bindir "${exec_prefix}/${BIN_INSTALL_DIR}") set(libdir "${exec_prefix}/${LIB_INSTALL_DIR}") - set(pkgdatadir "${prefix}${rime_data_dir}") + set(pkgdatadir "${prefix}/${RIME_DATA_DIR}") set(includedir "${prefix}/${CMAKE_INSTALL_INCLUDEDIR}") configure_file( ${PROJECT_SOURCE_DIR}/rime.pc.in @@ -202,7 +202,7 @@ endif() if(BUILD_DATA) file(GLOB rime_preset_data_files ${PROJECT_SOURCE_DIR}/data/preset/*.yaml) install(FILES ${rime_preset_data_files} - DESTINATION ${CMAKE_INSTALL_PREFIX}${rime_data_dir}) + DESTINATION ${CMAKE_INSTALL_PREFIX}/${RIME_DATA_DIR}) endif() if(BUILD_SHARED_LIBS) -- 2.29.2 From 7e762406a0ef65f0428966d27d903dd348c634ba Mon Sep 17 00:00:00 2001 From: Arfrever Frehtes Taifersar Arahesis Date: Sat, 12 Dec 2020 00:00:00 +0000 Subject: [PATCH 4/5] Support loading of externally built plugins (#431). ENABLE_EXTERNAL_PLUGINS enables loading of externally built plugins. RIME_PLUGINS_DIR specifies directory for externally built plugins and defaults to "${LIB_INSTALL_DIR}/rime-plugins". Externally built plugin should be .so file in that directory, with filename equal to internally used name of RIME plugin + ".so" suffix. Signed-off-by: Arfrever Frehtes Taifersar Arahesis --- CMakeLists.txt | 7 +++++++ src/CMakeLists.txt | 3 +++ src/rime/setup.cc | 31 +++++++++++++++++++++++++++++++ 3 files changed, 41 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index c37735c..3197ff5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -31,8 +31,10 @@ option(BOOST_USE_CXX11 "Boost has been built with C++11 support" OFF) option(BOOST_USE_SIGNALS2 "Boost use signals2 instead of signals" ON) option(ENABLE_ASAN "Enable Address Sanitizer (Unix Only)" OFF) option(INSTALL_PRIVATE_HEADERS "Install private headers (usually needed for externally built Rime plugins)" OFF) +option(ENABLE_EXTERNAL_PLUGINS "Enable loading of externally built Rime plugins (from directory set by RIME_PLUGINS_DIR variable)" OFF) set(RIME_DATA_DIR "share/rime-data" CACHE STRING "Target directory for Rime data") +set(RIME_PLUGINS_DIR "${LIB_INSTALL_DIR}/rime-plugins" CACHE STRING "Target directory for externally built Rime plugins") if(WIN32) set(ext ".exe") @@ -232,6 +234,11 @@ if(BUILD_SHARED_LIBS AND BUILD_SEPARATE_LIBS AND rime_plugins_objs) set(rime_plugins_library rime-plugins) endif() +add_definitions("-DRIME_PLUGINS_DIR=\"${CMAKE_INSTALL_PREFIX}/${RIME_PLUGINS_DIR}\"") +if(ENABLE_EXTERNAL_PLUGINS) + add_definitions(-DRIME_ENABLE_EXTERNAL_PLUGINS) +endif() + add_subdirectory(src) if(BUILD_SHARED_LIBS) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index cb52166..e1f2779 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -36,6 +36,9 @@ set(rime_optional_deps "") if(Gflags_FOUND) set(rime_optional_deps ${rime_optional_deps} ${Gflags_LIBRARY}) endif() +if(ENABLE_EXTERNAL_PLUGINS) + set(rime_optional_deps ${rime_optional_deps} dl) +endif() set(rime_core_deps ${Boost_LIBRARIES} diff --git a/src/rime/setup.cc b/src/rime/setup.cc index bb65a9f..ad5cbbb 100644 --- a/src/rime/setup.cc +++ b/src/rime/setup.cc @@ -5,6 +5,11 @@ // 2011-10-02 GONG Chen // +#ifdef RIME_ENABLE_EXTERNAL_PLUGINS +#include +#include +#endif // RIME_ENABLE_EXTERNAL_PLUGINS + #ifdef RIME_ENABLE_LOGGING #include #endif // RIME_ENABLE_LOGGING @@ -36,6 +41,32 @@ RIME_API void LoadModules(const char* module_names[]) { mm.LoadModule(module); } } + +#ifdef RIME_ENABLE_EXTERNAL_PLUGINS + fs::path plugins_dir = fs::path(RIME_PLUGINS_DIR); + fs::path plugins_files = plugins_dir / "*.so"; + glob_t glob_buffer; + if (glob(plugins_files.string().c_str(), 0, NULL, &glob_buffer) == 0) { + for (size_t i = 0; i < glob_buffer.gl_pathc; i++) { + fs::path plugin_file(glob_buffer.gl_pathv[i]); + fs::path plugin_name = plugin_file.stem(); + fs::file_status plugin_file_status = fs::status(plugin_file); + if (fs::is_regular_file(plugin_file) && + plugin_file_status.permissions() & (fs::owner_exe | fs::group_exe | fs::others_exe)) { + void* handle = dlopen(plugin_file.string().c_str(), RTLD_LAZY); + if (handle) { + if (RimeModule* module = mm.Find(plugin_name.string())) { + mm.LoadModule(module); + } + } + else { + LOG(ERROR) << "dlopen error: " << dlerror(); + } + } + } + globfree(&glob_buffer); + } +#endif } // assume member is a non-null pointer in struct *p. -- 2.29.2 From fae2e5f934bfd1b2aaac4408ab27aec2c0dc9cfb Mon Sep 17 00:00:00 2001 From: Arfrever Frehtes Taifersar Arahesis Date: Sat, 12 Dec 2020 00:00:00 +0000 Subject: [PATCH 5/5] Make private headers usable outside of librime source tree (#431). Previously rime/common.h would cause compilation failure due to relying on BOOST_SIGNALS2 preprocessor definition, and including non-existent boost/signals.hpp header when new Boost is used. BOOST_SIGNALS2 and RIME_ENABLE_LOGGING were the only preprocessor definitions used in librime private headers. BOOST_SIGNALS2 is now renamed to RIME_BOOST_SIGNALS2. RIME_BOOST_SIGNALS2 and RIME_ENABLE_LOGGING are now set in generated rime/build_config.h header, which is also installed. Signed-off-by: Arfrever Frehtes Taifersar Arahesis --- CMakeLists.txt | 11 ++++++++--- src/rime/build_config.h.in | 11 +++++++++++ src/rime/common.h | 6 ++++-- src/rime/lever/deployment_tasks.cc | 3 +++ src/rime/setup.cc | 2 ++ 5 files changed, 28 insertions(+), 5 deletions(-) create mode 100644 src/rime/build_config.h.in diff --git a/CMakeLists.txt b/CMakeLists.txt index 3197ff5..91a55b1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -72,7 +72,7 @@ endif() set(BOOST_COMPONENTS filesystem regex system) if(BOOST_USE_SIGNALS2) - add_definitions("-DBOOST_SIGNALS2") + set(RIME_BOOST_SIGNALS2 1) else() set(BOOST_COMPONENTS ${BOOST_COMPONENTS} signals) endif() @@ -100,7 +100,7 @@ if(ENABLE_LOGGING) add_definitions(-DGOOGLE_GLOG_DLL_DECL=) endif() - add_definitions(-DRIME_ENABLE_LOGGING) + set(RIME_ENABLE_LOGGING 1) endif() @@ -145,6 +145,11 @@ else() message(WARNING "X11/keysym.h not found.") endif() +configure_file( + "${PROJECT_SOURCE_DIR}/src/rime/build_config.h.in" + "${PROJECT_BINARY_DIR}/src/rime/build_config.h") + +include_directories(${PROJECT_BINARY_DIR}/src) include_directories(${PROJECT_SOURCE_DIR}/src) include_directories(${PROJECT_SOURCE_DIR}/thirdparty/include) link_directories(${PROJECT_SOURCE_DIR}/thirdparty/lib) @@ -193,7 +198,7 @@ install(FILES cmake/RimeConfig.cmake DESTINATION share/cmake/rime) file(GLOB rime_public_header_files ${PROJECT_SOURCE_DIR}/src/*.h) install(FILES ${rime_public_header_files} DESTINATION include) if(INSTALL_PRIVATE_HEADERS) - file(GLOB rime_private_header_files ${PROJECT_SOURCE_DIR}/src/rime/*.h) + file(GLOB rime_private_header_files ${PROJECT_SOURCE_DIR}/src/rime/*.h ${PROJECT_BINARY_DIR}/src/rime/*.h) install(FILES ${rime_private_header_files} DESTINATION include/rime) foreach(rime_private_header_files_dir algo config dict gear lever) file(GLOB rime_private_header_files ${PROJECT_SOURCE_DIR}/src/rime/${rime_private_header_files_dir}/*.h) diff --git a/src/rime/build_config.h.in b/src/rime/build_config.h.in new file mode 100644 index 0000000..ebd1652 --- /dev/null +++ b/src/rime/build_config.h.in @@ -0,0 +1,11 @@ +// +// Copyright RIME Developers +// Distributed under the BSD License +// +#ifndef RIME_BUILD_CONFIG_H_ +#define RIME_BUILD_CONFIG_H_ + +#cmakedefine RIME_BOOST_SIGNALS2 +#cmakedefine RIME_ENABLE_LOGGING + +#endif // RIME_BUILD_CONFIG_H_ diff --git a/src/rime/common.h b/src/rime/common.h index e2b91c1..cf6c3e9 100644 --- a/src/rime/common.h +++ b/src/rime/common.h @@ -7,6 +7,8 @@ #ifndef RIME_COMMON_H_ #define RIME_COMMON_H_ +#include + #include #include #include @@ -20,7 +22,7 @@ #include #include #define BOOST_BIND_NO_PLACEHOLDERS -#ifdef BOOST_SIGNALS2 +#ifdef RIME_BOOST_SIGNALS2 #include #include #else @@ -79,7 +81,7 @@ inline an New(Args&&... args) { return std::make_shared(std::forward(args)...); } -#ifdef BOOST_SIGNALS2 +#ifdef RIME_BOOST_SIGNALS2 using boost::signals2::connection; using boost::signals2::signal; #else diff --git a/src/rime/lever/deployment_tasks.cc b/src/rime/lever/deployment_tasks.cc index eea663e..e40bc6d 100644 --- a/src/rime/lever/deployment_tasks.cc +++ b/src/rime/lever/deployment_tasks.cc @@ -4,6 +4,9 @@ // // 2011-12-10 GONG Chen // + +#include + #include #include #include diff --git a/src/rime/setup.cc b/src/rime/setup.cc index ad5cbbb..008974f 100644 --- a/src/rime/setup.cc +++ b/src/rime/setup.cc @@ -5,6 +5,8 @@ // 2011-10-02 GONG Chen // +#include + #ifdef RIME_ENABLE_EXTERNAL_PLUGINS #include #include -- 2.29.2