Skip to content

Commit fd5173b

Browse files
committed
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.
1 parent 655bb34 commit fd5173b

File tree

9 files changed

+109
-13
lines changed

9 files changed

+109
-13
lines changed

CMakeLists.txt

+9-5
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ if(NOT CMAKE_BUILD_TYPE)
6060
# Set the possible values of build type for cmake-gui
6161
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release" "MinSizeRel" "RelWithDebInfo")
6262
endif()
63+
option(BUILD_SHARED_LIBS "Set to OFF to build static libraries" ON)
64+
if(NOT BUILD_SHARED_LIBS)
65+
add_definitions(-DVSOMEIP_STATIC_PLUGINS)
66+
endif()
6367

6468
# OS
6569
if (${CMAKE_SYSTEM_NAME} MATCHES "Linux")
@@ -258,7 +262,7 @@ file(GLOB ${VSOMEIP_NAME}-cfg_SRC
258262
)
259263
list(SORT ${VSOMEIP_NAME}-cfg_SRC)
260264
if (VSOMEIP_ENABLE_MULTIPLE_ROUTING_MANAGERS EQUAL 0)
261-
add_library(${VSOMEIP_NAME}-cfg SHARED ${${VSOMEIP_NAME}-cfg_SRC})
265+
add_library(${VSOMEIP_NAME}-cfg ${${VSOMEIP_NAME}-cfg_SRC})
262266
set_target_properties (${VSOMEIP_NAME}-cfg PROPERTIES VERSION ${VSOMEIP_VERSION} SOVERSION ${VSOMEIP_MAJOR_VERSION})
263267
if (MSVC)
264268
set_target_properties(${VSOMEIP_NAME}-cfg PROPERTIES COMPILE_DEFINITIONS "VSOMEIP_DLL_COMPILATION_PLUGIN")
@@ -292,7 +296,7 @@ endif()
292296

293297
list(SORT ${VSOMEIP_NAME}_SRC)
294298

295-
add_library(${VSOMEIP_NAME} SHARED ${${VSOMEIP_NAME}_SRC})
299+
add_library(${VSOMEIP_NAME} ${${VSOMEIP_NAME}_SRC})
296300
set_target_properties (${VSOMEIP_NAME} PROPERTIES VERSION ${VSOMEIP_VERSION} SOVERSION ${VSOMEIP_MAJOR_VERSION})
297301
if (MSVC)
298302
set_target_properties(${VSOMEIP_NAME} PROPERTIES COMPILE_DEFINITIONS "VSOMEIP_DLL_COMPILATION")
@@ -322,7 +326,7 @@ file(GLOB ${VSOMEIP_NAME}-sd_SRC
322326
)
323327
list(SORT ${VSOMEIP_NAME}-sd_SRC)
324328

325-
add_library(${VSOMEIP_NAME}-sd SHARED ${${VSOMEIP_NAME}-sd_SRC})
329+
add_library(${VSOMEIP_NAME}-sd ${${VSOMEIP_NAME}-sd_SRC})
326330
set_target_properties (${VSOMEIP_NAME}-sd PROPERTIES VERSION ${VSOMEIP_VERSION} SOVERSION ${VSOMEIP_MAJOR_VERSION})
327331
if (MSVC)
328332
set_target_properties(${VSOMEIP_NAME}-sd PROPERTIES COMPILE_DEFINITIONS "VSOMEIP_DLL_COMPILATION_PLUGIN")
@@ -339,7 +343,7 @@ file(GLOB_RECURSE ${VSOMEIP_NAME}-e2e_SRC
339343
)
340344
list(SORT ${VSOMEIP_NAME}-e2e_SRC)
341345

342-
add_library(${VSOMEIP_NAME}-e2e SHARED ${${VSOMEIP_NAME}-e2e_SRC})
346+
add_library(${VSOMEIP_NAME}-e2e ${${VSOMEIP_NAME}-e2e_SRC})
343347
set_target_properties (${VSOMEIP_NAME}-e2e PROPERTIES VERSION ${VSOMEIP_VERSION} SOVERSION ${VSOMEIP_MAJOR_VERSION})
344348
if (MSVC)
345349
set_target_properties(${VSOMEIP_NAME}-e2e PROPERTIES COMPILE_DEFINITIONS "VSOMEIP_DLL_COMPILATION_PLUGIN")
@@ -363,7 +367,7 @@ file(GLOB_RECURSE ${VSOMEIP_COMPAT_NAME}_SRC
363367
)
364368
list(SORT ${VSOMEIP_COMPAT_NAME}_SRC)
365369

