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)
85
append_torchlib_if_found(nnpack)
88
if(@USE_PYTORCH_QNNPACK@)
89
append_torchlib_if_found(pytorch_qnnpack)
93
append_torchlib_if_found(XNNPACK)
96
append_torchlib_if_found(caffe2_protos protobuf-lite protobuf protoc)
97
append_torchlib_if_found(onnx onnx_proto)
99
append_torchlib_if_found(fmt)
100
append_torchlib_if_found(cpuinfo clog)
102
if(NOT @USE_INTERNAL_PTHREADPOOL_IMPL@)
103
append_torchlib_if_found(pthreadpool)
106
append_torchlib_if_found(eigen_blas)
109
append_torchlib_if_found(fbgemm)
113
append_torchlib_if_found(dnnl mkldnn)
116
append_torchlib_if_found(sleef asmjit)
120
append_torchlib_if_found(kineto)
125
find_library(CAFFE2_NVRTC_LIBRARY caffe2_nvrtc PATHS "${TORCH_INSTALL_PREFIX}/lib")
126
list(APPEND TORCH_CUDA_LIBRARIES ${CAFFE2_NVRTC_LIBRARY})
128
set(TORCH_CUDA_LIBRARIES ${CUDA_NVRTC_LIB})
130
if(TARGET torch::nvtoolsext)
131
list(APPEND TORCH_CUDA_LIBRARIES torch::nvtoolsext)
134
if(@BUILD_SHARED_LIBS@)
135
find_library(C10_CUDA_LIBRARY c10_cuda PATHS "${TORCH_INSTALL_PREFIX}/lib")
136
list(APPEND TORCH_CUDA_LIBRARIES ${C10_CUDA_LIBRARY} ${Caffe2_PUBLIC_CUDA_DEPENDENCY_LIBS})
138
list(APPEND TORCH_LIBRARIES ${TORCH_CUDA_LIBRARIES})
141
# When we build libtorch with the old libstdc++ ABI, dependent libraries must too.
142
if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
143
set(TORCH_CXX_FLAGS "-D_GLIBCXX_USE_CXX11_ABI=@GLIBCXX_USE_CXX11_ABI@")
146
find_library(TORCH_LIBRARY torch PATHS "${TORCH_INSTALL_PREFIX}/lib")
147
# the statements below changes target properties on
148
# - the imported target from Caffe2Targets.cmake in shared library mode (see the find_package above)
149
# - this is untested whether it is the correct (or desired) methodology in CMake
150
# - the imported target created in this file in static library mode
151
if(NOT @BUILD_SHARED_LIBS@)
152
# do not set this property on the shared library target, as it will cause confusion in some builds
153
# as the configuration specific property is set in the Caffe2Targets.cmake file
154
set_target_properties(torch PROPERTIES
155
IMPORTED_LOCATION "${TORCH_LIBRARY}"
158
set_target_properties(torch PROPERTIES
159
INTERFACE_INCLUDE_DIRECTORIES "${TORCH_INCLUDE_DIRS}"
163
set_property(TARGET torch PROPERTY INTERFACE_COMPILE_OPTIONS "${TORCH_CXX_FLAGS}")
166
find_package_handle_standard_args(Torch DEFAULT_MSG TORCH_LIBRARY TORCH_INCLUDE_DIRS)