cmake_minimum_required(VERSION 3.10)
project(laser_geometry)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
include(CTest)

# Find dependencies
find_package(Eigen3 REQUIRED)
find_package(GTest REQUIRED)

# Add subdirectories for message dependencies
# if (NOT TARGET sensor_msgs)
#     add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../common_msgs/sensor_msgs ${CMAKE_BINARY_DIR}/sensor_msgs_build)
# endif()

# if (NOT TARGET geometry_msgs)
#     add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../common_msgs/geometry_msgs ${CMAKE_BINARY_DIR}/geometry_msgs_build)
# endif()

# if (NOT TARGET robot_time)
#     add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../robot_time ${CMAKE_BINARY_DIR}/robot_time_build)
# endif()

# Include directories
include_directories(
    include
    ${EIGEN3_INCLUDE_DIRS}
    ${GTEST_INCLUDE_DIRS}
)

# Create library
add_library(laser_geometry SHARED src/laser_geometry.cpp)

target_include_directories(laser_geometry
    PUBLIC
        $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
        $<INSTALL_INTERFACE:include/${PROJECT_NAME}>
)

target_link_libraries(laser_geometry PUBLIC
    sensor_msgs
    geometry_msgs
    robot_time
    tf3
    data_convert
)

if(TARGET Eigen3::Eigen)
    target_link_libraries(laser_geometry PUBLIC Eigen3::Eigen)
else()
    target_include_directories(laser_geometry PUBLIC ${EIGEN3_INCLUDE_DIRS})
endif()

# Causes the visibility macros to use dllexport rather than dllimport,
# which is appropriate when building the dll but not consuming it.
target_compile_definitions(laser_geometry PRIVATE "LASER_GEOMETRY_BUILDING_LIBRARY")

# Install targets
install(
    TARGETS laser_geometry
    # EXPORT laser_geometry
    ARCHIVE DESTINATION lib
    LIBRARY DESTINATION lib
    RUNTIME DESTINATION bin
    INCLUDES DESTINATION include
)

install(
    DIRECTORY include/
    DESTINATION include/${PROJECT_NAME}
)

# Tests
if(BUILD_TESTING)
    enable_testing()
    
    add_executable(projection_test test/projection_test.cpp)
    target_link_libraries(projection_test
        laser_geometry
        GTest::GTest
        GTest::Main
        pthread
    )
    add_test(NAME projection_test COMMAND projection_test)
endif()
