From eca06364544033920f0e6b96e06dfb673c94127f Mon Sep 17 00:00:00 2001 From: Geoff Phillips Date: Wed, 27 Sep 2023 18:17:39 +0200 Subject: [PATCH] Allow static libs - Remove SHARED option from add_library to allow static libs, CMake then respects the standard BUILD_SHARED_LIBS option. - Set the default value of BUILD_SHARED_LIBS to ON. - Modify plugin_manager_impl to call the plugin init function for built-in plugins via a hook, whilst still supporting external shared library plugins. - Add plugin init hook function to each built-in plugin. - Add plugin libs to link libs in CMake export config when static libs are enabled. --- CMakeLists.txt | 14 +++-- examples/routingmanagerd/CMakeLists.txt | 3 ++ .../src/configuration_plugin_impl.cpp | 11 ++++ .../src/e2e/profile/e2e_provider_impl.cpp | 10 ++++ .../plugin/include/plugin_manager_impl.hpp | 10 ++++ .../plugin/src/plugin_manager_impl.cpp | 51 ++++++++++++++++--- .../service_discovery/src/runtime_impl.cpp | 11 ++++ vsomeip3Config.cmake.in | 7 ++- vsomeipConfig.cmake.in | 7 ++- 9 files changed, 111 insertions(+), 13 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 7eabb3a81..eea948f26 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -60,6 +60,10 @@ if(NOT CMAKE_BUILD_TYPE) # Set the possible values of build type for cmake-gui set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release" "MinSizeRel" "RelWithDebInfo") endif() +option(BUILD_SHARED_LIBS "Set to OFF to build static libraries" ON) +if(NOT BUILD_SHARED_LIBS) + add_definitions(-DVSOMEIP_STATIC_PLUGINS) +endif() set(CMAKE_CXX_STANDARD 17) @@ -287,7 +291,7 @@ file(GLOB ${VSOMEIP_NAME}-cfg_SRC ) list(SORT ${VSOMEIP_NAME}-cfg_SRC) if (VSOMEIP_ENABLE_MULTIPLE_ROUTING_MANAGERS EQUAL 0) - add_library(${VSOMEIP_NAME}-cfg SHARED ${${VSOMEIP_NAME}-cfg_SRC}) + add_library(${VSOMEIP_NAME}-cfg ${${VSOMEIP_NAME}-cfg_SRC}) set_target_properties (${VSOMEIP_NAME}-cfg PROPERTIES VERSION ${VSOMEIP_VERSION} SOVERSION ${VSOMEIP_MAJOR_VERSION}) target_compile_features(${VSOMEIP_NAME}-cfg PRIVATE cxx_std_17) if (MSVC) @@ -322,7 +326,7 @@ endif() list(SORT ${VSOMEIP_NAME}_SRC) -add_library(${VSOMEIP_NAME} SHARED ${${VSOMEIP_NAME}_SRC}) +add_library(${VSOMEIP_NAME} ${${VSOMEIP_NAME}_SRC}) set_target_properties (${VSOMEIP_NAME} PROPERTIES VERSION ${VSOMEIP_VERSION} SOVERSION ${VSOMEIP_MAJOR_VERSION}) target_compile_features(${VSOMEIP_NAME} PRIVATE cxx_std_17) if (MSVC) @@ -354,7 +358,7 @@ file(GLOB ${VSOMEIP_NAME}-sd_SRC ) list(SORT ${VSOMEIP_NAME}-sd_SRC) -add_library(${VSOMEIP_NAME}-sd SHARED ${${VSOMEIP_NAME}-sd_SRC}) +add_library(${VSOMEIP_NAME}-sd ${${VSOMEIP_NAME}-sd_SRC}) target_compile_features(${VSOMEIP_NAME}-sd PRIVATE cxx_std_17) set_target_properties (${VSOMEIP_NAME}-sd PROPERTIES VERSION ${VSOMEIP_VERSION} SOVERSION ${VSOMEIP_MAJOR_VERSION}) if (MSVC) @@ -372,7 +376,7 @@ file(GLOB_RECURSE ${VSOMEIP_NAME}-e2e_SRC ) list(SORT ${VSOMEIP_NAME}-e2e_SRC) -add_library(${VSOMEIP_NAME}-e2e SHARED ${${VSOMEIP_NAME}-e2e_SRC}) +add_library(${VSOMEIP_NAME}-e2e ${${VSOMEIP_NAME}-e2e_SRC}) target_compile_features(${VSOMEIP_NAME}-e2e PRIVATE cxx_std_17) set_target_properties (${VSOMEIP_NAME}-e2e PROPERTIES VERSION ${VSOMEIP_VERSION} SOVERSION ${VSOMEIP_MAJOR_VERSION}) if (MSVC) @@ -404,7 +408,7 @@ file(GLOB_RECURSE ${VSOMEIP_COMPAT_NAME}_SRC ) list(SORT ${VSOMEIP_COMPAT_NAME}_SRC) -add_library(${VSOMEIP_COMPAT_NAME} SHARED ${${VSOMEIP_COMPAT_NAME}_SRC}) +add_library(${VSOMEIP_COMPAT_NAME} ${${VSOMEIP_COMPAT_NAME}_SRC}) target_compile_features(${VSOMEIP_COMPAT_NAME} PRIVATE cxx_std_17) set_target_properties (${VSOMEIP_COMPAT_NAME} PROPERTIES VERSION ${VSOMEIP_COMPAT_VERSION} SOVERSION ${VSOMEIP_COMPAT_MAJOR_VERSION}) if (MSVC) diff --git a/examples/routingmanagerd/CMakeLists.txt b/examples/routingmanagerd/CMakeLists.txt index e8a2397fc..3800cc6b8 100644 --- a/examples/routingmanagerd/CMakeLists.txt +++ b/examples/routingmanagerd/CMakeLists.txt @@ -9,6 +9,9 @@ target_link_libraries(routingmanagerd ${VSOMEIP_NAME} ${Boost_LIBRARIES} ${DL_LI if(${CMAKE_SYSTEM_NAME} MATCHES "QNX") target_link_libraries(routingmanagerd socket) endif() +if(NOT BUILD_SHARED_LIBS) + target_link_libraries(routingmanagerd ${VSOMEIP_NAME}-cfg ${VSOMEIP_NAME}-e2e ${VSOMEIP_NAME}-sd) +endif() add_dependencies(routingmanagerd ${VSOMEIP_NAME}) option(VSOMEIP_INSTALL_ROUTINGMANAGERD "Whether or not to install the routing manager daemon.") diff --git a/implementation/configuration/src/configuration_plugin_impl.cpp b/implementation/configuration/src/configuration_plugin_impl.cpp index cf84520b7..09230e1cf 100644 --- a/implementation/configuration/src/configuration_plugin_impl.cpp +++ b/implementation/configuration/src/configuration_plugin_impl.cpp @@ -8,7 +8,18 @@ #include "../include/configuration_plugin_impl.hpp" #include "../include/configuration_impl.hpp" +#ifdef VSOMEIP_STATIC_PLUGINS +namespace vsomeip_v3 { + +create_plugin_func plugin_manager_impl_init_hook_cfg() +{ + return configuration_plugin_impl::get_plugin; +} + +} // namespace vsomeip_v3 +#else VSOMEIP_PLUGIN(vsomeip_v3::configuration_plugin_impl) +#endif namespace vsomeip_v3 { diff --git a/implementation/e2e_protection/src/e2e/profile/e2e_provider_impl.cpp b/implementation/e2e_protection/src/e2e/profile/e2e_provider_impl.cpp index 3a9643ee9..b8c36a62b 100644 --- a/implementation/e2e_protection/src/e2e/profile/e2e_provider_impl.cpp +++ b/implementation/e2e_protection/src/e2e/profile/e2e_provider_impl.cpp @@ -58,8 +58,18 @@ value_t read_value_from_config(const std::shared_ptr &_con } // namespace +#ifdef VSOMEIP_STATIC_PLUGINS +namespace vsomeip_v3 { +create_plugin_func plugin_manager_impl_init_hook_e2e() +{ + return e2e::e2e_provider_impl::get_plugin; +} + +} // namespace vsomeip_v3 +#else VSOMEIP_PLUGIN(vsomeip_v3::e2e::e2e_provider_impl) +#endif namespace vsomeip_v3 { namespace e2e { diff --git a/implementation/plugin/include/plugin_manager_impl.hpp b/implementation/plugin/include/plugin_manager_impl.hpp index 9f755d60c..bfde9d7f4 100644 --- a/implementation/plugin/include/plugin_manager_impl.hpp +++ b/implementation/plugin/include/plugin_manager_impl.hpp @@ -53,8 +53,18 @@ class plugin_manager_impl : public plugin_manager { std::recursive_mutex plugins_mutex_; static std::shared_ptr the_plugin_manager__; + +#ifdef VSOMEIP_STATIC_PLUGINS + plugin_init_func get_static_init_func(const std::string &library_); +#endif }; +#ifdef VSOMEIP_STATIC_PLUGINS +vsomeip_v3::create_plugin_func plugin_manager_impl_init_hook_cfg(); +vsomeip_v3::create_plugin_func plugin_manager_impl_init_hook_sd(); +vsomeip_v3::create_plugin_func plugin_manager_impl_init_hook_e2e(); +#endif + } // namespace vsomeip_v3 #endif // VSOMEIP_V3_PLUGIN_MANAGER_IMPL_HPP diff --git a/implementation/plugin/src/plugin_manager_impl.cpp b/implementation/plugin/src/plugin_manager_impl.cpp index 23b7b892a..81b0b122f 100644 --- a/implementation/plugin/src/plugin_manager_impl.cpp +++ b/implementation/plugin/src/plugin_manager_impl.cpp @@ -49,6 +49,24 @@ plugin_manager_impl::~plugin_manager_impl() { plugins_.clear(); } +#ifdef VSOMEIP_STATIC_PLUGINS +plugin_init_func plugin_manager_impl::get_static_init_func(const std::string &library_) +{ + if (library_ == VSOMEIP_CFG_LIBRARY) { + return plugin_manager_impl_init_hook_cfg; + } + else if (library_ == VSOMEIP_SD_LIBRARY) { + return plugin_manager_impl_init_hook_sd; + } + else if (library_ == VSOMEIP_E2E_LIBRARY) { + return plugin_manager_impl_init_hook_e2e; + } + else { + return nullptr; + } +} +#endif + void plugin_manager_impl::load_plugins() { { std::lock_guard its_lock_start_stop(loader_mutex_); @@ -72,9 +90,18 @@ void plugin_manager_impl::load_plugins() { std::lock_guard its_lock_start_stop(plugins_mutex_); // Load plug-in info from libraries parsed before for (const auto& plugin_name : plugins) { - void* handle = load_library(plugin_name); - plugin_init_func its_init_func = reinterpret_cast( - load_symbol(handle, VSOMEIP_PLUGIN_INIT_SYMBOL)); + void* handle; + plugin_init_func its_init_func; +#ifdef VSOMEIP_STATIC_PLUGINS + handle = nullptr; + its_init_func = get_static_init_func(plugin_name); + if (!its_init_func) +#endif + { + handle = load_library(plugin_name); + its_init_func = reinterpret_cast( + load_symbol(handle, VSOMEIP_PLUGIN_INIT_SYMBOL)); + } if (its_init_func) { create_plugin_func its_create_func = (*its_init_func)(); if (its_create_func) { @@ -126,9 +153,18 @@ std::shared_ptr plugin_manager_impl::get_plugin(plugin_type_e _type, std::shared_ptr plugin_manager_impl::load_plugin(const std::string& _library, plugin_type_e _type, uint32_t _version) { - void* handle = load_library(_library); - plugin_init_func its_init_func = reinterpret_cast( - load_symbol(handle, VSOMEIP_PLUGIN_INIT_SYMBOL)); + void* handle; + plugin_init_func its_init_func; +#ifdef VSOMEIP_STATIC_PLUGINS + handle = nullptr; + its_init_func = get_static_init_func(_library); + if (!its_init_func) +#endif + { + handle = load_library(_library); + its_init_func = reinterpret_cast( + load_symbol(handle, VSOMEIP_PLUGIN_INIT_SYMBOL)); + } if (its_init_func) { create_plugin_func its_create_func = (*its_init_func)(); if (its_create_func) { @@ -154,6 +190,9 @@ bool plugin_manager_impl::unload_plugin(plugin_type_e _type) { const auto found_handle = handles_.find(_type); if (found_handle != handles_.end()) { for (const auto& its_name : found_handle->second) { + if (!its_name.second) { // Skip statically linked plugins + continue; + } #ifdef _WIN32 FreeLibrary((HMODULE)its_name.second); #else diff --git a/implementation/service_discovery/src/runtime_impl.cpp b/implementation/service_discovery/src/runtime_impl.cpp index f0fa456e0..1cd04a07c 100644 --- a/implementation/service_discovery/src/runtime_impl.cpp +++ b/implementation/service_discovery/src/runtime_impl.cpp @@ -12,7 +12,18 @@ #include "../include/runtime_impl.hpp" #include "../include/service_discovery_impl.hpp" +#ifdef VSOMEIP_STATIC_PLUGINS +namespace vsomeip_v3 { + +create_plugin_func plugin_manager_impl_init_hook_sd() +{ + return sd::runtime_impl::get_plugin; +} + +} // namespace vsomeip_v3 +#else VSOMEIP_PLUGIN(vsomeip_v3::sd::runtime_impl) +#endif namespace vsomeip_v3 { namespace sd { diff --git a/vsomeip3Config.cmake.in b/vsomeip3Config.cmake.in index d689970f4..d06cae56e 100644 --- a/vsomeip3Config.cmake.in +++ b/vsomeip3Config.cmake.in @@ -16,4 +16,9 @@ if (NOT TARGET vsomeip AND NOT vsomeip_BINARY_DIR) endif () # These are IMPORTED targets created by vsomeipTargets.cmake -set (VSOMEIP_LIBRARIES vsomeip3) +if (NOT @BUILD_SHARED_LIBS@) + link_directories(@CMAKE_INSTALL_PREFIX@/@INSTALL_LIB_DIR@) + set (VSOMEIP_LIBRARIES vsomeip3 vsomeip3-cfg vsomeip3-e2e vsomeip3-sd) +else () + set (VSOMEIP_LIBRARIES vsomeip3) +endif () diff --git a/vsomeipConfig.cmake.in b/vsomeipConfig.cmake.in index 38ea7ae24..6352f761c 100644 --- a/vsomeipConfig.cmake.in +++ b/vsomeipConfig.cmake.in @@ -16,4 +16,9 @@ if (NOT TARGET vsomeip AND NOT vsomeip_BINARY_DIR) endif () # These are IMPORTED targets created by vsomeipTargets.cmake -set (VSOMEIP_LIBRARIES vsomeip) +if (NOT @BUILD_SHARED_LIBS@) + link_directories(@CMAKE_INSTALL_PREFIX@/@INSTALL_LIB_DIR@) + set (VSOMEIP_LIBRARIES vsomeip vsomeip3-cfg vsomeip3-e2e vsomeip3-sd) +else () + set (VSOMEIP_LIBRARIES vsomeip) +endif ()