cmake_minimum_required(VERSION 3.22)
project(moveit_setup_app_plugins LANGUAGES CXX)

# Common cmake code applied to all moveit packages
find_package(moveit_common REQUIRED)
moveit_package()

find_package(ament_cmake REQUIRED)
find_package(ament_cmake_ros REQUIRED)
find_package(ament_index_cpp REQUIRED)
# rviz switched to Qt6 in 15.1.14: https://github.com/ros2/rviz/pull/1635
# (merged 2025-12-08, first released in the 15.1.14 tag on 2025-12-17 -- 15.1.0
# through 15.1.13 are still Qt5)
find_package(rviz2 QUIET)
if(rviz2_VERSION VERSION_GREATER_EQUAL 15.1.14)
  find_package(Qt6 REQUIRED COMPONENTS Core Widgets)
  set(QT_VERSION_MAJOR 6)
  # Hint for transitive deps that use find_package(QT NAMES Qt6 Qt5 ...) which
  # may incorrectly resolve to Qt5 due to CMake's ascending path order
  set(QT_DIR "${Qt6_DIR}")
else()
  find_package(Qt5 REQUIRED COMPONENTS Core Widgets)
  set(QT_VERSION_MAJOR 5)
endif()
find_package(moveit_ros_visualization REQUIRED)
find_package(moveit_setup_framework REQUIRED)
find_package(pluginlib REQUIRED)
find_package(rclcpp REQUIRED)

# Instruct CMake to run moc automatically when needed.
set(CMAKE_AUTOMOC ON)
add_definitions(-DQT_NO_KEYWORDS)

add_library(
  moveit_setup_app_plugins
  src/launches.cpp
  src/launches_widget.cpp
  src/launches_config.cpp
  src/perception_config.cpp
  src/perception.cpp
  src/perception_widget.cpp
  include/moveit_setup_app_plugins/launches_widget.hpp
  include/moveit_setup_app_plugins/perception_widget.hpp)
target_include_directories(
  moveit_setup_app_plugins
  PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
         $<INSTALL_INTERFACE:include/moveit_setup_app_plugins>)
target_link_libraries(
  moveit_setup_app_plugins
  ament_index_cpp::ament_index_cpp
  moveit_ros_visualization::moveit_ros_visualization
  moveit_setup_framework::moveit_setup_framework
  pluginlib::pluginlib
  rclcpp::rclcpp)

if(BUILD_TESTING)
  find_package(ament_cmake_gtest REQUIRED)
  ament_add_gtest(test_perception test/test_perception.cpp)
  target_link_libraries(test_perception moveit_setup_app_plugins)
endif()

install(DIRECTORY templates DESTINATION share/moveit_setup_app_plugins)

install(
  TARGETS moveit_setup_app_plugins
  EXPORT moveit_setup_app_pluginsTargets
  ARCHIVE DESTINATION lib
  LIBRARY DESTINATION lib
  RUNTIME DESTINATION bin
  INCLUDES
  DESTINATION include/moveit_setup_app_plugins)
install(FILES moveit_setup_framework_plugins.xml
        DESTINATION share/moveit_setup_app_plugins)
install(DIRECTORY include/ DESTINATION include/moveit_setup_app_plugins)

ament_export_targets(moveit_setup_app_pluginsTargets HAS_LIBRARY_TARGET)
pluginlib_export_plugin_description_file(moveit_setup_framework
                                         moveit_setup_framework_plugins.xml)

ament_package()
