Skip to content

Commit ae209ef

Browse files
committed
fix(LibCarla/ros2): address review comments and fix CycloneDDS runtime availability
Fix 5 issues flagged in the PR review and one structural CMake bug that caused --dds-middleware=cyclonedds to fail at runtime. Review fixes - FastDDSPublisherMiddleware: use current_count instead of total_count in on_publication_matched. total_count is cumulative and only grows, so _alive latched true permanently after the first match (DDS v1.4 §2.2.4.5). - FastDDSSubscriberMiddleware: same fix in on_subscription_matched. - cmake/cyclone_dds: remove CARLA_ROS2_DDS_FASTDDS from the CycloneDDS target compile definitions; each vendor lib should define only its own macro. - cmake/fast_dds: remove CARLA_ROS2_DDS_CYCLONEDDS from the FastDDS target compile definitions (same reason). - cmake/fast_dds: fix source glob that mistakenly included dds/cyclonedds/*.cpp instead of dds/fastdds/*.cpp. CMake restructure (root cause: CycloneDDS unavailable at runtime) Removing the cross-defines exposed a deeper problem: the shared ros2 sources (ROS2.cpp, all publishers/subscribers/types) were compiled into both carla_fastdds.a and carla_cyclonedds.a, each copy with only one middleware macro defined. The linker picks one copy at final link; whichever archive is scanned first wins all those symbols. The winning copy's #if defined guards then make the other middleware invisible at runtime: ERROR: ROS2: middleware 'CycloneDDS' is not compiled into this binary. Fix: introduce cmake/ros2/CMakeLists.txt, which compiles ALL ros2 code once with both CARLA_ROS2_DDS_FASTDDS and CARLA_ROS2_DDS_CYCLONEDDS defined, including dds/cyclonedds/CycloneDDSSertype.cpp (already guarded internally by #ifdef CARLA_ROS2_DDS_CYCLONEDDS). Both fast_dds and cyclone_dds become install-only subprojects (vendor headers and vendor .a files, no library targets). Carla.Build.cs links carla_ros2 for all compiled ros2 code, plus the vendor runtime archives (libddsc.a, libfastcdr.a, libfastrtps.a, libfoonathan_memory-0.7.3.a) unchanged. Final CMake layout: cmake/ros2/ -> libcarla_ros2.a (all ros2 code, both macros) cmake/fast_dds/ -> install-only (FastDDS headers + vendor .a) cmake/cyclone_dds/ -> install-only (CycloneDDS headers + vendor .a)
1 parent 2d80046 commit ae209ef

7 files changed

Lines changed: 75 additions & 112 deletions

File tree

LibCarla/cmake/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ elseif (CMAKE_BUILD_TYPE STREQUAL "Server")
2626
elseif (CMAKE_BUILD_TYPE STREQUAL "Pytorch")
2727
add_subdirectory("pytorch")
2828
elseif (CMAKE_BUILD_TYPE STREQUAL "ros2")
29+
add_subdirectory("ros2")
2930
add_subdirectory("fast_dds")
3031
add_subdirectory("cyclone_dds")
3132
else ()

LibCarla/cmake/cyclone_dds/CMakeLists.txt

Lines changed: 2 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -26,60 +26,5 @@ install(FILES ${cyclonedds_dependencies} DESTINATION lib)
2626
install(DIRECTORY "${CYCLONEDDS_INCLUDE_PATH}/dds" DESTINATION include)
2727
install(DIRECTORY "${CYCLONEDDS_INCLUDE_PATH}/ddsc" DESTINATION include)
2828

29-
file(GLOB libcarla_cyclonedds_sources
30-
"${libcarla_source_path}/carla/ros2/*.cpp"
31-
"${libcarla_source_path}/carla/ros2/publishers/*.cpp"
32-
"${libcarla_source_path}/carla/ros2/subscribers/*.cpp"
33-
"${libcarla_source_path}/carla/ros2/listeners/*.cpp"
34-
"${libcarla_source_path}/carla/ros2/types/*.cpp"
35-
"${libcarla_source_path}/carla/ros2/dds/cyclonedds/*.cpp")
36-
37-
# ==============================================================================
38-
# Create targets for debug and release in the same build type.
39-
# ==============================================================================
40-
41-
if (LIBCARLA_BUILD_RELEASE)
42-
add_library(carla_cyclonedds STATIC ${libcarla_cyclonedds_sources})
43-
44-
target_compile_definitions(carla_cyclonedds PRIVATE CARLA_ROS2_DDS_CYCLONEDDS CARLA_ROS2_DDS_FASTDDS)
45-
target_compile_options(carla_cyclonedds PRIVATE -fexceptions)
46-
47-
target_include_directories(carla_cyclonedds SYSTEM PRIVATE
48-
"${BOOST_INCLUDE_PATH}"
49-
"${RPCLIB_INCLUDE_PATH}")
50-
51-
# CycloneDDS C headers (dds/dds.h, dds/ddsi/*.h)
52-
target_include_directories(carla_cyclonedds PRIVATE "${CYCLONEDDS_INCLUDE_PATH}")
53-
# Fast-CDR headers (fastcdr/Cdr.h) — used by CdrSerialization.h
54-
target_include_directories(carla_cyclonedds PRIVATE "${FASTDDS_INCLUDE_PATH}")
55-
56-
target_link_libraries(carla_cyclonedds
57-
"${CYCLONEDDS_LIB_PATH}/libddsc.a"
58-
"${FASTDDS_LIB_PATH}/libfastcdr.a")
59-
install(TARGETS carla_cyclonedds DESTINATION lib)
60-
set_target_properties(carla_cyclonedds PROPERTIES COMPILE_FLAGS "${CMAKE_CXX_FLAGS_RELEASE}")
61-
62-
endif()
63-
64-
if (LIBCARLA_BUILD_DEBUG)
65-
66-
add_library(carla_cyclonedds_debug STATIC ${libcarla_cyclonedds_sources})
67-
68-
target_compile_definitions(carla_cyclonedds_debug PRIVATE CARLA_ROS2_DDS_CYCLONEDDS CARLA_ROS2_DDS_FASTDDS)
69-
target_compile_options(carla_cyclonedds_debug PRIVATE -fexceptions)
70-
71-
target_include_directories(carla_cyclonedds_debug SYSTEM PRIVATE
72-
"${BOOST_INCLUDE_PATH}"
73-
"${RPCLIB_INCLUDE_PATH}")
74-
75-
target_include_directories(carla_cyclonedds_debug PRIVATE "${CYCLONEDDS_INCLUDE_PATH}")
76-
target_include_directories(carla_cyclonedds_debug PRIVATE "${FASTDDS_INCLUDE_PATH}")
77-
78-
target_link_libraries(carla_cyclonedds_debug
79-
"${CYCLONEDDS_LIB_PATH}/libddsc.a"
80-
"${FASTDDS_LIB_PATH}/libfastcdr.a")
81-
install(TARGETS carla_cyclonedds_debug DESTINATION lib)
82-
set_target_properties(carla_cyclonedds_debug PROPERTIES COMPILE_FLAGS "${CMAKE_CXX_FLAGS_DEBUG}")
83-
target_compile_definitions(carla_cyclonedds_debug PUBLIC -DBOOST_ASIO_ENABLE_BUFFER_DEBUGGING)
84-
85-
endif()
29+
# Note: dds/cyclonedds/ compiled sources (CycloneDDSSertype.cpp) are included
30+
# in cmake/ros2/ (carla_ros2), compiled once with both middleware macros defined.

LibCarla/cmake/fast_dds/CMakeLists.txt

Lines changed: 3 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -27,54 +27,6 @@ install(DIRECTORY "${FASTDDS_INCLUDE_PATH}/fastcdr" DESTINATION include)
2727
install(DIRECTORY "${FASTDDS_INCLUDE_PATH}/fastdds" DESTINATION include)
2828
install(DIRECTORY "${FASTDDS_INCLUDE_PATH}/fastrtps" DESTINATION include)
2929

30-
31-
file(GLOB libcarla_fastdds_sources
32-
"${libcarla_source_path}/carla/ros2/*.cpp"
33-
"${libcarla_source_path}/carla/ros2/publishers/*.cpp"
34-
"${libcarla_source_path}/carla/ros2/subscribers/*.cpp"
35-
"${libcarla_source_path}/carla/ros2/listeners/*.cpp"
36-
"${libcarla_source_path}/carla/ros2/types/*.cpp"
37-
"${libcarla_source_path}/carla/ros2/dds/cyclonedds/*.cpp")
38-
39-
# ==============================================================================
40-
# Create targets for debug and release in the same build type.
41-
# ==============================================================================
42-
43-
if (LIBCARLA_BUILD_RELEASE)
44-
add_library(carla_fastdds STATIC ${libcarla_fastdds_sources})
45-
46-
target_compile_definitions(carla_fastdds PRIVATE CARLA_ROS2_DDS_FASTDDS CARLA_ROS2_DDS_CYCLONEDDS)
47-
target_compile_options(carla_fastdds PRIVATE -fexceptions)
48-
49-
target_include_directories(carla_fastdds SYSTEM PRIVATE
50-
"${BOOST_INCLUDE_PATH}"
51-
"${RPCLIB_INCLUDE_PATH}")
52-
53-
target_include_directories(carla_fastdds PRIVATE "${FASTDDS_INCLUDE_PATH}")
54-
target_include_directories(carla_fastdds PRIVATE "${CYCLONEDDS_INCLUDE_PATH}")
55-
target_include_directories(carla_fastdds PRIVATE "${libcarla_source_path}/carla/ros2")
56-
target_link_libraries(carla_fastdds fastrtps fastcdr "${FAST_DDS_LIBRARIES}")
57-
install(TARGETS carla_fastdds DESTINATION lib)
58-
set_target_properties(carla_fastdds PROPERTIES COMPILE_FLAGS "${CMAKE_CXX_FLAGS_RELEASE}")
59-
60-
endif()
61-
62-
if (LIBCARLA_BUILD_DEBUG)
63-
64-
add_library(carla_fastdds_debug STATIC ${libcarla_fastdds_sources})
65-
66-
target_compile_definitions(carla_fastdds_debug PRIVATE CARLA_ROS2_DDS_FASTDDS CARLA_ROS2_DDS_CYCLONEDDS)
67-
target_compile_options(carla_fastdds_debug PRIVATE -fexceptions)
68-
69-
target_include_directories(carla_fastdds_debug SYSTEM PRIVATE
70-
"${BOOST_INCLUDE_PATH}"
71-
"${RPCLIB_INCLUDE_PATH}")
72-
73-
target_include_directories(carla_fastdds_debug PRIVATE "${FASTDDS_INCLUDE_PATH}")
74-
target_include_directories(carla_fastdds_debug PRIVATE "${CYCLONEDDS_INCLUDE_PATH}")
75-
target_include_directories(carla_fastdds_debug PRIVATE "${libcarla_source_path}/carla/ros2")
76-
install(TARGETS carla_fastdds_debug DESTINATION lib)
77-
set_target_properties(carla_fastdds_debug PROPERTIES COMPILE_FLAGS "${CMAKE_CXX_FLAGS_DEBUG}")
78-
target_compile_definitions(carla_fastdds_debug PUBLIC -DBOOST_ASIO_ENABLE_BUFFER_DEBUGGING)
79-
80-
endif()
30+
# Note: dds/fastdds/ contains only header-only templates (no .cpp sources).
31+
# All compiled ros2 code is in cmake/ros2/ (carla_ros2), compiled once with
32+
# both CARLA_ROS2_DDS_FASTDDS and CARLA_ROS2_DDS_CYCLONEDDS defined.

LibCarla/cmake/ros2/CMakeLists.txt

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
cmake_minimum_required(VERSION 3.5.1)
2+
project(libcarla_ros2)
3+
4+
# All ros2 source files compiled ONCE with both middleware macros defined.
5+
# This ensures ROS2::Enable, DDSMiddlewareFactory::IsMiddlewareAvailable,
6+
# and all CreatePublisher<T>/CreateSubscriber<S> instantiations see both
7+
# CARLA_ROS2_DDS_FASTDDS and CARLA_ROS2_DDS_CYCLONEDDS at compile time.
8+
# cmake/fast_dds and cmake/cyclone_dds are install-only (no library targets);
9+
# all compiled code lives here.
10+
11+
file(GLOB libcarla_ros2_sources
12+
"${libcarla_source_path}/carla/ros2/*.cpp"
13+
"${libcarla_source_path}/carla/ros2/publishers/*.cpp"
14+
"${libcarla_source_path}/carla/ros2/subscribers/*.cpp"
15+
"${libcarla_source_path}/carla/ros2/listeners/*.cpp"
16+
"${libcarla_source_path}/carla/ros2/types/*.cpp"
17+
"${libcarla_source_path}/carla/ros2/dds/cyclonedds/*.cpp")
18+
19+
# ==============================================================================
20+
# Create targets for debug and release in the same build type.
21+
# ==============================================================================
22+
23+
if (LIBCARLA_BUILD_RELEASE)
24+
add_library(carla_ros2 STATIC ${libcarla_ros2_sources})
25+
26+
target_compile_definitions(carla_ros2 PRIVATE
27+
CARLA_ROS2_DDS_FASTDDS
28+
CARLA_ROS2_DDS_CYCLONEDDS)
29+
target_compile_options(carla_ros2 PRIVATE -fexceptions)
30+
31+
target_include_directories(carla_ros2 SYSTEM PRIVATE
32+
"${BOOST_INCLUDE_PATH}"
33+
"${RPCLIB_INCLUDE_PATH}")
34+
35+
target_include_directories(carla_ros2 PRIVATE
36+
"${FASTDDS_INCLUDE_PATH}"
37+
"${CYCLONEDDS_INCLUDE_PATH}"
38+
"${libcarla_source_path}/carla/ros2")
39+
40+
install(TARGETS carla_ros2 DESTINATION lib)
41+
set_target_properties(carla_ros2 PROPERTIES COMPILE_FLAGS "${CMAKE_CXX_FLAGS_RELEASE}")
42+
43+
endif()
44+
45+
if (LIBCARLA_BUILD_DEBUG)
46+
add_library(carla_ros2_debug STATIC ${libcarla_ros2_sources})
47+
48+
target_compile_definitions(carla_ros2_debug PRIVATE
49+
CARLA_ROS2_DDS_FASTDDS
50+
CARLA_ROS2_DDS_CYCLONEDDS)
51+
target_compile_options(carla_ros2_debug PRIVATE -fexceptions)
52+
53+
target_include_directories(carla_ros2_debug SYSTEM PRIVATE
54+
"${BOOST_INCLUDE_PATH}"
55+
"${RPCLIB_INCLUDE_PATH}")
56+
57+
target_include_directories(carla_ros2_debug PRIVATE
58+
"${FASTDDS_INCLUDE_PATH}"
59+
"${CYCLONEDDS_INCLUDE_PATH}"
60+
"${libcarla_source_path}/carla/ros2")
61+
62+
install(TARGETS carla_ros2_debug DESTINATION lib)
63+
set_target_properties(carla_ros2_debug PROPERTIES COMPILE_FLAGS "${CMAKE_CXX_FLAGS_DEBUG}")
64+
target_compile_definitions(carla_ros2_debug PUBLIC -DBOOST_ASIO_ENABLE_BUFFER_DEBUGGING)
65+
66+
endif()

LibCarla/source/carla/ros2/dds/fastdds/FastDDSPublisherMiddleware.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class FastDDSPublisherMiddleware
4848
void on_publication_matched(
4949
efd::DataWriter* writer,
5050
const efd::PublicationMatchedStatus& info) override {
51-
_alive.store(info.total_count > 0, std::memory_order_relaxed);
51+
_alive.store(info.current_count > 0, std::memory_order_relaxed);
5252
}
5353

5454
~FastDDSPublisherMiddleware() override {

LibCarla/source/carla/ros2/dds/fastdds/FastDDSSubscriberMiddleware.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class FastDDSSubscriberMiddleware
4949
void on_subscription_matched(
5050
efd::DataReader* reader,
5151
const efd::SubscriptionMatchedStatus& info) override {
52-
_alive.store(info.total_count > 0, std::memory_order_relaxed);
52+
_alive.store(info.current_count > 0, std::memory_order_relaxed);
5353
}
5454

5555
void on_data_available(efd::DataReader* reader) override {

Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Carla.Build.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -324,8 +324,7 @@ private void AddCarlaServerDependency(ReadOnlyTargetRules Target)
324324
PublicDefinitions.Add("CARLA_ROS2_DDS_FASTDDS");
325325
PublicDefinitions.Add("CARLA_ROS2_DDS_CYCLONEDDS");
326326

327-
PublicAdditionalLibraries.Add(Path.Combine(LibCarlaInstallPath, "lib", GetLibName("carla_fastdds")));
328-
PublicAdditionalLibraries.Add(Path.Combine(LibCarlaInstallPath, "lib", GetLibName("carla_cyclonedds")));
327+
PublicAdditionalLibraries.Add(Path.Combine(LibCarlaInstallPath, "lib", GetLibName("carla_ros2")));
329328
PublicAdditionalLibraries.Add(Path.Combine(LibCarlaInstallPath, "lib", "libddsc.a"));
330329
PublicAdditionalLibraries.Add(Path.Combine(LibCarlaInstallPath, "lib", "libfoonathan_memory-0.7.3.a"));
331330
PublicAdditionalLibraries.Add(Path.Combine(LibCarlaInstallPath, "lib", "libfastcdr.a"));

0 commit comments

Comments
 (0)