Skip to content

Static type support for rosidl_typesupport_c #110

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: rolling
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 29 additions & 9 deletions rosidl_typesupport_c/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,20 @@ cmake_minimum_required(VERSION 3.5)

project(rosidl_typesupport_c)

if(BUILD_SHARED_LIBS)
set(${PROJECT_NAME}_LIBRARY_TYPE "SHARED")
else()
set(${PROJECT_NAME}_LIBRARY_TYPE "STATIC")
endif()

if(rosidl_typesupport_c_LIBRARY_TYPE STREQUAL "STATIC"
AND NOT BUILD_TESTING
)
set(ROSIDL_TYPESUPPORT_STATIC_TYPESUPPORT ON)
else()
set(ROSIDL_TYPESUPPORT_STATIC_TYPESUPPORT OFF)
endif()

# Default to C11
if(NOT CMAKE_C_STANDARD)
set(CMAKE_C_STANDARD 11)
Expand All @@ -16,13 +30,17 @@ if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
endif()

find_package(ament_cmake_ros REQUIRED)
find_package(rcpputils REQUIRED)
find_package(rcutils REQUIRED)
find_package(rosidl_runtime_c REQUIRED)
if(NOT ROSIDL_TYPESUPPORT_STATIC_TYPESUPPORT)
find_package(rcpputils REQUIRED)
endif()

ament_export_dependencies(rcpputils)
ament_export_dependencies(rosidl_runtime_c)
ament_export_dependencies(rosidl_typesupport_interface)
if(NOT ROSIDL_TYPESUPPORT_STATIC_TYPESUPPORT)
ament_export_dependencies(rcpputils)
endif()

ament_export_include_directories(include)

Expand All @@ -36,15 +54,23 @@ if(WIN32)
target_compile_definitions(${PROJECT_NAME}
PRIVATE "ROSIDL_TYPESUPPORT_C_BUILDING_DLL")
endif()
target_compile_definitions(${PROJECT_NAME}
PRIVATE
$<$<BOOL:${ROSIDL_TYPESUPPORT_STATIC_TYPESUPPORT}>:ROSIDL_TYPESUPPORT_STATIC_TYPESUPPORT>
)
target_include_directories(${PROJECT_NAME} PUBLIC
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>"
"$<INSTALL_INTERFACE:include>")

ament_target_dependencies(${PROJECT_NAME}
"rcpputils"
"rcutils"
"rosidl_runtime_c"
)
if(NOT ROSIDL_TYPESUPPORT_STATIC_TYPESUPPORT)
ament_target_dependencies(${PROJECT_NAME}
"rcpputils"
)
endif()
ament_export_libraries(${PROJECT_NAME})
ament_export_targets(${PROJECT_NAME})

Expand Down Expand Up @@ -130,12 +156,6 @@ if(BUILD_TESTING)
ament_add_pytest_test(test_cli_extension test/test_cli_extension.py)
endif()

if(BUILD_SHARED_LIBS)
set(${PROJECT_NAME}_LIBRARY_TYPE "SHARED")
else()
set(${PROJECT_NAME}_LIBRARY_TYPE "STATIC")
endif()

