Skip to content

Commit cab5a95

Browse files
committed
Separate dynamic handling
Signed-off-by: Your Name <[email protected]> Inferring flags from build type Signed-off-by: Your Name <[email protected]> Remove extra include Signed-off-by: Your Name <[email protected]> Update Fix rebase Signed-off-by: Your Name <[email protected]> Fixes Add messages Update Fix Fix Messages Update Linter Lint Remove messages
1 parent 38d0a8d commit cab5a95

File tree

3 files changed

+119
-70
lines changed

3 files changed

+119
-70
lines changed

rosidl_typesupport_c/CMakeLists.txt

+13-7
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,19 @@ cmake_minimum_required(VERSION 3.5)
22

33
project(rosidl_typesupport_c)
44

5-
option(ROSIDL_TYPESUPPORT_STATIC_TYPESUPPORT "Enable static typesupport" OFF)
5+
if(BUILD_SHARED_LIBS)
6+
set(${PROJECT_NAME}_LIBRARY_TYPE "SHARED")
7+
else()
8+
set(${PROJECT_NAME}_LIBRARY_TYPE "STATIC")
9+
endif()
10+
11+
if(rosidl_typesupport_c_LIBRARY_TYPE STREQUAL "STATIC"
12+
AND NOT BUILD_TESTING
13+
)
14+
set(ROSIDL_TYPESUPPORT_STATIC_TYPESUPPORT ON)
15+
else()
16+
set(ROSIDL_TYPESUPPORT_STATIC_TYPESUPPORT OFF)
17+
endif()
618

719
# Default to C11
820
if(NOT CMAKE_C_STANDARD)
@@ -141,12 +153,6 @@ if(BUILD_TESTING)
141153
endif()
142154
endif()
143155

