cmake_minimum_required(VERSION 3.21)
# cmake_policy(SET CMP0146 NEW)

message("NVIDIA NX PLATFORM")
set(PROJECT_NAME VCarContainer)
project(${PROJECT_NAME} VERSION 1.0)


add_definitions(-std=c++11)
add_definitions(-DAPI_EXPORTS)

set(CMAKE_CXX_STANDARD 11)
#set(CMAKE_BUILD_TYPE Debug)
set(CMAKE_BUILD_TYPE Release)
#add_definitions("-Wall -g")

find_package(CUDA REQUIRED)
#message(STATUS "${CUDA_INCLUDE_DIRS}")

#opencv
find_package(OpenCV REQUIRED)
# message(STATUS "${OpenCV_LIBS}")
# message(STATUS "${OpenCV_INCLUDE_DIRS}")



# 设置编译工具
#set(CMAKE_SYSTEM_NAME Linux)
#set(CMAKE_C_COMPILER "gcc")
#set(CMAKE_CXX_COMPILER "g++")
# 设置arm交叉编译工具
 set(CMAKE_SYSTEM_NAME Linux)
 set(CMAKE_SYSTEM_PROCESSOR aarch64)
 set(CMAKE_C_COMPILER "aarch64-linux-gnu-gcc")
 set(CMAKE_CXX_COMPILER "aarch64-linux-gnu-g++")


# 设置项目生成目录
set(EXECUTABLE_OUTPUT_PATH  ${CMAKE_CURRENT_LIST_DIR}/app)

# 设置调用库文件的位置
set(CMAKE_SKIP_BUILD_RPATH FALSE)
set(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE)
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
set(CMAKE_INSTALL_RPATH "./lib")

set(SYS_USR_INCLUDE_DIR "/usr/include")
set(SYS_USR_LIB_DIR "/usr/lib")
set(SYS_USR_LOCAL_INCLUDE_DIR "/usr/local/include")
set(SYS_USR_LOCAL_LIB_DIR "/usr/local/lib")
#set(AARCH64_LINUX_INCLUDE_DIR "/usr/include/x86_64-linux-gnu")
#set(AARCH64_LINUX_LIB_DIR "/usr/lib/x86_64-linux-gnu")
 set(AARCH64_LINUX_INCLUDE_DIR "/usr/include/aarch64-linux-gnu")
 set(AARCH64_LINUX_LIB_DIR "/usr/lib/aarch64-linux-gnu")

#opencv3.2.0的库文件在/usr/lib/aarch64-linux-gnu下 头文件路径在/usr/include/opencv2
#opencv4.5.5的库文件在/usr/local/lib下 头文件路径在/usr/local/include/opencv4
#目前使用最新版opencv4.5.5
set(OPENCV_INCLUDE_DIR  ${SYS_USR_LOCAL_INCLUDE_DIR}/opencv4)
set(OPENCV_LIB_DIR  ${SYS_USR_LOCAL_LIB_DIR})

#set(CUDA_DIR  "/usr/local/cuda-11.2")
set(CUDA_DIR  "/usr/local/cuda-10.2")
set(CUDA_INCLUDE_DIR  ${CUDA_DIR}/include)
set(CUDA_LIB_DIR  ${CUDA_DIR}/lib64)

set(TENSORRT_INCLUDE_DIR  ${AARCH64_LINUX_INCLUDE_DIR})  #tensorrt的头文件在/usr/include/aarch64-linux-gnu下
set(TENSORRT_LIB_DIR  ${AARCH64_LINUX_LIB_DIR})   #tensorrt的库文件在/usr/lib/aarch64-linux-gnu下

set(DRM_INCLUDE_DIR ${SYS_USR_INCLUDE_DIR}/libdrm)  #DRM的头文件在/usr/include/libdrm下

set(TEGRA_LIB_DIR ${AARCH64_LINUX_LIB_DIR}/tegra)   #tegra库文件路径/usr/lib/aarch64-linux-gnu/tegra

#set(PCL_INCLUDE ${SYS_USR_LOCAL_INCLUDE_DIR}/pcl-1.7) #pcl头文件路径


