cmake_minimum_required(VERSION 3.8)
project(omni_node)

if(CMAKE_CXX_STANDARD GREATER_EQUAL 20)
  set(CMAKE_CXX_STANDARD 17)
endif()
if(NOT CMAKE_CXX_STANDARD)
  set(CMAKE_CXX_STANDARD 17)
endif()
set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(std_msgs REQUIRED)
find_package(rcl_interfaces REQUIRED)
find_package(nlohmann_json REQUIRED)

# Read XLM_SDK_ROOT: from cmake cache (-DXLM_SDK_ROOT=... via colcon --cmake-args)
# or from environment variable XLM_SDK_ROOT
if(DEFINED ENV{XLM_SDK_ROOT})
  set(XLM_SDK_ROOT "$ENV{XLM_SDK_ROOT}")
endif()
if(NOT XLM_SDK_ROOT)
  message(FATAL_ERROR "XLM_SDK_ROOT is not set. Pass -DXLM_SDK_ROOT=/path/to/sdk via colcon --cmake-args")
endif()
message(STATUS "XLM_SDK_ROOT = ${XLM_SDK_ROOT}")

add_library(omni_node_lib STATIC
  src/omni_engine.cpp
  src/omni_node.cpp
)

ament_target_dependencies(omni_node_lib
  rclcpp
  std_msgs
  rcl_interfaces
  nlohmann_json
)

target_include_directories(omni_node_lib
  PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include
  # 系统路径优先于 SDK，避免 SDK 中损坏的 nlohmann 版本
  SYSTEM PUBLIC /usr/include/aarch64-linux-gnu
  PRIVATE ${XLM_SDK_ROOT}/include
)

ament_export_libraries(omni_node_lib)

add_executable(omni_node
  src/main.cpp
)

target_include_directories(omni_node
  SYSTEM PUBLIC /usr/include/aarch64-linux-gnu
  PRIVATE ${XLM_SDK_ROOT}/include
)

ament_target_dependencies(omni_node
  rclcpp
  std_msgs
  rcl_interfaces
  rmw_fastrtps_cpp
  rosidl_typesupport_cpp
  nlohmann_json
)

target_link_libraries(omni_node
  omni_node_lib
  ${XLM_SDK_ROOT}/lib/libxlm.so
)

install(TARGETS omni_node omni_node_lib
  DESTINATION lib/${PROJECT_NAME}
)

install(DIRECTORY
  launch/
  config/
  DESTINATION share/${PROJECT_NAME}/
)

# Register shell environment hook so xlm SDK lib is prepended to LD_LIBRARY_PATH
ament_environment_hooks("${CMAKE_CURRENT_SOURCE_DIR}/env/omni_node.sh.in")

ament_package()
