# 获取编译时间
string(TIMESTAMP COMPILE_TIME 20%y_%m_%d-%H.%M.%S)
set(build_time ${COMPILE_TIME})

# 获取当前版本信息
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/src/Version.h.in ${CMAKE_CURRENT_SOURCE_DIR}/src/Version.h)

add_definitions(-D_SILENCE_EXPERIMENTAL_FILESYSTEM_DEPRECATION_WARNING)

SET(ENABLE_CUDA FALSE)


IF(ENABLE_CUDA)
    add_definitions(-DENABLE_CUDA)
    # 开启cmake的cuda支持
    enable_language(CUDA)
    set(CUDA_ARCHITECTURES "50;52;60;70;75;80")
    set(CMAKE_CUDA_ARCHITECTURES 50 52 60 70 75 80)
    # 注意配置cuda的运行时库设置
    if(WIN32)
        if(CMAKE_BUILD_TYPE MATCHES  "Debug")
            set(CUDA_NVCC_FLAGS "${CUDA_NVCC_FLAGS} -Xcompiler=/MDd")
        elseif(CMAKE_BUILD_TYPE MATCHES  "RelWithDebInfo")
            set(CUDA_NVCC_FLAGS "${CUDA_NVCC_FLAGS} -Xcompiler=/MD")
        elseif(CMAKE_BUILD_TYPE MATCHES  "Release")
            set(CUDA_NVCC_FLAGS "${CUDA_NVCC_FLAGS} -Xcompiler=/MD")
        else()
            set(CUDA_NVCC_FLAGS "${CUDA_NVCC_FLAGS} -Xcompiler=/MD")
        endif()
    endif(WIN32)
ENDIF(ENABLE_CUDA)

message("CMAKE_BUILD_TYPE: " ${CMAKE_BUILD_TYPE})

# 检查 CMAKE_BUILD_TYPE 是否为空
if(NOT CMAKE_BUILD_TYPE)
    message(FATAL_ERROR "CMAKE_BUILD_TYPE is not set. Please specify a build type (e.g., Debug, Release).")
endif()



if(MSVC)
    add_definitions(-D_CRT_SECURE_NO_WARNINGS)
    add_compile_options(/wd4996)
endif()


# 配置ceres
#set(Ceres_DIR "../3rdParty/my_ceres_vc17/lib/cmake/Ceres")

IF(WIN32)
    set(Ceres_DIR "../3rdParty")
    include_directories(${Ceres_DIR}/include)
    link_directories(${Ceres_DIR}/lib)
    file(GLOB CERES_LIBRARIES ${Ceres_DIR}/lib/*.lib)
ELSE(WIN32)
    find_package(Ceres REQUIRED)
ENDIF()


# 配置openmp
find_package(OpenMP REQUIRED)

# 公共头文件（放置在最前面，所有后面定义的模块才都能找到）
SET(ArithTrkPubInc ${CMAKE_SOURCE_DIR}/public_include)
include_directories(${ArithTrkPubInc}) # 引入算法公共头文件

# 全局包含opencv
link_directories(${OpenCV_LIBS_DIR})
include_directories(${OpenCV_INCLUDE_DIRS})

include_directories(${CUDA_INCLUDE_DIRS})

set(ArithSrcDIR_MAIN "src") # 库源文件路径
set(ArithSrcDIR_LOG "src/Log") # 日志文件路径

# 使用当前目录、Common目录源码构建
file(GLOB libsrcs ${ArithSrcDIR_MAIN}/mapKernel.cu ${ArithSrcDIR_MAIN}/*.cpp ${ArithSrcDIR_MAIN}/*.c ${ArithSrcDIR_MAIN}/*.h ${ArithSrcDIR_MAIN}/*.hpp)
file(GLOB CommonSrc ${ArithSrcDIR_MAIN}/utils/*.cpp ${ArithSrcDIR_MAIN}/utils/*.c ${ArithSrcDIR_MAIN}/utils/*.h ${ArithSrcDIR_MAIN}/utils/*.hpp)
file(GLOB LogSrc ${ArithSrcDIR_LOG}/*.cpp ${ArithSrcDIR_LOG}/*.c ${ArithSrcDIR_LOG}/*.h ${ArithSrcDIR_LOG}/*.hpp)


message("-------------------")
message(STATUS ${CMAKE_BUILD_TYPE})

if(WIN32)
    # 添加全局的CUDA编译选项
    # set(CUDA_NVCC_FLAGS "${CUDA_NVCC_FLAGS} -Xcompiler -D__CUDA_NO_HALF_SUPPORT")
    IF(ENABLE_CUDA)
        cuda_add_library(${LIB_STITCH} SHARED ${libsrcs} ${CommonSrc} ${LogSrc}) # 构建算法库
    ELSE()
        add_library(${LIB_STITCH} SHARED ${libsrcs} ${CommonSrc} ${LogSrc}) # 构建算法库
    ENDIF()
else()
    add_library(${LIB_STITCH} SHARED ${libsrcs} ${CommonSrc}) # 构建算法库
endif()

# 设置包含路径
target_include_directories(${LIB_STITCH} PUBLIC
    ${ArithTrkPubInc}
    ${ArithSrcDIR_MAIN}
    ${ArithSrcDIR_MAIN}/utils
    ${ArithSrcDIR_MAIN}/Log
)

# 算法库链接
target_link_libraries(${LIB_STITCH}
    OpenMP::OpenMP_CXX
    ${OpenCV_LIBS}
    ${CERES_LIBRARIES}
    ${CUDA_LIBRARIES}
)

set(LIBRARY_OUTPUT_PATH ${CMAKE_SOURCE_DIR}/Bin) # 输出算法库路径
