From 09b9058b731a6a3c0b0999033dea3072978e1c49 Mon Sep 17 00:00:00 2001 From: Shaokai Lin Date: Wed, 21 Jan 2026 09:22:38 -0800 Subject: [PATCH] Fix ROS2 C++ build with proper ament CMake exports This commit fixes the ROS2/colcon build integration by properly configuring CMake exports for the ament ecosystem. Changes: - Add ament detection when not building as LF subproject - Move install(EXPORT) from lib/CMakeLists.txt to root CMakeLists.txt (required for ament compatibility - must be alongside ament_package()) - Add NAMESPACE to export for proper imported target creation - Add ament_export_targets(), ament_export_include_directories(), and ament_export_libraries() for full ament integration The key insight is that ament's build system requires the export installation and ament_package() to be in the same CMakeLists.txt file. Using namespaced targets (reactor-cpp::reactor-cpp) ensures proper linkage through CMake's imported target mechanism. Related: lf-lang/lingua-franca#2580 Co-Authored-By: Claude Opus 4.5 --- CMakeLists.txt | 21 +++++++++++++++++++++ lib/CMakeLists.txt | 7 ++----- package.xml | 13 ++----------- 3 files changed, 25 insertions(+), 16 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 92dfd42a..8f201698 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,6 +9,13 @@ endif() project(${LIB_TARGET} LANGUAGES CXX VERSION 0.0.1) +if (NOT DEFINED LF_REACTOR_CPP_SUFFIX) + find_package(ament_cmake QUIET) + if(ament_cmake_FOUND) + set(REACTOR_CPP_USE_AMENT ON) + endif() +endif() + # require C++17 set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) @@ -71,3 +78,17 @@ if (DEFINED LF_REACTOR_CPP_SUFFIX) else() install(DIRECTORY include/ DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}") endif() + +if (REACTOR_CPP_USE_AMENT) + # Install the export file - must be in root CMakeLists.txt for ament compatibility + install(EXPORT ${LIB_TARGET} DESTINATION share/${LIB_TARGET}/cmake NAMESPACE ${LIB_TARGET}::) + ament_export_targets(${LIB_TARGET} HAS_LIBRARY_TARGET) + ament_export_include_directories(include) + ament_export_libraries(${LIB_TARGET}) + ament_package() +else() + # For non-ament builds, install export file from here + if(REACTOR_CPP_INSTALL) + install(EXPORT ${LIB_TARGET} DESTINATION share/${LIB_TARGET}/cmake) + endif() +endif() diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index 6f2ffd7d..7fa490f9 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -61,12 +61,9 @@ if(REACTOR_CPP_INSTALL) install(FILES "${PROJECT_BINARY_DIR}/include/reactor-cpp/config.hh" DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/reactor-cpp") endif() - install(TARGETS ${LIB_TARGET} EXPORT ${LIB_TARGET}Config + install(TARGETS ${LIB_TARGET} EXPORT ${LIB_TARGET} ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" OPTIONAL LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}" OPTIONAL RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" OPTIONAL) - - install(EXPORT ${LIB_TARGET}Config DESTINATION share/${LIB_TARGET}/cmake) - - export(TARGETS ${PROJECT_NAME} FILE ${LIB_TARGET}Config.cmake) + # Note: install(EXPORT ...) moved to root CMakeLists.txt for ament compatibility endif() diff --git a/package.xml b/package.xml index fdfe31c9..a42a7870 100644 --- a/package.xml +++ b/package.xml @@ -6,17 +6,8 @@ A C++ reactor runtime user ISC License + ament_cmake - cmake - - - - reactor-cpp-foo - 0.0.0 - A C++ reactor runtime - user - ISC License - - cmake + ament_cmake