cmake_minimum_required(VERSION 3.8)
project(camera)

if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
  add_compile_options(-Wall -Wextra -Wpedantic)
endif()

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(sensor_msgs REQUIRED)
find_package(OpenCV REQUIRED)

set(HOBOT_INCLUDE_DIR "/usr/hobot/include")
set(HOBOT_LIB_DIR "/usr/hobot/lib")
set(HOBOT_CJSON_INCLUDE_DIR "/usr/include/cjson")

# Allow overriding Hobot backend enablement
option(CAMERA_ENABLE_HOBOT "Enable Hobot VIO/VFLOW backend" ON)

# Robust Hobot detection for this board:
# - Libraries live in /usr/hobot/lib (sample Makefile links libmultimedia.so)
# - Some headers live in /usr/include (e.g. sp_vio.h)
set(HAVE_HOBOT FALSE)
if(CAMERA_ENABLE_HOBOT AND EXISTS "${HOBOT_LIB_DIR}/libmultimedia.so")
  set(HAVE_HOBOT TRUE)
endif()

message(STATUS "CAMERA_ENABLE_HOBOT=${CAMERA_ENABLE_HOBOT} HAVE_HOBOT=${HAVE_HOBOT} HOBOT_INCLUDE_DIR=${HOBOT_INCLUDE_DIR} HOBOT_LIB_DIR=${HOBOT_LIB_DIR}")

include_directories(
  include
  ${OpenCV_INCLUDE_DIRS}
)

add_executable(camera_node
  src/camera_node.cpp
  src/mipi_vflow_backend.cpp
)

ament_target_dependencies(camera_node
  rclcpp
  sensor_msgs
)

target_link_libraries(camera_node
  ${OpenCV_LIBS}
)

target_compile_definitions(camera_node PRIVATE
  $<$<BOOL:${HAVE_HOBOT}>:CAMERA_HAVE_HOBOT=1>
)

if(HAVE_HOBOT)
  target_include_directories(camera_node PRIVATE
    ${HOBOT_INCLUDE_DIR}
    ${HOBOT_INCLUDE_DIR}/aarch64-linux-gnu
    /usr/include
    ${HOBOT_CJSON_INCLUDE_DIR}
    /app/multimedia_samples/utils
    /app/multimedia_samples
    /app/multimedia_samples/vp_sensors
    /app/multimedia_samples/vp_sensors/include
  )

  file(GLOB_RECURSE VP_SENSORS_SRCS
    /app/multimedia_samples/vp_sensors/*.c
  )

  if(VP_SENSORS_SRCS)
    target_sources(camera_node PRIVATE ${VP_SENSORS_SRCS})
  else()
    message(WARNING "vp_sensors sources not found under /app/multimedia_samples/vp_sensors; MIPI backend may fail to link")
  endif()

  target_link_directories(camera_node PRIVATE
    ${HOBOT_LIB_DIR}
  )

  target_link_libraries(camera_node
    vpf
    vio
    cam
    hbmem
    multimedia
  )
endif()

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

ament_package()