ament_package(
CONFIG_EXTRAS "rosidl_typesupport_c-extras.cmake.in"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,15 +118,19 @@ target_include_directories(${rosidl_generate_interfaces_TARGET}${_target_suffix}
# if only a single typesupport is used this package will directly reference it
# therefore it needs to link against the selected typesupport
if(NOT typesupports MATCHES ";")
set(SINGLE_TYPE_SUPPORT ON)
target_include_directories(${rosidl_generate_interfaces_TARGET}${_target_suffix}
PUBLIC
"$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/${typesupports}>")
target_link_libraries(${rosidl_generate_interfaces_TARGET}${_target_suffix}
${rosidl_generate_interfaces_TARGET}__${typesupports})
else()
set(SINGLE_TYPE_SUPPORT OFF)
if("${rosidl_typesupport_c_LIBRARY_TYPE}" STREQUAL "STATIC")
message(FATAL_ERROR "Multiple typesupports [${typesupports}] but static "
"linking was requested")
target_compile_definitions(${rosidl_generate_interfaces_TARGET}${_target_suffix}
PRIVATE
ROSIDL_TYPESUPPORT_STATIC_TYPESUPPORT
)
endif()
endif()

Expand All @@ -150,15 +154,33 @@ add_dependencies(
${rosidl_generate_interfaces_TARGET}
${rosidl_generate_interfaces_TARGET}${_target_suffix}
)
add_dependencies(
${rosidl_generate_interfaces_TARGET}${_target_suffix}
${rosidl_generate_interfaces_TARGET}__rosidl_generator_c
${rosidl_generate_interfaces_TARGET}__rosidl_typesupport_c
)

if(NOT rosidl_generate_interfaces_SKIP_INSTALL)
install(
TARGETS ${rosidl_generate_interfaces_TARGET}${_target_suffix}
EXPORT ${rosidl_generate_interfaces_TARGET}
ARCHIVE DESTINATION lib
LIBRARY DESTINATION lib
RUNTIME DESTINATION bin
)
if(SINGLE_TYPE_SUPPORT)
install(
TARGETS
${rosidl_generate_interfaces_TARGET}${_target_suffix}
${rosidl_generate_interfaces_TARGET}__${typesupports}
EXPORT ${rosidl_generate_interfaces_TARGET}
ARCHIVE DESTINATION lib
LIBRARY DESTINATION lib
RUNTIME DESTINATION bin
)
else()
install(
TARGETS ${rosidl_generate_interfaces_TARGET}${_target_suffix}
EXPORT ${rosidl_generate_interfaces_TARGET}
ARCHIVE DESTINATION lib
LIBRARY DESTINATION lib
RUNTIME DESTINATION bin
)
endif()

ament_export_libraries(${rosidl_generate_interfaces_TARGET}${_target_suffix})
endif()

Expand Down
21 changes: 21 additions & 0 deletions rosidl_typesupport_c/resource/msg__type_support.cpp.em
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,34 @@ typedef struct _@(message.structure.namespaced_type.name)_type_support_data_t
void * data[@(len(type_supports))];
} _@(message.structure.namespaced_type.name)_type_support_data_t;

#ifdef ROSIDL_TYPESUPPORT_STATIC_TYPESUPPORT
#ifdef __cplusplus
extern "C"
{
#endif
@[for type_support in sorted(type_supports)]@
rosidl_message_type_support_t * ROSIDL_TYPESUPPORT_INTERFACE__MESSAGE_SYMBOL_NAME(@(type_support), @(', '.join([package_name] + list(interface_path.parents[0].parts))), @(message.structure.namespaced_type.name))();
@[end for]@
#ifdef __cplusplus
}
#endif

static _@(message.structure.namespaced_type.name)_type_support_data_t _@(message.structure.namespaced_type.name)_message_typesupport_data = {
{
@[for type_support in sorted(type_supports)]@
(void*) ROSIDL_TYPESUPPORT_INTERFACE__MESSAGE_SYMBOL_NAME(@(type_support), @(', '.join([package_name] + list(interface_path.parents[0].parts))), @(message.structure.namespaced_type.name)),
@[end for]@
}
};
#else
static _@(message.structure.namespaced_type.name)_type_support_data_t _@(message.structure.namespaced_type.name)_message_typesupport_data = {
{
@[for type_support in sorted(type_supports)]@
0, // will store the shared library later
@[end for]@
}
};
#endif // ROSIDL_TYPESUPPORT_STATIC_TYPESUPPORT

static const type_support_map_t _@(message.structure.namespaced_type.name)_message_typesupport_map = {
@(len(type_supports)),
Expand Down
21 changes: 21 additions & 0 deletions rosidl_typesupport_c/resource/srv__type_support.cpp.em
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,34 @@ typedef struct _@(service.namespaced_type.name)_type_support_data_t
void * data[@(len(type_supports))];
} _@(service.namespaced_type.name)_type_support_data_t;

#ifdef ROSIDL_TYPESUPPORT_STATIC_TYPESUPPORT
#ifdef __cplusplus
extern "C"
{
#endif
@[for type_support in sorted(type_supports)]@
rosidl_service_type_support_t * ROSIDL_TYPESUPPORT_INTERFACE__SERVICE_SYMBOL_NAME(@(type_support), @(', '.join([package_name] + list(interface_path.parents[0].parts))), @(service.namespaced_type.name))();
@[end for]@
#ifdef __cplusplus
}
#endif

static _@(service.namespaced_type.name)_type_support_data_t _@(service.namespaced_type.name)_service_typesupport_data = {
{
@[for type_support in sorted(type_supports)]@
(void*) ROSIDL_TYPESUPPORT_INTERFACE__SERVICE_SYMBOL_NAME(@(type_support), @(', '.join([package_name] + list(interface_path.parents[0].parts))), @(service.namespaced_type.name)),
@[end for]@
}
};
#else
static _@(service.namespaced_type.name)_type_support_data_t _@(service.namespaced_type.name)_service_typesupport_data = {
{
@[for type_support in sorted(type_supports)]@
0, // will store the shared library later
@[end for]@
}
};
#endif // ROSIDL_TYPESUPPORT_STATIC_TYPESUPPORT

static const type_support_map_t _@(service.namespaced_type.name)_service_typesupport_map = {
@(len(type_supports)),
Expand Down
102 changes: 102 additions & 0 deletions rosidl_typesupport_c/src/dynamic_support_dispatch.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// Copyright 2021 Open Source Robotics Foundation, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef DYNAMIC_SUPPORT_DISPATCH_HPP_
#define DYNAMIC_SUPPORT_DISPATCH_HPP_

#include <cstddef>
#include <cstdio>
#include <cstring>

#include <memory>
#include <stdexcept>
#include <list>
#include <string>

#include "rcpputils/shared_library.hpp"
#include "rcutils/error_handling.h"
#include "rcutils/snprintf.h"
#include "rosidl_typesupport_c/type_support_map.h"

namespace rosidl_typesupport_c
{

static void *
handle_shared_library_from_name(
const type_support_map_t * map,
size_t map_item,
const char * identifier
)
{
rcpputils::SharedLibrary * lib = nullptr;

if (!map->data[map_item]) {
char library_basename[1024];
int ret = rcutils_snprintf(
library_basename, 1023, "%s__%s",
map->package_name, identifier);
if (ret < 0) {
RCUTILS_SET_ERROR_MSG("Failed to format library name");
return nullptr;
}

std::string library_name;
try {
library_name = rcpputils::get_platform_library_name(library_basename);
} catch (const std::exception & e) {
RCUTILS_SET_ERROR_MSG_WITH_FORMAT_STRING(
"Failed to compute library name for '%s' due to %s",
library_basename, e.what());
return nullptr;
}

try {
lib = new rcpputils::SharedLibrary(library_name);
} catch (const std::runtime_error & e) {
RCUTILS_SET_ERROR_MSG_WITH_FORMAT_STRING(
"Could not load library %s: %s", library_name.c_str(), e.what());
return nullptr;
} catch (const std::bad_alloc & e) {
RCUTILS_SET_ERROR_MSG_WITH_FORMAT_STRING(
"Could not load library %s: %s", library_name.c_str(), e.what());
return nullptr;
}
map->data[map_item] = lib;
}

auto clib = static_cast<const rcpputils::SharedLibrary *>(map->data[map_item]);
lib = const_cast<rcpputils::SharedLibrary *>(clib);

void * sym = nullptr;

try {
if (!lib->has_symbol(map->symbol_name[map_item])) {
RCUTILS_SET_ERROR_MSG_WITH_FORMAT_STRING(
"Failed to find symbol '%s' in library", map->symbol_name[map_item]);
return nullptr;
}
sym = lib->get_symbol(map->symbol_name[map_item]);
} catch (const std::exception & e) {
RCUTILS_SET_ERROR_MSG_WITH_FORMAT_STRING(
"Failed to get symbol '%s' in library: %s",
map->symbol_name[map_item], e.what());
return nullptr;
}

return sym;
}

} // namespace rosidl_typesupport_c

#endif // DYNAMIC_SUPPORT_DISPATCH_HPP_
Loading