# nvidia ascend common include
include_directories(
        #ai_matrix include
        ${PROJECT_SOURCE_DIR}/ai_matrix
        ${PROJECT_SOURCE_DIR}/ai_matrix/framework
        ${PROJECT_SOURCE_DIR}/ai_matrix/mylog
        ${PROJECT_SOURCE_DIR}/ai_matrix/myqueue
        ${PROJECT_SOURCE_DIR}/ai_matrix/myshell
        ${PROJECT_SOURCE_DIR}/ai_matrix/Utils
#        ${PROJECT_SOURCE_DIR}/ai_matrix/Http
        ${PROJECT_SOURCE_DIR}/ai_matrix/Config

        #nvidia ascend common cann include
        ${PROJECT_SOURCE_DIR}/base/BlockingQueue
        ${PROJECT_SOURCE_DIR}/base/CBase64
        ${PROJECT_SOURCE_DIR}/base/CommandParser
        ${PROJECT_SOURCE_DIR}/base/CommonDataType
        ${PROJECT_SOURCE_DIR}/base/ConfigParser
        ${PROJECT_SOURCE_DIR}/base/ErrorCode
        ${PROJECT_SOURCE_DIR}/base/FileManager
        ${PROJECT_SOURCE_DIR}/base/Log
        ${PROJECT_SOURCE_DIR}/base/

        ${PROJECT_SOURCE_DIR}/common
        ${PROJECT_SOURCE_DIR}/interface
        #common engine include
        ${PROJECT_SOURCE_DIR}/engine/DataSourceEngine
        ${PROJECT_SOURCE_DIR}/engine/DecodeEngine
        ${PROJECT_SOURCE_DIR}/engine/MoveEngine
        ${PROJECT_SOURCE_DIR}/engine/SaveMoveImageEngine
        ${PROJECT_SOURCE_DIR}/engine/SaveMoveInfoEngine
        ${PROJECT_SOURCE_DIR}/engine/Step1DataReadEngine
        ${PROJECT_SOURCE_DIR}/engine/Step1InferenceEngine
        ${PROJECT_SOURCE_DIR}/engine/Step1FilterInferenceEngine
        ${PROJECT_SOURCE_DIR}/engine/DetectDivideEngine
        ${PROJECT_SOURCE_DIR}/engine/Step2DataReadEngine
        ${PROJECT_SOURCE_DIR}/engine/Step2InferenceEngine
        ${PROJECT_SOURCE_DIR}/engine/CharacterConversionEngine
        ${PROJECT_SOURCE_DIR}/engine/SelectBestEngine
        ${PROJECT_SOURCE_DIR}/engine/SaveResultCSVEngine
        ${PROJECT_SOURCE_DIR}/engine/DataUploadEngine
        ${PROJECT_SOURCE_DIR}/engine/ApiEngine
        ${PROJECT_SOURCE_DIR}/engine/ControlEngine
        ${PROJECT_SOURCE_DIR}/engine/SaveDebugImageEngine
        ${PROJECT_SOURCE_DIR}/engine/WSServerEngine
        ${PROJECT_SOURCE_DIR}/engine/CornerInferenceEngine
        ${PROJECT_SOURCE_DIR}/engine/MergeEngine
        ${PROJECT_SOURCE_DIR}/engine/DeleteExpiredFolderEngine

        #common tools rtsp_server include
        ${PROJECT_SOURCE_DIR}/tools/rtsp_server/3rdpart/md5
        ${PROJECT_SOURCE_DIR}/tools/rtsp_server/net/
        ${PROJECT_SOURCE_DIR}/tools/rtsp_server/xop/
        ${PROJECT_SOURCE_DIR}/tools/yolov5/include/
        ${PROJECT_SOURCE_DIR}/tools/Http
        ${PROJECT_SOURCE_DIR}/tools/WebSocket

        ${CUDA_INCLUDE_DIR}
        ${TENSORRT_INCLUDE_DIR}
        ${DRM_INCLUDE_DIR}
        ${OpenCV_DIR}
        ${AARCH64_LINUX_INCLUDE_DIR}
        ${SYS_USR_LOCAL_INCLUDE_DIR}
)

link_directories(${SYS_USR_LOCAL_LIB_DIR}
        ${AARCH64_LINUX_LIB_DIR}
        ${OPENCV_LIB_DIR}
        ${CUDA_LIB_DIR}
        ${TENSORRT_LIB_DIR}
        ${TEGRA_LIB_DIR}
)

