# MIT License
#
# Copyright (c) 2025 Meher V.R. Malladi.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
# documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
# persons to whom the Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
# Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

set(RKO_LIO_SCALAR
    "float"
    CACHE STRING "Floating point type used throughout rko_lio (float or double)")
message(STATUS "rko_lio scalar type: ${RKO_LIO_SCALAR}")

set(GENERATED_INCLUDE_DIR ${CMAKE_CURRENT_BINARY_DIR}/include)
configure_file(scalar.hpp.in ${GENERATED_INCLUDE_DIR}/rko_lio/core/scalar.hpp @ONLY)

add_library(rko_lio.core STATIC)

add_library(rko_lio::core ALIAS rko_lio.core)

compat_target_sources(
  rko_lio.core
  PRIVATE lio.cpp
          deskew.cpp
          voxel_hash_map.cpp
          voxel_down_sample.cpp
          process_timestamps.cpp
          preprocess_scan.cpp
  PUBLIC
  FILE_SET
  HEADERS
  FILES ../../rko_lio/core/deskew.hpp
        ../../rko_lio/core/error.hpp
        ../../rko_lio/core/lio.hpp
        ../../rko_lio/core/preprocess_scan.hpp
        ../../rko_lio/core/process_timestamps.hpp
        ../../rko_lio/core/profiler.hpp
        ../../rko_lio/core/voxel_hash_map.hpp
        ../../rko_lio/core/util.hpp
        ../../rko_lio/core/voxel_down_sample.hpp
  BASE_DIRS ../..)

# the generated header lives in the build tree, so it needs its own base dir
if(CMAKE_VERSION VERSION_GREATER_EQUAL "3.23")
  target_sources(
    rko_lio.core
    PUBLIC FILE_SET
           generated_headers
           TYPE
           HEADERS
           BASE_DIRS
           ${GENERATED_INCLUDE_DIR}
           FILES
           ${GENERATED_INCLUDE_DIR}/rko_lio/core/scalar.hpp)
else()
  target_include_directories(rko_lio.core PUBLIC $<BUILD_INTERFACE:${GENERATED_INCLUDE_DIR}>)
endif()

target_link_libraries(
  rko_lio.core
  PUBLIC TBB::tbb
         tsl::robin_map
         Sophus::Sophus
         Eigen3::Eigen
         spdlog::spdlog)

target_compile_features(rko_lio.core PUBLIC cxx_std_20)

set_target_properties(rko_lio.core PROPERTIES POSITION_INDEPENDENT_CODE ON EXPORT_NAME core)
