cmake_minimum_required(VERSION 3.0.2)
project(lane_route_planner VERSION 0.1.0 LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

if(DEFINED CATKIN_DEVEL_PREFIX OR DEFINED CATKIN_TOPLEVEL)
  set(BUILDING_WITH_CATKIN TRUE)
  find_package(catkin REQUIRED COMPONENTS
    lane_route_core
    robot_costmap_2d
    robot_nav_core
    robot_cpp
    robot_geometry_msgs
    robot_nav_msgs
    robot_protocol_msgs
    data_convert
    robot_time
  )
  catkin_package(
    INCLUDE_DIRS include
    LIBRARIES lane_route_planner
    CATKIN_DEPENDS lane_route_core robot_costmap_2d robot_nav_core robot_cpp robot_geometry_msgs
                   robot_nav_msgs robot_protocol_msgs data_convert robot_time
  )
else()
  set(BUILDING_WITH_CATKIN FALSE)
  set(CMAKE_POSITION_INDEPENDENT_CODE ON)
  # In a standalone workspace these dependencies are targets exported by the parent project.
  # Installed dependencies can instead be resolved by the standard include/library search paths.
  if(NOT TARGET lane_route_core)
    find_path(LANE_ROUTE_CORE_INCLUDE_DIR lane_route_core/route_planner.h)
    find_library(LANE_ROUTE_CORE_LIBRARY NAMES lane_route_core)
    if(NOT LANE_ROUTE_CORE_INCLUDE_DIR OR NOT LANE_ROUTE_CORE_LIBRARY)
      message(FATAL_ERROR "lane_route_core headers and library are required for standalone mode")
    endif()
  else()
    set(LANE_ROUTE_CORE_LIBRARY lane_route_core)
  endif()
endif()

find_package(Boost REQUIRED COMPONENTS system filesystem thread)

add_library(lane_route_planner SHARED
  src/lane_route_planner.cpp
  src/lane_route_session.cpp
)
target_include_directories(lane_route_planner PUBLIC
  $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
  $<INSTALL_INTERFACE:include>
  ${catkin_INCLUDE_DIRS}
  ${LANE_ROUTE_CORE_INCLUDE_DIR}
)
if(BUILDING_WITH_CATKIN)
  target_link_libraries(lane_route_planner
    PUBLIC ${catkin_LIBRARIES}
    PRIVATE Boost::system Boost::filesystem Boost::thread
  )
  add_dependencies(lane_route_planner ${catkin_EXPORTED_TARGETS})
else()
  target_link_libraries(lane_route_planner
    PUBLIC ${LANE_ROUTE_CORE_LIBRARY} robot_costmap_2d robot_cpp robot_time
    PRIVATE Boost::system Boost::filesystem Boost::thread
  )
  # These are header-only in this workspace; link targets only when a parent provides them.
  foreach(header_target robot_nav_core robot_geometry_msgs robot_nav_msgs robot_protocol_msgs data_convert)
    if(TARGET ${header_target})
      target_link_libraries(lane_route_planner PUBLIC ${header_target})
    endif()
  endforeach()
  # PluginLoaderHelper searches only the top of the build directory, not its subdirectories.
  # Without this the .so stays in the subdirectory and move_base2 cannot load LaneRoutePlanner.
  set_target_properties(lane_route_planner PROPERTIES
    LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}
    BUILD_RPATH "${CMAKE_BINARY_DIR}"
  )
endif()
target_compile_options(lane_route_planner PRIVATE -O3)

# Tests are OFF by default in both build modes; enable with -DBUILD_LANE_ROUTE_PLANNER_TESTS=ON.
option(BUILD_LANE_ROUTE_PLANNER_TESTS "Build adapter tests" OFF)
if(BUILD_LANE_ROUTE_PLANNER_TESTS)
  find_package(GTest REQUIRED)
  find_package(Threads REQUIRED)
  find_package(yaml-cpp REQUIRED)
  if(TARGET yaml-cpp::yaml-cpp)
    set(LANE_ROUTE_YAML_TARGET yaml-cpp::yaml-cpp)
  else()
    set(LANE_ROUTE_YAML_TARGET yaml-cpp)
  endif()
  add_executable(lane_route_planner_test test/lane_route_planner_test.cpp)
  target_link_libraries(lane_route_planner_test PRIVATE
    lane_route_planner GTest::GTest GTest::Main Threads::Threads ${LANE_ROUTE_YAML_TARGET}
  )
endif()

if(BUILDING_WITH_CATKIN)
  install(TARGETS lane_route_planner
    ARCHIVE DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
    LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
    RUNTIME DESTINATION ${CATKIN_GLOBAL_BIN_DESTINATION}
  )
  install(DIRECTORY include/lane_route_planner DESTINATION ${CATKIN_GLOBAL_INCLUDE_DESTINATION})
else()
  install(TARGETS lane_route_planner
    ARCHIVE DESTINATION lib
    LIBRARY DESTINATION lib
    RUNTIME DESTINATION bin
  )
  install(DIRECTORY include/lane_route_planner DESTINATION include)
endif()
