cmake_minimum_required(VERSION 3.10)
project(rm_arm_control)

# 设置 C 标准
set(CMAKE_C_STANDARD 11)

# 添加忽略定义重定义警告的选项
if(CMAKE_C_COMPILER_ID STREQUAL "GNU" OR CMAKE_C_COMPILER_ID STREQUAL "Clang")
    add_compile_options(-Wno-define-redefinition)
endif()
add_compile_options(-march=native -O3 -DEIGEN_VECTORIZE_SSE4_2)
add_compile_options(-g)

#set(CMAKE_BUILD_TYPE Release)

# 全局设置 C++/C 编译选项
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O3")

set(ROS_DOMAIN_ID_STR $ENV{ROS_DOMAIN_ID})
if(NOT ROS_DOMAIN_ID_STR)
  set(ROS_DOMAIN_ID_STR "0")  # 未设置时默认 0
endif()

# 2. 验证是否为非负整数（用正则表达式）
if(ROS_DOMAIN_ID_STR MATCHES "^[0-9]+$")
  # 是有效整数，转换为数字（用 math 命令）
  math(EXPR DOMAIN_ID_NUM "${ROS_DOMAIN_ID_STR}")
else()
  # 无效值，警告并使用默认 0
  message(WARNING "Invalid ROS_DOMAIN_ID (${ROS_DOMAIN_ID_STR}), using default 0")
  set(DOMAIN_ID_NUM 0)
endif()

# 3. 根据域 ID 判断环境
if(DOMAIN_ID_NUM EQUAL 10)
  message(STATUS "Detected simulation environment (ROS_DOMAIN_ID=10)")
  add_compile_definitions(SIMULATION)  # 模拟环境宏
else()
  message(STATUS "Detected real robot environment (ROS_DOMAIN_ID=${DOMAIN_ID_NUM})")
  add_compile_definitions(REAL_ROBOT)   # 实机环境宏
endif()

# 依赖
find_package(ament_cmake REQUIRED)
find_package(Eigen3 REQUIRED)  # 查找Eigen3库
find_package(rclcpp REQUIRED)
find_package(rclcpp_action REQUIRED)
find_package(geometry_msgs REQUIRED)
find_package(sensor_msgs REQUIRED)
find_package(trajectory_msgs REQUIRED)
find_package(control_msgs REQUIRED)
find_package(gjk_interface REQUIRED)
find_package(interfaces REQUIRED)
find_package(PkgConfig REQUIRED)
pkg_check_modules(TINYXML2 REQUIRED tinyxml2)

include_directories(
  include
  ${CMAKE_CURRENT_SOURCE_DIR}/../include
  ${EIGEN3_INCLUDE_DIRS}  # 引入Eigen3的头文件路径
  ${TINYXML2_INCLUDE_DIR}
)

# 设置源文件
set(SOURCES
    src/rm_arm_handler.cpp
    src/rm_arm_node.cpp
    simulator/trapezoidal_trajectory_kinematics.cpp
    simulator/rm_driver_kinematics.cpp
    simulator/col_simulator.cpp
    src/goal_manager.cpp
    driver/arm_driver.cpp
    driver/rm_arm_driver.cpp
    src/main.cpp
    src/init_set.cpp
)

# 添加可执行文件
add_executable(rm_arm_control ${SOURCES})

ament_target_dependencies(rm_arm_control 
  rclcpp
  rclcpp_action
  geometry_msgs 
  sensor_msgs
  trajectory_msgs
  control_msgs
  gjk_interface
  interfaces
)

# 查找库
find_library(RMAN_API_LIB NAMES api_cpp PATHS "${CMAKE_CURRENT_SOURCE_DIR}/Robotic_Arm/lib")

# 链接库
target_link_libraries(rm_arm_control ${RMAN_API_LIB})
target_link_libraries(rm_arm_control ${TINYXML2_LIBRARIES})

# 如果是 UNIX 平台且不是 ARM 平台，确保动态库文件可以在运行时被找到（这一步通常不是必须的，但在某些配置下可能需要）
if(UNIX)
    add_custom_command(TARGET rm_arm_control POST_BUILD
            COMMAND ${CMAKE_COMMAND} -E copy_if_different
            "${CMAKE_CURRENT_SOURCE_DIR}/Robotic_Arm/lib/libapi_cpp.so"
            $<TARGET_FILE_DIR:rm_arm_control>)
endif()

install(TARGETS
  rm_arm_control
  DESTINATION lib/${PROJECT_NAME})

# 安装第三方库到安装目录(重要)
install(DIRECTORY
  ${CMAKE_CURRENT_SOURCE_DIR}/Robotic_Arm/lib
  DESTINATION lib/..
  FILES_MATCHING PATTERN "*.so*"
)

install(
  DIRECTORY launch urdf meshes  # 安装 launch、urdf、meshes 目录
  DESTINATION share/${PROJECT_NAME}
)

ament_package()