366-
add_library(${VSOMEIP_COMPAT_NAME} SHARED ${${VSOMEIP_COMPAT_NAME}_SRC})
370+
add_library(${VSOMEIP_COMPAT_NAME} ${${VSOMEIP_COMPAT_NAME}_SRC})
367371
set_target_properties (${VSOMEIP_COMPAT_NAME} PROPERTIES VERSION ${VSOMEIP_COMPAT_VERSION} SOVERSION ${VSOMEIP_COMPAT_MAJOR_VERSION})
368372
if (MSVC)
369373
set_target_properties(${VSOMEIP_COMPAT_NAME} PROPERTIES COMPILE_DEFINITIONS "VSOMEIP_DLL_COMPILATION_PLUGIN")

examples/routingmanagerd/CMakeLists.txt

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
# Daemon
77
add_executable(routingmanagerd routingmanagerd.cpp)
88
target_link_libraries(routingmanagerd ${VSOMEIP_NAME} ${Boost_LIBRARIES} ${DL_LIBRARY} ${DLT_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT})
9+
if(NOT BUILD_SHARED_LIBS)
10+
target_link_libraries(routingmanagerd ${VSOMEIP_NAME}-cfg ${VSOMEIP_NAME}-e2e ${VSOMEIP_NAME}-sd)
11+
endif()
912
add_dependencies(routingmanagerd ${VSOMEIP_NAME})
1013

1114
option(VSOMEIP_INSTALL_ROUTINGMANAGERD "Whether or not to install the routing manager daemon.")

implementation/configuration/src/configuration_plugin_impl.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,18 @@
88
#include "../include/configuration_plugin_impl.hpp"
99
#include "../include/configuration_impl.hpp"
1010

11+
#ifdef VSOMEIP_STATIC_PLUGINS
12+
namespace vsomeip_v3 {
13+
14+
create_plugin_func plugin_manager_impl_init_hook_cfg()
15+
{
16+
return configuration_plugin_impl::get_plugin;
17+
}
18+
19+
} // namespace vsomeip_v3
20+
#else
1121
VSOMEIP_PLUGIN(vsomeip_v3::configuration_plugin_impl)
22+
#endif
1223

