xref: /aosp_15_r20/external/pytorch/cmake/TorchConfig.cmake.in (revision da0073e96a02ea20f0ac840b70461e3646d07c45)
1# FindTorch
2# -------
3#
4# Finds the Torch library
5#
6# This will define the following variables:
7#
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
12#
13# and the following imported targets:
14#
15#   torch
16macro(append_torchlib_if_found)
17  foreach (_arg ${ARGN})
18    find_library(${_arg}_LIBRARY ${_arg} PATHS "${TORCH_INSTALL_PREFIX}/lib")
19    if(${_arg}_LIBRARY)
20      list(APPEND TORCH_LIBRARIES ${${_arg}_LIBRARY})
21    else()
22      message(WARNING "static library ${${_arg}_LIBRARY} not found.")
23    endif()
24  endforeach()
25endmacro()
26
27macro(append_wholearchive_lib_if_found)
28  foreach (_arg ${ARGN})
29    find_library(${_arg}_LIBRARY ${_arg} PATHS "${TORCH_INSTALL_PREFIX}/lib")
30    if(${_arg}_LIBRARY)
31      if(APPLE)
32        list(APPEND TORCH_LIBRARIES "-Wl,-force_load,${${_arg}_LIBRARY}")
33      elseif(MSVC)
34        list(APPEND TORCH_LIBRARIES "-WHOLEARCHIVE:${${_arg}_LIBRARY}")
35      else()
36        # Linux
37        list(APPEND TORCH_LIBRARIES "-Wl,--whole-archive ${${_arg}_LIBRARY} -Wl,--no-whole-archive")
38      endif()
39    else()
40      message(WARNING "static library ${${_arg}_LIBRARY} not found.")
41    endif()
42  endforeach()
43endmacro()
44
45include(FindPackageHandleStandardArgs)
46
47if(DEFINED ENV{TORCH_INSTALL_PREFIX})
48  set(TORCH_INSTALL_PREFIX $ENV{TORCH_INSTALL_PREFIX})
49else()
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)
53endif()
54
55# Include directories.
56if(EXISTS "${TORCH_INSTALL_PREFIX}/include")
57  set(TORCH_INCLUDE_DIRS
58    ${TORCH_INSTALL_PREFIX}/include
59    ${TORCH_INSTALL_PREFIX}/include/torch/csrc/api/include)
60else()
61  set(TORCH_INCLUDE_DIRS
62    ${TORCH_INSTALL_PREFIX}/include
63    ${TORCH_INSTALL_PREFIX}/include/torch/csrc/api/include)
64endif()
65
66# Library dependencies.
67if(@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)
71else()
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)
75  if(@USE_CUDA@)
76    append_wholearchive_lib_if_found(torch_cuda c10_cuda)
77  endif()
78
79  # We need manually add dependent libraries when they are not linked into the
80  # shared library.
81  # TODO: this list might be incomplete.
82  append_torchlib_if_found(c10)
83
84  if(@USE_NNPACK@)
85    append_torchlib_if_found(nnpack)
86  endif()
87
88  if(@USE_PYTORCH_QNNPACK@)
89    append_torchlib_if_found(pytorch_qnnpack)
90  endif()
91
92  if(@USE_XNNPACK@)
93    append_torchlib_if_found(XNNPACK)
94  endif()
95
96  append_torchlib_if_found(caffe2_protos protobuf-lite protobuf protoc)
97  append_torchlib_if_found(onnx onnx_proto)
98
99  append_torchlib_if_found(fmt)
100  append_torchlib_if_found(cpuinfo clog)
101
102  if(NOT @USE_INTERNAL_PTHREADPOOL_IMPL@)
103    append_torchlib_if_found(pthreadpool)
104  endif()
105
106  append_torchlib_if_found(eigen_blas)
107
108  if(@USE_FBGEMM@)
109    append_torchlib_if_found(fbgemm)
110  endif()
111
112  if(@USE_MKLDNN@)
113    append_torchlib_if_found(dnnl mkldnn)
114  endif()
115
116  append_torchlib_if_found(sleef asmjit)
117endif()
118
119if(@USE_KINETO@)
120  append_torchlib_if_found(kineto)
121endif()
122
123if(@USE_CUDA@)
124  if(MSVC)
125    find_library(CAFFE2_NVRTC_LIBRARY caffe2_nvrtc PATHS "${TORCH_INSTALL_PREFIX}/lib")
126    list(APPEND TORCH_CUDA_LIBRARIES ${CAFFE2_NVRTC_LIBRARY})
127  else()
128    set(TORCH_CUDA_LIBRARIES ${CUDA_NVRTC_LIB})
129  endif()
130  if(TARGET torch::nvtoolsext)
131    list(APPEND TORCH_CUDA_LIBRARIES torch::nvtoolsext)
132  endif()
133
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})
137  endif()
138  list(APPEND TORCH_LIBRARIES ${TORCH_CUDA_LIBRARIES})
139endif()
140
141# When we build libtorch with the old libstdc++ ABI, dependent libraries must too.
142if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
143  set(TORCH_CXX_FLAGS "-D_GLIBCXX_USE_CXX11_ABI=@GLIBCXX_USE_CXX11_ABI@")
144endif()
145
146find_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
151if(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}"
156  )
157endif()
158set_target_properties(torch PROPERTIES
159    INTERFACE_INCLUDE_DIRECTORIES "${TORCH_INCLUDE_DIRS}"
160    CXX_STANDARD 17
161)
162if(TORCH_CXX_FLAGS)
163  set_property(TARGET torch PROPERTY INTERFACE_COMPILE_OPTIONS "${TORCH_CXX_FLAGS}")
164endif()
165
166find_package_handle_standard_args(Torch DEFAULT_MSG TORCH_LIBRARY TORCH_INCLUDE_DIRS)
167