Skip to content

Commit

Permalink
Allow static libs
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
hefroy committed Sep 28, 2023
1 parent 655bb34 commit 2ba9e24
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 13 deletions.
14 changes: 9 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()

# OS
if (${CMAKE_SYSTEM_NAME} MATCHES "Linux")
Expand Down Expand Up @@ -258,7 +262,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})
if (MSVC)
set_target_properties(${VSOMEIP_NAME}-cfg PROPERTIES COMPILE_DEFINITIONS "VSOMEIP_DLL_COMPILATION_PLUGIN")
Expand Down Expand Up @@ -292,7 +296,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})
if (MSVC)
set_target_properties(${VSOMEIP_NAME} PROPERTIES COMPILE_DEFINITIONS "VSOMEIP_DLL_COMPILATION")
Expand Down Expand Up @@ -322,7 +326,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})
set_target_properties (${VSOMEIP_NAME}-sd PROPERTIES VERSION ${VSOMEIP_VERSION} SOVERSION ${VSOMEIP_MAJOR_VERSION})
if (MSVC)
set_target_properties(${VSOMEIP_NAME}-sd PROPERTIES COMPILE_DEFINITIONS "VSOMEIP_DLL_COMPILATION_PLUGIN")
Expand All @@ -339,7 +343,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})
set_target_properties (${VSOMEIP_NAME}-e2e PROPERTIES VERSION ${VSOMEIP_VERSION} SOVERSION ${VSOMEIP_MAJOR_VERSION})
if (MSVC)
set_target_properties(${VSOMEIP_NAME}-e2e PROPERTIES COMPILE_DEFINITIONS "VSOMEIP_DLL_COMPILATION_PLUGIN")
Expand All @@ -363,7 +367,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})
set_target_properties (${VSOMEIP_COMPAT_NAME} PROPERTIES VERSION ${VSOMEIP_COMPAT_VERSION} SOVERSION ${VSOMEIP_COMPAT_MAJOR_VERSION})
if (MSVC)
set_target_properties(${VSOMEIP_COMPAT_NAME} PROPERTIES COMPILE_DEFINITIONS "VSOMEIP_DLL_COMPILATION_PLUGIN")
Expand Down
3 changes: 3 additions & 0 deletions examples/routingmanagerd/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
# Daemon
add_executable(routingmanagerd routingmanagerd.cpp)
target_link_libraries(routingmanagerd ${VSOMEIP_NAME} ${Boost_LIBRARIES} ${DL_LIBRARY} ${DLT_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT})
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.")
Expand Down
11 changes: 11 additions & 0 deletions implementation/configuration/src/configuration_plugin_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,18 @@ value_t read_value_from_config(const std::shared_ptr<vsomeip_v3::cfg::e2e> &_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 {
Expand Down
10 changes: 10 additions & 0 deletions implementation/plugin/include/plugin_manager_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,18 @@ class plugin_manager_impl : public plugin_manager {
std::recursive_mutex plugins_mutex_;

static std::shared_ptr<plugin_manager_impl> 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
51 changes: 45 additions & 6 deletions implementation/plugin/src/plugin_manager_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::mutex> its_lock_start_stop(loader_mutex_);
Expand All @@ -72,9 +90,18 @@ void plugin_manager_impl::load_plugins() {
std::lock_guard<std::recursive_mutex> 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<plugin_init_func>(
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<plugin_init_func>(
load_symbol(handle, VSOMEIP_PLUGIN_INIT_SYMBOL));
}
if (its_init_func) {
create_plugin_func its_create_func = (*its_init_func)();
if (its_create_func) {
Expand Down Expand Up @@ -126,9 +153,18 @@ std::shared_ptr<plugin> plugin_manager_impl::get_plugin(plugin_type_e _type,

std::shared_ptr<plugin> 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<plugin_init_func>(
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<plugin_init_func>(
load_symbol(handle, VSOMEIP_PLUGIN_INIT_SYMBOL));
}
if (its_init_func) {
create_plugin_func its_create_func = (*its_init_func)();
if (its_create_func) {
Expand All @@ -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
Expand Down
11 changes: 11 additions & 0 deletions implementation/service_discovery/src/runtime_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 6 additions & 1 deletion vsomeip3Config.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -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 ()
7 changes: 6 additions & 1 deletion vsomeipConfig.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -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 ()

0 comments on commit 2ba9e24

Please sign in to comment.