1324
namespace vsomeip_v3 {
1425

implementation/e2e_protection/src/e2e/profile/e2e_provider_impl.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,18 @@ value_t read_value_from_config(const std::shared_ptr<vsomeip_v3::cfg::e2e> &_con
5050

5151
} // namespace
5252

53+
#ifdef VSOMEIP_STATIC_PLUGINS
54+
namespace vsomeip_v3 {
5355

56+
create_plugin_func plugin_manager_impl_init_hook_e2e()
57+
{
58+
return e2e::e2e_provider_impl::get_plugin;
59+
}
60+
61+
} // namespace vsomeip_v3
62+
#else
5463
VSOMEIP_PLUGIN(vsomeip_v3::e2e::e2e_provider_impl)
64+
#endif
5565

5666
namespace vsomeip_v3 {
5767
namespace e2e {

implementation/plugin/include/plugin_manager_impl.hpp

+10
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,18 @@ class plugin_manager_impl : public plugin_manager {
5353
std::recursive_mutex plugins_mutex_;
5454

5555
static std::shared_ptr<plugin_manager_impl> the_plugin_manager__;
56+
57+
#ifdef VSOMEIP_STATIC_PLUGINS
58+
plugin_init_func get_static_init_func(const std::string &library_);
59+
#endif
5660
};
5761

62+
#ifdef VSOMEIP_STATIC_PLUGINS
63+
vsomeip_v3::create_plugin_func plugin_manager_impl_init_hook_cfg();
64+
vsomeip_v3::create_plugin_func plugin_manager_impl_init_hook_sd();
65+
vsomeip_v3::create_plugin_func plugin_manager_impl_init_hook_e2e();
66+
#endif
67+
5868
} // namespace vsomeip_v3
5969

6070
#endif // VSOMEIP_V3_PLUGIN_MANAGER_IMPL_HPP

implementation/plugin/src/plugin_manager_impl.cpp

+45-6
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,24 @@ plugin_manager_impl::~plugin_manager_impl() {
4949
plugins_.clear();
5050
}
5151

52+
#ifdef VSOMEIP_STATIC_PLUGINS
53+
plugin_init_func plugin_manager_impl::get_static_init_func(const std::string &library_)
54+
{
55+
if (library_ == VSOMEIP_CFG_LIBRARY) {
56+
return plugin_manager_impl_init_hook_cfg;
57+
}
58+
else if (library_ == VSOMEIP_SD_LIBRARY) {
59+
return plugin_manager_impl_init_hook_sd;
60+
}
61+
else if (library_ == VSOMEIP_E2E_LIBRARY) {
62+
return plugin_manager_impl_init_hook_e2e;
63+
}
64+
else {
65+
return nullptr;
66+
}
67+
}
68+
#endif
69+
5270
void plugin_manager_impl::load_plugins() {
5371
{
5472
std::lock_guard<std::mutex> its_lock_start_stop(loader_mutex_);
@@ -72,9 +90,18 @@ void plugin_manager_impl::load_plugins() {
7290
std::lock_guard<std::recursive_mutex> its_lock_start_stop(plugins_mutex_);
7391
// Load plug-in info from libraries parsed before
7492
for (const auto& plugin_name : plugins) {
75-
void* handle = load_library(plugin_name);
76-
plugin_init_func its_init_func = reinterpret_cast<plugin_init_func>(
77-
load_symbol(handle, VSOMEIP_PLUGIN_INIT_SYMBOL));
93+
void* handle;
94+
plugin_init_func its_init_func;
95+
#ifdef VSOMEIP_STATIC_PLUGINS
96+
handle = nullptr;
97+
its_init_func = get_static_init_func(plugin_name);
98+
if (!its_init_func)
99+
#endif
100+
{
101+
handle = load_library(plugin_name);
102+
its_init_func = reinterpret_cast<plugin_init_func>(
103+
load_symbol(handle, VSOMEIP_PLUGIN_INIT_SYMBOL));
104+
}
78105
if (its_init_func) {
79106
create_plugin_func its_create_func = (*its_init_func)();
80107
if (its_create_func) {
@@ -126,9 +153,18 @@ std::shared_ptr<plugin> plugin_manager_impl::get_plugin(plugin_type_e _type,
126153

127154
std::shared_ptr<plugin> plugin_manager_impl::load_plugin(const std::string& _library,
128155
plugin_type_e _type, uint32_t _version) {
129-
void* handle = load_library(_library);
130-
plugin_init_func its_init_func = reinterpret_cast<plugin_init_func>(
131-
load_symbol(handle, VSOMEIP_PLUGIN_INIT_SYMBOL));
156+
void* handle;
157+
plugin_init_func its_init_func;
158+
#ifdef VSOMEIP_STATIC_PLUGINS
159+
handle = nullptr;
160+
its_init_func = get_static_init_func(_library);
161+
if (!its_init_func)
162+
#endif
163+
{
164+
handle = load_library(_library);
165+
its_init_func = reinterpret_cast<plugin_init_func>(
166+
load_symbol(handle, VSOMEIP_PLUGIN_INIT_SYMBOL));
167+
}
132168
if (its_init_func) {
133169
create_plugin_func its_create_func = (*its_init_func)();
134170
if (its_create_func) {
@@ -154,6 +190,9 @@ bool plugin_manager_impl::unload_plugin(plugin_type_e _type) {
154190
const auto found_handle = handles_.find(_type);
155191
if (found_handle != handles_.end()) {
156192
for (const auto& its_name : found_handle->second) {
193+
if (!its_name.second) { // Skip statically linked plugins
194+
continue;
195+
}
157196
#ifdef _WIN32
158197
FreeLibrary((HMODULE)its_name.second);
159198
#else

implementation/service_discovery/src/runtime_impl.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,18 @@
1212
#include "../include/runtime_impl.hpp"
1313
#include "../include/service_discovery_impl.hpp"
1414

15+
#ifdef VSOMEIP_STATIC_PLUGINS
16+
namespace vsomeip_v3 {
17+
18+
create_plugin_func plugin_manager_impl_init_hook_sd()
19+
{
20+
return sd::runtime_impl::get_plugin;
21+
}
22+
23+
} // namespace vsomeip_v3
24+
#else
1525
VSOMEIP_PLUGIN(vsomeip_v3::sd::runtime_impl)
26+
#endif
1627

1728
namespace vsomeip_v3 {
1829
namespace sd {

vsomeip3Config.cmake.in

+5-1
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,8 @@ if (NOT TARGET vsomeip AND NOT vsomeip_BINARY_DIR)
1616
endif ()
1717

1818
# These are IMPORTED targets created by vsomeipTargets.cmake
19-
set (VSOMEIP_LIBRARIES vsomeip3)
19+
if (NOT @BUILD_SHARED_LIBS@)
20+
set (VSOMEIP_LIBRARIES vsomeip3 vsomeip3-cfg vsomeip3-e2e vsomeip3-sd)
21+
else ()
22+
set (VSOMEIP_LIBRARIES vsomeip3)
23+
endif ()

vsomeipConfig.cmake.in

+5-1
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,8 @@ if (NOT TARGET vsomeip AND NOT vsomeip_BINARY_DIR)
1616
endif ()
1717

1818
# These are IMPORTED targets created by vsomeipTargets.cmake
19-
set (VSOMEIP_LIBRARIES vsomeip)
19+
if (NOT @BUILD_SHARED_LIBS@)
20+
set (VSOMEIP_LIBRARIES vsomeip vsomeip3-cfg vsomeip3-e2e vsomeip3-sd)
21+
else ()
22+
set (VSOMEIP_LIBRARIES vsomeip)
23+
endif ()

0 commit comments

Comments
 (0)