cmake_minimum_required(VERSION 3.16)
project(maixsense_d640_driver VERSION 0.1.0 LANGUAGES CXX)

find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(sensor_msgs REQUIRED)
find_package(stereo_msgs REQUIRED)
find_package(geometry_msgs REQUIRED)
find_package(std_msgs REQUIRED)
find_package(std_srvs REQUIRED)
find_package(diagnostic_msgs REQUIRED)
find_package(tf2_ros REQUIRED)
find_package(OpenCV REQUIRED COMPONENTS core imgproc imgcodecs calib3d)
find_package(MaixStereoDepth CONFIG REQUIRED COMPONENTS opencv)

add_executable(maixsense_d640_driver src/driver_node.cpp)
target_compile_features(maixsense_d640_driver PRIVATE cxx_std_17)
target_compile_options(maixsense_d640_driver PRIVATE -Wall -Wextra -Wpedantic)
target_link_libraries(maixsense_d640_driver
    MaixStereoDepth::maixsense_d640 MaixStereoDepth::opencv ${OpenCV_LIBS})
ament_target_dependencies(maixsense_d640_driver
    rclcpp sensor_msgs stereo_msgs geometry_msgs std_msgs std_srvs diagnostic_msgs tf2_ros)

# Optional component: requesting it requires rclcpp_components explicitly.
# Standalone-only builds retain no compile-time dependency on the component API.
option(MSD_ROS_BUILD_COMPONENT "Build the optional rclcpp component plugin" OFF)
if(MSD_ROS_BUILD_COMPONENT)
    find_package(rclcpp_components REQUIRED)
    add_library(maixsense_d640_component SHARED src/driver_node.cpp)
    target_compile_definitions(maixsense_d640_component PRIVATE MSD_COMPONENT_BUILD=1)
    target_compile_features(maixsense_d640_component PRIVATE cxx_std_17)
    target_compile_options(maixsense_d640_component PRIVATE -Wall -Wextra -Wpedantic)
    target_link_libraries(maixsense_d640_component
        MaixStereoDepth::maixsense_d640 MaixStereoDepth::opencv ${OpenCV_LIBS})
    ament_target_dependencies(maixsense_d640_component rclcpp rclcpp_components
        sensor_msgs stereo_msgs geometry_msgs std_msgs std_srvs diagnostic_msgs tf2_ros)
    rclcpp_components_register_nodes(maixsense_d640_component "maixsense_d640_ros::Driver")
    install(TARGETS maixsense_d640_component ARCHIVE DESTINATION lib LIBRARY DESTINATION lib RUNTIME DESTINATION bin)
endif()

# Keep a ROS installation runnable after it leaves the build environment.  The
# SDK target's imported location is the production prefix selected by
# CMAKE_PREFIX_PATH, so this does not hard-code a developer checkout path.
if(UNIX AND NOT APPLE)
    # Imported targets exported by the SDK carry their location in a
    # configuration-specific property (IMPORTED_LOCATION_RELWITHDEBINFO for the
    # desktop preset, _RELEASE for production). Looking only at _RELEASE leaves
    # RelWithDebInfo installs without a RUNPATH, so walk every exported
    # configuration and fall back to the configuration-agnostic property.
    set(MSD_SDK_LIBRARY "")
    get_target_property(MSD_SDK_LIBRARY
        MaixStereoDepth::maixsense_d640 IMPORTED_LOCATION)
    if(NOT MSD_SDK_LIBRARY)
        get_target_property(MSD_SDK_CONFIGS
            MaixStereoDepth::maixsense_d640 IMPORTED_CONFIGURATIONS)
        if(NOT MSD_SDK_CONFIGS)
            set(MSD_SDK_CONFIGS NOCONFIG)
        endif()
        foreach(config IN LISTS MSD_SDK_CONFIGS)
            get_target_property(MSD_SDK_LIBRARY
                MaixStereoDepth::maixsense_d640 IMPORTED_LOCATION_${config})
            if(MSD_SDK_LIBRARY)
                break()
            endif()
        endforeach()
    endif()
    if(MSD_SDK_LIBRARY)
        get_filename_component(MSD_SDK_LIBRARY_DIR "${MSD_SDK_LIBRARY}" DIRECTORY)
        set_target_properties(maixsense_d640_driver PROPERTIES
            INSTALL_RPATH "${MSD_SDK_LIBRARY_DIR}")
        if(MSD_ROS_BUILD_COMPONENT)
            set_target_properties(maixsense_d640_component PROPERTIES INSTALL_RPATH "${MSD_SDK_LIBRARY_DIR}")
        endif()
    endif()
endif()

install(TARGETS maixsense_d640_driver DESTINATION lib/${PROJECT_NAME})
install(DIRECTORY launch config DESTINATION share/${PROJECT_NAME}
    PATTERN "__pycache__" EXCLUDE PATTERN "*.pyc" EXCLUDE)
install(PROGRAMS scripts/d640 DESTINATION lib/${PROJECT_NAME})
# Descriptive public command; retain d640 for existing workflows.
install(PROGRAMS scripts/d640 DESTINATION lib/${PROJECT_NAME} RENAME maixsense_d640)
if(BUILD_TESTING)
    enable_testing()
    add_executable(driver_contract_tests tests/driver_contract_tests.cpp)
    target_compile_features(driver_contract_tests PRIVATE cxx_std_17)
    target_include_directories(driver_contract_tests PRIVATE src)
    target_compile_options(driver_contract_tests PRIVATE -Wall -Wextra -Wpedantic -UNDEBUG)
    add_test(NAME driver_contract_tests COMMAND driver_contract_tests)
    find_package(Python3 REQUIRED COMPONENTS Interpreter)
    add_test(NAME driver_workflow COMMAND ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/tests/test_workflow.py)
    if(MSD_ROS_BUILD_COMPONENT)
        find_package(class_loader REQUIRED)
        add_executable(driver_component_probe tests/component_probe.cpp)
        target_compile_features(driver_component_probe PRIVATE cxx_std_17)
        target_compile_options(driver_component_probe PRIVATE -Wall -Wextra -Wpedantic)
        ament_target_dependencies(driver_component_probe rclcpp rclcpp_components class_loader sensor_msgs std_msgs)
        add_test(NAME driver_component_factory COMMAND driver_component_probe $<TARGET_FILE:maixsense_d640_component>)
        set_tests_properties(driver_component_factory PROPERTIES TIMEOUT 15
            ENVIRONMENT "ROS_LOCALHOST_ONLY=1;ROS_DOMAIN_ID=93")
    endif()
endif()
ament_package()
