cmake_minimum_required(VERSION 3.29.0 FATAL_ERROR)
project(LibtorchSample VERSION 0.1.0)

# cuda 忽略vs版本
set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} -allow-unsupported-compiler")

# 查找 torch 包
set(CMAKE_PREFIX_PATH "S:/libtorch")
find_package(Torch REQUIRED)

add_executable(LibtorchSample LibtorchSample.cpp)
target_link_libraries(LibtorchSample ${TORCH_LIBRARIES})

set_property(TARGET LibtorchSample PROPERTY CXX_STANDARD 17)

# 定义函数，如果文件不存在复制
function(copy_if_not_exists src dest)
    if(NOT EXISTS "${dest}")
        add_custom_command(
            TARGET LibtorchSample POST_BUILD
            COMMAND ${CMAKE_COMMAND} -E copy_if_different "${src}" "${dest}"
        )
    endif()
endfunction()

set(CUSTOM_LIB_DIR "../lib")
# 将自定义库中的dll拷贝到输出目录
file(GLOB CUSTOM_LIB_DLLS "${CUSTOM_LIB_DIR}/*.dll")
foreach(dll ${CUSTOM_LIB_DLLS})
    get_filename_component(dll_name ${dll} NAME)
    copy_if_not_exists("${dll}" "${CMAKE_BINARY_DIR}/${CMAKE_BUILD_TYPE}/${dll_name}")
endforeach()