#源文件
#公共源文件
file(GLOB_RECURSE COMMON_SRCS_LISTS
        main.cpp
        #ai_matrix src
        ${PROJECT_SOURCE_DIR}/ai_matrix/framework/*.cpp
        ${PROJECT_SOURCE_DIR}/ai_matrix/mylog/*.cpp
        ${PROJECT_SOURCE_DIR}/ai_matrix/myqueue/*.cpp
        ${PROJECT_SOURCE_DIR}/ai_matrix/myshell/*.cpp
        ${PROJECT_SOURCE_DIR}/ai_matrix/Utils/*.cpp
        ${PROJECT_SOURCE_DIR}/ai_matrix/Http/*.cpp
        ${PROJECT_SOURCE_DIR}/ai_matrix/Config/*.cpp

        #common cann src
        ${PROJECT_SOURCE_DIR}/base/CommandParser/*.cpp
        ${PROJECT_SOURCE_DIR}/base/ConfigParser/*.cpp
        ${PROJECT_SOURCE_DIR}/base/ErrorCode/*.cpp
        ${PROJECT_SOURCE_DIR}/base/FileManager/*.cpp
        ${PROJECT_SOURCE_DIR}/base/Log/*.cpp

        #common engine src
        ${PROJECT_SOURCE_DIR}/common/*.cpp
        ${PROJECT_SOURCE_DIR}/interface/*.cpp
        ${PROJECT_SOURCE_DIR}/engine/DataSourceEngine/*.cpp
        ${PROJECT_SOURCE_DIR}/engine/DecodeEngine/*.cpp
        ${PROJECT_SOURCE_DIR}/engine/MoveEngine/*.cpp
        ${PROJECT_SOURCE_DIR}/engine/SaveMoveImageEngine/*.cpp
        ${PROJECT_SOURCE_DIR}/engine/SaveMoveInfoEngine/*.cpp
        ${PROJECT_SOURCE_DIR}/engine/Step1DataReadEngine/*.cpp
        ${PROJECT_SOURCE_DIR}/engine/Step1InferenceEngine/*.cpp
        ${PROJECT_SOURCE_DIR}/engine/Step1FilterInferenceEngine/*.cpp
        ${PROJECT_SOURCE_DIR}/engine/DetectDivideEngine/*.cpp
        ${PROJECT_SOURCE_DIR}/engine/Step2DataReadEngine/*.cpp
        ${PROJECT_SOURCE_DIR}/engine/Step2InferenceEngine/*.cpp
        ${PROJECT_SOURCE_DIR}/engine/CharacterConversionEngine/*.cpp
        ${PROJECT_SOURCE_DIR}/engine/SelectBestEngine/*.cpp
        ${PROJECT_SOURCE_DIR}/engine/SaveResultCSVEngine/*.cpp
        ${PROJECT_SOURCE_DIR}/engine/DataUploadEngine/*.cpp
        ${PROJECT_SOURCE_DIR}/engine/ApiEngine/*.cpp
        ${PROJECT_SOURCE_DIR}/engine/ControlEngine/*.cpp
        ${PROJECT_SOURCE_DIR}/engine/SaveDebugImageEngine/*.cpp
        ${PROJECT_SOURCE_DIR}/engine/WSServerEngine/*.cpp
        ${PROJECT_SOURCE_DIR}/engine/CornerInferenceEngine/*.cpp
        ${PROJECT_SOURCE_DIR}/engine/MergeEngine/*.cpp
        ${PROJECT_SOURCE_DIR}/engine/DeleteExpiredFolderEngine/*.cpp

        #common tools rtsp_server src
        ${PROJECT_SOURCE_DIR}/tools/rtsp_server/net/*.cpp
        ${PROJECT_SOURCE_DIR}/tools/rtsp_server/xop/*.cpp
        ${PROJECT_SOURCE_DIR}/tools/yolov5/src/*.cpp
        ${PROJECT_SOURCE_DIR}/tools/yolov5/src/*.cu
)

cuda_add_executable(${PROJECT_NAME} ${COMMON_SRCS_LISTS} ${SRCS_LISTS})
# add_executable(${PROJECT_NAME} ${COMMON_SRCS_LISTS} ${SRCS_LISTS})



target_link_libraries(${PROJECT_NAME} pthread)       #other
target_link_libraries(${PROJECT_NAME} nvinfer nvonnxparser nvcaffe_parser nvinfer_plugin)   #TensorRT
target_link_libraries(${PROJECT_NAME} cudart cuda)   #CUDA

target_link_libraries(${PROJECT_NAME}
        ${OpenCV_LIBS}  #third party librarys
        avformat avcodec avutil avfilter swresample swscale postproc #VideoCodecV2
        yaml-cpp
        jsoncpp curl boost_system boost_filesystem ssh2
        -Wl,-z,relro,-z,now,-z,noexecstack -pie -s
        aws-cpp-sdk-core aws-cpp-sdk-s3
)