4
# Finds the Torch library
6
# This will define the following variables:
8
# TORCH_FOUND -- True if the system has the Torch library
9
# TORCH_INCLUDE_DIRS -- The include directories for torch
10
# TORCH_LIBRARIES -- Libraries to link against
11
# TORCH_CXX_FLAGS -- Additional (required) compiler flags
13
# and the following imported targets:
16
macro(append_torchlib_if_found)
17
foreach (_arg ${ARGN})
18
find_library(${_arg}_LIBRARY ${_arg} PATHS "${TORCH_INSTALL_PREFIX}/lib")
20
list(APPEND TORCH_LIBRARIES ${${_arg}_LIBRARY})
22
message(WARNING "static library ${${_arg}_LIBRARY} not found.")
27
macro(append_wholearchive_lib_if_found)
28
foreach (_arg ${ARGN})
29
find_library(${_arg}_LIBRARY ${_arg} PATHS "${TORCH_INSTALL_PREFIX}/lib")
32
list(APPEND TORCH_LIBRARIES "-Wl,-force_load,${${_arg}_LIBRARY}")
34
list(APPEND TORCH_LIBRARIES "-WHOLEARCHIVE:${${_arg}_LIBRARY}")
37
list(APPEND TORCH_LIBRARIES "-Wl,--whole-archive ${${_arg}_LIBRARY} -Wl,--no-whole-archive")
40
message(WARNING "static library ${${_arg}_LIBRARY} not found.")
45
include(FindPackageHandleStandardArgs)
47
if(DEFINED ENV{TORCH_INSTALL_PREFIX})
48
set(TORCH_INSTALL_PREFIX $ENV{TORCH_INSTALL_PREFIX})
50
# Assume we are in <install-prefix>/share/cmake/Torch/TorchConfig.cmake
51
get_filename_component(CMAKE_CURRENT_LIST_DIR "${CMAKE_CURRENT_LIST_FILE}" PATH)
52
get_filename_component(TORCH_INSTALL_PREFIX "${CMAKE_CURRENT_LIST_DIR}/../../../" ABSOLUTE)
56
if(EXISTS "${TORCH_INSTALL_PREFIX}/include")
57
set(TORCH_INCLUDE_DIRS
58
${TORCH_INSTALL_PREFIX}/include
59
${TORCH_INSTALL_PREFIX}/include/torch/csrc/api/include)
61
set(TORCH_INCLUDE_DIRS
62
${TORCH_INSTALL_PREFIX}/include
63
${TORCH_INSTALL_PREFIX}/include/torch/csrc/api/include)
66
# Library dependencies.
67
if(@BUILD_SHARED_LIBS@)
68
find_package(Caffe2 REQUIRED PATHS ${CMAKE_CURRENT_LIST_DIR}/../Caffe2)
69
set(TORCH_LIBRARIES torch ${Caffe2_MAIN_LIBS})
70
append_torchlib_if_found(c10)
72
add_library(torch STATIC IMPORTED) # set imported_location at the bottom
73
#library need whole archive
74
append_wholearchive_lib_if_found(torch torch_cpu)
76
append_wholearchive_lib_if_found(torch_cuda c10_cuda)
79
# We need manually add dependent libraries when they are not linked into the
81
# TODO: this list might be incomplete.
82
append_torchlib_if_found(c10)
84
append_torchlib_if_found(Caffe2_perfkernels_avx512 Caffe2_perfkernels_avx2 Caffe2_perfkernels_avx)
88
append_torchlib_if_found(nnpack)
91
if(@USE_PYTORCH_QNNPACK@)
92
append_torchlib_if_found(pytorch_qnnpack)
96
append_torchlib_if_found(qnnpack)
100
append_torchlib_if_found(XNNPACK)
103
append_torchlib_if_found(caffe2_protos protobuf-lite protobuf protoc)
104
append_torchlib_if_found(onnx onnx_proto)
106
append_torchlib_if_found(foxi_loader fmt)
107
append_torchlib_if_found(cpuinfo clog)
109
if(NOT @USE_INTERNAL_PTHREADPOOL_IMPL@)
110
append_torchlib_if_found(pthreadpool)
113
append_torchlib_if_found(eigen_blas)
116
append_torchlib_if_found(fbgemm)
120
append_torchlib_if_found(dnnl mkldnn)
123
append_torchlib_if_found(sleef asmjit)
127
append_torchlib_if_found(kineto)
132
if(NOT NVTOOLEXT_HOME)
133
set(NVTOOLEXT_HOME "C:/Program Files/NVIDIA Corporation/NvToolsExt")
135
if(DEFINED ENV{NVTOOLSEXT_PATH})
136
set(NVTOOLEXT_HOME $ENV{NVTOOLSEXT_PATH})
138
set(TORCH_CUDA_LIBRARIES
139
${NVTOOLEXT_HOME}/lib/x64/nvToolsExt64_1.lib
141
list(APPEND TORCH_INCLUDE_DIRS ${NVTOOLEXT_HOME}/include)
142
find_library(CAFFE2_NVRTC_LIBRARY caffe2_nvrtc PATHS "${TORCH_INSTALL_PREFIX}/lib")
143
list(APPEND TORCH_CUDA_LIBRARIES ${CAFFE2_NVRTC_LIBRARY})
145
set(TORCH_CUDA_LIBRARIES
146
${CUDA_TOOLKIT_ROOT_DIR}/lib/libcudart.dylib
147
${CUDA_TOOLKIT_ROOT_DIR}/lib/libnvrtc.dylib
148
${CUDA_TOOLKIT_ROOT_DIR}/lib/libnvToolsExt.dylib
151
find_library(LIBNVTOOLSEXT libnvToolsExt.so PATHS ${CUDA_TOOLKIT_ROOT_DIR}/lib64/)
152
set(TORCH_CUDA_LIBRARIES
158
if(@BUILD_SHARED_LIBS@)
159
find_library(C10_CUDA_LIBRARY c10_cuda PATHS "${TORCH_INSTALL_PREFIX}/lib")
160
list(APPEND TORCH_CUDA_LIBRARIES ${C10_CUDA_LIBRARY})
162
list(APPEND TORCH_LIBRARIES ${TORCH_CUDA_LIBRARIES})
165
# When we build libtorch with the old libstdc++ ABI, dependent libraries must too.
166
if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
167
set(TORCH_CXX_FLAGS "-D_GLIBCXX_USE_CXX11_ABI=@GLIBCXX_USE_CXX11_ABI@")
170
find_library(TORCH_LIBRARY torch PATHS "${TORCH_INSTALL_PREFIX}/lib")
171
# the statements below changes target properties on
172
# - the imported target from Caffe2Targets.cmake in shared library mode (see the find_package above)
173
# - this is untested whether it is the correct (or desired) methodology in CMake
174
# - the imported target created in this file in static library mode
175
if(NOT @BUILD_SHARED_LIBS@)
176
# do not set this property on the shared library target, as it will cause confusion in some builds
177
# as the configuration specific property is set in the Caffe2Targets.cmake file
178
set_target_properties(torch PROPERTIES
179
IMPORTED_LOCATION "${TORCH_LIBRARY}"
182
set_target_properties(torch PROPERTIES
183
INTERFACE_INCLUDE_DIRECTORIES "${TORCH_INCLUDE_DIRS}"
187
set_property(TARGET torch PROPERTY INTERFACE_COMPILE_OPTIONS "${TORCH_CXX_FLAGS}")
190
find_package_handle_standard_args(Torch DEFAULT_MSG TORCH_LIBRARY TORCH_INCLUDE_DIRS)