cmake_minimum_required(VERSION 3.16)
project(smw_contention_guard VERSION 0.1.0 LANGUAGES C CXX)

if(NOT (${CMAKE_CXX_COMPILER_ID} STREQUAL "Clang" OR ${CMAKE_CXX_COMPILER_ID} STREQUAL "GNU"))
  message(FATAL_ERROR "Unsupported compiler: ${CMAKE_CXX_COMPILER_ID}")
endif()

set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_C_EXTENSIONS OFF)

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

add_compile_options(-Wall -Wextra -Wpedantic)

# Configuration options
option(BUILD_DOCS "Build and Install Documentation (Requires Doxygen)" OFF)  # TODO
option(SMW_COMPAT_ROS2_ROLLING "Build with ROS2 Rolling compatibility" OFF)

# find dependencies
find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(smw_base_application REQUIRED)
find_package(smw_util REQUIRED)
find_package(smw_interfaces REQUIRED)
find_package(smw_comm REQUIRED)

set(SMW_DEPS
  rclcpp
  smw_base_application
  smw_util
  smw_interfaces
  smw_comm
)

# Libraries
set(SMW_LIB ${PROJECT_NAME})

add_library(${SMW_LIB} SHARED 
  src/critical_application.cpp
  src/non_critical_application.cpp
  src/contention_guard.cpp
  src/contention_function.cpp
)

target_include_directories(${SMW_LIB}
  PUBLIC include
)

# Define SMW_COMPAT_ROS2_ROLLING compile definition, this will switch on/off
# some feature not compatible with ROS2 Humble.
if(SMW_COMPAT_ROS2_ROLLING)
  target_compile_definitions(${SMW_LIB} PRIVATE SMW_COMPAT_ROS2_ROLLING)
endif()

ament_target_dependencies(${SMW_LIB}
  PUBLIC ${SMW_DEPS}
)

install(TARGETS ${SMW_LIB}
  LIBRARY DESTINATION lib
  ARCHIVE DESTINATION lib
  RUNTIME DESTINATION bin
)

install(DIRECTORY include/
  DESTINATION include/
)

add_executable(contention_guard
  src/main.cpp
)
target_link_libraries(contention_guard
 PRIVATE ${SMW_LIB}
)

install(TARGETS
  contention_guard
  RUNTIME DESTINATION lib/${PROJECT_NAME}
)

# if(BUILD_TESTING)    
#   find_package(ament_lint_auto REQUIRED)
# 
#   set(ament_cmake_copyright_FOUND TRUE)
#   set(ament_cmake_cpplint_FOUND TRUE)
# 
#   ament_lint_auto_find_test_dependencies()
# 
#   add_subdirectory(test)
# endif()

ament_export_include_directories(include)
ament_export_libraries(${SMW_LIB})
ament_export_dependencies(${SMW_DEPS})

ament_package()
