cmake_minimum_required(VERSION 3.16)
project(controller_manager LANGUAGES CXX)

find_package(ros2_control_cmake REQUIRED)
set_compiler_options()
export_windows_symbols()

set(THIS_PACKAGE_INCLUDE_DEPENDS
  controller_interface
  controller_manager_msgs
  diagnostic_updater
  fmt
  generate_parameter_library
  hardware_interface
  libstatistics_collector
  lifecycle_msgs
  pluginlib
  rclcpp
  realtime_tools
  std_msgs
)

find_package(ament_cmake REQUIRED)
find_package(ament_cmake_python REQUIRED)
find_package(ament_cmake_core REQUIRED)
find_package(ament_cmake_gen_version_h REQUIRED)
find_package(backward_ros REQUIRED)
foreach(Dependency IN ITEMS ${THIS_PACKAGE_INCLUDE_DEPENDS})
  find_package(${Dependency} REQUIRED)
endforeach()

generate_parameter_library(controller_manager_parameters
  src/controller_manager_parameters.yaml
)

add_library(controller_manager SHARED
  src/controller_manager.cpp
)
target_compile_features(controller_manager PUBLIC cxx_std_17)
target_include_directories(controller_manager PUBLIC
  $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
  $<INSTALL_INTERFACE:include/controller_manager>
)
target_link_libraries(controller_manager PUBLIC
                      controller_manager_parameters
                      controller_interface::controller_interface
                      diagnostic_updater::diagnostic_updater
                      hardware_interface::hardware_interface
                      pluginlib::pluginlib
                      rclcpp::rclcpp
                      rclcpp_lifecycle::rclcpp_lifecycle
                      realtime_tools::realtime_tools
                      libstatistics_collector::libstatistics_collector
                      fmt::fmt
                      ${std_msgs_TARGETS}
                      ${controller_manager_msgs_TARGETS})

add_executable(ros2_control_node src/ros2_control_node.cpp)
target_link_libraries(ros2_control_node PRIVATE
  controller_manager
)