144-
if(BUILD_SHARED_LIBS)
145-
set(${PROJECT_NAME}_LIBRARY_TYPE "SHARED")
146-
else()
147-
set(${PROJECT_NAME}_LIBRARY_TYPE "STATIC")
148-
endif()
149-
150156
ament_package(
151157
CONFIG_EXTRAS "rosidl_typesupport_c-extras.cmake.in"
152158
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
// Copyright 2021 Open Source Robotics Foundation, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#ifndef DYNAMIC_SUPPORT_DISPATCH_HPP_
16+
#define DYNAMIC_SUPPORT_DISPATCH_HPP_
17+
18+
#include <cstddef>
19+
#include <cstdio>
20+
#include <cstring>
21+
22+
#include <memory>
23+
#include <stdexcept>
24+
#include <list>
25+
#include <string>
26+
27+
#include "rcpputils/shared_library.hpp"
28+
#include "rcutils/error_handling.h"
29+
#include "rcutils/snprintf.h"
30+
#include "rosidl_typesupport_c/type_support_map.h"
31+
32+
namespace rosidl_typesupport_c
33+
{
34+
35+
static void *
36+
handle_shared_library_from_name(
37+
const type_support_map_t * map,
38+
size_t map_item,
39+
const char * identifier
40+
)
41+
{
42+
rcpputils::SharedLibrary * lib = nullptr;
43+
44+
if (!map->data[map_item]) {
45+
char library_basename[1024];
46+
int ret = rcutils_snprintf(
47+
library_basename, 1023, "%s__%s",
48+
map->package_name, identifier);
49+
if (ret < 0) {
50+
RCUTILS_SET_ERROR_MSG("Failed to format library name");
51+
return nullptr;
52+
}
53+
54+
std::string library_name;
55+
try {
56+
library_name = rcpputils::get_platform_library_name(library_basename);
57+
} catch (const std::exception & e) {
58+
RCUTILS_SET_ERROR_MSG_WITH_FORMAT_STRING(
59+
"Failed to compute library name for '%s' due to %s",
60+
library_basename, e.what());
61+
return nullptr;
62+
}
63+
64+
try {
65+
lib = new rcpputils::SharedLibrary(library_name);
66+
} catch (const std::runtime_error & e) {
67+
RCUTILS_SET_ERROR_MSG_WITH_FORMAT_STRING(
68+
"Could not load library %s: %s", library_name.c_str(), e.what());
69+
return nullptr;
70+
} catch (const std::bad_alloc & e) {
71+
RCUTILS_SET_ERROR_MSG_WITH_FORMAT_STRING(
72+
"Could not load library %s: %s", library_name.c_str(), e.what());
73+
return nullptr;
74+
}
75+
map->data[map_item] = lib;
76+
}
77+
78+
auto clib = static_cast<const rcpputils::SharedLibrary *>(map->data[map_item]);
79+
lib = const_cast<rcpputils::SharedLibrary *>(clib);
80+
81+
void * sym = nullptr;
82+
83+
try {
84+
if (!lib->has_symbol(map->symbol_name[map_item])) {
85+
RCUTILS_SET_ERROR_MSG_WITH_FORMAT_STRING(
86+
"Failed to find symbol '%s' in library", map->symbol_name[map_item]);
87+
return nullptr;
88+
}
89+
sym = lib->get_symbol(map->symbol_name[map_item]);
90+
} catch (const std::exception & e) {
91+
RCUTILS_SET_ERROR_MSG_WITH_FORMAT_STRING(
92+
"Failed to get symbol '%s' in library: %s",
93+
map->symbol_name[map_item], e.what());
94+
return nullptr;
95+
}
96+
97+
return sym;
98+
}
99+
100+
} // namespace rosidl_typesupport_c
101+
102+
#endif // DYNAMIC_SUPPORT_DISPATCH_HPP_

rosidl_typesupport_c/src/type_support_dispatch.hpp

+4-63
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,7 @@
1616
#define TYPE_SUPPORT_DISPATCH_HPP_
1717

1818
#ifndef ROSIDL_TYPESUPPORT_STATIC_TYPESUPPORT
19-
20-
#include <cstddef>
21-
#include <cstdio>
22-
#include <cstring>
23-
24-
#include <memory>
25-
#include <stdexcept>
26-
#include <string>
27-
28-
#include "rcpputils/shared_library.hpp"
29-
19+
#include "./dynamic_support_dispatch.hpp"
3020
#endif // ROSIDL_TYPESUPPORT_STATIC_TYPESUPPORT
3121

3222
#include "rcutils/error_handling.h"
@@ -57,58 +47,9 @@ get_typesupport_handle_function(
5747
}
5848
typedef const TypeSupport * (* funcSignature)(void);
5949
#ifndef ROSIDL_TYPESUPPORT_STATIC_TYPESUPPORT
60-
rcpputils::SharedLibrary * lib = nullptr;
61-
62-
if (!map->data[i]) {
63-
char library_basename[1024];
64-
int ret = rcutils_snprintf(
65-
library_basename, 1023, "%s__%s",
66-
map->package_name, identifier);
67-
if (ret < 0) {
68-
RCUTILS_SET_ERROR_MSG("Failed to format library name");
69-
return nullptr;
70-
}
71-
72-
std::string library_name;
73-
try {
74-
library_name = rcpputils::get_platform_library_name(library_basename);
75-
} catch (const std::exception & e) {
76-
RCUTILS_SET_ERROR_MSG_WITH_FORMAT_STRING(
77-
"Failed to compute library name for '%s' due to %s",
78-
library_basename, e.what());
79-
return nullptr;
80-
}
81-
82-
try {
83-
lib = new rcpputils::SharedLibrary(library_name);
84-
} catch (const std::runtime_error & e) {
85-
RCUTILS_SET_ERROR_MSG_WITH_FORMAT_STRING(
86-
"Could not load library %s: %s", library_name.c_str(), e.what());
87-
return nullptr;
88-
} catch (const std::bad_alloc & e) {
89-
RCUTILS_SET_ERROR_MSG_WITH_FORMAT_STRING(
90-
"Could not load library %s: %s", library_name.c_str(), e.what());
91-
return nullptr;
92-
}
93-
map->data[i] = lib;
94-
}
95-
auto clib = static_cast<const rcpputils::SharedLibrary *>(map->data[i]);
96-
lib = const_cast<rcpputils::SharedLibrary *>(clib);
97-
98-
void * sym = nullptr;
99-
100-
try {
101-
if (!lib->has_symbol(map->symbol_name[i])) {
102-
RCUTILS_SET_ERROR_MSG_WITH_FORMAT_STRING(
103-
"Failed to find symbol '%s' in library", map->symbol_name[i]);
104-
return nullptr;
105-
}
106-
sym = lib->get_symbol(map->symbol_name[i]);
107-
} catch (const std::exception & e) {
108-
RCUTILS_SET_ERROR_MSG_WITH_FORMAT_STRING(
109-
"Failed to get symbol '%s' in library: %s",
110-
map->symbol_name[i], e.what());
111-
return nullptr;
50+
void * sym = handle_shared_library_from_name(map, i, identifier);
51+
if (nullptr == sym) {
52+
continue;
11253
}
11354
funcSignature func = reinterpret_cast<funcSignature>(sym);
11455
#else

0 commit comments

Comments
 (0)