cmake_minimum_required(VERSION 3.8)
project(unitree_motor_serial_driver)

# 设置 C++ 标准
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3")

# 包含 ROS 2 依赖
find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(rclcpp_components REQUIRED)
find_package(std_msgs REQUIRED)
find_package(rc_msgs REQUIRED)

# 包含目录和库路径
include_directories(include)
link_directories(lib)

# 设置全局 RPATH
set(CMAKE_INSTALL_RPATH "${CMAKE_CURRENT_SOURCE_DIR}/lib")
set(CMAKE_BUILD_RPATH "${CMAKE_CURRENT_SOURCE_DIR}/lib")

# 根据系统架构选择库
if(CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "aarch64") 
    set(EXTRA_LIBS libUnitreeMotorSDK_Arm64.so)
else()
    set(EXTRA_LIBS libUnitreeMotorSDK_Linux64.so)
endif()

# 创建组件库
add_library(unitree_motor_components SHARED
  src/unitree_motor_serial_driver.cpp
)
ament_target_dependencies(unitree_motor_components
  rclcpp
  rclcpp_components
  rc_msgs
)
target_link_libraries(unitree_motor_components ${EXTRA_LIBS})
rclcpp_components_register_nodes(unitree_motor_components
  "unitree_motor_serial_driver::MotorControlNode"
)

# # 创建独立可执行文件
# add_executable(goM8010_6_motor src/goM8010_6_motor.cpp)
# ament_target_dependencies(goM8010_6_motor rclcpp rc_msgs)
# target_link_libraries(goM8010_6_motor ${EXTRA_LIBS})

# 创建节点可执行文件（非组件方式）
# add_executable(unitree_motor_serial_driver_node src/unitree_motor_serial_driver_node.cpp)
# target_link_libraries(unitree_motor_serial_driver_node unitree_motor_components)
# ament_target_dependencies(unitree_motor_serial_driver_node rclcpp)

# 安装目标
install(TARGETS
  # goM8010_6_motor
  # unitree_motor_serial_driver_node
  unitree_motor_components
  RUNTIME DESTINATION lib/${PROJECT_NAME}
  ARCHIVE DESTINATION lib
  LIBRARY DESTINATION lib
)

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

# 安装头文件
install(DIRECTORY include/
  DESTINATION include/
)

# ROS 2 包配置
ament_package()