if(BUILD_TESTING)
  find_package(ament_cmake_gmock REQUIRED)
  find_package(ros2_control_test_assets REQUIRED)
  find_package(example_interfaces REQUIRED)

  # Plugin Libraries that are built and installed for use in testing
  add_library(test_controller SHARED test/test_controller/test_controller.cpp)
  target_link_libraries(test_controller PUBLIC
                        controller_manager
                        ${example_interfaces_TARGETS})
  pluginlib_export_plugin_description_file(controller_interface test/test_controller/test_controller.xml)
  install(
    TARGETS test_controller
    DESTINATION lib
  )

  add_library(test_controller_failed_init SHARED
    test/test_controller_failed_init/test_controller_failed_init.cpp
  )
  target_link_libraries(test_controller_failed_init PUBLIC controller_manager)
  pluginlib_export_plugin_description_file(
    controller_interface test/test_controller_failed_init/test_controller_failed_init.xml)
  install(
    TARGETS test_controller_failed_init
    DESTINATION lib
  )

  add_library(test_chainable_controller SHARED
    test/test_chainable_controller/test_chainable_controller.cpp
  )
  target_link_libraries(test_chainable_controller PUBLIC
                        controller_manager
                        realtime_tools::realtime_tools)
  pluginlib_export_plugin_description_file(
    controller_interface test/test_chainable_controller/test_chainable_controller.xml)
  install(
    TARGETS test_chainable_controller
    DESTINATION lib
  )

  ament_add_gmock(test_controller_manager
    test/test_controller_manager.cpp
    TIMEOUT 180
  )
  target_link_libraries(test_controller_manager
    controller_manager
    test_controller
    test_chainable_controller
    ros2_control_test_assets::ros2_control_test_assets
  )

  ament_add_gmock(test_controller_manager_with_namespace
    test/test_controller_manager_with_namespace.cpp
  )
  target_link_libraries(test_controller_manager_with_namespace
    controller_manager
    test_controller
    ros2_control_test_assets::ros2_control_test_assets
  )

  ament_add_gmock(test_controller_manager_hardware_error_handling
    test/test_controller_manager_hardware_error_handling.cpp
  )
  target_link_libraries(test_controller_manager_hardware_error_handling
    controller_manager
    test_controller
    ros2_control_test_assets::ros2_control_test_assets
  )

  ament_add_gmock(test_load_controller
    test/test_load_controller.cpp
    APPEND_ENV AMENT_PREFIX_PATH=${ament_index_build_path}_$<CONFIG>
  )
  target_link_libraries(test_load_controller
    controller_manager
    test_controller
    test_controller_failed_init
    ros2_control_test_assets::ros2_control_test_assets
  )

  ament_add_gmock(test_cleanup_controller
    test/test_cleanup_controller.cpp
    APPEND_ENV AMENT_PREFIX_PATH=${ament_index_build_path}_$<CONFIG>
  )
  target_link_libraries(test_cleanup_controller
    controller_manager
    test_controller
    test_controller_failed_init
    ros2_control_test_assets::ros2_control_test_assets
  )

  ament_add_gmock(test_controllers_chaining_with_controller_manager
    test/test_controllers_chaining_with_controller_manager.cpp
  )
  target_link_libraries(test_controllers_chaining_with_controller_manager
    controller_manager
    test_chainable_controller
    test_controller
    ros2_control_test_assets::ros2_control_test_assets
  )

  ament_add_gmock(test_controller_manager_srvs
    test/test_controller_manager_srvs.cpp
    APPEND_ENV AMENT_PREFIX_PATH=${ament_index_build_path}_$<CONFIG>
  )
  target_link_libraries(test_controller_manager_srvs
    controller_manager
    test_controller
    test_chainable_controller
    ros2_control_test_assets::ros2_control_test_assets
    ${controller_manager_msgs_TARGETS}
  )
  set_tests_properties(test_controller_manager_srvs PROPERTIES TIMEOUT 120)

  ament_add_gmock(test_controller_manager_urdf_passing
    test/test_controller_manager_urdf_passing.cpp
  )
  target_link_libraries(test_controller_manager_urdf_passing
    controller_manager
    test_controller
    ros2_control_test_assets::ros2_control_test_assets
    ${controller_manager_msgs_TARGETS}
  )

  add_library(test_controller_with_interfaces SHARED
    test/test_controller_with_interfaces/test_controller_with_interfaces.cpp
  )
  target_link_libraries(test_controller_with_interfaces PUBLIC
    controller_manager
  )
  pluginlib_export_plugin_description_file(
    controller_interface test/test_controller_with_interfaces/test_controller_with_interfaces.xml)
  install(
    TARGETS test_controller_with_interfaces
    DESTINATION lib
  )

  add_library(test_controller_failed_activate SHARED
  test/test_controller_failed_activate/test_controller_failed_activate.cpp
  )
  target_link_libraries(test_controller_failed_activate PUBLIC
    controller_manager
  )
  target_compile_definitions(test_controller_failed_activate PRIVATE "CONTROLLER_MANAGER_BUILDING_DLL")
  pluginlib_export_plugin_description_file(
    controller_interface test/test_controller_failed_activate/test_controller_failed_activate.xml)
  install(
    TARGETS test_controller_failed_activate
    DESTINATION lib
  )

  ament_add_gmock(test_release_interfaces
    test/test_release_interfaces.cpp
    APPEND_ENV AMENT_PREFIX_PATH=${ament_index_build_path}_$<CONFIG>
  )
  target_link_libraries(test_release_interfaces
    controller_manager
    test_controller
    test_controller_with_interfaces
    test_controller_failed_activate
    ros2_control_test_assets::ros2_control_test_assets
  )

  ament_add_gmock(test_spawner_unspawner
    test/test_spawner_unspawner.cpp
    TIMEOUT 180
  )
  target_link_libraries(test_spawner_unspawner
    controller_manager
    test_controller
    ros2_control_test_assets::ros2_control_test_assets
  )
  target_compile_definitions(
    test_spawner_unspawner
    PRIVATE PARAMETERS_FILE_PATH="${CMAKE_CURRENT_LIST_DIR}/test/")

  ament_add_gmock(test_advanced_spawner
    test/test_advanced_spawner.cpp
    TIMEOUT 180
  )
  target_link_libraries(test_advanced_spawner
    controller_manager
    test_controller
    ros2_control_test_assets::ros2_control_test_assets
  )
  target_compile_definitions(
    test_advanced_spawner
    PRIVATE PARAMETERS_FILE_PATH="${CMAKE_CURRENT_LIST_DIR}/test/")

  ament_add_gmock(test_hardware_spawner
    test/test_hardware_spawner.cpp
    TIMEOUT 120
  )
  target_link_libraries(test_hardware_spawner
    controller_manager
    test_controller
    ros2_control_test_assets::ros2_control_test_assets
  )

  ament_add_gmock(test_hardware_management_srvs
    test/test_hardware_management_srvs.cpp
  )
  target_link_libraries(test_hardware_management_srvs
    controller_manager
    test_controller
    ros2_control_test_assets::ros2_control_test_assets
    ${controller_manager_msgs_TARGETS}
  )

  find_package(ament_cmake_pytest REQUIRED)
  install(FILES test/test_ros2_control_node.yaml
    DESTINATION test)
  ament_add_pytest_test(test_ros2_control_node test/test_ros2_control_node_launch.py)
  ament_add_pytest_test(test_test_utils test/test_test_utils.py)
endif()

install(
  DIRECTORY include/
  DESTINATION include/controller_manager
)
install(
  TARGETS controller_manager controller_manager_parameters
  EXPORT export_controller_manager
  RUNTIME DESTINATION bin
  LIBRARY DESTINATION lib
  ARCHIVE DESTINATION lib
)
install(
  TARGETS ros2_control_node
  RUNTIME DESTINATION lib/controller_manager
)

ament_python_install_package(controller_manager
  SCRIPTS_DESTINATION lib/controller_manager
)
ament_export_targets(export_controller_manager HAS_LIBRARY_TARGET)
ament_export_dependencies(${THIS_PACKAGE_INCLUDE_DEPENDS})
ament_package()
ament_generate_version_header(${PROJECT_NAME})
