set(SON_PROJECT_NAME tof3d)

message("========== Load son project ${SON_PROJECT_NAME} ==========" )

unset(OPTION_ENABLE_TENSORRT_DETECT_CODE CACHE)
option(OPTION_ENABLE_TENSORRT_DETECT_CODE "" OFF)
message("<=${SON_PROJECT_NAME}=> OPTION_ENABLE_TENSORRT_DETECT_CODE: " ${OPTION_ENABLE_TENSORRT_DETECT_CODE})

if (OPTION_ENABLE_TENSORRT_DETECT_CODE)
    set(CMAKE_CUDA_ARCHITECTURES 60 61 62 70 72 75 86)
    set(CMAKE_CUDA_COMPILER /usr/local/cuda/bin/nvcc)
    project(${SON_PROJECT_NAME} LANGUAGES CXX CUDA)

    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17 -O3")
    set(CMAKE_CXX_STANDARD 17)

    option(CUDA_USE_STATIC_CUDA_RUNTIME OFF)

    # CUDA
    find_package(CUDA REQUIRED)
    message(STATUS "CUDA Libs: \n${CUDA_LIBRARIES}\n")
    get_filename_component(CUDA_LIB_DIR ${CUDA_LIBRARIES} DIRECTORY)
    message(STATUS "CUDA Headers: \n${CUDA_INCLUDE_DIRS}\n")

    # OpenCV
    message(STATUS "OpenCV Libs: \n${OpenCV_LIBS}\n")
    message(STATUS "OpenCV Libraries: \n${OpenCV_LIBRARIES}\n")
    message(STATUS "OpenCV Headers: \n${OpenCV_INCLUDE_DIRS}\n")

    if(${OpenCV_VERSION} VERSION_GREATER_EQUAL 4.7.0)
        message(STATUS "Build with -DBATCHED_NMS")
        add_definitions(-DBATCHED_NMS)
    endif()

    # TensorRT
    set(TensorRT_INCLUDE_DIRS /usr/include/aarch64-linux-gnu)
    set(TensorRT_LIBRARIES /usr/lib/aarch64-linux-gnu)
    message(STATUS "TensorRT Libs: \n${TensorRT_LIBRARIES}\n")
    message(STATUS "TensorRT Headers: \n${TensorRT_INCLUDE_DIRS}\n")

    list(APPEND INCLUDE_DIRS
            ${CUDA_INCLUDE_DIRS}
            ${TensorRT_INCLUDE_DIRS}
    )

    list(APPEND ALL_LIBS
            nvinfer
            nvinfer_plugin
            ${CUDA_LIBRARIES}
            ${CUDA_LIB_DIR}
            ${TensorRT_LIBRARIES}
    )

    aux_source_directory(${CMAKE_CURRENT_LIST_DIR}/detect yolov8_detect)
else ()
    aux_source_directory(${CMAKE_CURRENT_LIST_DIR}/detect/onnx_detect yolov8_detect)
endif ()

find_package(gRPC CONFIG REQUIRED)
find_package(Ceres REQUIRED)

option(OPTION_COMMUNICATION_WITH_PLC "plc通信" OFF)
message("<=${SON_PROJECT_NAME}=> OPTION_COMMUNICATION_WITH_PLC: " ${OPTION_COMMUNICATION_WITH_PLC})

# 获取系统架构
message("SYSTEM: ${CMAKE_SYSTEM_PROCESSOR}")

if (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "aarch64")
        set(NebulaSDK_PATH "${CMAKE_CURRENT_LIST_DIR}/vzense/NebulaSDK/AArch64")
elseif (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "x86_64")
        set(NebulaSDK_PATH "${CMAKE_CURRENT_LIST_DIR}/vzense/NebulaSDK/x86_64")
endif()

include_directories(
        ${INCLUDE_DIRS}
        ${CMAKE_CURRENT_LIST_DIR}
        ${PCL_INCLUDE_DIRS}
        ${OpenCV_INCLUDE_DIRS}
        ${PROTOBUF_INCLUDE_DIR}
        ${CERES_INCLUDE_DIR}
        ${NebulaSDK_PATH}/include
)

aux_source_directory(${CMAKE_CURRENT_LIST_DIR}/proto proto)
aux_source_directory(${CMAKE_CURRENT_LIST_DIR}/vzense vzense)

aux_source_directory(${CMAKE_CURRENT_LIST_DIR}/communication communication)
aux_source_directory(${CMAKE_CURRENT_LIST_DIR}/communication/grpc communication_grpc)
#aux_source_directory(${CMAKE_CURRENT_LIST_DIR}/communication/mqtt communication_mqtt)
aux_source_directory(${CMAKE_CURRENT_LIST_DIR}/communication/rabbitmq communication_rabbitmq)

aux_source_directory(${CMAKE_CURRENT_LIST_DIR}/detect detect)
aux_source_directory(${CMAKE_CURRENT_LIST_DIR}/detect/ceres_detect ceres_detect)

set(SON_PROJECT_SOURCE_LIST ${SON_PROJECT_SOURCE_LIST}
        ${proto}
        ${vzense}
        ${communication}
        ${communication_grpc}
        ${communication_mqtt}
        ${communication_rabbitmq}
        ${detect}
        ${ceres_detect}
        ${yolov8_detect}
        ${CMAKE_CURRENT_LIST_DIR}/tof3DMain.cpp
)

set(SON_PROJECT_DEPEND_LIST ${SON_PROJECT_DEPEND_LIST}
        liblog
        libthread
        librabbitmq
        libmessage
        ${NebulaSDK_PATH}/lib/libNebula_api.so
        ${absl_LIBRARIES}
        protobuf::libprotobuf
        gRPC::grpc++_reflection
        ${OpenCV_LIBRARIES}
        libpcl
        ${CERES_LIBRARIES}
        ${ALL_LIBS}
)

add_executable(${SON_PROJECT_NAME} ${SON_PROJECT_SOURCE_LIST})
target_link_libraries(${SON_PROJECT_NAME} ${SON_PROJECT_DEPEND_LIST})
target_compile_definitions(${SON_PROJECT_NAME} PRIVATE PROJECT_NAME="${SON_PROJECT_NAME}")
if (OPTION_ENABLE_TENSORRT_DETECT_CODE)
        target_compile_definitions(${SON_PROJECT_NAME} PRIVATE ENABLE_TENSORRT_DETECT)
endif ()

install(TARGETS ${SON_PROJECT_NAME}
        LIBRARY DESTINATION lib  # 动态库安装路径
        ARCHIVE DESTINATION lib  # 静态库安装路径
        RUNTIME DESTINATION bin  # 可执行文件安装路径
        PUBLIC_HEADER DESTINATION include  # 头文件安装路径
)

