cmake_minimum_required(VERSION 3.5)
project(ros2_control_can)

# Default to C99
if(NOT CMAKE_C_STANDARD)
  set(CMAKE_C_STANDARD 99)
endif()


# Default to C++14
if(NOT CMAKE_CXX_STANDARD)
  set(CMAKE_CXX_STANDARD 14)
endif()

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

find_package(ament_cmake REQUIRED)
find_package(hardware_interface REQUIRED)
find_package(controller_manager REQUIRED)
find_package(rclcpp REQUIRED)
find_package(rclcpp_lifecycle REQUIRED)
find_package(pluginlib REQUIRED)

set( GENERATED_CAN_HEADER
  ${CMAKE_CURRENT_SOURCE_DIR}/include/can1__main_ft24.h
)

set( 
  GENERATED_CAN_SRC
  ${CMAKE_CURRENT_SOURCE_DIR}/src/can1__main_ft24.cpp
)

add_custom_command(OUTPUT ${GENERATED_CAN_HEADER} ${GENERATED_CAN_SRC}
  COMMAND python3 -m cantools generate_c_source --prune ${CMAKE_CURRENT_SOURCE_DIR}/dbc/CAN1-MainFT24.dbc
  COMMAND mv ${CMAKE_CURRENT_SOURCE_DIR}/src/can1__main_ft24.h ${CMAKE_CURRENT_SOURCE_DIR}/include/can1__main_ft24.h
  COMMAND mv ${CMAKE_CURRENT_SOURCE_DIR}/src/can1__main_ft24.c ${CMAKE_CURRENT_SOURCE_DIR}/src/can1__main_ft24.cpp
  WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/src"
  DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/dbc/CAN1-MainFT24.dbc"
  PRE_BUILD
  COMMENT "Generating CAN header files and code from DBC file"
  VERBATIM
)

add_library(
  ros2_control_can
  SHARED 
  src/ros2_control_can.cpp
  src/wheel.cpp
  ${GENERATED_CAN_HEADER}
  ${GENERATED_CAN_SRC}
)

target_include_directories(
  ros2_control_can
  PRIVATE
  include
)

ament_target_dependencies(
  ros2_control_can
  hardware_interface
  controller_manager
  rclcpp
  rclcpp_lifecycle
  pluginlib
)

pluginlib_export_plugin_description_file(
  hardware_interface 
  robot_hardware.xml
)

install(
  TARGETS ros2_control_can
  DESTINATION lib
)

ament_export_libraries(
  ros2_control_can
)

ament_export_dependencies(
  hardware_interface
  controller_manager
  rclcpp
  pluginlib
)

ament_package()


