Skip to content

Commit 8a5eb0b

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 64f8d4e commit 8a5eb0b

File tree

9 files changed

+111
-13
lines changed

9 files changed

+111
-13
lines changed

CMakeLists.txt

Lines changed: 9 additions & 5 deletions
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")
@@ -263,7 +267,7 @@ file(GLOB ${VSOMEIP_NAME}-cfg_SRC
263267
)
264268
list(SORT ${VSOMEIP_NAME}-cfg_SRC)
265269
if (VSOMEIP_ENABLE_MULTIPLE_ROUTING_MANAGERS EQUAL 0)
266-
add_library(${VSOMEIP_NAME}-cfg SHARED ${${VSOMEIP_NAME}-cfg_SRC})
270+
add_library(${VSOMEIP_NAME}-cfg ${${VSOMEIP_NAME}-cfg_SRC})
267271
set_target_properties (${VSOMEIP_NAME}-cfg PROPERTIES VERSION ${VSOMEIP_VERSION} SOVERSION ${VSOMEIP_MAJOR_VERSION})
268272
if (MSVC)
269273
set_target_properties(${VSOMEIP_NAME}-cfg PROPERTIES COMPILE_DEFINITIONS "VSOMEIP_DLL_COMPILATION_PLUGIN")
@@ -297,7 +301,7 @@ endif()
297301

298302
list(SORT ${VSOMEIP_NAME}_SRC)
299303

300-
add_library(${VSOMEIP_NAME} SHARED ${${VSOMEIP_NAME}_SRC})
304+
add_library(${VSOMEIP_NAME} ${${VSOMEIP_NAME}_SRC})
301305
set_target_properties (${VSOMEIP_NAME} PROPERTIES VERSION ${VSOMEIP_VERSION} SOVERSION ${VSOMEIP_MAJOR_VERSION})
302306
if (MSVC)
303307
set_target_properties(${VSOMEIP_NAME} PROPERTIES COMPILE_DEFINITIONS "VSOMEIP_DLL_COMPILATION")
@@ -327,7 +331,7 @@ file(GLOB ${VSOMEIP_NAME}-sd_SRC
327331
)
328332
list(SORT ${VSOMEIP_NAME}-sd_SRC)
329333

330-
add_library(${VSOMEIP_NAME}-sd SHARED ${${VSOMEIP_NAME}-sd_SRC})
334+
add_library(${VSOMEIP_NAME}-sd ${${VSOMEIP_NAME}-sd_SRC})
331335
set_target_properties (${VSOMEIP_NAME}-sd PROPERTIES VERSION ${VSOMEIP_VERSION} SOVERSION ${VSOMEIP_MAJOR_VERSION})
332336
if (MSVC)
333337
set_target_properties(${VSOMEIP_NAME}-sd PROPERTIES COMPILE_DEFINITIONS "VSOMEIP_DLL_COMPILATION_PLUGIN")
@@ -344,7 +348,7 @@ file(GLOB_RECURSE ${VSOMEIP_NAME}-e2e_SRC
344348
)
345349
list(SORT ${VSOMEIP_NAME}-e2e_SRC)
346350

347-
add_library(${VSOMEIP_NAME}-e2e SHARED ${${VSOMEIP_NAME}-e2e_SRC})
351+
add_library(${VSOMEIP_NAME}-e2e ${${VSOMEIP_NAME}-e2e_SRC})
348352
set_target_properties (${VSOMEIP_NAME}-e2e PROPERTIES VERSION ${VSOMEIP_VERSION} SOVERSION ${VSOMEIP_MAJOR_VERSION})
349353
if (MSVC)
350354
set_target_properties(${VSOMEIP_NAME}-e2e PROPERTIES COMPILE_DEFINITIONS "VSOMEIP_DLL_COMPILATION_PLUGIN")
@@ -371,7 +375,7 @@ file(GLOB_RECURSE ${VSOMEIP_COMPAT_NAME}_SRC
371375
)
372376
list(SORT ${VSOMEIP_COMPAT_NAME}_SRC)
373377

374-
add_library(${VSOMEIP_COMPAT_NAME} SHARED ${${VSOMEIP_COMPAT_NAME}_SRC})
378+
add_library(${VSOMEIP_COMPAT_NAME} ${${VSOMEIP_COMPAT_NAME}_SRC})
375379
set_target_properties (${VSOMEIP_COMPAT_NAME} PROPERTIES VERSION ${VSOMEIP_COMPAT_VERSION} SOVERSION ${VSOMEIP_COMPAT_MAJOR_VERSION})
376380
if (MSVC)
377381
set_target_properties(${VSOMEIP_COMPAT_NAME} PROPERTIES COMPILE_DEFINITIONS "VSOMEIP_DLL_COMPILATION_PLUGIN")

examples/routingmanagerd/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ target_link_libraries(routingmanagerd ${VSOMEIP_NAME} ${Boost_LIBRARIES} ${DL_LI
99
if(${CMAKE_SYSTEM_NAME} MATCHES "QNX")
1010
target_link_libraries(routingmanagerd socket)
1111
endif()
12+
if(NOT BUILD_SHARED_LIBS)
13+
target_link_libraries(routingmanagerd ${VSOMEIP_NAME}-cfg ${VSOMEIP_NAME}-e2e ${VSOMEIP_NAME}-sd)
14+
endif()
1215
add_dependencies(routingmanagerd ${VSOMEIP_NAME})
1316

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

implementation/configuration/src/configuration_plugin_impl.cpp

Lines changed: 11 additions & 0 deletions
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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,18 @@ value_t read_value_from_config(const std::shared_ptr<vsomeip_v3::cfg::e2e> &_con
5454

5555
} // namespace
5656

57+
#ifdef VSOMEIP_STATIC_PLUGINS
58+
namespace vsomeip_v3 {
5759

60+
create_plugin_func plugin_manager_impl_init_hook_e2e()
61+
{
62+
return e2e::e2e_provider_impl::get_plugin;
63+
}
64+
65+
} // namespace vsomeip_v3
66+
#else
5867
VSOMEIP_PLUGIN(vsomeip_v3::e2e::e2e_provider_impl)
68+
#endif
5969

6070
namespace vsomeip_v3 {
6171
namespace e2e {

implementation/plugin/include/plugin_manager_impl.hpp

Lines changed: 10 additions & 0 deletions
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

Lines changed: 45 additions & 6 deletions
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

Lines changed: 11 additions & 0 deletions
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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,9 @@ 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+
link_directories(@CMAKE_INSTALL_PREFIX@/@INSTALL_LIB_DIR@)
21+
set (VSOMEIP_LIBRARIES vsomeip3 vsomeip3-cfg vsomeip3-e2e vsomeip3-sd)
22+
else ()
23+
set (VSOMEIP_LIBRARIES vsomeip3)
24+
endif ()

vsomeipConfig.cmake.in

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,9 @@ 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+
link_directories(@CMAKE_INSTALL_PREFIX@/@INSTALL_LIB_DIR@)
21+
set (VSOMEIP_LIBRARIES vsomeip vsomeip3-cfg vsomeip3-e2e vsomeip3-sd)
22+
else ()
23+
set (VSOMEIP_LIBRARIES vsomeip)
24+
endif ()

0 commit comments

Comments
 (0)