cmake_minimum_required(VERSION 3.16)
project(skylink_bridge LANGUAGES CXX)

# ============================================================================
# 编译标准和优化选项
# ============================================================================

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra")

# 如果未指定编译类型，默认为 Release
if(NOT CMAKE_BUILD_TYPE)
  set(CMAKE_BUILD_TYPE Release CACHE STRING "Build type" FORCE)
endif()

# ============================================================================
# 查找依赖
# ============================================================================

find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(sensor_msgs REQUIRED)
find_package(geometry_msgs REQUIRED)
find_package(cv_bridge REQUIRED)
find_package(image_transport REQUIRED)
find_package(mavros_msgs REQUIRED)

# OpenCV
find_package(OpenCV REQUIRED COMPONENTS core imgproc)

# JPEG 库
find_package(JPEG REQUIRED)

# 线程库
find_package(Threads REQUIRED)

# ============================================================================
# 包含目录
# ============================================================================

include_directories(
  include
)

# ============================================================================
# 源文件
# ============================================================================

set(SOURCES
  src/udp_sender_node.cpp
  src/camera_driver_node.cpp
)

set(HEADERS
  include/skylink_bridge/udp_sender_node.h
  include/protocol.h
)

# ============================================================================
# 创建节点可执行文件
# ============================================================================

add_executable(udp_sender_node src/udp_sender_node.cpp ${HEADERS})
add_executable(camera_driver_node src/camera_driver_node.cpp)

# 链接库 - UDP Sender
target_link_libraries(udp_sender_node
  ${rclcpp_LIBRARIES}
  ${sensor_msgs_LIBRARIES}
  ${geometry_msgs_LIBRARIES}
  ${cv_bridge_LIBRARIES}
  ${image_transport_LIBRARIES}
  ${mavros_msgs_LIBRARIES}
  ${OpenCV_LIBRARIES}
  ${JPEG_LIBRARIES}
  Threads::Threads
)

# 链接库 - Camera Driver
target_link_libraries(camera_driver_node
  ${rclcpp_LIBRARIES}
  ${sensor_msgs_LIBRARIES}
  ${cv_bridge_LIBRARIES}
  ${OpenCV_LIBRARIES}
  Threads::Threads
)

# 目标级别的包含目录
ament_target_dependencies(udp_sender_node
  rclcpp
  sensor_msgs
  geometry_msgs
  cv_bridge
  image_transport
  mavros_msgs
)

ament_target_dependencies(camera_driver_node
  rclcpp
  sensor_msgs
  cv_bridge
)

# ============================================================================
# 安装规则
# ============================================================================

# 安装可执行文件
install(TARGETS udp_sender_node camera_driver_node
  DESTINATION lib/${PROJECT_NAME}
)

# 安装启动文件
install(DIRECTORY launch/
  DESTINATION share/${PROJECT_NAME}/launch
)

# 安装配置文件
install(DIRECTORY config/
  DESTINATION share/${PROJECT_NAME}/config
)

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

# ============================================================================
# Ament 导出
# ============================================================================

ament_export_dependencies(
  rclcpp
  sensor_msgs
  geometry_msgs
  cv_bridge
  image_transport
  mavros_msgs
  OpenCV
)

ament_package()
