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

if(DEFINED CATKIN_DEVEL_PREFIX OR DEFINED CATKIN_TOPLEVEL)
  set(BUILDING_WITH_CATKIN TRUE)
else()
  set(BUILDING_WITH_CATKIN FALSE)
endif()

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

# The tests and the eval tool build coarse graphs with robot_costmap_2d. The library itself does not use it.
# Tests are OFF by default in both build modes; enable with -DBUILD_LANE_ROUTE_TESTS=ON.
option(BUILD_LANE_ROUTE_TESTS "Build lane_route_core tests and eval tool" OFF)

if(BUILDING_WITH_CATKIN)
  find_package(catkin REQUIRED COMPONENTS robot_costmap_2d)
  catkin_package(
    INCLUDE_DIRS include
    LIBRARIES lane_route_core
    CATKIN_DEPENDS robot_costmap_2d
  )
else()
  set(CMAKE_POSITION_INDEPENDENT_CODE ON)
endif()

add_library(lane_route_core SHARED
  src/clothoid.cpp
  src/graph_compiler.cpp
  src/grid_checks.cpp
  src/lattice_cspace.cpp
  src/lattice_heuristic.cpp
  src/lattice_search.cpp
  src/lattice_smoother.cpp
  src/lattice_tier.cpp
  src/path_verifier.cpp
  src/route_graph_path.cpp
  src/route_graph_search.cpp
  src/route_graph_tier.cpp
  src/route_planner.cpp
  src/standability.cpp
  src/swing_chain.cpp
)
target_include_directories(lane_route_core PUBLIC
  $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
  $<INSTALL_INTERFACE:include>
)
# The workspace builds Debug by default. This runs on the planner thread of a 2-core NUC, so it stays optimised.
target_compile_options(lane_route_core PRIVATE -O3)
if(NOT BUILDING_WITH_CATKIN)
  # Same output directory as the other standalone libraries, so build_robot ships as one flat directory.
  set_target_properties(lane_route_core PROPERTIES
    LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}
    BUILD_RPATH "${CMAKE_BINARY_DIR}"
  )
endif()

if(BUILD_LANE_ROUTE_TESTS)
  find_package(GTest REQUIRED)
  if(BUILDING_WITH_CATKIN)
    set(COSTMAP_INCLUDES ${catkin_INCLUDE_DIRS})
    set(COSTMAP_LIBRARIES ${catkin_LIBRARIES})
  else()
    set(COSTMAP_LIBRARIES robot_costmap_2d)
  endif()

  add_executable(lane_route_core_test test/lane_route_core_test.cpp)
  target_include_directories(lane_route_core_test PRIVATE tools ${COSTMAP_INCLUDES})
  target_link_libraries(lane_route_core_test PRIVATE lane_route_core ${COSTMAP_LIBRARIES} GTest::GTest GTest::Main)

  add_executable(lane_route_eval tools/lane_route_eval.cpp)
  target_include_directories(lane_route_eval PRIVATE tools ${COSTMAP_INCLUDES})
  target_link_libraries(lane_route_eval PRIVATE lane_route_core ${COSTMAP_LIBRARIES})
  target_compile_options(lane_route_eval PRIVATE -O2)
endif()

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